githooks - Git hook: add a new file to repo if a new branch is created -
i writing git hook checks if new branch created, , if add predefined files repo new branch(some config files). because branch in process of being created, logic fails.
currently doing in post-receive
hook, looks this:
#!/bin/sh read oldrev newrev refname branch=$(git rev-parse --symbolic --abbrev-ref $refname) echo "branch $branch" echo "oldrev $oldrev , newrev $newrev" # if $oldrev 0000...0000, it's new branch # check if branch of format "feature_<name>" zero="0000000000000000000000000000000000000000" if [ "$oldrev" = "$zero" ] && [[ $branch =~ feature_.+ ]]; #create temp repo temp_repo=`mktemp -d /tmp/repo.xxxxx` cd $temp_repo git clone $git_url #here create config file needed, called file_name git checkout "$branch" git add "$file_name" git commit -m "added config file" git push origin $branch fi
this works existing branch, newly created branch gives error fatal: not git repository: '.'
.
i not sure in hook should use logic, don't know git
. idea how can go this?
thanks
if you're in hook , want run "normal" git commands, need unset git_dir
environment variable (inside hook it's set .
).
that said, not seem me right approach. should work seems little surprising: if git push origin abc:feature_def
i'll have re-fetch , merge origin pick newly-committed $file_name
file. not make more sense require include file myself, it's there in commit on branch feature_def
?
if so, pre-receive or update hook place checking. simplified example (untested):
#! /bin/sh # update hook - check if new branch named # feature_*, , if so, require config file refname=$1 oldrev=$2 newrev=$3 # begin boilerplate null_sha1=0000000000000000000000000000000000000000 # kind of ref it? also, short name branch-or-tag case $refname in refs/heads/*) reftype=branch; shortname=${refname#refs/heads/};; refs/tags/*) reftype=tag; shortname=${refname#refs/tags/};; *) reftype=other;; esac # what's happening ref? # note: if update, there potentially 2 different objtypes, # new 1 here case $oldrev,$newrev in $null_sha1,*) action=create; objtype=$(git cat-file -t $newrev);; *,$null_sha1) action=delete; objtype=$(git cat-file -t $oldrev);; *,*) action=update; objtype=$(git cat-file -t $newrev);; esac # end boilerplate # code check feature branch. top level file named xyzzy.conf must exist. check_feature_branch() { if ! git show $refname:xyzzy.conf >/dev/null 2>&1; echo "new branch $branch not contain xyzzy.conf @ top level" >&2 exit 1 fi } # check whether we're creating branch named feature_* case $action,$reftype,$shortname in create,branch,feature_*) check_feature_branch;; *) ;; esac # if got here must ok exit 0
Comments
Post a Comment