From: Michael Rappazzo <rappazzo@gmail.com>
To: gitster@pobox.com, sunshine@sunshineco.com, dturner@twopensource.com
Cc: git@vger.kernel.org, Michael Rappazzo <rappazzo@gmail.com>
Subject: [PATCH v6 1/2] worktree: add 'for_each_worktree' function
Date: Sun, 30 Aug 2015 15:10:38 -0400 [thread overview]
Message-ID: <1440961839-40575-2-git-send-email-rappazzo@gmail.com> (raw)
In-Reply-To: <1440961839-40575-1-git-send-email-rappazzo@gmail.com>
for_each_worktree iterates through each worktree and invokes a callback
function. The main worktree (if not bare) is always encountered first,
followed by worktrees created by `git worktree add`.
If the callback function returns a non-zero value, iteration stops, and
the callback's return value is returned from the for_each_worktree
function.
Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
---
builtin/worktree.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 83 insertions(+)
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 430b51e..7b3cb96 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -26,6 +26,14 @@ static int show_only;
static int verbose;
static unsigned long expire;
+/*
+ * The signature for the callback function for the for_each_worktree()
+ * function below. The memory pointed to by the callback arguments
+ * is only guaranteed to be valid for the duration of a single
+ * callback invocation.
+ */
+typedef int each_worktree_fn(const char *path, const char *git_dir, void *cb_data);
+
static int prune_worktree(const char *id, struct strbuf *reason)
{
struct stat st;
@@ -359,6 +367,81 @@ static int add(int ac, const char **av, const char *prefix)
return add_worktree(path, branch, &opts);
}
+/*
+ * Iterate through each worktree and invoke the callback function. If
+ * the callback function returns non-zero, the iteration stops, and
+ * this function returns that return value
+ */
+static int for_each_worktree(each_worktree_fn fn, void *cb_data)
+{
+ struct strbuf worktree_path = STRBUF_INIT;
+ struct strbuf worktree_git = STRBUF_INIT;
+ const char *common_dir;
+ int main_is_bare = 0;
+ int ret = 0;
+
+ common_dir = get_git_common_dir();
+ if (!strcmp(common_dir, get_git_dir())) {
+ /* simple case - this is the main repo */
+ main_is_bare = is_bare_repository();
+ if (!main_is_bare) {
+ strbuf_addstr(&worktree_path, get_git_work_tree());
+ } else {
+ strbuf_addstr(&worktree_path, common_dir);
+ }
+ } else {
+ strbuf_addstr(&worktree_path, common_dir);
+ /* If the common_dir DOES NOT end with '/.git', then it is bare */
+ main_is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
+ }
+ strbuf_addstr(&worktree_git, worktree_path.buf);
+
+ if (!main_is_bare) {
+ strbuf_addstr(&worktree_git, "/.git");
+
+ ret = fn(worktree_path.buf, worktree_git.buf, cb_data);
+ if (ret)
+ goto done;
+ }
+ strbuf_addstr(&worktree_git, "/worktrees");
+
+ if (is_directory(worktree_git.buf)) {
+ DIR *dir = opendir(worktree_git.buf);
+ if (dir) {
+ struct stat st;
+ struct dirent *d;
+ size_t base_path_len = worktree_git.len;
+
+ while ((d = readdir(dir)) != NULL) {
+ if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
+ continue;
+
+ strbuf_setlen(&worktree_git, base_path_len);
+ strbuf_addf(&worktree_git, "/%s/gitdir", d->d_name);
+
+ if (stat(worktree_git.buf, &st)) {
+ fprintf(stderr, "Unable to read worktree git dir: %s\n", worktree_git.buf);
+ continue;
+ }
+
+ strbuf_reset(&worktree_path);
+ strbuf_read_file(&worktree_path, worktree_git.buf, st.st_size);
+ strbuf_strip_suffix(&worktree_path, "/.git\n");
+
+ strbuf_strip_suffix(&worktree_git, "/gitdir");
+ ret = fn(worktree_path.buf, worktree_git.buf, cb_data);
+ if (ret)
+ break;
+ }
+ }
+ closedir(dir);
+ }
+done:
+ strbuf_release(&worktree_git);
+ strbuf_release(&worktree_path);
+ return ret;
+}
+
int cmd_worktree(int ac, const char **av, const char *prefix)
{
struct option options[] = {
--
2.5.0
next prev parent reply other threads:[~2015-08-30 19:10 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-30 19:10 [PATCH v6 0/2] worktree: for-each function and list command Michael Rappazzo
2015-08-30 19:10 ` Michael Rappazzo [this message]
2015-08-31 5:11 ` [PATCH v6 1/2] worktree: add 'for_each_worktree' function Eric Sunshine
2015-08-31 17:56 ` Junio C Hamano
2015-08-31 18:44 ` David Turner
2015-08-31 19:03 ` Eric Sunshine
2015-08-31 19:54 ` David Turner
2015-08-31 18:57 ` Mike Rappazzo
2015-08-31 19:22 ` Eric Sunshine
2015-08-31 19:47 ` Eric Sunshine
2015-08-31 19:54 ` Mike Rappazzo
2015-08-31 21:37 ` Eric Sunshine
2015-08-30 19:10 ` [PATCH v6 2/2] worktree: add 'list' command Michael Rappazzo
2015-08-31 6:23 ` Eric Sunshine
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1440961839-40575-2-git-send-email-rappazzo@gmail.com \
--to=rappazzo@gmail.com \
--cc=dturner@twopensource.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).