3

I would like to launch a script locally when a branch is created.

I found that i must use a post-checkout hook 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 new rev is not NULL_SHA1 has expected but exactly the same has old rev. From the git documentation it seems logical. And then no echo at all from action_meth().

My question is how can i detect that the branch on which we are moving is a newly created branch (and this locally as the point of new rev == NULL_SHA seems to be ok when using an update hook server side).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
Nemesis
  • 2,750
  • 2
  • 22
  • 20

1 Answers1

0

I suppose the client-side post-checkout hook is called after the branch creation.
Which means it isn't meant to detect a branch creation.

All you could then test 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 the current branch has only one commit, which could indicate a new branch or at least a branch created but not use to add any new commits)
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250