11

I have a post-commit hook that does stuff in ruby. It works very well but in some cases I would like to skip the code execution when I do a git rebase or git commit --amend.

Does someone have an idea how I could not trigger the post-commit hook in these cases or any work around?

codeMonkey
  • 562
  • 8
  • 19
Gregory
  • 557
  • 6
  • 17
  • I can't cause this hook to run with `git commit --amend`, and I'm pretty confident it's never happened to me. I'm curious to know whether that's still an issue for you, and in what circumstances it happens? – phils Nov 30 '12 at 07:59
  • related: https://stackoverflow.com/q/65610742/1098683 – codeMonkey Mar 12 '21 at 09:39

2 Answers2

8

When rebasing, there's a directory called rebase-merge present in the .git folder. That could be an approach to disable the hook during a rebase (the start of a rebase btw is indicated by the pre-rebase hook).

Regarding the --amend however, I can't help you.

eckes
  • 64,417
  • 29
  • 168
  • 201
  • You mean detect the presence of the folder or something like that? – Gregory Jul 06 '12 at 20:23
  • Yes. Don't know ruby enough to be able to present an example but surely you could also check in ruby whether there's a folder called `../rebase-merge` (relative to the hook directory) when your post-commit hook gets called – eckes Jul 06 '12 at 20:32
1

If you want to detect git commit --amend from a hook, this is your best option

bash

if [[ $(ps -ocommand= -p $PPID) == "git commit --amend" ]]; then
    echo "always allow git commit --amend"
    exit 0
fi

ruby

parent=`ps -ocommand= -p #{Process.ppid}`.chomp
if parent == "git commit --amend"
  # Always allow an amend
  puts "always allow git commit --amend"
  exit 0
end

git and shell aliases are expanded in the shell output, so this covers those cases too

Inspiration: https://github.com/brigade/overcommit/issues/146

Marchy
  • 123
  • 1
  • 8
  • This solution would miss some cases if there were any other command line flags supplied to git. – sbleon Jan 13 '22 at 15:19
  • That's fair! It has been four years since I wrote this, though I think I can modify it to handle that situation... Perhaps instead of seeing what the parent process is equal to, we could see what it contains? So for bash change `"git commit --amend"` to `*"git"*"commit"*"--amend"*` and for ruby do something similar – Marchy Jan 14 '22 at 16:14
  • For ruby perhaps do something similar, maybe change `== "git commit --amend"` to `=~ /git.*commit.*--amend.*/`, I'm too many laptops away from the original problem I was solving but hopefully that helps if you were attempting something similar and hit this issue regarding flags – Marchy Jan 14 '22 at 16:21