Git: how to check the current pushed branch in a preliminary click?

In my project, I have a preliminary push git hook that runs unit tests with every click.

But when I try to push changes from one branch, while remaining on the other, unit tests will be performed for the active branch, and not for the currently pressed one.

For example, if I try to push changes from my new_feature branch, while my working directory reflects the structure of the development branch, a preliminary trigger will trigger unit tests for the development branch, not for new_feature.

The main idea to get rid of this is to check the current pushed branch in the pretopic hook. But I do not know how to get information about the current push branch inside the hook: this information is not contained in the hook arguments.

+7
git githooks
source share
2 answers

From the githooks :

 Information about what is to be pushed is provided on the hook standard input with lines of the form: <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF For instance, if the command git push origin master:foreign were run the hook would receive a line like the following: refs/heads/master 67890 refs/heads/foreign 12345 although the full, 40-character SHA-1s would be supplied. 

in which exactly the branch you click is located. With it, you can check and check in this working tree.

Here is an example hook script:

 #!/bin/sh z40=0000000000000000000000000000000000000000 IFS=' ' while read local_ref local_sha remote_ref remote_sha do current_sha1=$(git rev-parse HEAD) current_branch=$(git rev-parse --abbrev-ref HEAD) if [ "$local_sha" != $z40 ] && [ "$local_sha" != "$current_sha1" ]; then git checkout $local_sha # do unit testing... git checkout $current_branch fi done exit 0 
+7
source share

A pre-hook (introduced by git 1.8.2, April 2013 ) was introduced to commit ec55559 , as well as this example :

This hook is called ' git push ' and can be used to prevent pushing. The hook is called with two parameters that provide the name and location of the remote destination, if the named console is not used, both values ​​will be the same.

Information about what needs to be pressed is provided according to the standard hook input with lines of the form:

 <local ref> SP <local sha1> SP <remote ref> SP <remote sha1> LF 

For example, if the +git push origin master:foreign+ command was run, the hook would get a line similar to the following:

 refs/heads/master 67890 refs/heads/foreign 12345 

although full 40-character SHA1s will be shipped.

  • If the foreign ref does not exist yet, <remote SHA1> will be 40 0 .
  • If ref should be removed, <local ref> will be delivered as (delete) , and <local SHA1> will be 40 0 .
  • If the local commit was specified by something other than a name that could be expanded (for example, HEAD~ or SHA1), it will be provided as it was originally provided.

So, check if the local link contains the name of the correct branch, i.e. "currently pressed branch" if you specify the same name in your git push command (i.e. do not use only git push ).

+3
source share

All Articles