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

[PATCH v3 09/12] revision.c: --all adds HEAD from all worktrees

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Apr 19, 2017, 11:01 UTC
Message-ID
<20170419110145.5086-10-pclouds@gmail.com>
In-Reply-To
<20170419110145.5086-1-pclouds@gmail.com>
Unless single_worktree is set, --all now adds HEAD from all worktrees.

Since reachable.c code does not use setup_revisions(), we need to call other_head_refs_submodule() explicitly there to have the same effect on "git prune", so that we won't accidentally delete objects needed by some other HEADs.

A new FIXME is added because we would need something like
    int refs_other_head_refs(struct ref_store *, each_ref_fn, cb_data);
in addition to other_head_refs() to handle it, which might require
    int get_submodule_worktrees(const char *submodule, int flags);
It could be a separate topic to reduce the scope of this one.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 reachable.c      |  1 +
 refs.c           | 22 ++++++++++++++++++++++
 refs.h           |  1 +
 revision.c       | 13 +++++++++++++
 submodule.c      |  2 ++
 t/t5304-prune.sh | 12 ++++++++++++
 6 files changed, 51 insertions(+)
diff --git a/reachable.c b/reachable.c
index a8a979bd4f..a3b938b46c 100644
--- a/reachable.c
+++ b/reachable.c
@@ -177,6 +177,7 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
 
 	/* detached HEAD is not included in the list above */
 	head_ref(add_one_ref, revs);
+	other_head_refs(add_one_ref, revs);
 
 	/* Add all reflog info */
 	if (mark_reflog)
diff --git a/refs.c b/refs.c
index 537052f7ba..23e3607674 100644
--- a/refs.c
+++ b/refs.c
@@ -1780,3 +1780,25 @@ int rename_ref(const char *oldref, const char *newref, const char *logmsg)
 {
 	return refs_rename_ref(get_main_ref_store(), oldref, newref, logmsg);
 }
+
+int other_head_refs(each_ref_fn fn, void *cb_data)
+{
+	struct worktree **worktrees, **p;
+	int ret = 0;
+
+	worktrees = get_worktrees(0);
+	for (p = worktrees; *p; p++) {
+		struct worktree *wt = *p;
+		struct ref_store *refs;
+
+		if (wt->is_current)
+			continue;
+
+		refs = get_worktree_ref_store(wt);
+		ret = refs_head_ref(refs, fn, cb_data);
+		if (ret)
+			break;
+	}
+	free_worktrees(worktrees);
+	return ret;
+}
diff --git a/refs.h b/refs.h
index e06db37118..cc71b6c7a0 100644
--- a/refs.h
+++ b/refs.h
@@ -247,6 +247,7 @@ int refs_for_each_remote_ref(struct ref_store *refs,
 			     each_ref_fn fn, void *cb_data);
 
 int head_ref(each_ref_fn fn, void *cb_data);
+int other_head_refs(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,
diff --git a/revision.c b/revision.c
index c329070c89..040a0064f6 100644
--- a/revision.c
+++ b/revision.c
@@ -2105,6 +2105,13 @@ static int handle_revision_pseudo_opt(const char *submodule,
 	int argcount;
 
 	if (submodule) {
+		/*
+		 * We need some something like get_submodule_worktrees()
+		 * before we can go through all worktrees of a submodule,
+		 * .e.g with adding all HEADs from --all, which is not
+		 * supported right now, so stick to single worktree.
+		 */
+		assert(revs->single_worktree != 0);
 		refs = get_submodule_ref_store(submodule);
 	} else
 		refs = get_main_ref_store();
@@ -2122,6 +2129,12 @@ static int handle_revision_pseudo_opt(const char *submodule,
 	if (!strcmp(arg, "--all")) {
 		handle_refs(refs, revs, *flags, refs_for_each_ref);
 		handle_refs(refs, revs, *flags, refs_head_ref);
+		if (!revs->single_worktree) {
+			struct all_refs_cb cb;
+
+			init_all_refs_cb(&cb, revs, *flags);
+			other_head_refs(handle_one_ref, &cb);
+		}
 		clear_ref_exclusion(&revs->ref_excludes);
 	} else if (!strcmp(arg, "--branches")) {
 		handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
diff --git a/submodule.c b/submodule.c
index a31f68812c..8c5af6e7f3 100644
--- a/submodule.c
+++ b/submodule.c
@@ -1225,6 +1225,8 @@ static int find_first_merges(struct object_array *result, const char *path,
 			oid_to_hex(&a->object.oid));
 	init_revisions(&revs, NULL);
 	rev_opts.submodule = path;
+	/* FIXME: can't handle linked worktrees in submodules yet */
+	revs.single_worktree = path != NULL;
 	setup_revisions(ARRAY_SIZE(rev_args)-1, rev_args, &revs, &rev_opts);
 
 	/* save all revisions from the above list that contain b */
diff --git a/t/t5304-prune.sh b/t/t5304-prune.sh
index cba45c7be9..683bdb031c 100755
--- a/t/t5304-prune.sh
+++ b/t/t5304-prune.sh
@@ -292,4 +292,16 @@ test_expect_success 'prune: handle index in multiple worktrees' '
 	test_cmp second-worktree/blob actual
 '
 
+test_expect_success 'prune: handle HEAD in multiple worktrees' '
+	git worktree add --detach third-worktree &&
+	echo "new blob for third-worktree" >third-worktree/blob &&
+	git -C third-worktree add blob &&
+	git -C third-worktree commit -m "third" &&
+	rm .git/worktrees/third-worktree/index &&
+	test_must_fail git -C third-worktree show :blob &&
+	git prune --expire=now &&
+	git -C third-worktree show HEAD:blob >actual &&
+	test_cmp third-worktree/blob actual
+'
+
 test_done
-- 
2.11.0.157.gd943d85
Previous: Michael HaggertyNext: Michael Haggerty
Message 22 of 30 in “Fix git-gc losing objects in multi worktree”
  1. 00/12 Fix git-gc losing objects in multi worktreeNguyễn Thái Ngọc Duy, Apr 19, 2017
  2. 01/12 revision.h: new flag in struct rev_info wrt. worktree-related refsNguyễn Thái Ngọc Duy, Apr 19, 2017
  3. 02/12 revision.c: refactor add_index_objects_to_pending()Nguyễn Thái Ngọc Duy, Apr 19, 2017
  4. 03/12 revision.c: --indexed-objects add objects from all worktreesNguyễn Thái Ngọc Duy, Apr 19, 2017
  5. 04/12 refs.c: refactor get_submodule_ref_store(), share common free blockNguyễn Thái Ngọc Duy, Apr 19, 2017
  6. Michael HaggertyApr 22, 2017
  7. 05/12 refs: move submodule slash stripping code to get_submodule_ref_storeNguyễn Thái Ngọc Duy, Apr 19, 2017
  8. Johannes SixtApr 19, 2017
  9. Duy NguyenApr 20, 2017
  10. Duy NguyenApr 20, 2017
  11. Johannes SixtApr 20, 2017
  12. Michael HaggertyApr 22, 2017
  13. Stefan BellerApr 24, 2017
  14. 06/12 refs: add refs_head_ref()Nguyễn Thái Ngọc Duy, Apr 19, 2017
  15. Michael HaggertyApr 22, 2017
  16. Duy NguyenApr 22, 2017
  17. 07/12 revision.c: use refs_for_each*() instead of for_each_*_submodule()Nguyễn Thái Ngọc Duy, Apr 19, 2017
  18. 08/12 refs: remove dead for_each_*_submodule()Nguyễn Thái Ngọc Duy, Apr 19, 2017
  19. Ramsay JonesApr 19, 2017
  20. Junio C HamanoApr 20, 2017
  21. Michael HaggertyApr 22, 2017
  22. 09/12 revision.c: --all adds HEAD from all worktreesNguyễn Thái Ngọc Duy, Apr 19, 2017
  23. Michael HaggertyApr 22, 2017
  24. 10/12 files-backend: make reflog iterator go through per-worktree reflogNguyễn Thái Ngọc Duy, Apr 19, 2017
  25. Michael HaggertyApr 22, 2017
  26. Duy NguyenApr 23, 2017
  27. Michael HaggertyMay 17, 2017
  28. 11/12 revision.c: --reflog add HEAD reflog from all worktreesNguyễn Thái Ngọc Duy, Apr 19, 2017
  29. 12/12 rev-list: expose and document --single-worktreeNguyễn Thái Ngọc Duy, Apr 19, 2017
  30. Michael HaggertyApr 22, 2017

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.