troutwine
troutwine

Reputation: 3831

Is it possible to run a git commit-msg hook over past commits?

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.

Upvotes: 5

Views: 1133

Answers (2)

binarin
binarin

Reputation: 64

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).

Upvotes: 3

fork0
fork0

Reputation: 3459

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

Upvotes: 0

Related Questions