0

Normally when I commit a change to a submodule in Git (and if that's the only change), I will provide the result of git diff <submodule-name> to the body of the commit message (with diff.submodule set to log in my git config). So the message would look something like:

Updated Core Submodule

Submodule Core eaedd3f..0721763:
  < Fixed ZA-123: Crash in rendering module
  < Merge 'develop' into 'master'

I've been trying to write a script that will automate this, to which I can tie to an alias in my git config. To generate the text above, ideally I'd like to run a command such as:

$ git submodule-commit Core

This would perform the following (roughly):

  1. Stage any change to specified submodule (Core in this case)
  2. Diff the change to the submodule and store that output text for the message generated later
  3. Run git commit and provide the subject line (Updated $1 Submodule, or something of that sort, where $1 is Core in this example).
  4. Add the stored diff result from earlier to the commit message as well

I've not had any luck writing a simple script for this. I'm not very experienced with bash / shell scripts. Could anyone help provide an implementation that accomplishes this?

3
  • You could start with the --oneline option for git log. Jun 28, 2015 at 16:12
  • 1
    Running git log --oneline eaedd3f..0721763 | sed 's,^, < ,' will give you the submodule commits formatted as above (but with the short commit ID included), given the range of commits to include. Jun 28, 2015 at 16:22
  • That's the easy part. How to pipe that into git commit with a prepended subject text plus 2 newlines is what I can't figure out. Jun 28, 2015 at 17:16

1 Answer 1

5

The key bit of syntax you're missing here is:

git commit -m "$(printf "Updated $submodule Submodule\n\n" ; git diff $submodule)"

The use of the $() form of command substitution inside double quotes sends the output of git diff... to git commit as a commit message with newlines intact.

I used printf here instead of echo to prepend the subject line since for anything even slightly complex — like dealing with embedded escapes — echo is basically nonportable, for historical reasons.

The rest of the script is left as an exercise to the student. :)

3
  • Again your solution is simple. If that's all I had to do, I wouldn't have posted here to begin with. You've conveniently left out what I feel is the most difficult part to solve: The string concatenation of subject line + 2 line feeds + result of git diff $submodule. Jun 28, 2015 at 18:39
  • @void.pointer: Added the header line. I didn't know it wasn't part of "git diff" output. Jun 28, 2015 at 19:04
  • Wow much better answer! Thanks for editing. Honestly, I wasn't expecting it to be so simple. I thought I would have to do some crazy awk stuff and piping. Jun 29, 2015 at 1:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .