|
| 1 | +#!/bin/sh |
| 2 | +# |
| 3 | +# pre-commit hook: bumps the version in Cargo.toml as part of every commit. |
| 4 | +# The bump is staged before git writes the commit, so nothing is amended |
| 5 | +# afterwards: the hash git prints is final and a signed commit is signed once. |
| 6 | +# |
| 7 | +# Versions are X.Y.Z. Each commit bumps Z. |
| 8 | +# |
| 9 | +# GITST_BUMP=minor git commit ... bumps Y and resets Z. |
| 10 | +# GITST_BUMP=major git commit ... bumps X and resets Y and Z. |
| 11 | +# GITST_BUMP=none git commit ... keeps the version. |
| 12 | +# |
| 13 | +# Left alone: amends (they keep the version of the commit they replace), a |
| 14 | +# commit whose staged Cargo.toml already changes the version (set by hand), |
| 15 | +# `git commit <paths>`, and commits made by merge, rebase, cherry-pick and |
| 16 | +# revert. `--no-verify` skips this hook, and with it the bump. |
| 17 | +# |
| 18 | +# Only the staged Cargo.toml and Cargo.lock are bumped, plus the same line in |
| 19 | +# the working tree, so unstaged edits to those files stay out of the commit. |
| 20 | +# |
| 21 | +# Enable once per clone: git config core.hooksPath .githooks |
| 22 | + |
| 23 | +[ "$GITST_BUMP" = none ] && exit 0 |
| 24 | + |
| 25 | +GIT_DIR="$(git rev-parse --git-dir)" |
| 26 | + |
| 27 | +if [ -d "$GIT_DIR/rebase-merge" ] || [ -d "$GIT_DIR/rebase-apply" ] \ |
| 28 | + || [ -f "$GIT_DIR/CHERRY_PICK_HEAD" ] || [ -f "$GIT_DIR/REVERT_HEAD" ] \ |
| 29 | + || [ -f "$GIT_DIR/MERGE_HEAD" ]; then |
| 30 | + exit 0 |
| 31 | +fi |
| 32 | +# `git commit <paths>` commits from a temporary index, and a bump staged |
| 33 | +# there would be missing from the real one afterwards. |
| 34 | +case "$GIT_INDEX_FILE" in |
| 35 | +*next-index*) exit 0 ;; |
| 36 | +esac |
| 37 | +# A root commit keeps the version it was made with. |
| 38 | +git rev-parse -q --verify HEAD >/dev/null || exit 0 |
| 39 | + |
| 40 | +# An amend shows on git's command line (aliases are expanded by then). |
| 41 | +# Without ps, fall back on the author date git passes to hooks, which an |
| 42 | +# amend keeps from HEAD. |
| 43 | +ARGS="$(ps -o args= -p "$PPID" 2>/dev/null)" |
| 44 | +if [ -n "$ARGS" ]; then |
| 45 | + case " $ARGS " in |
| 46 | + *" --amend "*) exit 0 ;; |
| 47 | + esac |
| 48 | +elif [ "$GIT_AUTHOR_DATE" = "@$(git log -1 --format=%ad --date=raw)" ]; then |
| 49 | + exit 0 |
| 50 | +fi |
| 51 | + |
| 52 | +cd "$(git rev-parse --show-toplevel)" || exit 0 |
| 53 | + |
| 54 | +version_of() { |
| 55 | + sed -n 's/^version = "\([0-9]*\.[0-9]*\.[0-9]*\)".*/\1/p' | head -n 1 |
| 56 | +} |
| 57 | + |
| 58 | +HEAD_VERSION="$(git show HEAD:Cargo.toml 2>/dev/null | version_of)" |
| 59 | +STAGED="$(git show :Cargo.toml 2>/dev/null | version_of)" |
| 60 | +[ -n "$HEAD_VERSION" ] && [ -n "$STAGED" ] || exit 0 |
| 61 | + |
| 62 | +NEW="$STAGED" |
| 63 | +if [ "$STAGED" = "$HEAD_VERSION" ]; then |
| 64 | + MAJOR="$(echo "$STAGED" | cut -d. -f1)" |
| 65 | + MINOR="$(echo "$STAGED" | cut -d. -f2)" |
| 66 | + PATCH="$(echo "$STAGED" | cut -d. -f3)" |
| 67 | + case "$GITST_BUMP" in |
| 68 | + major) MAJOR=$((MAJOR + 1)) MINOR=0 PATCH=0 ;; |
| 69 | + minor) MINOR=$((MINOR + 1)) PATCH=0 ;; |
| 70 | + *) PATCH=$((PATCH + 1)) ;; |
| 71 | + esac |
| 72 | + NEW="$MAJOR.$MINOR.$PATCH" |
| 73 | +fi |
| 74 | + |
| 75 | +# Only the first `version =` line: the [package] one. |
| 76 | +bump_toml() { perl -pe "\$d ||= s/^version = \"[0-9.]+\"/version = \"$NEW\"/"; } |
| 77 | +# Cargo.lock too, also after a hand edit, so release builds can use --locked. |
| 78 | +bump_lock() { perl -0pe "s/(\[\[package\]\]\nname = \"gitst\"\nversion = \")[0-9.]+/\${1}$NEW/"; } |
| 79 | + |
| 80 | +# Rewrites the staged copy of $1 through $2, and the working copy the same way. |
| 81 | +bump() { |
| 82 | + git ls-files --error-unmatch -- "$1" >/dev/null 2>&1 || return 0 |
| 83 | + mode="$(git ls-files -s -- "$1" | cut -d' ' -f1)" |
| 84 | + blob="$(git show ":$1" | $2 | git hash-object -w --stdin)" || return 0 |
| 85 | + git update-index --cacheinfo "$mode,$blob,$1" || return 0 |
| 86 | + $2 <"$1" >"$1.bump" && cat "$1.bump" >"$1" |
| 87 | + rm -f "$1.bump" |
| 88 | +} |
| 89 | +bump Cargo.toml bump_toml |
| 90 | +bump Cargo.lock bump_lock |
| 91 | + |
| 92 | +[ "$NEW" = "$HEAD_VERSION" ] || echo "gitst $HEAD_VERSION -> $NEW" |
| 93 | +exit 0 |
0 commit comments