threads / discuss / 34025

Can `git blame` show the date that each line was merged?

Subject: Can `git blame` show the date that each line was merged?

## tl;dr

5 messages between Jun 4, 2013 and Jun 4, 2013.

replies: 4people: 3as markdown or json

Matt McClure· Jun 4, 2013, 13:39 UTC · lore

Can `git blame` show the date that each line was merged to the current branch rather than the date it was committed?

Aside: in some trial and error I notice this oddity:
    $ git blame --merges
    usage: git blame [options] [rev-opts] [rev] [--] file
        [rev-opts] are documented in git-rev-list(1)
    ...
    $ git help rev-list | grep -F -e --merges
                        [ --merges ]
           --merges
               --min-parents=2 is the same as --merges.
--max-parents=0 gives all
-- 
Matt McClure
http://matthewlmcclure.com
http://www.mapmyfitness.com/profile/matthewlmcclure
Jeff King· Jun 4, 2013, 15:56 UTC · re: Matt McClure · lore

Re: Can `git blame` show the date that each line was merged?

On Tue, Jun 04, 2013 at 09:39:45AM -0400, Matt McClure wrote:
> Can `git blame` show the date that each line was merged to the current
> branch rather than the date it was committed?

Not exactly. Git does not record when a commit entered a particular branch (or what the "ours" branch was called during a merge). If you follow a topic branch workflow in which an integrator merges each branch into master, then following the first parent of each merge will show the commits directly on master.

You can see this in action in git.git, which follows such a workflow, by doing "git log --first-parent", which shows only the commits created directly on master (mostly merges of topics, with a few trivial fixups interspersed).

Similarly, you should be able to do "git blame --first-parent foo.c" to pass blame only along the first-parent lines. Unfortunately, while "blame" uses the regular revision code to parse its options, it does its own traversal and does not respect each option. However, the patch to teach it about --first-parent is pretty trivial:

diff --git a/builtin/blame.c b/builtin/blame.c
index 57a487e..0fb67af 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -1199,6 +1199,8 @@ static int num_scapegoats(struct rev_info *revs, struct commit *commit)
 {
 	int cnt;
 	struct commit_list *l = first_scapegoat(revs, commit);
+	if (revs->first_parent_only)
+		return l ? 1 : 0;
 	for (cnt = 0; l; l = l->next)
 		cnt++;
 	return cnt;

(though I suspect it would interact oddly with the "--reverse" option,
and we would want to either declare them mutually exclusive or figure
out some sane semantics).

> Aside: in some trial and error I notice this oddity:
> 
>     $ git blame --merges
>     usage: git blame [options] [rev-opts] [rev] [--] file
> 
>         [rev-opts] are documented in git-rev-list(1)
>     ...

Your problem is not the presence of "--merges" here, but that you forgot
the necessary "file" argument. Try "git blame --merges foo.c".

However, this suffers from the same problem as --first-parent, in that
it is accepted but not respected. Doing so would not be impossible, but
it is a little more than the two-liner above.

-Peff
Matt McClure· Jun 4, 2013, 17:26 UTC · re: Jeff King · lore

Re: Can `git blame` show the date that each line was merged?

On Tue, Jun 4, 2013 at 11:56 AM, Jeff King <peff@peff.net> wrote:
Show 10 quoted lines
>> Aside: in some trial and error I notice this oddity:
>>
>>     $ git blame --merges
>>     usage: git blame [options] [rev-opts] [rev] [--] file
>>
>>         [rev-opts] are documented in git-rev-list(1)
>>     ...
>
> Your problem is not the presence of "--merges" here, but that you forgot
> the necessary "file" argument. Try "git blame --merges foo.c".
Oops. Thanks.
-- 
Matt McClure
http://matthewlmcclure.com
http://www.mapmyfitness.com/profile/matthewlmcclure
Junio C Hamano· Jun 4, 2013, 17:28 UTC · re: Jeff King · lore

Re: Can `git blame` show the date that each line was merged?

Jeff King <peff@peff.net> writes:
Show 17 quoted lines
> diff --git a/builtin/blame.c b/builtin/blame.c
> index 57a487e..0fb67af 100644
> --- a/builtin/blame.c
> +++ b/builtin/blame.c
> @@ -1199,6 +1199,8 @@ static int num_scapegoats(struct rev_info *revs, struct commit *commit)
>  {
>  	int cnt;
>  	struct commit_list *l = first_scapegoat(revs, commit);
> +	if (revs->first_parent_only)
> +		return l ? 1 : 0;
>  	for (cnt = 0; l; l = l->next)
>  		cnt++;
>  	return cnt;
>
> (though I suspect it would interact oddly with the "--reverse" option,
> and we would want to either declare them mutually exclusive or figure
> out some sane semantics).

It is entirely unclear who the first child is, so I tend to think that they have to be mutually exclusive.

Show 14 quoted lines
>> Aside: in some trial and error I notice this oddity:
>> 
>>     $ git blame --merges
>>     usage: git blame [options] [rev-opts] [rev] [--] file
>> 
>>         [rev-opts] are documented in git-rev-list(1)
>>     ...
>
> Your problem is not the presence of "--merges" here, but that you forgot
> the necessary "file" argument. Try "git blame --merges foo.c".
>
> However, this suffers from the same problem as --first-parent, in that
> it is accepted but not respected. Doing so would not be impossible, but
> it is a little more than the two-liner above.

What the command does when it "respects" it is unclear to me. In a history like this:

---A---B---C
    \       \
     E---F---G---H

and starting at H, pretend everything that happened in, B, C, E and F since A was done by G? Who gets the blame for what A or H did?

Jeff King· Jun 4, 2013, 17:44 UTC · re: Junio C Hamano · lore

Re: Can `git blame` show the date that each line was merged?

On Tue, Jun 04, 2013 at 10:28:06AM -0700, Junio C Hamano wrote:
Show 6 quoted lines
> > (though I suspect it would interact oddly with the "--reverse" option,
> > and we would want to either declare them mutually exclusive or figure
> > out some sane semantics).
> 
> It is entirely unclear who the first child is, so I tend to think
> that they have to be mutually exclusive.

That's my thinking, too, but I didn't want to rule out somebody thinking of something clever.

Show 16 quoted lines
> > Your problem is not the presence of "--merges" here, but that you forgot
> > the necessary "file" argument. Try "git blame --merges foo.c".
> >
> > However, this suffers from the same problem as --first-parent, in that
> > it is accepted but not respected. Doing so would not be impossible, but
> > it is a little more than the two-liner above.
> 
> What the command does when it "respects" it is unclear to me.
> In a history like this:
> 
> ---A---B---C
>     \       \
>      E---F---G---H
> 
> and starting at H, pretend everything that happened in, B, C, E and
> F since A was done by G?  Who gets the blame for what A or H did?

In general, I would expect "git blame" with revision arguments to behave as if it was fed the history graph (including parent rewriting). So in this case, I would think it would blame everything before G on G (assuming there are no merges before A), and everything in H would be "not yet committed".

That being said, we do not seem to rewrite parents for min/max parent cases even in "git log". I'm not sure why, nor can I seem to provoke it with simplification options. So maybe I am missing something clever.

-Peff

← back to recent threads