3

I've only recently found out what a pre-commit hook is, and from what I understand it's a file (.git/hooks/pre-commit) that tracks my code for errors before the changes are committed to my server (DigitalOcean). So I want to make this file but I don't know what code to add to it. I've tried to find example files of pre-commit for Django projects but cannot find any. Advice appreciated.

Zorgan
  • 8,227
  • 23
  • 106
  • 207
  • Since there is no magical "check all my code" command: what sbould be checked? Unit / integration / behavior tests? Code quality? Conventions? ... – Klaus D. Jan 20 '18 at 05:26
  • I assume there's no way for it to behave like the Django debugger when working offline - so I guess I just want it to check for things like `print()` statements - things that work offline but shouldn't be added to a live production server. – Zorgan Jan 20 '18 at 05:42
  • It sounds like what you really want is some kind of continuous integration service life Travis which runs your tests on commit. – Daniel Roseman Jan 20 '18 at 08:29

1 Answers1

2

what I understand it's a file (.git/hooks/pre-commit) that tracks my code for errors before the changes are committed to my server (DigitalOcean)

TO me, a pre-commit hook is a script which will execute before you commit locally in your repo. If "locally" is DigitalOcean, then yes, you are correct.
But if locally is on your machine before pushing to DigitalOcean, then no: a remote repo is not involved: that script is executed before a commit, and the fact that you push (or not) has no influence.

And don't forget that a pre-commit hook can be skipped entirely anyway with

git commit --no-verify
# or
git commit --n

What that script does is anything you want.

That being said, you can see a pre-commit hook example with Django here.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • So lets assume there are three layers: 1. Locally (Offline repo) | 2. Bitbucket | 3. Digital Ocean (Remote server). Am I correct in saying the pre-commit would happen before I commit from my Local offline repo to my Bitbucket repo? – Zorgan Jan 20 '18 at 06:52
  • @Zorgan a pre-commit is purely a *local* operation, local to your local repo. There is no "to another repo": a commit (and its pre-commit hook) is not concerned with the existence of a remote repo: it has no notion of a remote repo. You can have a pre-commit hook in a local repo with no remote repo at all. – VonC Jan 20 '18 at 07:33
  • @Zorgan When you have several "layers", and want to enforce a policy, a pre-ceive server-side hook (https://git-scm.com/book/gr/v2/Customizing-Git-Git-Hooks#_server_side_hooks) is more appropriate: you can commit, but won't be able to push because the remote server-side will reject the commit. – VonC Jan 20 '18 at 07:35
  • So the pre-commit of the local repo is performed on itself? I thought the pre-commit only executes before you commit to another repo (e.g. Bitbucket)? How exactly do you perform the pre-commit on the same repo? – Zorgan Jan 20 '18 at 07:45
  • @Zorgan "committing to another repo" would be in your case "pushing your local commits to that other repo". And pushing is not mandatory after a pre-commit hook and a commit (both done on the local repo). You can do many commit before deciding to push anywhere. – VonC Jan 20 '18 at 07:52
  • @Zorgan "How exactly do you perform the pre-commit on the same repo": a pre-commit hook is *always* performed on the same repo: a commit is a local operation, local to your local repo. It lives in your `/path/to/local/repo/.git/hooks/pre-commit` file. – VonC Jan 20 '18 at 07:53
  • Thanks. I've added the hook file however I'm not sure how to actually execute it? I've performed `git commit` however it didn't indicate to me that the pre-commit hook worked. – Zorgan Jan 22 '18 at 06:06
  • @Zorgan Add an echo command in your pre-commit hook: you will see it executed (make sure the script is executable) – VonC Jan 22 '18 at 07:19