git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v10 4/5] worktree: add details to the worktree struct

From
Michael Rappazzo <rappazzo@gmail.com>
Date
Oct 8, 2015, 17:01 UTC
Message-ID
<1444323665-64590-4-git-send-email-rappazzo@gmail.com>
In-Reply-To
<1444323665-64590-1-git-send-email-rappazzo@gmail.com>

In addition to the absolute path in the worktree struct, add the location of the git dir, the head ref (if not detached), the head revision sha1, whether or not head is detached, and whether or not the worktree is a bare repo.

Signed-off-by: Michael Rappazzo <rappazzo@gmail.com>
---
 worktree.c | 55 ++++++++++++++++++++++++++++++++++++++++++++-----------
 worktree.h |  4 ++++
 2 files changed, 48 insertions(+), 11 deletions(-)
diff --git a/worktree.c b/worktree.c
index c2e6db0..981f810 100644
--- a/worktree.c
+++ b/worktree.c
@@ -9,6 +9,8 @@ void free_worktrees(struct worktree **worktrees)
 
 	for (i = 0; worktrees[i]; i++) {
 		free(worktrees[i]->path);
+		free(worktrees[i]->git_dir);
+		free(worktrees[i]->head_ref);
 		free(worktrees[i]);
 	}
 	free (worktrees);
@@ -50,6 +52,21 @@ static int parse_ref(char *path_to_ref, struct strbuf *ref, int *is_detached)
 }
 
 /**
+ * Add the head_sha1 and head_ref (if not detached) to the given worktree
+ */
+static void add_head_info(struct strbuf *head_ref, struct worktree *worktree)
+{
+	if (head_ref->len) {
+		if (worktree->is_detached) {
+			get_sha1_hex(head_ref->buf, worktree->head_sha1);
+		} else {
+			resolve_ref_unsafe(head_ref->buf, 0, worktree->head_sha1, NULL);
+			worktree->head_ref = strbuf_detach(head_ref, NULL);
+		}
+	}
+}
+
+/**
  * get the main worktree
  */
 static struct worktree *get_main_worktree(void)
@@ -59,19 +76,29 @@ static struct worktree *get_main_worktree(void)
 	struct strbuf worktree_path = STRBUF_INIT;
 	struct strbuf gitdir = STRBUF_INIT;
 	struct strbuf head_ref = STRBUF_INIT;
+	int is_bare = 0;
+	int is_detached = 0;
 
 	strbuf_addf(&gitdir, "%s", absolute_path(get_git_common_dir()));
 	strbuf_addbuf(&worktree_path, &gitdir);
-	if (!strbuf_strip_suffix(&worktree_path, "/.git"))
+	is_bare = !strbuf_strip_suffix(&worktree_path, "/.git");
+	if (is_bare)
 		strbuf_strip_suffix(&worktree_path, "/.");
 
 	strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
 
-	if (parse_ref(path.buf, &head_ref, NULL) >= 0) {
-		worktree = xmalloc(sizeof(struct worktree));
-		worktree->path = strbuf_detach(&worktree_path, NULL);
-		worktree->git_dir = strbuf_detach(&gitdir, NULL);
-	}
+	if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
+		goto done;
+
+	worktree = xmalloc(sizeof(struct worktree));
+	worktree->path = strbuf_detach(&worktree_path, NULL);
+	worktree->git_dir = strbuf_detach(&gitdir, NULL);
+	worktree->is_bare = is_bare;
+	worktree->head_ref = NULL;
+	worktree->is_detached = is_detached;
+	add_head_info(&head_ref, worktree);
+
+done:
 	strbuf_release(&path);
 	strbuf_release(&gitdir);
 	strbuf_release(&worktree_path);
@@ -86,6 +113,7 @@ static struct worktree *get_linked_worktree(const char *id)
 	struct strbuf worktree_path = STRBUF_INIT;
 	struct strbuf gitdir = STRBUF_INIT;
 	struct strbuf head_ref = STRBUF_INIT;
+	int is_detached = 0;
 
 	if (!id)
 		die("Missing linked worktree name");
@@ -107,11 +135,16 @@ static struct worktree *get_linked_worktree(const char *id)
 	strbuf_reset(&path);
 	strbuf_addf(&path, "%s/worktrees/%s/HEAD", get_git_common_dir(), id);
 
-	if (parse_ref(path.buf, &head_ref, NULL) >= 0) {
-		worktree = xmalloc(sizeof(struct worktree));
-		worktree->path = strbuf_detach(&worktree_path, NULL);
-		worktree->git_dir = strbuf_detach(&gitdir, NULL);
-	}
+	if (parse_ref(path.buf, &head_ref, &is_detached) < 0)
+		goto done;
+
+	worktree = xmalloc(sizeof(struct worktree));
+	worktree->path = strbuf_detach(&worktree_path, NULL);
+	worktree->git_dir = strbuf_detach(&gitdir, NULL);
+	worktree->is_bare = 0;
+	worktree->head_ref = NULL;
+	worktree->is_detached = is_detached;
+	add_head_info(&head_ref, worktree);
 
 done:
 	strbuf_release(&path);
diff --git a/worktree.h b/worktree.h
index 7022029..b4b3dda 100644
--- a/worktree.h
+++ b/worktree.h
@@ -4,6 +4,10 @@
 struct worktree {
 	char *path;
 	char *git_dir;
+	char *head_ref;
+	unsigned char head_sha1[20];
+	int is_detached;
+	int is_bare;
 };
 
 /* Functions for acting on the information about worktrees. */
-- 
2.6.0
Previous: Michael RappazzoNext: Michael Rappazzo
Message 4 of 5 in “worktree: add top-level worktree.c”
  1. 1/5 worktree: add top-level worktree.cMichael Rappazzo, Oct 8, 2015
  2. 2/5 worktree: refactor find_linked_symref functionMichael Rappazzo, Oct 8, 2015
  3. 3/5 worktree: add a function to get worktree detailsMichael Rappazzo, Oct 8, 2015
  4. 4/5 worktree: add details to the worktree structMichael Rappazzo, Oct 8, 2015
  5. 5/5 worktree: add 'list' commandMichael Rappazzo, Oct 8, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.