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

Re: [PATCH v2] reflog-walk: don't segfault on non-commit sha1's in the reflog

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 30, 2015, 22:42 UTC
Message-ID
<xmqqk2nvd0cz.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20151230221705.GA4025@spirit>
Dennis Kaarsemaker <dennis@kaarsemaker.net> writes:
> The really correct way of fixing this bug may actually be a level higher, and
> making git reflog not rely on information about parent commits, whether they
> are fake or not.
I tend to agree.

The only common thing between "git reflog" wants to do (i.e. showing the objects referred to by reflog entries) and what the normal "git log" was/is designed to do is "we have many things, and we show them one by one". As the "many things" we have in the context of the normal "git log" are all commits, it is reasonable that the internal interface (i.e. revision.c::get_revision()) to iterate over these "many things" returns a "struct commit *" and it also is reasonable that "show them one by one" is done by calling log_tree_commit() in builtin/log.c::cmd_log_walk(). Neither is suitable to deal with series of reflog entries in general. A proper implementation of "git reflog" would have liked to be able to iterate over "many things" by returning "struct object *" one-by-one, and then do the equivalent of the switch() statement in builtin/log.c::cmd_show() to show these objects.

The way "git log" was abused and made to show entries from reflog is one of the ugly and unfortunate hacks in our codebase.

However, I see that there are one of two things that you could do to make this part of code do slightly better than stopping at the first non-commit object:

 - pretend that the non-commit entry never existed in the first
   place and return the commit that appears in the reflog next.
 - fabricate a fake "commit" object that says "I am not a commit;
   I merely exist to represent that the reflog you are walking has
   this non-commit object at this point in the sequence" and return
   it, instead of giving NULL in the error path.
Show 18 quoted lines
> diff --git a/t/t1410-reflog.sh b/t/t1410-reflog.sh
> index b79049f..130d671 100755
> --- a/t/t1410-reflog.sh
> +++ b/t/t1410-reflog.sh
> @@ -325,4 +325,17 @@ test_expect_success 'parsing reverse reflogs at BUFSIZ boundaries' '
>  	test_cmp expect actual
>  '
>  
> +test_expect_success 'no segfaults for reflog containing non-commit sha1s' '
> +	git update-ref --create-reflog -m "Creating ref" \
> +		refs/tests/tree-in-reflog HEAD &&
> +	git update-ref -m "Forcing tree" refs/tests/tree-in-reflog HEAD^{tree} &&
> +	git update-ref -m "Restoring to commit" refs/tests/tree-in-reflog HEAD &&
> +	git reflog refs/tests/tree-in-reflog
> +'
> +
> +test_expect_failure 'reflog containing non-commit sha1s displays fully' '
> +	git reflog refs/tests/tree-in-reflog > actual &&
Please write this without space after the redirection operator, i.e.
	git reflog refs/tests/tree-in-reflog >actual &&
> +	test_line_count = 3 actual
> +'
> +
>  test_done
Previous: Dennis KaarsemakerNext: Dennis Kaarsemaker
Message 14 of 25 in “Segfault in git reflog”
  1. Dennis KaarsemakerDec 30, 2015
  2. Duy NguyenDec 30, 2015
  3. Dennis KaarsemakerDec 30, 2015
  4. Duy NguyenDec 30, 2015
  5. Duy NguyenDec 30, 2015
  6. Dennis KaarsemakerDec 30, 2015
  7. Duy NguyenDec 30, 2015
  8. reflog-walk: don't segfault on non-commit sha1's in the reflogDennis Kaarsemaker, Dec 30, 2015
  9. Junio C HamanoDec 30, 2015
  10. Dennis KaarsemakerDec 30, 2015
  11. Junio C HamanoDec 30, 2015
  12. Dennis KaarsemakerDec 30, 2015
  13. reflog-walk: don't segfault on non-commit sha1's in the reflogDennis Kaarsemaker, Dec 30, 2015
  14. Junio C HamanoDec 30, 2015
  15. reflog-walk: don't segfault on non-commit sha1's in the reflogDennis Kaarsemaker, Dec 30, 2015
  16. Junio C HamanoDec 31, 2015
  17. Dennis KaarsemakerDec 31, 2015
  18. Dennis KaarsemakerDec 31, 2015
  19. reflog-walk: don't segfault on non-commit sha1's in the reflogDennis Kaarsemaker, Jan 5, 2016
  20. Eric SunshineJan 6, 2016
  21. Dennis KaarsemakerJan 6, 2016
  22. Eric SunshineJan 6, 2016
  23. Eric SunshineJan 6, 2016
  24. Dennis KaarsemakerJan 6, 2016
  25. Duy NguyenJan 6, 2016

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.