Skip to content

Commit a3e42ce

Browse files
committed
Test new posix compatible pre-commit
1 parent 3df0398 commit a3e42ce

File tree

2 files changed

+105
-62
lines changed

2 files changed

+105
-62
lines changed

.githooks/pre-commit

Lines changed: 104 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,149 @@
1-
#!/usr/bin/sh
2-
#
3-
# An example hook script to verify what is about to be committed.
4-
# Called by "git commit" with no arguments. The hook should
5-
# exit with non-zero status after issuing an appropriate message if
6-
# it wants to stop the commit.
7-
#
8-
# To enable this hook, rename this file to "pre-commit".
9-
#
10-
# Exit on error
11-
# set -e
1+
#!/usr/bin/env sh
2+
# POSIX /bin/sh hook: works on Debian (dash) and Fedora/RHEL
3+
# set -e # uncomment if you want hard-fail on first error
4+
set -u
125

136
project_dir=$(git rev-parse --show-toplevel)
147

158
psrfix() {
16-
command -v php-cs-fixer >/dev/null || return
17-
$(command -v php-cs-fixer) fix $project_dir/packages/web --rules=@PSR2
18-
git add $project_dir/packages/web
9+
command -v php-cs-fixer >/dev/null 2>&1 || return 0
10+
php_cs_fix_bin=$(command -v php-cs-fixer)
11+
"$php_cs_fix_bin" fix "$project_dir/packages/web" --rules=@PSR2
12+
git add "$project_dir/packages/web"
1913
}
2014

2115
updateLanguage() {
22-
command -v xgettext >/dev/null || return
23-
command -v msgcat >/dev/null || return
24-
command -v msgmerge >/dev/null || return
25-
xgettext --language=PHP --from-code=UTF-8 --output="$project_dir/packages/web/management/languages/messages.pot" --omit-header --no-location $(find $project_dir/packages/web/ -name "*.php")
26-
msgcat --sort-output -o "$project_dir/packages/web/management/languages/messages.pot" "$project_dir/packages/web/management/languages/messages.pot"
27-
for PO_FILE in $(find $project_dir/packages/web/management/languages/ -type f -name *.po); do
28-
msgmerge --update --backup=none $PO_FILE $project_dir/packages/web/management/languages/messages.pot 2>/dev/null >/dev/null
29-
msgcat --sort-output -o $PO_FILE $PO_FILE
16+
command -v xgettext >/dev/null 2>&1 || return 0
17+
command -v msgcat >/dev/null 2>&1 || return 0
18+
command -v msgmerge >/dev/null 2>&1 || return 0
19+
20+
pot="$project_dir/packages/web/management/languages/messages.pot"
21+
tmp_list="$(mktemp)"
22+
23+
# Build a newline-separated list of PHP files (safe for typical paths)
24+
find "$project_dir/packages/web" -type f -name '*.php' -print > "$tmp_list"
25+
26+
if [ -s "$tmp_list" ]; then
27+
xgettext --language=PHP --from-code=UTF-8 \
28+
--output="$pot" --omit-header --no-location \
29+
-f "$tmp_list"
30+
msgcat --sort-output -o "$pot" "$pot"
31+
fi
32+
33+
# Update all .po files
34+
find "$project_dir/packages/web/management/languages" -type f -name '*.po' -print \
35+
| while IFS= read -r po; do
36+
[ -f "$po" ] || continue
37+
msgmerge --update --backup=none "$po" "$pot" >/dev/null 2>&1 || true
38+
msgcat --sort-output -o "$po" "$po"
3039
done
40+
41+
rm -f "$tmp_list"
3142
}
3243

33-
updateLanguage
34-
#psrfix
44+
# --- tag-aware helpers (POSIX) ---
45+
nearest_tag() {
46+
ref="${1:-HEAD}"
47+
git describe --tags --abbrev=0 "$ref" 2>/dev/null || echo ""
48+
}
3549

36-
# Get the current branch name
37-
gitbranch=$(git branch --show-current)
50+
count_since_tag() {
51+
ref="${1:-HEAD}"
52+
tag="$(nearest_tag "$ref")"
53+
if [ -n "$tag" ]; then
54+
git rev-list "${tag}..${ref}" --count 2>/dev/null || echo 0
55+
else
56+
git rev-list "$ref" --count 2>/dev/null || echo 0
57+
fi
58+
}
3859

39-
# Get the latest tag commit
40-
gitcom=$(git rev-list --tags --no-walk --max-count=1)
60+
baseversion_from_tag() {
61+
tag="$1"
62+
if [ -z "$tag" ]; then
63+
echo "0.0"
64+
return
65+
fi
66+
# Drop last dot segment: X.Y.Z -> X.Y ; X -> X
67+
echo "$tag" | awk -F. '{ if (NF>1){NF--; print $0} else {print $0} }' OFS='.'
68+
}
4169

42-
# Count commits from the last tag to HEAD
43-
gitcount=$(git rev-list master..HEAD --count)
70+
updateLanguage
71+
#psrfix
4472

45-
# Extract the first part of the branch name
46-
branchon=$(echo ${gitbranch} | awk -F'-' '{print $1}')
73+
# Current branch (fallback if detached)
74+
gitbranch=$(git branch --show-current 2>/dev/null || git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")
4775

48-
# Extract the second part of the branch name
49-
branchend=$(echo ${gitbranch} | awk -F'-' '{print $2}')
76+
# Extract parts around first '-'
77+
branchon=${gitbranch%%-*}
78+
branchend=${gitbranch#*-}
79+
[ "$branchend" = "$gitbranch" ] && branchend=""
5080

51-
# Define the path to the system file
5281
system_file="$project_dir/packages/web/lib/fog/system.class.php"
5382

54-
current_version=$(grep "define('FOG_VERSION'" $system_file | sed "s/.*FOG_VERSION', '\([^']*\)');/\1/")
83+
# Current values (best-effort)
84+
current_version=$(grep "define('FOG_VERSION'" "$system_file" | sed "s/.*FOG_VERSION', '\([^']*\)');/\1/")
85+
current_channel=$(grep "define('FOG_CHANNEL'" "$system_file" | sed "s/.*FOG_CHANNEL', '\([^']*\)');/\1/")
5586

56-
verbegin=""
5787
channel=""
88+
trunkversion=""
5889

59-
case $branchon in
90+
case "$branchon" in
6091
dev)
61-
# Describe the tag and append the commit count correctly
62-
tagversion=$(git describe --tags ${gitcom})
63-
baseversion=${tagversion%.*} # Retain everything before the last segment
64-
lastrevision=${tagversion##*.} # Extracts the last segment
92+
tag="$(nearest_tag HEAD)"
93+
baseversion="$(baseversion_from_tag "$tag")"
94+
gitcount="$(count_since_tag HEAD)"
6595
trunkversion="${baseversion}.${gitcount}"
6696
channel="Patches"
6797
;;
6898
stable)
69-
# Describe the tag and append the commit count correctly
70-
tagversion=$(git describe --tags ${gitcom})
71-
baseversion=${tagversion%.*} # Retain everything before the last segment
72-
lastrevision=${tagversion##*.} # Extracts the last segment
99+
# Derive from dev-branch if it exists, else HEAD
100+
if git rev-parse --verify dev-branch >/dev/null 2>&1; then
101+
tag="$(nearest_tag dev-branch)"
102+
baseversion="$(baseversion_from_tag "$tag")"
103+
gitcount="$(count_since_tag dev-branch)"
104+
else
105+
tag="$(nearest_tag HEAD)"
106+
baseversion="$(baseversion_from_tag "$tag")"
107+
gitcount="$(count_since_tag HEAD)"
108+
fi
73109
trunkversion="${baseversion}.${gitcount}"
74-
gitcount=$(git rev-list master..dev-branch --count) # Get the gitcount from dev-branch instead
75110
channel="Patches"
76111
;;
77112
working)
78-
# Generate a version number based on the branchend and commit count for the working branch
79-
verbegin="${branchend}.0-beta"
80-
trunkversion="${verbegin}.${gitcount}"
113+
gitcount="$(count_since_tag HEAD)"
114+
trunkversion="${branchend}.0-beta.${gitcount}"
81115
channel="Beta"
82116
;;
83117
rc)
84118
channel="Release Candidate"
85119
version_prefix="${branchend}.0-RC"
86-
if [[ $current_version =~ ${version_prefix}-([0-9]+) ]]; then
87-
last_rc_version=${BASH_REMATCH[1]}
88-
next_rc_version=$((last_rc_version + 1))
120+
# If current_version matches PREFIX-N, increment N; else start at 1
121+
n=$(printf '%s\n' "$current_version" | sed -n "s/^${version_prefix}-\([0-9][0-9]*\)\$/\1/p")
122+
if [ -n "$n" ]; then
123+
n=$((n + 1))
124+
trunkversion="${version_prefix}-${n}"
89125
else
90126
trunkversion="${version_prefix}-1"
91127
fi
92128
;;
93129
feature)
94-
verbegin="${branchend}.0-feature"
95-
trunkversion="${verbegin}.${gitcount}"
130+
gitcount="$(count_since_tag HEAD)"
131+
trunkversion="${branchend}.0-feature.${gitcount}"
96132
channel="Feature"
97133
;;
134+
*)
135+
# Unknown naming: keep existing values
136+
trunkversion="${current_version:-0.0.0}"
137+
channel="${current_channel:-Patches}"
138+
;;
98139
esac
99140

141+
# Update the version and channel if computed
142+
if [ -n "$trunkversion" ] && [ -n "$channel" ]; then
143+
sed -i \
144+
-e "s/define('FOG_VERSION', *'.*');/define('FOG_VERSION', '${trunkversion}');/g" \
145+
-e "s/define('FOG_CHANNEL', *'.*');/define('FOG_CHANNEL', '${channel}');/g" \
146+
"$system_file"
147+
git add "$system_file"
148+
fi
100149

101-
# Update the version and channel in the system file
102-
sed -i "s/define('FOG_VERSION',.*);/define('FOG_VERSION', '$trunkversion');/g" $system_file
103-
sed -i "s/define('FOG_CHANNEL',.*);/define('FOG_CHANNEL', '$channel');/g" $system_file
104-
105-
# Add the modified system file to the staging area
106-
git add $system_file

packages/web/lib/fog/system.class.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static function _versionCompare()
5353
public function __construct()
5454
{
5555
self::_versionCompare();
56-
define('FOG_VERSION', '1.5.10.1708');
56+
define('FOG_VERSION', '1.5.10.9');
5757
define('FOG_SCHEMA', 273);
5858
define('FOG_BCACHE_VER', 141);
5959
define('FOG_CLIENT_VERSION', '0.13.0');

0 commit comments

Comments
 (0)