How can I get a git submodule's associated commit ID from a past commit in the parent clone?
Is there a way, short of actually checking out the parent commit, to determine a submodule's SHA-1 commit ID based on a commit ID in the parent clone? I know I can find the currently associated SHA-1 with git submodule
.
Here's an example:
- I have a clone with a single submodule
foo
that has changed several times in the last month. - I have a tag in the parent clone that is a few weeks old called
released-1.2.3
. I want to find out what the associated SHA-1 offoo
was for this tagged commit. - I could simply check out
released-1.2.3
and usegit submodule
to see, but I'm wondering if there's a way to do this without affecting the working tree, as I want to script it.
I want to do this because I want to construct a script to do a 'diff' on all changes within a submodule between two commits within the parent repository - i.e. "tell me what files changed within the submodule foo
between these two commits in the parent."
Solution 1:
You may use git-ls-tree
to see what the SHA-1 id of a given path was during a given commit:
$ git ls-tree released-1.2.3 foo
160000 commit c0f065504bb0e8cfa2b107e975bb9dc5a34b0398 foo
(My first thought was git show released-1.2.3 foo
, but that fails with "fatal: bad object".)
Since you are scripting the output, you will probably want to get just the SHA-1 id by itself, e.g.:
$ git ls-tree released-1.2.3 foo | awk '{print $3}'
c0f065504bb0e8cfa2b107e975bb9dc5a34b0398
Also: When writing scripts around git, try to stick to the plumbing commands, as described in the manual. They have a more stable interface, while the more familiar “porcelain” commands will possibly change in incompatible ways.
Solution 2:
This one worked for me:
$ git rev-parse released-1.2.3^{commit}:foo
<SHA1>
Perhaps also quite easy to use in script.
Solution 3:
I did find one promising avenue:
$ git log --raw <since>..<until> --submodule -- <path/to/submodule>
With the --raw option, this does print out the (abbreviated) SHA-1 IDs corresponding to the submodule's associated commits. Unfortunately the output is very verbose and will take some work to process in a script.
What I really need is a git facility that, given a parent commit ID, gives me the latest change to a submodule prior to that commit. I.e. which submodule SHA-1 'git submodule update' would check out if I were to actually checkout that parent commit.