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

[PATCH 6/6] checkout: do not die when leaving broken detached HEAD

From
Jeff King <peff@peff.net>
Date
Oct 24, 2013, 08:54 UTC
Message-ID
<20131024085452.GF1346@sigill.intra.peff.net>
In-Reply-To
<20131024085213.GA1267@sigill.intra.peff.net>

If we move away from a detached HEAD that has broken or corrupted commits, we might die in two places:

  1. Printing the "old HEAD was..." message.
  2. Printing the list of orphaned commits.

In both cases, we ignore the return value of parse_commit and feed the resulting commit to the pretty-print machinery, which will die() upon failing to read the commit object itself.

Since both cases are ancillary to the real operation being performed, let's be more robust and keep going. This lets users more easily checkout away from broken history.

Signed-off-by: Jeff King <peff@peff.net>
---
 builtin/checkout.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 34a2e43..1df55c0 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -380,8 +380,8 @@ static void show_local_changes(struct object *head,
 static void describe_detached_head(const char *msg, struct commit *commit)
 {
 	struct strbuf sb = STRBUF_INIT;
-	parse_commit(commit);
-	pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
+	if (!parse_commit(commit))
+		pp_commit_easy(CMIT_FMT_ONELINE, commit, &sb);
 	fprintf(stderr, "%s %s... %s\n", msg,
 		find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV), sb.buf);
 	strbuf_release(&sb);
@@ -677,12 +677,12 @@ static int add_pending_uninteresting_ref(const char *refname,
 
 static void describe_one_orphan(struct strbuf *sb, struct commit *commit)
 {
-	parse_commit(commit);
 	strbuf_addstr(sb, "  ");
 	strbuf_addstr(sb,
 		find_unique_abbrev(commit->object.sha1, DEFAULT_ABBREV));
 	strbuf_addch(sb, ' ');
-	pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
+	if (!parse_commit(commit))
+		pp_commit_easy(CMIT_FMT_ONELINE, commit, sb);
 	strbuf_addch(sb, '\n');
 }
 
-- 
1.8.4.1.898.g8bf8a41.dirty
Previous: Jeff King
Message 7 of 7 in “fix some parse_commit segfaults”
  1. 0/6 fix some parse_commit segfaultsJeff King, Oct 24, 2013
  2. 1/6 log_tree_diff: die when we fail to parse a commitJeff King, Oct 24, 2013
  3. 2/6 assume parse_commit checks commit->object.parsedJeff King, Oct 24, 2013
  4. 3/6 assume parse_commit checks for NULL commitJeff King, Oct 24, 2013
  5. 4/6 use parse_commit_or_die instead of segfaultingJeff King, Oct 24, 2013
  6. 5/6 use parse_commit_or_die instead of custom messageJeff King, Oct 24, 2013
  7. 6/6 checkout: do not die when leaving broken detached HEADJeff King, Oct 24, 2013

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.