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

[PATCH 3/4] mark_reachable_objects(): optionally collect broken refs

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Sep 24, 2015, 09:13 UTC
Message-ID
<bcceeae4c8f7f690502651dd6cc537b2f4918fac.1443085919.git.johannes.schindelin@gmx.de>
In-Reply-To
<cover.1443085919.git.johannes.schindelin@gmx.de>

The behavior of `mark_reachable_objects()` without this patch is that it dies if it encounters a broken ref. This is sometimes undesirable, e.g. when garbage collecting in a repository with a stale remote HEAD.

So let's introduce an optional parameter to collect such broken refs. The behavior of the function is unchanged if that parameter is `NULL`.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 builtin/prune.c  |  2 +-
 builtin/reflog.c |  2 +-
 reachable.c      | 26 ++++++++++++++++++++------
 reachable.h      |  3 ++-
 4 files changed, 24 insertions(+), 9 deletions(-)
diff --git a/builtin/prune.c b/builtin/prune.c
index 10b03d3..d6f664f 100644
--- a/builtin/prune.c
+++ b/builtin/prune.c
@@ -136,7 +136,7 @@ int cmd_prune(int argc, const char **argv, const char *prefix)
 	if (show_progress)
 		progress = start_progress_delay(_("Checking connectivity"), 0, 0, 2);
 
-	mark_reachable_objects(&revs, 1, expire, progress);
+	mark_reachable_objects(&revs, 1, expire, progress, NULL);
 	stop_progress(&progress);
 	for_each_loose_file_in_objdir(get_object_directory(), prune_object,
 				      prune_cruft, prune_subdir, NULL);
diff --git a/builtin/reflog.c b/builtin/reflog.c
index f96ca2a..cb8758a 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -583,7 +583,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
 		init_revisions(&cb.cmd.revs, prefix);
 		if (flags & EXPIRE_REFLOGS_VERBOSE)
 			printf("Marking reachable objects...");
-		mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL);
+		mark_reachable_objects(&cb.cmd.revs, 0, 0, NULL, NULL);
 		if (flags & EXPIRE_REFLOGS_VERBOSE)
 			putchar('\n');
 	}
diff --git a/reachable.c b/reachable.c
index 9cff25b..1fc7ada 100644
--- a/reachable.c
+++ b/reachable.c
@@ -15,6 +15,11 @@ struct connectivity_progress {
 	unsigned long count;
 };
 
+struct add_one_data {
+	struct rev_info *revs;
+	struct string_list *broken_refs;
+};
+
 static void update_progress(struct connectivity_progress *cp)
 {
 	cp->count++;
@@ -25,10 +30,14 @@ static void update_progress(struct connectivity_progress *cp)
 static int add_one_ref(const char *path, const struct object_id *oid,
 		       int flag, void *cb_data)
 {
-	struct object *object = parse_object_or_die(oid->hash, path);
-	struct rev_info *revs = (struct rev_info *)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);
 
-	add_pending_object(revs, object, "");
+	if (!object)
+		string_list_append(data->broken_refs, path);
+	else
+		add_pending_object(data->revs, object, "");
 
 	return 0;
 }
@@ -153,9 +162,11 @@ 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 progress *progress,
+			    struct string_list *broken_refs)
 {
 	struct connectivity_progress cp;
+	struct add_one_data data;
 
 	/*
 	 * Set up revision parsing, and mark us as being interested
@@ -168,11 +179,14 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog,
 	/* Add all refs from the index file */
 	add_index_objects_to_pending(revs, 0);
 
+	data.revs = revs;
+	data.broken_refs = broken_refs;
+
 	/* Add all external refs */
-	for_each_ref(add_one_ref, revs);
+	for_each_ref(add_one_ref, &data);
 
 	/* detached HEAD is not included in the list above */
-	head_ref(add_one_ref, revs);
+	head_ref(add_one_ref, &data);
 
 	/* Add all reflog info */
 	if (mark_reflog)
diff --git a/reachable.h b/reachable.h
index d23efc3..39de1c7 100644
--- a/reachable.h
+++ b/reachable.h
@@ -5,6 +5,7 @@ struct progress;
 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 *);
+				   unsigned long mark_recent, struct progress *,
+				   struct string_list *broken_refs);
 
 #endif
-- 
2.5.2.windows.2
Previous: Jeff KingNext: Jeff King
Message 5 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.