Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions tools/git/hooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -168,24 +168,28 @@ amend_commit() {

# Checks if commits exist to push, as `git push` will execute regardless of whether commits exist to push or not.
has_commits() {
local commits

echo 'Checking if remote branch exists...' >&2
if git branch -r | grep "${GIT_CURRENT_BRANCH}" > /dev/null; then
echo 'Remote branch exists.' >&2
echo 'Checking for commits to push...' >&2
commits=$(git log "${remote}/${GIT_CURRENT_BRANCH}..${GIT_CURRENT_BRANCH}" --oneline --)
else
echo 'Remote branch does not exist.' >&2
echo 'Checking for commits to push...' >&2
commits=$(git log "${GIT_CURRENT_BRANCH}" --oneline --)
fi
if [[ -z "${commits}" ]]; then
echo 'No commits to push.'
return 1
fi
echo 'Found commits to push.'
return 0
local commits
local remote_ref="${remote}/${GIT_CURRENT_BRANCH}"

echo 'Checking if remote branch exists...' >&2
# We use rev-parse to test the actual ref.
# 2>/dev/null makes it silent on failure; the exit code is what matters.
if git rev-parse --verify "refs/remotes/${remote_ref}" > /dev/null 2>&1; then
echo 'Remote branch exists.' >&2
echo 'Checking for commits to push...' >&2
commits=$(git log "${remote_ref}..${GIT_CURRENT_BRANCH}" --oneline --)
else
echo 'Remote branch does not exist.' >&2
echo 'Checking for commits to push...' >&2
commits=$(git log "${GIT_CURRENT_BRANCH}" --oneline --)
fi

if [[ -z "${commits}" ]]; then
echo 'No commits to push.'
return 1
fi
echo 'Found commits to push.'
return 0
}

# Checks licenses.
Expand Down