# [RFD/WIP PATCH] rev-list: list all heads with --all

1 messages from 2015-11-09 to 2015-11-09. Participants: Michael J Gruber.
Thread: https://gitlist.dev/t/40755

## Michael J Gruber, 2015-11-09 15:24

Subject: [RFD/WIP PATCH] rev-list: list all heads with --all
Message-ID: <c224529fd3d2651baa0f9232a842e4ae7dd788cb.1447082178.git.git@drmicha.warpmail.net>
URL: https://gitlist.dev/e/c224529fd3d2651baa0f9232a842e4ae7dd788cb.1447082178.git.git%40drmicha.warpmail.net

```
HEAD is a worktree specific sysmref, so that a repository with multiple
worktrees can have multiple HEADs, or HEADs in a worktree different from
the current worktree.

So far, "rev-parse --all" adds only the HEAD from the current worktree
to the list of refs (besides branches etc.). So, a detached HEAD from a
different checkout would be missed unless a shared ref (or current HEAD)
points to it (or descents from it). As a consequence, "git prune" can
prune detached HEADs from worktrees and leave the repo in an
inconsistent state.

Make "rev-parse --all" add the HEADs from all worktrees. This results in
a non-worktree-specific ref list amd solves the pruning problem.

Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net>
---

This patch solves the pruning problem with worktrees, but I feel quite
uneasy about substituting the ref solving at the very heart of refs.c. So
please consider this a mere poc and a request for discussion/input
about how to do this the right way.

I'll cook up a test in proper form, but all you have to do is "worktree
add --detach new" and build a new commit on top of the detached head in
the new worktree. Compare "git prune -n" in "new" and the main worktree...

In essence, I feel the worktree interface still has to evolve a bit: I'd
rather for_each_worktree than loop myself, and if many call sites need to
be aware of multiple heads or worktrees than get_worktrees() should be
part of our init stuff, not here.

 refs.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)

diff --git a/refs.c b/refs.c
index bab92d7..c355171 100644
--- a/refs.c
+++ b/refs.c
@@ -5,6 +5,7 @@
 #include "tag.h"
 #include "dir.h"
 #include "string-list.h"
+#include "worktree.h"
 
 struct ref_lock {
 	char *ref_name;
@@ -2091,10 +2092,11 @@ static int do_for_each_ref(struct ref_cache *refs, const char *base,
 	return do_for_each_entry(refs, base, do_one_ref, &data);
 }
 
-static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
+static int do_head_refs(const char *submodule, each_ref_fn fn, void *cb_data)
 {
 	struct object_id oid;
-	int flag;
+	struct worktree **worktrees;
+	int i, retval;
 
 	if (submodule) {
 		if (resolve_gitlink_ref(submodule, "HEAD", oid.hash) == 0)
@@ -2103,20 +2105,25 @@ static int do_head_ref(const char *submodule, each_ref_fn fn, void *cb_data)
 		return 0;
 	}
 
-	if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag))
-		return fn("HEAD", &oid, flag, cb_data);
+	worktrees = get_worktrees();
+	retval = 0;
+	for (i=0; worktrees[i]; i++) {
+		hashcpy(oid.hash, worktrees[i]->head_sha1);
+		retval = retval || fn("HEAD", &oid, worktrees[i]->is_detached ? 0 : REF_ISSYMREF, cb_data);
+	}
 
-	return 0;
+	free_worktrees(worktrees);
+	return retval;
 }
 
 int head_ref(each_ref_fn fn, void *cb_data)
 {
-	return do_head_ref(NULL, fn, cb_data);
+	return do_head_refs(NULL, fn, cb_data);
 }
 
 int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data)
 {
-	return do_head_ref(submodule, fn, cb_data);
+	return do_head_refs(submodule, fn, cb_data);
 }
 
 int for_each_ref(each_ref_fn fn, void *cb_data)
-- 
2.6.3.507.gca54332

```
