gitgit-submodulesgit-plumbing

Git plumbing commands to detect submodule update and get the hashes in staging area or working directory


In porcelain mode, git diff advertises submodule changes:

$ cd /opt/staging # This is the main Git repository
$ cd software/zemke-rhyne # This is a submodule
$ git pull
$ git diff                                                                                                                  
diff --git a/software/zemke-rhyne b/software/zemke-rhyne
index 0fff2fc..cb69287 160000
--- a/software/zemke-rhyne
+++ b/software/zemke-rhyne
@@ -1 +1 @@
-Subproject commit 0fff2fc52bdc2a62a2647110e712b1b3baa48613
+Subproject commit cb69287a3540054ff85cd14199cf36b4b5098c1a

What plumbing commands could give the following information?

The goal is for example to automate a commit message to briefly document the changes in the updated submodule through git rev-list.


Solution

  • In order to find out whether the submodule was updated, you can use

    $ git diff-files --quiet software/zemke-rhyne
    

    The exit status will indicate whether the submodule is up-to-date (0) or out-of-date (1).

    The old hash is shown for example by ls-tree:

    $ git ls-tree @ software/zemke-rhyne
    160000 commit 0fff2fc52bdc2a62a2647110e712b1b3baa48613  software/zemke-rhyne
    

    Another way would be

    $ git ls-files -s software/zemke-rhyne
    160000 0fff2fc52bdc2a62a2647110e712b1b3baa48613 0   software/zemke-rhyne
    

    (What's the difference? Would be great if somebody could comment…)

    There's also a way to find out both whether the submodule was updated and the previous hash. If the submodule is unmodified, the following command's output is empty, otherwise it looks like this:

    $ git diff-files software/zemke-rhyne
    :160000 160000 0fff2fc52bdc2a62a2647110e712b1b3baa48613 0000000000000000000000000000000000000000 M  software/zemke-rhyne
    

    Unfortunately, diff-files misses a perfect opportunity of also showing the updated hash. To see it, all I could come up with is to use rev-list in the submodule directory. This seems a little inelegant and fragile…

    $ git -C software/zemke-rhyne rev-list -n 1 @
    cb69287a3540054ff85cd14199cf36b4b5098c1a