Git discovery in cache after checking that we are creating a new branch
I would like to run the script locally when the branch is created.
I found that I have to use the hook after checking and wrote the following script:
#! /bin/sh
# update hook
oldrev=$1
newrev=$2
type=$3
branch_name="$(git symbolic-ref HEAD 2>/dev/null)"
echo "branch is $branch_name"
echo "oldrev is $oldrev and newrev is $newrev"
type_shortname(){
type="branch"
shortname="$(git symbolic-ref HEAD --short)"
}
NULL_SHA1=0000000000000000000000000000000000000000
if test $type = 1;then
type_shortname
fi
echo $type
echo $shortname
case $oldrev,$newrev in
$NULL_SHA1,*) action=create;;
*,*);;
esac
action_meth()
{
echo "create"
}
case $action,$type in
create,branch) action_meth;;
* );;
esac
exit 0
But the new rev is not NULL_SHA1 expected, but the old rev has exactly the same. From the git documentation, this seems logical. And then there is no echo at all from action_meth()
.
My question is, how can I detect that the branch we are moving on is a newly created branch (and this is local as the point new rev == NULL_SHA
seems to be acceptable when using the server side of grabbing updates).
source to share
I suppose the client post-checkout hook is called after the branch is created .
This means that it is not designed to detect the creation of a branch.
Anything you could check if:
- is the current branch. HEAD is part of 2 branches (
git branch --contains
) , - its parent commit is not part of the current branch (which means that the current branch has only one commit, which can point to a new branch, or at least a branch created but not used to add any new commits).
source to share