How to find MR from commit

Hi.

Is it possible to find the gitlab merge request associated with a git commit (SHA number) in master? Or do the commits need to be tagged somehow?

Based on ref, I have find-merge and show-merge alias set as follows:

# in ~/.gitconfig
[alias]
  find-merge = "!sh -c 'commit=$0 && branch=${1:-HEAD} && (git rev-list $commit..$branch --ancestry-path | cat -n; git rev-list $commit..$branch --first-parent | cat -n) | sort -k2 -s | uniq -f1 -d | sort -n |  tail -1 | cut -f2'"
  show-merge = "!sh -c 'merge=$(git find-merge $0 $1) && [ -n \"$merge\" ] && git show $merge'"

With these aliases set, for any commit of interest, say b55b54b60e, I do the following:

git show-merge b55b54b60e master

I get the following result which shows the merge commit for the selected commit on the master branch.

commit faa45a7949ca44ce7aec6f848eac68996a5ced08 (master)
Merge: 0e6b872d44 b55b54b60e
Author: Utkarsh Ayachit <utkarsh.ayachit@kitware.com>
Date:   Sun Jan 12 16:40:28 2020 +0000

    Merge topic 'module-cleanups'
    
    b55b54b60e Move vtkPSLACReader to IOParallelNetCDF
    
    Acked-by: Kitware Robot <kwrobot@kitware.com>
    Acked-by: Cory Quammen <cory.quammen@kitware.com>
    Merge-request: !6383

The log for the merge commit has the merge request id mentioned, e.g. Merge-request: !6383. You can then browse to merge request using the URL: http://gitlab.kitware.com/vtk/vtk/merge_requests/[MERGE REQUEST NUMBER].

and what if the log doesn’t mention the merge id?

Does it mean there was no MR? In which case, how do I notify the author of an error?

For VTK, 99% of merges mention the merge id. The robot that does all the merges following a Do: merge request on the MR adds the appropriate message automatically (there are very very few exceptions where that robot may be circumvented).

Note that VTK has not used Gitlab throughout its life cycle. There was several year of development which happened before Gitlab and hence will not have any MRs associated with those changes.

In tortoiseGit, for your example, I see

but here’s the commit I’m interested in

The alias works just fine for your commit of interest too:

> git show-merge b7621

commit 24025ed278301d7462ca0e144f4384957c9d8324
Merge: 29ab569b39 b762161639
Author: Sean McBride <sean@rogue-research.com>
Date:   Mon Oct 14 17:18:29 2019 +0000

    Merge topic 'Bug17677'

    b762161639 Fixed bug #17677: use a thread_local to avoid returning a dangling pointer

    Acked-by: Kitware Robot <kwrobot@kitware.com>
    Acked-by: David Thompson <david.thompson@kitware.com>
    Acked-by: Cory Quammen <cory.quammen@kitware.com>
    Merge-request: !6027

Ok. Thanks. Strange that I can’t see that information in TortoiseGit.

Actually MR 6027 is not the right commit.

You have typed the url incorrectly. Here’s the correct one:
https://gitlab.kitware.com/vtk/vtk/merge_requests/6027

DUH. Thanks.

I use this one liner to do this:

$ git log --merges --grep b7621
commit 24025ed278301d7462ca0e144f4384957c9d8324
Merge: 29ab569 b762161
Author: Sean McBride <sean@rogue-research.com>
Date:   Mon Oct 14 17:18:29 2019 +0000

    Merge topic 'Bug17677'
    
    b762161639 Fixed bug #17677: use a thread_local to avoid returning a dangling pointer
    
    Acked-by: Kitware Robot <kwrobot@kitware.com>
    Acked-by: David Thompson <david.thompson@kitware.com>
    Acked-by: Cory Quammen <cory.quammen@kitware.com>
    Merge-request: !6027
1 Like

@cory.quammen I see the trick is not to use more than 10 characters from the SHA

I use this script: https://raw.githubusercontent.com/mhagger/git-when-merged/master/bin/git-when-merged like this:

git when-merged -d b7621

Which gets the same information, but isn’t limited to only the last 10 commits in the merge commit message since it traverses the history (they get elided if there are too many).

image

You need to download the script and at the path where it is located to your PATH environment variable.

1 Like