0

I have written a git hook that prevents files over a certain size from being committed. However, I would only like a pop-up warning and prevention of a commit the first time the user tries to commit, and they would have the opportunity to ignore the warning and commit anyway. The post-commit wouldn't really work for this since I would like to block the commit once.

I was looking at taking user input but it seems like STDIN for git hooks don't really work without a very hacky approach (that doesn't work for me anyway), and there isn't really any other way to "ignore" a pre-commit hook and commit anyway.

Is there an alternative to skipping a git hook on the second go without just completely disabling the hook? Preferably using bash.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 8
    Use `--no-verify` the second time? Otherwise you have to (somehow, somewhere) store the state of whether or not this is the first time the user's tried to commit the given staged changes. – jonrsharpe Jul 05 '22 at 14:17
  • 2
    A quick-and-dirty approach would be to test inside your hook an environmentvariable: `if [[ ${FORCE:-0} == 1 ]];then; do not reject the commit`. If the user then does the commit with `FORCE=1 git commit`, he won't be rejected. Admittedly, this is really not elegant, but has the advantage that it also works inside a script. – user1934428 Jul 05 '22 at 14:28
  • 3
    As @jonrsharpe says, just use `--no-verify/-n`. You can add an error message to your hook script: `echo "rejecting commit because of file size. Use -n to override" >&2; exit 1`. Note that if you do this, you may want to structure your hook such that you only emit this message if all other conditions are satisfied. (eg, there's no other reason you want to reject the commit). Or, list all the reasons the script would reject the commit, inform the user of `-n`, and trust the user. – William Pursell Jul 05 '22 at 15:38
  • @user1934428 please don’t invent a new bespoke way to do something which git already does via a standard, known, mechanism. – AD7six Jul 05 '22 at 19:48
  • 2
    Does this answer your question? [Skip Git commit hooks](https://stackoverflow.com/questions/7230820/skip-git-commit-hooks) Note this is probably better than having the hook allow it the second time automatically like you asked. Let the user decide for certain when they want it to happen. – TTT Jul 05 '22 at 20:00
  • @AD7six : Maybe you could sketch this mechanism or ? It may be well-known to experienced git-users, but at least not to me and not to the OP. The `-no-verify` is not really an alternative, because it skips **all** checks. In the case discussed here, the pre-commit hook might do several checks, and my proposal allows to bypass only some of them (for instance the size check). – user1934428 Jul 06 '22 at 06:10
  • 1
    I very much doubt the op has multiple pre commit hooks and a need to skip only one of them - `—no-verify` exists for this use case, I disagree it is not an alternative. – AD7six Jul 06 '22 at 06:13
  • Thank you all. I do have one git hook but actually do have multiple conditions. I'll try these out and update with the option that works most effectively. – snarulaFF Jul 07 '22 at 07:25

0 Answers0