|
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 |
12 | 5 |
|
13 | 6 | project_dir=$(git rev-parse --show-toplevel) |
14 | 7 |
|
15 | 8 | 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" |
19 | 13 | } |
20 | 14 |
|
21 | 15 | 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" |
30 | 39 | done |
| 40 | + |
| 41 | + rm -f "$tmp_list" |
31 | 42 | } |
32 | 43 |
|
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 | +} |
35 | 49 |
|
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 | +} |
38 | 59 |
|
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 | +} |
41 | 69 |
|
42 | | -# Count commits from the last tag to HEAD |
43 | | -gitcount=$(git rev-list master..HEAD --count) |
| 70 | +updateLanguage |
| 71 | +#psrfix |
44 | 72 |
|
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 "") |
47 | 75 |
|
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="" |
50 | 80 |
|
51 | | -# Define the path to the system file |
52 | 81 | system_file="$project_dir/packages/web/lib/fog/system.class.php" |
53 | 82 |
|
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/") |
55 | 86 |
|
56 | | -verbegin="" |
57 | 87 | channel="" |
| 88 | +trunkversion="" |
58 | 89 |
|
59 | | -case $branchon in |
| 90 | +case "$branchon" in |
60 | 91 | 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)" |
65 | 95 | trunkversion="${baseversion}.${gitcount}" |
66 | 96 | channel="Patches" |
67 | 97 | ;; |
68 | 98 | 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 |
73 | 109 | trunkversion="${baseversion}.${gitcount}" |
74 | | - gitcount=$(git rev-list master..dev-branch --count) # Get the gitcount from dev-branch instead |
75 | 110 | channel="Patches" |
76 | 111 | ;; |
77 | 112 | 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}" |
81 | 115 | channel="Beta" |
82 | 116 | ;; |
83 | 117 | rc) |
84 | 118 | channel="Release Candidate" |
85 | 119 | 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}" |
89 | 125 | else |
90 | 126 | trunkversion="${version_prefix}-1" |
91 | 127 | fi |
92 | 128 | ;; |
93 | 129 | feature) |
94 | | - verbegin="${branchend}.0-feature" |
95 | | - trunkversion="${verbegin}.${gitcount}" |
| 130 | + gitcount="$(count_since_tag HEAD)" |
| 131 | + trunkversion="${branchend}.0-feature.${gitcount}" |
96 | 132 | channel="Feature" |
97 | 133 | ;; |
| 134 | + *) |
| 135 | + # Unknown naming: keep existing values |
| 136 | + trunkversion="${current_version:-0.0.0}" |
| 137 | + channel="${current_channel:-Patches}" |
| 138 | + ;; |
98 | 139 | esac |
99 | 140 |
|
| 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 |
100 | 149 |
|
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 |
|
0 commit comments