0

I have a pre-push script in my .git/hooks folder. This script runs fine when pushing from a local branch that has a pre-existing remote upstream branch. But it fails to run when I create a new branch off of master, say test-pre-push, commit a build-breaking change for example, and then enter the command git push --set-upstream origin test-pre-push. The commit and the branch are pushed to the remote repo. How can I ensure the script is run with each and every push to remote?

Check below for my pre-push script:

#!/bin/bash

#The command to run
CMD='./gradlew testDebugUnitTest'


# Check if we actually have commits to push
commits=$(git log @\{u\}..)
if [ -z "$commits" ]; then
 exit 0
fi
$CMD
RESULT=$?
if [ $RESULT -ne 0 ]; then
 echo "failed $CMD"
 exit 1
fi
exit 0
Avilio
  • 581
  • 6
  • 5
  • Hard to say without looking at the code. Could it be the script is executed but does nothing because it's unprepared to deal with a new remote branch? – phd May 07 '20 at 21:22
  • I'm not sure. Let me update the question with the script I have. – Avilio May 07 '20 at 21:30
  • 3
    If you're creating a new branch, then `git log @{u}..` is going to return no results (because the upstream branch doesn't exist yet). – larsks May 07 '20 at 21:36
  • `exit 0` is almost certainly the culprit. [Recently](https://stackoverflow.com/search?tab=newest&q=%5bgithooks%5d%20pre-push) there were a few good examples of `pre-push` hooks, I recommend to take a look: [1](https://stackoverflow.com/a/61621713/7976758), [2](https://stackoverflow.com/q/61653972/7976758). Also see [the docs](https://git-scm.com/docs/githooks#_pre_push). – phd May 07 '20 at 21:36
  • 2
    The culprit is the erroneous check against `@{u}`. – larsks May 07 '20 at 21:37
  • Wow, thanks guys! This was my first question ever on here. Regretting waiting so long. – Avilio May 07 '20 at 21:47

0 Answers0