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

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

From
Jeff King <peff@peff.net>
Date
Oct 8, 2013, 14:00 UTC
Message-ID
<20131008140005.GE7650@sigill.intra.peff.net>
In-Reply-To
<20131008134843.GA7530@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 user more easily checkout away from broken history.

Note that the call in describe_detached_head is also used to describe the new commit we are moving to. We would want to die in that case, but that is already handled much earlier, when we parse the commit to get the tree to move to.

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 describe_detached_head(const char *msg, struct commit *commit)
 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 void describe_one_orphan(struct strbuf *sb, struct commit *commit)
 
 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.4.gf327177
Previous: Jeff King
Message 6 of 6 in “parse_commit cleanups”
  1. 0/5 parse_commit cleanupsJeff King, Oct 8, 2013
  2. 1/5 assume parse_commit checks commit->object.parsedJeff King, Oct 8, 2013
  3. 2/5 assume parse_commit checks for NULL commitJeff King, Oct 8, 2013
  4. 3/5 use parse_commit_or_die instead of segfaultingJeff King, Oct 8, 2013
  5. 4/5 use parse_commit_or_die instead of custom messageJeff King, Oct 8, 2013
  6. 5/5 checkout: do not die when leaving broken detached HEADJeff King, Oct 8, 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.