[PATCH v2 1/2] refs: Add for_each_worktree_ref for iterating over all worktree HEADs

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Manish Goregaokar <manishearth@xxxxxxxxx>

To ensure that `git prune` does not remove refs checked out
in other worktrees, we need to include these HEADs in the
set of roots. This adds the iteration function necessary
to do this.

Signed-off-by: Manish Goregaokar <manishearth@xxxxxxxxx>
---
 refs.c | 20 ++++++++++++++++++++
 refs.h |  1 +
 2 files changed, 21 insertions(+)

diff --git a/refs.c b/refs.c
index 2d71774..7dc82ba 100644
--- a/refs.c
+++ b/refs.c
@@ -3,10 +3,12 @@
  */
 
 #include "cache.h"
+#include "commit.h"
 #include "lockfile.h"
 #include "refs.h"
 #include "refs/refs-internal.h"
 #include "object.h"
+#include "worktree.h"
 #include "tag.h"
 
 /*
@@ -1157,6 +1159,24 @@ int head_ref(each_ref_fn fn, void *cb_data)
 	return head_ref_submodule(NULL, fn, cb_data);
 }
 
+int for_each_worktree_ref(each_ref_fn fn, void *cb_data)
+{
+	int i, flag, retval = 0;
+	struct object_id oid;
+	struct worktree **worktrees = get_worktrees(GWT_SORT_LINKED);
+	struct commit* commit;
+	for (i = 0; worktrees[i]; i++) {
+		if ((commit = lookup_commit_reference(worktrees[i]->head_sha1))) {
+			oid = commit->object.oid;
+			if (!read_ref_full("HEAD", RESOLVE_REF_READING, oid.hash, &flag)) {
+				if ((retval = fn("HEAD", &oid, flag, cb_data)))
+					return retval;
+			}
+		}
+	}
+	return retval;
+}
+
 /*
  * Call fn for each reference in the specified submodule for which the
  * refname begins with prefix. If trim is non-zero, then trim that
diff --git a/refs.h b/refs.h
index 9fbff90..425a853 100644
--- a/refs.h
+++ b/refs.h
@@ -192,6 +192,7 @@ typedef int each_ref_fn(const char *refname,
  * stop the iteration.
  */
 int head_ref(each_ref_fn fn, void *cb_data);
+int for_each_worktree_ref(each_ref_fn fn, void *cb_data);
 int for_each_ref(each_ref_fn fn, void *cb_data);
 int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data);
 int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data,
-- 
2.10.1




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]