Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
molleweide opened this issue Aug 11, 2021 · 0 comments
Open

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

molleweide opened this issue Aug 11, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@molleweide
Copy link

molleweide commented Aug 11, 2021

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

  1. Hook sample content:
#!/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`
  1. 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

#!/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:
#
#   <local ref> <local oid> <remote ref> <remote oid>
#
# 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 </dev/null | tr '[0-9a-f]' '0')

while read local_ref local_oid remote_ref remote_oid
do
	if test "$local_oid" = "$zero"
	then
		# Handle delete
		:
	else
		if test "$remote_oid" = "$zero"
		then
			# New branch, examine all commits
			range="$local_oid"
		else
			# Update to existing branch, examine new commits
			range="$remote_oid..$local_oid"
		fi

		# Check for WIP commit
		commit=$(git rev-list -n 1 --grep '^WIP' "$range")
		if test -n "$commit"
		then
			echo >&2 "Found WIP commit in $local_ref, not pushing"
			exit 1
		fi
	fi
done

exit 0
@molleweide molleweide changed the title Manage custom installer path with git post-checkout hooks GIT HOOKS: manage custom installer paths during development. Aug 12, 2021
@NTBBloodbath NTBBloodbath added the enhancement New feature or request label Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants