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

5 messages from 2013-06-04 to 2013-06-04. Participants: Matt McClure, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/34025

## Matt McClure, 2013-06-04 13:39

Subject: Can `git blame` show the date that each line was merged?
Message-ID: <CAJELnLEiK1C9PeimSwDoJoy=wFbFF0+KoK3jhXSAV4b2DsBKqw@mail.gmail.com>
URL: https://gitlist.dev/e/CAJELnLEiK1C9PeimSwDoJoy%3DwFbFF0%2BKoK3jhXSAV4b2DsBKqw%40mail.gmail.com

```
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, 2013-06-04 15:56

Subject: Re: Can `git blame` show the date that each line was merged?
Message-ID: <20130604155605.GA15953@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20130604155605.GA15953%40sigill.intra.peff.net
In-Reply-To: <CAJELnLEiK1C9PeimSwDoJoy=wFbFF0+KoK3jhXSAV4b2DsBKqw@mail.gmail.com>

```
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, 2013-06-04 17:26

Subject: Re: Can `git blame` show the date that each line was merged?
Message-ID: <CAJELnLFy_by_7qmEqAyaNxV128ASN5VYoewwftWEt3ege3++zA@mail.gmail.com>
URL: https://gitlist.dev/e/CAJELnLFy_by_7qmEqAyaNxV128ASN5VYoewwftWEt3ege3%2B%2BzA%40mail.gmail.com
In-Reply-To: <20130604155605.GA15953@sigill.intra.peff.net>

```
On Tue, Jun 4, 2013 at 11:56 AM, Jeff King <peff@peff.net> wrote:
>> 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, 2013-06-04 17:28

Subject: Re: Can `git blame` show the date that each line was merged?
Message-ID: <7vfvwx9461.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vfvwx9461.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20130604155605.GA15953@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> 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.

>> 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, 2013-06-04 17:44

Subject: Re: Can `git blame` show the date that each line was merged?
Message-ID: <20130604174420.GA25318@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20130604174420.GA25318%40sigill.intra.peff.net
In-Reply-To: <7vfvwx9461.fsf@alter.siamese.dyndns.org>

```
On Tue, Jun 04, 2013 at 10:28:06AM -0700, Junio C Hamano wrote:

> > (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.

> > 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

```
