-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-tag-version.sh
executable file
·81 lines (72 loc) · 2.25 KB
/
git-tag-version.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/bash
set -eo pipefail
# Import common functions
DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=./common.sh
source "$DIR/common.sh"
USAGE="Usage: $0 [OPTIONS]
Create git version tags for a Rust project.
OPTIONS: All options are optional
-h | --help
Display these instructions.
-d | --dryrun
Only print commands instead of executing them.
-p | --push
Push tags to remote.
--verbose
Display commands being executed.
"
DRYRUN=false
PUSH=false
while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
print "$USAGE"
exit 1
;;
-d | --dryrun)
DRYRUN=true
;;
-p | --push)
PUSH=true
;;
-v | --verbose)
set -x
;;
esac
shift
done
current_tag=""
for commit_hash in $(git log --format="%H" --reverse -- Cargo.toml); do
# Use git show to display changes in the commit for Cargo.toml,
# and filter for added lines modifying the version
version_line=$(git show "$commit_hash":Cargo.toml | grep '^version = ' || true)
if [ -n "$version_line" ]; then
version_number=$(echo "$version_line" | awk -F'"' '/version = / {print $2}')
if [ "$current_tag" = "$version_number" ]; then
print_yellow "Skip $version_number: $commit_hash"
continue
else
current_tag="$version_number"
fi
print_magenta "$version_number"
if [ -n "$version_number" ]; then
if git tag -l | grep -q "^${version_number}$"; then
print_red "Found version tag without v-prefix, removing..."
run_command git tag -d "$version_number"
if [ "$PUSH" = true ]; then
run_command git push origin --delete "$version_number"
fi
fi
tag="v$version_number"
if git tag -l | grep -q "^${tag}$"; then
print_red "Tag $tag already exists, skipping..."
else
run_command git tag -a "$tag" "$commit_hash" -m "Version $version_number"
fi
if [ "$PUSH" = true ]; then
run_command git push origin "$tag"
fi
fi
fi
done