5

I have a commit-msg hook that I run for all of my local commits and, for a certain project, I require to have been run. Problem is, when I pull in from other forks some of my compatriots have not run this commit-msg hook. To date I've been doing

$ git rebase --interactive $commit_parent

as outlined very similarly here. Pick the commits that haven't been done properly, re-edit and so on. All very workable, but also tedious as I'm doing it by hand.

How can I automate this? The hook requires no oversight.

Community
  • 1
  • 1
troutwine
  • 3,721
  • 3
  • 28
  • 62
  • What exactly does your commit-msg hook do? Does it extract information from commit messages or does it modify the commit message in some way? The solution will be different depending on what you want to achieve. – Anti Veeranna Oct 22 '11 at 21:32
  • Specifically, the commit-msg hook I'm using is gerrit's ChangeID [hook](http://code.google.com/p/gerrit/source/browse/gerrit-server/src/main/resources/com/google/gerrit/server/tools/root/hooks/commit-msg). What considerations would change the nature of the answer? – troutwine Oct 23 '11 at 13:56
  • Modifying commits that are already shared with other people can cause a lot of problems. Just extracting information is ok. – Alex Jasmin Oct 24 '11 at 06:34
  • Yep, I'm aware. I'm not pushing the modified commits back into the same repository. – troutwine Oct 24 '11 at 13:59

2 Answers2

3

This can be achieved in automatic fashion using custom editor:

#!/bin/bash
# Save to rebase_editor.sh, chmod +x it
file="$1"
if head -n1 "$file" | egrep -q '^pick' "$file" ; then
  perl -pi -e 's/^pick/r/' "$file"
fi

and then running:

GIT_EDITOR=./rebase_editor.sh git rebase -i <some-rev>

On the first invocation, our "editor" will get a list of commits from rebase -i, where it will change every pick to r (which is reword). In this mode git will start immediately calling our "editor" with a message of every rebased commit (and without any pauses that it usually makes in pick mode where you can change your commit itself).

binarin
  • 64
  • 5
0

I suppose it is very easy, given the description of the hook in "hooks" manpage:

It takes a single parameter, the name of the file
that holds the proposed commit log message.

Just run the hooks script for every commit:

git rev-list HEAD |while read h; do
    git show --format='%B' $h > tmp && sh .git/hooks/commit-msg.sample tmp
done
rm tmp
fork0
  • 3,401
  • 23
  • 23