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

[PATCH v2 0/4] Fix gc failure when a remote HEAD goes stale

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Sep 28, 2015, 14:01 UTC
Message-ID
<cover.1443448748.git.johannes.schindelin@gmx.de>
In-Reply-To
<xmqqr3lnuzqu.fsf@gitster.mtv.corp.google.com>

There has been a report in the Git for Windows project that gc fails sometimes: https://github.com/git-for-windows/git/issues/423

It turns out that there are cases when a remote HEAD can go stale and it is not the user's fault at all. It can happen, for example, if the active branch in the remote repository gets renamed.

Git's garbage collector should handle this gracefully. The best this developer could come up with, is to simply warn and delete broken symrefs.

Thanks to Junio and Peff for their really valuable sanity check.
Interdiff vs v1 below diffstat.
Johannes Schindelin (4):
  gc: demonstrate failure with stale remote HEAD
  pack-objects: do not get distracted by broken symrefs
  mark_reachable_objects(): optionally collect broken symrefs
  gc: remove broken refs
 builtin/prune.c  | 12 +++++++++++-
 builtin/reflog.c |  2 +-
 reachable.c      | 31 +++++++++++++++++++++++++------
 reachable.h      |  3 ++-
 t/t6500-gc.sh    | 15 +++++++++++++++
 5 files changed, 54 insertions(+), 9 deletions(-)
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index ef2f794..1c63f8f 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -2499,7 +2499,6 @@ static void get_object_list(int ac, const char **av)
 	int flags = 0;
 
 	init_revisions(&revs, NULL);
-	revs.ignore_missing = 1;
 	save_commit_buffer = 0;
 	setup_revisions(ac, av, &revs, NULL);
 
diff --git a/builtin/prune.c b/builtin/prune.c
index 1a30f65..337b12a 100644
--- a/builtin/prune.c
+++ b/builtin/prune.c
@@ -101,7 +101,7 @@ static void remove_temporary_files(const char *path)
 int cmd_prune(int argc, const char **argv, const char *prefix)
 {
 	struct rev_info revs;
-	struct string_list broken_refs = STRING_LIST_INIT_DUP;
+	struct string_list broken_symrefs = STRING_LIST_INIT_DUP;
 	struct progress *progress = NULL;
 	const struct option options[] = {
 		OPT__DRY_RUN(&show_only, N_("do not remove, show only")),
@@ -140,9 +140,9 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
 		progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
 
 	revs.ignore_missing = 1;
-	mark_reachable_objects(&revs, 1, expire, progress, &broken_refs);
-	for (i = 0; i < broken_refs.nr; i++) {
-		char *path = broken_refs.items[i].string;
+	mark_reachable_objects(&revs, 1, expire, progress, &broken_symrefs);
+	for (i = 0; i < broken_symrefs.nr; i++) {
+		char *path = broken_symrefs.items[i].string;
 		printf("Removing stale ref %s\n", path);
 		if (!show_only && delete_ref(path, NULL, REF_NODEREF))
 			die("Could not remove stale ref %s", path);
diff --git a/reachable.c b/reachable.c
index 1fc7ada..25c4932 100644
--- a/reachable.c
+++ b/reachable.c
@@ -17,7 +17,7 @@ struct connectivity_progress {
 
 struct add_one_data {
 	struct rev_info *revs;
-	struct string_list *broken_refs;
+	struct string_list *broken_symrefs;
 };
 
 static void update_progress(struct connectivity_progress *cp)
@@ -31,13 +31,18 @@ static int add_one_ref(const char *path, const struct object_id *oid,
 		       int flag, void *cb_data)
 {
 	struct add_one_data *data = (struct add_one_data *)cb_data;
-	struct object *object = data->broken_refs ? parse_object(oid->hash) :
-		parse_object_or_die(oid->hash, path);
+	struct object *object;
 
-	if (!object)
-		string_list_append(data->broken_refs, path);
-	else
-		add_pending_object(data->revs, object, "");
+	if ((flag & REF_ISSYMREF) && (flag & REF_ISBROKEN)) {
+		if (data->broken_symrefs)
+			string_list_append(data->broken_symrefs, path);
+		else
+			warning("ref is broken: %s", path);
+		return 0;
+	}
+
+	object = parse_object_or_die(oid->hash, path);
+	add_pending_object(data->revs, object, "");
 
 	return 0;
 }
@@ -163,7 +168,7 @@ int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
 void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
 			    unsigned long mark_recent,
 			    struct progress *progress,
-			    struct string_list *broken_refs)
+			    struct string_list *broken_symrefs)
 {
 	struct connectivity_progress cp;
 	struct add_one_data data;
@@ -180,7 +185,7 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
 	add_index_objects_to_pending(revs, 0);
 
 	data.revs = revs;
-	data.broken_refs = broken_refs;
+	data.broken_symrefs = broken_symrefs;
 
 	/* Add all external refs */
 	for_each_ref(add_one_ref, &data);
diff --git a/reachable.h b/reachable.h
index 39de1c7..06f1400 100644
--- a/reachable.h
+++ b/reachable.h
@@ -6,6 +6,6 @@ extern int add_unseen_recent_objects_to_traversal(struct rev_info *revs,
 						  unsigned long timestamp);
 extern void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
 				   unsigned long mark_recent, struct progress *,
-				   struct string_list *broken_refs);
+				   struct string_list *broken_symrefs);
 
 #endif
-- 
2.5.3.windows.1.3.gc322723
Previous: Jeff KingNext: Johannes Schindelin
Message 11 of 28 in “Fix gc failure when a remote HEAD goes stale”
  1. 0/4 Fix gc failure when a remote HEAD goes staleJohannes Schindelin, Sep 24, 2015
  2. 1/4 gc: demonstrate failure with stale remote HEADJohannes Schindelin, Sep 24, 2015
  3. 2/4 pack-objects: do not get distracted by stale refsJohannes Schindelin, Sep 24, 2015
  4. Jeff KingSep 24, 2015
  5. 3/4 mark_reachable_objects(): optionally collect broken refsJohannes Schindelin, Sep 24, 2015
  6. Jeff KingSep 24, 2015
  7. 4/4 gc: remove broken refsJohannes Schindelin, Sep 24, 2015
  8. Jeff KingSep 24, 2015
  9. Junio C HamanoSep 25, 2015
  10. Jeff KingSep 25, 2015
  11. 0/4 Fix gc failure when a remote HEAD goes staleJohannes Schindelin, Sep 28, 2015
  12. 1/4 gc: demonstrate failure with stale remote HEADJohannes Schindelin, Sep 28, 2015
  13. 2/4 pack-objects: do not get distracted by broken symrefsJohannes Schindelin, Sep 28, 2015
  14. 3/4 mark_reachable_objects(): optionally collect broken symrefsJohannes Schindelin, Sep 28, 2015
  15. 4/4 gc: remove broken symrefsJohannes Schindelin, Sep 28, 2015
  16. Junio C HamanoSep 28, 2015
  17. Junio C HamanoSep 28, 2015
  18. Johannes SchindelinSep 28, 2015
  19. Junio C HamanoOct 5, 2015
  20. Jeff KingSep 28, 2015
  21. Johannes SchindelinSep 28, 2015
  22. 0/2 Fix gc failure when a remote HEAD goes staleJohannes Schindelin, Oct 6, 2015
  23. 1/2 gc: demonstrate failure with stale remote HEADJohannes Schindelin, Oct 6, 2015
  24. 2/2 pack-objects: do not get distracted by broken symrefsJohannes Schindelin, Oct 6, 2015
  25. Junio C HamanoOct 7, 2015
  26. Johannes SchindelinOct 8, 2015
  27. Junio C HamanoOct 8, 2015
  28. Johannes SchindelinOct 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.