code_fodder
code_fodder

Reputation: 16391

list commit hashes for submodules recursively between to commits in the super repo

I have a super-repo structure like this (for example):

A --+--> B --> D
    |
    +--> C --+--> E
             |
             +--> F

Though in reality it's much bigger and deeper.

Ultimately I am trying get a list of all merges that occured between two versions at the top level. So in repo A I can do a git log between two dates - showing just the merges to branch development:

git log --merges --first-parent development --oneline --since="1 month ago" --until="today"
36bd8f6 (HEAD -> development) Merge branch 'feature-184' into 'development'
9e09290 Merge branch 'feature-173' into 'development'
b287736 Merge branch 'feature-268' into 'development'
a33099f Merge branch 'feature-262' into 'development'
186dbbe Merge branch 'feature-261' into 'development'
a32269a Merge branch 'feature-266' into 'development'

I can do this for any repo by data or by pair of hashes, so git log --merges --first-parent development --oneline a32269a..36bd8f6 would give the same result

So now lets say I have selected the two hashes from repo A a32269a -> 36bd8f6. What I want to do now is get the commit hashes for each of the repos below that match the two hashes at repo A.

For example when I checkout A at 36bd8f6 (development), and do a submodule update recursive, then repo F might be at git hash aaaaaaa2. Then when I checkout A at a32269a (~1 month ago), and submodule update, then repo F might be at hash aaaaaaa1.

So I want to find these pair of hashes for each repo. I can do this by checking out repo A at the two commits I am interested in (plus update the submodules)... but this is slow and means checking out different versions and updating subdmoules etc.. so I have to make sure my submodules are all in a clean state etc... This is all doable, but not so convenient.

Is there a way I can query git to ask it. "What is submodule F's git commit hash when submodule A is at commit hash "xxxxxxx"?

Upvotes: 0

Views: 664

Answers (1)

LeGEC
LeGEC

Reputation: 52081

To get the hash of a direct submodule, you can use :

git rev-parse [commit]:[path/to/submodule]

# for example :
$ git rev-parse a32269a:B
eacf32a     # complete hash of the commit for 'B' in a32269a
$ git rev-parse a32269a:C
eacf33b

I wasn't able to make this command work to get nested submodules straight from repo A
(e.g : git rev-parse a32269a:C/F doesn't work),
you can however :

  • cd into C to get info from that repo :
$ cd C
$ git rev-parse eacf33b:F
  • or use --git-dir to scan the repository information of C submodule :
# the repo information for submodule C is stored under '.git/modules/' :
$ git --git-dir .git/modules/C rev-parse eacf33b:F

git ls-tree will also output the commit hash of a submodule at a given revision :

# if submodule is at "top level" of your repo :
$ git ls-tree <commit>
# if submodule is in 'parent/dir/'
$ git ls-tree <commit>:parent/dir

# sample output :
100644 blob f0d8b44a89c2516a73f330c15219569b84198fd4    .gitmodules
100644 blob 72943a16fb2c8f38f9dde202b7a70ccc19c52f34    a.txt
040000 tree a82b9a1f72f28fb99c34af337bac5326db0822ed    dir
160000 commit 60163ec080c11fb75294846faf146af23e616627  mymodule  # <- submodule

As with git rev-parse, you will need to cd into the submodule or explicitly provide the --git-dir of the submodule to dive into nested submodules.

Upvotes: 2

Related Questions