Is there a Git command for adding a notice/warning before pushing to master

Viewed 2326

I have a use case where others depend on me not having a flag enabled when they pull down from master, but I need to have it enabled while working on the repo locally.

I'm hoping/wishing? that Git has a way to add a notice or warning that triggers when a push to master is requested...as a "reminder" of sorts.

So maybe my workflow could look like this:

  1. Develop, develop, develop.
  2. git add -A
  3. git status
  4. git commit -m "Added feature for..."
  5. git checkout master
  6. git merge that-branch
  7. git push origin master
  8. WARN "This is a custom message to remind you to disable the flag."
  9. git push origin master
  10. --> Files pushed to master after second push attempt
1 Answers

You can force the right setting when you push, with a pre-push hook.

All you need to do is to write a script (in any language you prefer) to test whether you have the flag set to the correct setting, and if not, to fail with an error message.

You could also make a pre-commit hook that disallows commits to a certain branch (e.g. master) when the flag is set incorrectly.


However, there is one big problem with this strategy: you're encoding part of your workflow into the history of the project, every time you commit with the flag in the unpublishable state. (A similar example would be to commit temporary files generated by your IDE, and then to delete them before publishing. They would still be a part of your history (unless you rebase) and this is obviously bad.)

A better strategy would be to modify your project or tools to turn on the developer setting when a certain file is present (maybe an empty file called .development), and to ignore that file, so that it never shows up in your history, and you still get the right setting locally simply by creating that file. How difficult this is to do depends on what other technologies you're working with. I haven't figured out how to do this with cabal for example.

Another strategy which would work in every case, but be less convenient, would be to add a pre-commit hook which would always reject the developer setting. So, when you're working locally with the developer setting turned on, you'll get an error message when you commit. To commit at all, you'd have to avoid adding the unpublishable state, which might require using git add --patch, and definitely means you can't use git commit --all.

Related