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

Re: finding the merge of two or more commits

From
Jakub Narebski <jnareb@gmail.com>
Date
Oct 30, 2009, 23:38 UTC
Message-ID
<m3iqdwcv17.fsf@localhost.localdomain>
In-Reply-To
<3a69fa7c0910291412l439f7f61vd3b55a77cd7e10b5@mail.gmail.com>
E R <pc88mxer@gmail.com> writes:
Show 17 quoted lines
> Given two commits c1 and c2, is it possible to ask git if there are
> any commits in the repository that were created by either a sequence
> of commands like:
> 
> git checkout c1
> git merge c2
> 
> or:
> 
> git checkout c2
> git merge c1
> 
> with any required conflict resolution?
> 
> That is, I don't want to merge c1 and c2 myself, but I want to know if
> someone else has merged c1 and c2, performed any conflict resolution
> and committed the result.

I assume that commits c1 and c2 are not one ancestor of the other (are not in fast-forward relation).

Translating your question into question about DAG of revisions, you want to check if there is branch for which both c1 and c2 are reachable from:

  c1sha=$(git rev-parse c1)
  c2sha=$(git rev-parse c2)
  git for-each-ref --format="%(refname)" refs/heads/ |
  while read refname
  do 
      b1=$(git merge-base c1 $refname)
      b2=$(git merge-base c2 $refname)
      if [ "$b1" = "$c1sha" -a "$b2" = "$c2sha" ]
      then
          print ${refname#refs/heads/}
      fi
  done

Instead of comparing git-merge-base with SHA-1 of c1 and c2 respoectively, you can count commits:

  git for-each-ref --format="%(refname)" refs/heads/ |
  while read refname
  do 
      count1=$(git rev-list c1..$refname | wc -l)
      count2=$(git rev-list c2..$refname | wc -l)
      if [ "$count1" > 0  -a  "$count2" > 0 ]
      then
          print ${refname#refs/heads/}
      fi
  done
Not tested!
-- 
Jakub Narebski
Poland
ShadeHawk on #git
Previous: Jeff King
Message 4 of 4 in “finding the merge of two or more commits”
  1. E ROct 29, 2009
  2. Avery PennarunOct 29, 2009
  3. Jeff KingOct 30, 2009
  4. Jakub NarebskiOct 30, 2009

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.