NTBBloodbath / cheovim

Neovim configuration switcher written in Lua. Inspired by chemacs.
GNU General Public License v2.0
332 stars 14 forks source link

GIT HOOKS: manage custom installer paths during development. #5

Open molleweide opened 2 years ago

molleweide commented 2 years ago

I am looking into git hooks to automate the process of setting up custom config paths when developing and then resetting the paths when pushing back to upstream.

Use-case: doom-nvim

The default of doom-nvim is for user to install to ~/.config/nvim but since I/we are using Cheovim I do believe that most of us are using a custom installer location. Therefor, it would be nice to setup post-checkout and pre-push hooks that take care of modifying the doom root path so that one doesn't need to always update these paths manually when switching between local dev/personal branch and a, (in my case), temporary fetch of upstream development in order to apply patch/rebase/cherry-pick commits etc.

This could then be a recommendation in contribution docs to add these two hooks should one like to.

Note

Personally I have never used hooks before so I am posting my research here. ;)

resources

Below are interesting links that I have found.

## resources: post-checkout https://stackoverflow.com/questions/1011557/is-there-a-way-to-trigger-a-hook-after-a-new-branch-has-been-checked-out-in-git https://stackoverflow.com/questions/2141492/git-clone-and-post-checkout-hook https://gist.github.com/lyrixx/5867424 https://gist.github.com/stefansundin/82051ad2c8565999b914 ## resources: pre-push https://gist.github.com/mosra/19abea23cdf6b82ce891c9410612e7e1 https://gist.github.com/ashtanko/5f5221262af66d79c775d68d814f4e22

Example 1: post-checkout

do stuff when checking out branch locally

example source

Example:

1. Create the hook (script): touch .git/hooks/post-checkout chmod u+x .git/hooks/post-checkout 2. Hook sample content: ``` bash #!/bin/bash set -e printf '\npost-checkout hook\n\n' prevHEAD=$1 newHEAD=$2 checkoutType=$3 [[ $checkoutType == 1 ]] && checkoutType='branch' || checkoutType='file' ; echo 'Checkout type: '$checkoutType echo ' prev HEAD: '`git name-rev --name-only $prevHEAD` echo ' new HEAD: '`git name-rev --name-only $newHEAD` ``` 3. Result ``` #> git checkout - Switched to branch 'molldev' post-checkout hook Checkout type: branch prev HEAD: pr_prep_branch new HEAD: molldev ```

Example 2: pre-push

taken from .git/hooks/pre-push.sample

``` bash #!/bin/sh # An example hook script to verify what is about to be pushed. Called by "git # push" after it has checked the remote status, but before anything has been # pushed. If this script exits with a non-zero status nothing will be pushed. # # This hook is called with the following parameters: # # $1 -- Name of the remote to which the push is being done # $2 -- URL to which the push is being done # # If pushing without using a named remote those arguments will be equal. # # Information about the commits which are being pushed is supplied as lines to # the standard input in the form: # # # # This sample shows how to prevent push of commits where the log message starts # with "WIP" (work in progress). remote="$1" url="$2" zero=$(git hash-object --stdin &2 "Found WIP commit in $local_ref, not pushing" exit 1 fi fi done exit 0 ```