Git post commit: skip --amend and rebase
Asked Answered
C

2

12

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?

Ceporah answered 6/7, 2012 at 18:27 Comment(2)
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?Harpsichord
related: https://mcmap.net/q/473328/-git-skip-post-commit-hook/1098683Predictor
H
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.

Histaminase answered 6/7, 2012 at 18:55 Comment(2)
You mean detect the presence of the folder or something like that?Ceporah
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 calledHistaminase
S
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

Sheeree answered 28/3, 2018 at 13:0 Comment(3)
This solution would miss some cases if there were any other command line flags supplied to git.Mong
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 similarSheeree
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 flagsSheeree

© 2022 - 2024 — McMap. All rights reserved.