Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changes made by merge commits not found by `git bisect` or `git log -S --all`

Tags:

git

git-bisect

I made a fix to a file, and committed it to my git repository.

Some time later, I found that it had regressed. I wanted to know in which commit the fix had been taken out, so I tried git bisect and git log --all -S TERM, where "TERM" was a string added by my fix and which appeared nowhere else in the project.

I found that git bisect blamed an unrelated commit, and I found that git log --all -S TERM only listed the commit in which I added TERM, and didn't list any commits as having removed it, even though it was no longer in the file at "master".

After some manual searching, I found that there had been a merge commit between two branches. One branch had my fix and one did not. The merge author had picked the branch without my fix (strange, as there was no conflict in the file).

My questions are:

  • Why can git bisect not find merge changes? I don't see anything in the manpage about this limitation. Is there a different way to use it that will find the bad merge in the above scenario?
  • Why does git log --all -S TERM not list the merge commit? The manpage says it lists commits which "introduce or remove an instance of ". Does that merge commit not remove my string? Is there a different way to use it that will find the bad merge in the above scenario?
  • If git bisect and git log -S are useless in the above scenario, what is an efficient way to find the bad merge? It look me quite a long time to track down the change in question without these tools.
like image 602
Rich Avatar asked Jun 26 '14 12:06

Rich


2 Answers

Why does git log --all -S TERM not list the merge commit?

You must add the -m option to it:

git log -m --all -S TERM

In order to understand why a special option is needed in that case, let's first look at the operation of git log -p which must include the changes in the listed history. As you can easily check, by default for merge commits changes are NOT shown. The reason is that a change is the difference between two revisions whereas for a merge commit we have three (or more) revisions. The -m option to git log addresses that problem:

Diff Formatting

...

-m

This flag makes the merge commits show the full diff like regular commits; for each merge parent, a separate log entry and diff is generated. An exception is that only diff against the first parent is shown when --first-parent option is given; in that case, the output represents the changes the merge brought into the then-current branch.

Then, assuming that git log -S works as if by processing the diff returned by git log -p you should admit that by default the merge commits would be excluded from the results. However, fortunately you can combine the -S and -m options in git log and it works as expected.

like image 59
Leon Avatar answered Sep 25 '22 14:09

Leon


Solution using git bisect

git bisect start $badCommit $goodCommit
git bisect run bash -c "! git merge-base --is-ancestor $goodCommit HEAD || git grep -q $TERM"
bit bisect reset

Note: this solution use git bisect run for simplicity. You use git bisect in "interactive" mode too. See Explanation section below.

Main idea

The idea is to restrict the commits analyzed by the bisect algorithm to the ones in the ancestry path.

The idea was suggested by CB Bailey in this comment, although I didn't find a way using git rev-list.

Explanation

For the part about git bisect, the explanation is a bit tricky. It is based on how the bisect algorithm works. The algorithm makes the assumption that all ancestors of a good commit are good. Thus, when testing a specific commit in git bisect, you must mark commits that are not descendants of the good commit as good too. This typically not the case when using only a simple grep function. You can tell if commits are descendant of another commit using git merge-base --is-ancestor $goodCommit HEAD (more details on How can I tell if one commit is an ancestor of another commit (or vice-versa)? or checking if a commit is an ancestor of another)

like image 30
Jidehem Avatar answered Sep 22 '22 14:09

Jidehem