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 v8 2/4] worktree: refactor find_linked_symref function
Date: Fri, 18 Sep 2015 09:30:25 -0400 [thread overview]
Message-ID: <1442583027-47653-3-git-send-email-rappazzo@gmail.com> (raw)
In-Reply-To: <1442583027-47653-1-git-send-email-rappazzo@gmail.com>
Refactoring will help transition this code to provide additional useful
worktree functions.
Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
---
worktree.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++----------------
1 file changed, 64 insertions(+), 22 deletions(-)
diff --git a/worktree.c b/worktree.c
index 10e1496..5c75875 100644
--- a/worktree.c
+++ b/worktree.c
@@ -3,6 +3,60 @@
#include "strbuf.h"
#include "worktree.h"
+/*
+ * read 'path_to_ref' into 'ref'. Also if is_detached is not NULL,
+ * set is_detached to 1 if the ref is detatched.
+ *
+ * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside $GIT_DIR so
+ * for linked worktrees, `resolve_ref_unsafe()` won't work (it uses
+ * git_path). Parse the ref ourselves.
+ *
+ * return -1 if the ref is not a proper ref, 0 otherwise (success)
+ */
+static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
+{
+ if (is_detached)
+ *is_detached = 0;
+ if (!strbuf_readlink(ref, path_to_ref, 0)) {
+ if (!starts_with(ref->buf, "refs/")
+ || check_refname_format(ref->buf, 0))
+ return -1;
+
+ } else if (strbuf_read_file(ref, path_to_ref, 0) >= 0) {
+ if (starts_with(ref->buf, "ref:")) {
+ strbuf_remove(ref, 0, strlen("ref:"));
+ strbuf_trim(ref);
+ if (check_refname_format(ref->buf, 0))
+ return -1;
+ } else if (is_detached)
+ *is_detached = 1;
+ }
+ return 0;
+}
+
+static char *find_main_symref(const char *symref, const char *branch)
+{
+ struct strbuf sb = STRBUF_INIT;
+ struct strbuf path = STRBUF_INIT;
+ struct strbuf gitdir = STRBUF_INIT;
+ char *existing = NULL;
+
+ strbuf_addf(&path, "%s/%s", get_git_common_dir(), symref);
+ if (parse_ref(path.buf, &sb, NULL) == -1)
+ goto done;
+ if (strcmp(sb.buf, branch))
+ goto done;
+ strbuf_addstr(&gitdir, get_git_common_dir());
+ strbuf_strip_suffix(&gitdir, ".git");
+ existing = strbuf_detach(&gitdir, NULL);
+done:
+ strbuf_release(&path);
+ strbuf_release(&sb);
+ strbuf_release(&gitdir);
+
+ return existing;
+}
+
static char *find_linked_symref(const char *symref, const char *branch,
const char *id)
{
@@ -11,36 +65,24 @@ static char *find_linked_symref(const char *symref, const char *branch,
struct strbuf gitdir = STRBUF_INIT;
char *existing = NULL;
+ if (!id)
+ goto done;
/*
* $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside
* $GIT_DIR so resolve_ref_unsafe() won't work (it uses
* git_path). Parse the ref ourselves.
*/
- if (id)
- strbuf_addf(&path, "%s/worktrees/%s/%s", get_git_common_dir(), id, symref);
- else
- strbuf_addf(&path, "%s/%s", get_git_common_dir(), symref);
+ strbuf_addf(&path, "%s/worktrees/%s/%s", get_git_common_dir(), id, symref);
- if (!strbuf_readlink(&sb, path.buf, 0)) {
- if (!starts_with(sb.buf, "refs/") ||
- check_refname_format(sb.buf, 0))
- goto done;
- } else if (strbuf_read_file(&sb, path.buf, 0) >= 0 &&
- starts_with(sb.buf, "ref:")) {
- strbuf_remove(&sb, 0, strlen("ref:"));
- strbuf_trim(&sb);
- } else
+ if (parse_ref(path.buf, &sb, NULL) == -1)
goto done;
if (strcmp(sb.buf, branch))
goto done;
- if (id) {
- strbuf_reset(&path);
- strbuf_addf(&path, "%s/worktrees/%s/gitdir", get_git_common_dir(), id);
- if (strbuf_read_file(&gitdir, path.buf, 0) <= 0)
- goto done;
- strbuf_rtrim(&gitdir);
- } else
- strbuf_addstr(&gitdir, get_git_common_dir());
+ strbuf_reset(&path);
+ strbuf_addf(&path, "%s/worktrees/%s/gitdir", get_git_common_dir(), id);
+ if (strbuf_read_file(&gitdir, path.buf, 0) <= 0)
+ goto done;
+ strbuf_rtrim(&gitdir);
strbuf_strip_suffix(&gitdir, ".git");
existing = strbuf_detach(&gitdir, NULL);
@@ -59,7 +101,7 @@ char *find_shared_symref(const char *symref, const char *target)
struct dirent *d;
char *existing;
- if ((existing = find_linked_symref(symref, target, NULL)))
+ if ((existing = find_main_symref(symref, target)))
return existing;
strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
--
2.5.0
next prev parent reply other threads:[~2015-09-18 13:31 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-18 13:30 [PATCH v8 0/4] worktree: list functions and command Michael Rappazzo
2015-09-18 13:30 ` [PATCH v8 1/4] worktree: add top-level worktree.c Michael Rappazzo
2015-09-22 17:16 ` Junio C Hamano
2015-09-18 13:30 ` Michael Rappazzo [this message]
2015-09-22 17:44 ` [PATCH v8 2/4] worktree: refactor find_linked_symref function Junio C Hamano
2015-09-18 13:30 ` [PATCH v8 3/4] worktree: add functions to get worktree details Michael Rappazzo
2015-09-22 18:10 ` Junio C Hamano
2015-09-18 13:30 ` [PATCH v8 4/4] worktree: add 'list' command Michael Rappazzo
2015-09-22 19:42 ` Junio C Hamano
2015-09-24 2:58 ` Mike Rappazzo
2015-09-24 5:00 ` Junio C Hamano
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=1442583027-47653-3-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).