Skip to content

Commit 8176da3

Browse files
committed
Check if remote branch exists
1 parent 1e14c8e commit 8176da3

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

tools/git/hooks/pre-push

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,18 @@ cleanup() {
4242

4343
# Checks if commits exist to push, as `git push` will execute regardless of whether commits exist to push or not.
4444
has_commits() {
45-
echo 'Checking for commits to push...' >&2
46-
local commits="$(git log origin/${GIT_CURRENT_BRANCH}..${GIT_CURRENT_BRANCH} -- --oneline)"
45+
local commits
46+
47+
echo 'Checking if remote branch exists...' >&2
48+
if git branch -r | grep "${GIT_CURRENT_BRANCH}"; then
49+
echo 'Remote branch exists.' >&2
50+
echo 'Checking for commits to push...' >&2
51+
commits=$(git log origin/"${GIT_CURRENT_BRANCH}".."${GIT_CURRENT_BRANCH}" -- --oneline)
52+
else
53+
echo 'Remote branch does not exist.' >&2
54+
echo 'Checking for commits to push...' >&2
55+
commits=$(git log "${GIT_CURRENT_BRANCH}" -- --oneline)
56+
fi
4757
if [[ -z "${commits}" ]]; then
4858
echo 'No commits to push.'
4959
return 1
@@ -82,6 +92,7 @@ main() {
8292
if [[ "$?" -ne 0 ]]; then
8393
on_error 1
8494
fi
95+
exit 1
8596
check_licenses
8697
if [[ "$?" -ne 0 ]]; then
8798
on_error 1

0 commit comments

Comments
 (0)