Skip to content

Commit 471f729

Browse files
committed
Improve pre-commit versioning hook
1 parent c752412 commit 471f729

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

Planet/versioning.xcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
CURRENT_PROJECT_VERSION = 2617
1+
CURRENT_PROJECT_VERSION = 2619

git-hooks/pre-commit.sh

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
#!/bin/sh
2-
buildNumber=`git rev-list --count main`
3-
echo "CURRENT_PROJECT_VERSION = $buildNumber" > Planet/versioning.xcconfig
4-
echo "Set CURRENT_PROJECT_VERSION to $buildNumber"
5-
git add Planet/versioning.xcconfig
2+
3+
set -eu
4+
5+
repo_root=$(git rev-parse --show-toplevel)
6+
git_dir=$(git rev-parse --git-dir)
7+
case "$git_dir" in
8+
/*) ;;
9+
*) git_dir="$repo_root/$git_dir" ;;
10+
esac
11+
12+
actual_index="$git_dir/index"
13+
current_index="${GIT_INDEX_FILE:-$actual_index}"
14+
version_file="Planet/versioning.xcconfig"
15+
build_number=$(( $(git rev-list --count HEAD) + 1 ))
16+
version_line="CURRENT_PROJECT_VERSION = $build_number"
17+
18+
cd "$repo_root"
19+
20+
current_line=""
21+
if [ -f "$version_file" ]; then
22+
current_line=$(cat "$version_file")
23+
fi
24+
25+
if [ "$current_line" != "$version_line" ]; then
26+
printf '%s\n' "$version_line" > "$version_file"
27+
printf 'Set CURRENT_PROJECT_VERSION to %s\n' "$build_number"
28+
git add -- "$version_file"
29+
30+
# Partial commits can run hooks against a temporary index.
31+
if [ "$current_index" != "$actual_index" ]; then
32+
GIT_INDEX_FILE="$actual_index" git add -- "$version_file"
33+
fi
34+
fi

0 commit comments

Comments
 (0)