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

Re: How to merge by subtree while preserving history?

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 27, 2009, 17:20 UTC
Message-ID
<7veiwievbv.fsf@gitster.siamese.dyndns.org>
In-Reply-To
<BA69E81F-C93F-4AD8-8486-A27B0A284D9A@gmail.com>
David Reitter <david.reitter@gmail.com> writes:
> ...  Is there a command that gives me
> the diff  for a revision pair, restricted to what happened to content
> in a given file in the current tree?

You can get a half of it from blame (and I presume the other half by running the procedure in reverse).

"git blame" has an obscure switch -S that lets you lie about the ancestry by allowing you to install a graft (this is primarily used by the annotate operation of git-cvsserver).

Suppose you have revisions A and B, and a lot of code in a file F in the original revision A migrated to many other places in a later revision B over time. You want to see where each and every line in F from A ended up in B.

To compute this, you pretend as if the history originates at B (i.e. B is the root commit), and A is a direct descendant of it, and blame each and every line of F in A, with a very agressive setting. E.g.

	{
		echo $(git rev-parse A) $(git rev-parse B)
                echo $(git rev-parse B)
	} >tmp-graft
        git blame -C -C -C -w -S tmp-graft A -- F

I'll leave it as an exercise to the readers how to compute "where did each and every line in G in B came from A?"

Note that in order for this to work, it needs a fix to "blame -S" that I posted about 10 days ago: aa9ea77 (blame: read custom grafts given by -S before calling setup_revisions(), 2009-03-18); the fix is sitting in 'pu', because as far as I know nobody has cared about the breakage other than I, at least until now.

I've attached a script that uses this trick to compute "How much of what Linus originally wrote still survives." People who attended GitTogether'08 may have seen the result.

---

#!/bin/sh # How much of the very original version from Linus survive?

_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]' _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"

initial=$(git rev-parse --verify e83c5163316f89bfbde7d9ab23ca2e25604af290) && this=$(git rev-parse --verify ${1-HEAD}^0) || exit

tmp="/var/tmp/Linus.$$" trap 'rm -f "$tmp".*' 0

# We blame each file in the initial revision pretending as if it is a
# direct descendant of the given version, and also pretend that the
# latter is a root commit.  This way, lines in the initial revision
# that survived to the other version can be identified (they will be
# attributed to the other version).
graft="$tmp.graft" &&
{
	echo "$initial $this"
	echo "$this"
} >"$graft" || exit
opts='-C -C -C -w'
git ls-tree -r "$initial" |
while read mode type sha1 name
do
	git blame $opts --porcelain -S "$graft" "$initial" -- "$name" |
	sed -ne "s/^\($_x40\) .*/\1/p" |
	sort |
	uniq -c | {
		# There are only two commits in the fake history, so
		# there won't be at most two output from the above.
		read cnt1 commit1
		read cnt2 commit2
		if test -z "$commit2"
		then
			cnt2=0
		fi
		if test "$initial" != "$commit1"
		then
			cnt_surviving=$cnt1
		else
			cnt_surviving=$cnt2
		fi
		cnt_total=$(( $cnt1 + $cnt2 ))
		echo "$cnt_surviving $cnt_total	$name"
	}
done | {
	total=0
	surviving=0
	while read s t n
	do
		total=$(( $total + $t )) surviving=$(( $surviving + $s ))
		printf "%6d / %-6d	%s\n" $s $t $n
	done
	printf "%6d / %-6d	%s\n" $surviving $total Total
}
Previous: David Reitter
Message 4 of 4 in “How to merge by subtree while preserving history?”
  1. David ReitterMar 26, 2009
  2. Miklos VajnaMar 27, 2009
  3. David ReitterMar 27, 2009
  4. Junio C HamanoMar 27, 2009

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.