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

Re: Git blame only current branch

From
Jeff King <peff@peff.net>
Date
Dec 12, 2011, 16:55 UTC
Message-ID
<20111212165542.GA4802@sigill.intra.peff.net>
In-Reply-To
<d615954f-bed8-482d-a2e3-e1e741d6dd23@mail>
On Mon, Dec 12, 2011 at 10:24:47AM -0500, Stephen Bash wrote:
> I'm curious if there's a method to make git blame merge commits that
> introduce code to the given branch rather than commits on the original
> (topic) branch?  For example:

Usually when you are interested in seeing merges like this in git-log, you would use one of "--first-parent" or "--merges". However, though "git blame" takes revision arguments, it does its own traversal of the graph that does not respect those options.

Modifying it to do --first-parent is pretty easy:
diff --git a/builtin/blame.c b/builtin/blame.c
index 80febbe..c19a8cd 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -1191,6 +1191,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;

With that, "git blame --first-parent" produces reasonable results for
me. But of course I didn't do more than 30 seconds of testing, so it is
entirely possible there are corner cases or unforeseen side effects.

Handling --merges is probably a little trickier, as you need to consider
only some commits as scapegoats, but still traverse through everything
to find the merges.

-Peff
Previous: Stephen BashNext: Stephen Bash
Message 2 of 10 in “Git blame only current branch”
  1. Stephen BashDec 12, 2011
  2. Jeff KingDec 12, 2011
  3. Stephen BashDec 12, 2011
  4. andreas.t.auer_gtml_37453@ursus.ath.cxDec 12, 2011
  5. Vijay LakshminarayananDec 13, 2011
  6. Jeff KingDec 13, 2011
  7. Junio C HamanoDec 13, 2011
  8. Vijay LakshminarayananDec 13, 2011
  9. Frans KlaverDec 13, 2011
  10. Junio C HamanoDec 13, 2011

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.