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

Re: [PATCH] filter-branch: consider refs can refer to an object other than commit or tag

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 21, 2018, 17:21 UTC
Message-ID
<xmqqwoy5pcno.fsf@gitster-ct.c.googlers.com>
In-Reply-To
<1521646299-16193-1-git-send-email-orga.chem.job@gmail.com>
Yuki Kokubun <orga.chem.job@gmail.com> writes:
Show 5 quoted lines
> "git filter-branch -- --all" can be confused when refs that refer to objects
> other than commits or tags exists.
> Because "git rev-parse --all" that is internally used can return refs that
> refer to an object other than commit or tag. But it is not considered in the
> phase of updating refs.

Could you describe what the consequence of that is? We have a ref that points directly at a blob object, or a ref that points at a tag object that points at a blob object. The current code leaves both of these refs in "$tempdir/heads". Then...?

	... goes and looks ...
There is a loop that looks like this:
	while read ref
	do
		sha1=$(git rev-parse "$ref^0")
		...
	done <"$tempdir/heads"
which would break on anything but a commit-ish.
Show 5 quoted lines
>  # The refs should be updated if their heads were rewritten
>  git rev-parse --no-flags --revs-only --symbolic-full-name \
> -	--default HEAD "$@" > "$tempdir"/raw-heads || exit
> +	--default HEAD "$@" > "$tempdir"/raw-objects || exit
> +# refs/replace can refer to an object other than commit or tag

Mention of replace refs in the proposed log message gives an easy to understand example and is a good idea, but this in code comment does not have to single out the replace refs. A tag can also point at an object with any type, e.g. "git tag v2.6.11-tree v2.6.11^{tree}" would make "refs/tags/v2.6.11-tree" point at the tree at the top level of the tree-ish "v2.6.11". It probably is OK to drop this comment altogether.

Show 9 quoted lines
> +while read ref
> +do
> +	type=$(git cat-file -t "$ref")
> +	if test $type = commit || test $type = tag
> +	then
> +		echo "$ref"
> +	fi
> +done >"$tempdir"/raw-heads <"$tempdir"/raw-objects
>  sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads

So... is the idea to limit the set of refs to be rewritten to those that point at commits and tags? As I already alluded to, I do not think you want to accept a ref that points at any tag object---only the ones that point at a tag that points at a commit-ish, so that the code will not barf when doing "$ref^0".

So perhaps
	git rev-parse --no-flags ... >"$tempdir/raw-heads" || exit
	while read ref
	do
		case "$ref" in ^?*) continue ;; esac
		if git rev-parse --verify "$ref^0" 2>/dev/null
                then
			echo "$ref"
		fi
	done >"$tempdir/heads" <"$tempdir/raw-heads"

or something? Note that you do not need the "sed" as the loop already excludes the negative revs.

Show 17 quoted lines
>  test -s "$tempdir"/heads ||
> diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh
> index 7cb60799b..efeaf5887 100755
> --- a/t/t7003-filter-branch.sh
> +++ b/t/t7003-filter-branch.sh
> @@ -470,4 +470,17 @@ test_expect_success 'tree-filter deals with object name vs pathname ambiguity' '
>  	git show HEAD:$ambiguous
>  '
>  
> +test_expect_success 'rewrite repository including refs/replace that point to non commit object' '
> +	test_when_finished "git reset --hard original" &&
> +	tree=$(git rev-parse HEAD^{tree}) &&
> +	test_when_finished "git replace -d $tree" &&
> +	echo A >new &&
> +	git add new &&
> +	new_tree=$(git write-tree) &&
> +	git replace $tree $new_tree &&
Perhaps something like this here:
	git tag -a "tag to a tree" treetag $new_tree &&
can tell su how well it works with a tag that points at a tree?
Show 6 quoted lines
> +	git reset --hard HEAD &&
> +	git filter-branch -f -- --all >filter-output 2>&1 &&
> +	! fgrep fatal filter-output
> +'
> +
>  test_done
Previous: Yuki KokubunNext: Yuki Kokubun
Message 3 of 20 in “filter-branch: consider refs/replace can refer to an object other than commit”
  1. filter-branch: consider refs/replace can refer to an object other than commitYuki Kokubun, Mar 21, 2018
  2. filter-branch: consider refs can refer to an object other than commit or tagYuki Kokubun, Mar 21, 2018
  3. Junio C HamanoMar 21, 2018
  4. Yuki KokubunMar 21, 2018
  5. Junio C HamanoMar 21, 2018
  6. Yuki KokubunMar 22, 2018
  7. Junio C HamanoMar 22, 2018
  8. Yuki KokubunMar 23, 2018
  9. filter-branch: fix errors caused by refs that cannot be used with ^0Yuki Kokubun, Mar 23, 2018
  10. Junio C HamanoMar 23, 2018
  11. Yuki KokubunMar 24, 2018
  12. filter-branch: fix errors caused by refs that point at non-committishYuki Kokubun, Mar 24, 2018
  13. Junio C HamanoMar 25, 2018
  14. Yuki KokubunMar 25, 2018
  15. Junio C HamanoMar 25, 2018
  16. filter-branch: fix errors caused by refs that point at non-committishYuki Kokubun, Mar 25, 2018
  17. Yuki KokubunMar 25, 2018
  18. Junio C HamanoApr 8, 2018
  19. filter-branch: fix errors caused by refs that point at non-committishYuki Kokubun, Mar 25, 2018
  20. Junio C HamanoMar 25, 2018

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.