clang-format: handle branch without commits

When a branch has no commits of its own, e.g. when it is the baseline
branch itself, git rev-list returns nothing and first_commit is empty.
The empty value was still used to build "$first_commit^", which git
rejects as a bad revision.

git-clang-format then failed without writing anything to stdout, which
the output check reads as "formatting required". Pushing the baseline
branch therefore failed the CI formatting check, reporting a style
violation that does not exist.

Report that there is nothing to format instead.
git rev-list also returns nothing when the baseline ref does not exist
as it only reports that on stderr, so verify the baseline separately to
keep that case a loud failure rather than a silent pass.

Ticket: #9097
pull/16253/head
Juliana Fajardini 1 month ago committed by Victor Julien
parent 1283580858
commit 6aa97aa8a9

@ -340,8 +340,11 @@ function HelpCommand {
# Do not compare with main directly as it will diff with the latest commit
# on main. If our branch has not been rebased on the latest main, this
# would result in including all new commits on main!
# Returns non-zero when baseline doesn't exist
function FirstCommitOfBranch {
start="${SURICATA_BRANCH:-origin/main}"
# don't leak a local variable
local start="${SURICATA_BRANCH:-origin/main}"
git rev-parse --verify --quiet "$start^{commit}" >/dev/null || return 1
local first_commit=$(git rev-list $start..HEAD | tail -n 1)
echo $first_commit
}
@ -397,7 +400,16 @@ function CheckBranch {
# Find first commit on branch. Use $first_commit^ if you need the
# commit on main we branched off.
local first_commit=$(FirstCommitOfBranch)
local first_commit
first_commit=$(FirstCommitOfBranch) ||
Die "Baseline branch '${SURICATA_BRANCH:-origin/main}' does not exist"
# No commits of our own, e.g. when the branch is the baseline itself.
if [ -z "$first_commit" ]; then
if [ $quiet -ne 1 ]; then
echo "no commits on branch"
fi
return $EXIT_CODE_OK
fi
# git-clang-format is a python script that does not like SIGPIPE shut down
# by "| head" prematurely. Use work-around with writing to tmpfile first.
@ -465,7 +477,13 @@ function ReformatBranch {
# Find first commit on branch. Use $first_commit^ if you need the
# commit on main we branched off.
local first_commit=$(FirstCommitOfBranch)
local first_commit
first_commit=$(FirstCommitOfBranch) ||
Die "Baseline branch '${SURICATA_BRANCH:-origin/main}' does not exist"
if [ -z "$first_commit" ]; then
echo "no commits on branch"
ExitWith $EXIT_CODE_OK
fi
echo "First commit on branch: $first_commit"
$GIT_CLANG_FORMAT --style file --extensions c,h $with_unstaged $first_commit^
@ -541,6 +559,7 @@ function ReformatCommitsOnBranch {
# Find first commit on branch. Use $first_commit^ if you need the
# commit on main we branched off.
# CheckBranch above already bailed out on a missing baseline.
local first_commit=$(FirstCommitOfBranch)
echo "First commit on branch: $first_commit"
# Use --force in case it's run a second time on the same branch

Loading…
Cancel
Save