Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
864 views
in Technique[技术] by (71.8m points)

intellij idea - Can I configure git blame to always ignore certain commits? Want to fix git blame once and for all

I am in a repository where git blame has effectively been broken.

There are two commits I want to ignore in git blame.

  • Commit 1 destroyed a lot of files.
  • Commit 2 immediately reverted commit 1.

Every time I git blame a line now I see the author of [commit 2] instead of the true logical author.

I end up having to do a git log [file in question] instead, or another one of the solutions listed in this question.

These two commits make me sad whenever I use the Annotate feature in Intellij (which is basically git blame).

Has anybody ever fixed this problem before without rewriting history?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

These two commits make me sad whenever I use the Annotate feature in Intellij (which is basically git blame).
Has anybody ever fixed this problem before without rewriting history?

Before Q3 2019, no.
But with Git 2.23, you will be able to instruct git blame to ignore those two problematic commits. (IntelliJ "annotate" feature might take a while before catching up)

Michael Platings comments though:

git blame --ignore-rev works on the assumption that the specified commit made an uninteresting change (e.g. reformatting).
Unfortunately both removing and adding a file are quite drastic changes so --ignore-rev won't help here.

That being said, git blame can now ignore commits (even maybe not in this particular case).

In general, since Git 2.23:

"git blame" learned to "ignore" commits in the history, whose effects (as well as their presence) get ignored.

And you can register that in your git config! You don't even need to pass those commits in parameters on every git blame call.

See commit 78fafbb (30 Jun 2019), and commit 1d028dc (20 Jun 2019) by Michael Platings (``).
See commit 07a54dc (28 Jun 2019) by Jeff King (peff).
See commit f0cbe74, commit a07a977 (20 Jun 2019), and commit 1fc7338, commit 8934ac8, commit ae3f36d, commit 55f808f, commit f93895f, commit 24eb33e (15 May 2019) by Barret Rhoden (brho).
(Merged by Junio C Hamano -- gitster -- in commit 209f075, 19 Jul 2019)

blame: add the ability to ignore commits and their changes

Commits that make formatting changes or function renames are often not interesting when blaming a file.
A user may deem such a commit as 'not interesting' and want to ignore and its changes it when assigning blame.

For example, say a file has the following git history / rev-list:

---O---A---X---B---C---D---Y---E---F

Commits X and Y both touch a particular line, and the other commits do not:

X: "Take a third parameter"
-MyFunc(1, 2);
+MyFunc(1, 2, 3);

Y: "Remove camelcase"
-MyFunc(1, 2, 3);
+my_func(1, 2, 3);

git-blame will blame Y for the change.
I'd like to be able to ignore Y: both the existence of the commit as well as any changes it made.
This differs from -S rev-list, which specifies the list of commits to process for the blame.
We would still process Y, but just don't let the blame 'stick.'

This patch adds the ability for users to ignore a revision with --ignore-rev=rev, which may be repeated.
They can specify a set of files of full object names of revs, e.g. SHA-1 hashes, one per line.
A single file may be specified with the blame.ignoreRevFile config option or with --ignore-rev-file=file.
Both the config option and the command line option may be repeated multiple times.

An empty file name "" will clear the list of revs from previously processed files.
Config options are processed before command line options.

For a typical use case, projects will maintain the file containing revisions for commits that perform mass reformatting, and their users have the option to ignore all of the commits in that file.

Additionally, a user can use the --ignore-rev option for one-off investigation.
To go back to the example above, X was a substantive change to the function, but not the change the user is interested in.
The user inspected X, but wanted to find the previous change to that line - perhaps a commit that introduced that function call.

To make this work, we can't simply remove all ignored commits from the rev-list.
We need to diff the changes introduced by Y so that we can ignore them.
We let the blames get passed to Y, just like when processing normally.
When Y is the target, we make sure that Y does not keep any blames.
Any changes that Y is responsible for get passed to its parent. Note we make one pass through all of the scapegoats (parents) to attempt to pass blame normally; we don't know if we need to ignore the commit until we've checked all of the parents.

The blame_entry will get passed up the tree until we find a commit that has a diff chunk that affects those lines.

One issue is that the ignored commit did make some change, and there is no general solution to finding the line in the parent commit that corresponds to a given line in the ignored commit.
That makes it hard to attribute a particular line within an ignored commit's diff correctly.

For example, the parent of an ignored commit has this, say at line 11:

commit-a 11) #include "a.h"
commit-b 12) #include "b.h"

Commit X, which we will ignore, swaps these lines:

commit-X 11) #include "b.h"
commit-X 12) #include "a.h"

We can pass that blame entry to the parent, but line 11 will be attributed to commit A, even though "include b.h" came from commit B.
The blame mechanism will be looking at the parent's view of the file at line number 11.

ignore_blame_entry() is set up to allow alternative algorithms for guessing per-line blames.
Any line that is not attributed to the parent will continue to be blamed on the ignored commit as if that commit was not ignored.
Upcoming patches have the ability to detect these lines and mark them in the blame output.

The existing algorithm is simple: blame each line on the corresponding line in the parent's diff chunk.
Any lines beyond that stay with the target.

For example, the parent of an ignored commit has this, say at line 11:

commit-a 11) void new_func_1(void *x, void *y);
commit-b 12) void new_func_2(void *x, void *y);
commit-c 13) some_line_c
commit-d 14) some_line_d

After a commit 'X', we have:

commit-X 11) void new_func_1(void *x,
commit-X 12)                 void *y);
commit-X 13) void new_func_2(void *x,
commit-X 14)                 void *y);
commit-c 15) some_line_c
commit-d 16) some_line_d

Commit X nets two additionally lines: 13 and 14.
The current guess_line_blames() algorithm will not attribute these to the parent, whose diff chunk is only two lines - not four.

When we ignore with the current algorithm, we get:

commit-a 11) void new_func_1(void *x,
commit-b 12)                 void *y);
commit-X 13) void new_func_2(void *x,
commit-X 14)                 void *y);
commit-c 15) some_line_c
commit-d 16) some_line_d

Note that line 12 was blamed on B, though B was the commit for new_func_2(), not new_func_1().
Even when guess_line_blames() finds a line in the parent, it may still be incorrect.


git blame new documentation:

--ignore-rev <rev>::
Ignore changes made by the revision when assigning blame, as if the
change never happened.  Lines that were changed or added by an ignored
commit will be blamed on the previous commit that changed that line or
nearby lines.  This option may be specified multiple times to ignore
more than one revision.

--ignore-revs-file <file>:

Ignore revisions listed in file, which must be in the same format as an fsck.skipList.
This option may be repeated, and these files will be processed after any files specified with the blame.ignoreRevsFile config option.
An empty file name, "", will clear the list of revs from previously processed files.

git config new documentation:

blame.ignoreRevsFile:

Ignore revisions listed in the file, one unabbreviated object name per line, in git blame.
Whitespace and comments beginning with # are ignored.
This option may be repeated multiple times.
Empty file names will reset the list of ignored revisions.
This option will be handled before the command line option --ignore-revs-file.


Since the line detection is not always perfect:

blame: add config options for the output of ignored or unblamable lines

When ignoring commits, the commit that is blamed might not be responsible for the change, due to the inaccuracy of our heuristic.
Users might want to know when a particular line has a potentially inaccurate blame.

Furthermore, guess_line_blames() may fail to find any parent commit for a given line touched by an ignored commit.
Those 'unblamable' lines remain blamed on an ignored commit.
Users


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...