pyratelog

personal blog
git clone git://git.pyratebeard.net/pyratelog.git
Log | Files | Refs | README

20221111-what_the_hook.md (3233B)


      1 Git [hooks](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks){target="_blank" rel="noreferrer"} are often overlooked or underused.  They can be very useful, and even used as a rudimentary CI/CD workflow.
      2 
      3 While these tips can work for any repo, this could be viewed as an expansion on my [tech_grimoire](20220525-tech_grimoire.html){target="_blank" rel="noreferrer"} post.
      4 
      5 For note taking I make use of the [VimWiki](https://vimwiki.github.io/){target="_blank" rel="noreferrer"} plugin to Vim.  My wiki directory is a git repository, which is then cloned on to one of my servers and using [Gollum](https://github.com/gollum/gollum){target="_blank" rel="noreferrer"} is  available as a website.
      6 
      7 This has worked well for years, but one thing I have not been good at is committing and pushing changes straight away.  In order to improve my workflow I make use of two git hooks, one in the local repo and one in the remote bare repo.
      8 
      9 Git hooks are scripts which can be run at various times in a git workflow.  When they run is based on the name of the script.
     10 
     11 If you look in the .git/hooks directory of a repo you should see some sample scripts.
     12 
     13 ```
     14 ──── ─ ls -1 .git/hooks
     15 applypatch-msg.sample
     16 commit-msg.sample
     17 fsmonitor-watchman.sample
     18 post-update.sample
     19 pre-applypatch.sample
     20 pre-commit.sample
     21 pre-merge-commit.sample
     22 pre-push.sample
     23 pre-rebase.sample
     24 pre-receive.sample
     25 prepare-commit-msg.sample
     26 push-to-checkout.sample
     27 update.sample
     28 ```
     29 
     30 For my wiki I make of the `post-commit` hook.
     31 ```
     32 #!/bin/sh
     33 git push origin master
     34 ```
     35 
     36 That's it.  As soon as I make a commit the master branch is automatically pushed.
     37 
     38 This works but I wanted to go one step further, automatically adding changed files and making the commit.  The `prepare-commit-msg` hook is useful when you want to populate the commit message with something, but I couldn't figure out an easy way to do what I wanted with hooks.
     39 
     40 In the end I used a command in my ~/.vimrc
     41 ```
     42 set :Gac git commit -a -m "updates"
     43 ```
     44 
     45 Now, when I have finished updating my wiki I incant
     46 ```
     47 :Gac
     48 ```
     49 
     50 and the updates will be committed and pushed, albeit with a generic commit message.
     51 
     52 Once the changes are pushed to my git server they hit another hook, `post-receive`.
     53 ```
     54 #!/bin/sh
     55 ssh wikiserver "cd /grimoire ; git pull"
     56 ```
     57 
     58 When the remote bare repo receives any updates the hook will perform a `git pull` of my wiki on the server I host it on, causing my Gollum page to be updated.
     59 
     60 The final thing I have done is to change the hooks directory in my local repo.  With the default dir, .git/hooks, nothing can be tracked by git.
     61 
     62 Instead I created a directory in the repo, .githooks and put my `post-commit` hook there.  This way it can be tracked in the repo.
     63 
     64 An update to the config is required so git  knows where the hooks are
     65 ```
     66 git config core.hooksPath .githooks
     67 ```
     68 *Note: this option was introduced in got v2.9, so make sure you have that version or higher*
     69 
     70 As far as I am aware there is no way to track hooks in a remote bare repo.  If you know of a way I would be interested to hear about it.
     71 
     72 Who needs a convoluted CI/CD pipeline when a couple of one line scripts will do?  Typing `:Gac` is easy enough that my wiki should now stay up to date.