-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_version
More file actions
executable file
·100 lines (85 loc) · 2.58 KB
/
auto_version
File metadata and controls
executable file
·100 lines (85 loc) · 2.58 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env bash
POSITIONAL=()
update_patch_version=true
update_major_version=false
update_minor_version=false
while [[ $# -gt 0 ]]; do
case "$1" in
-p | --patch)
update_patch_version=true
shift 1
;;
-m | --major)
update_major_version=true
shift 1
;;
-n | --minor)
update_minor_version=true
shift 1
;;
*)
POSITIONAL+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL[@]}"
function git_current_branch() {
git symbolic-ref --short HEAD 2>/dev/null
or git rev-parse --short HEAD 2>/dev/null
}
current_branch=$(git_current_branch)
if [[ ! $current_branch =~ ^(master|main)$ ]]; then
echo "this script can only work with branch master or main"
exit 255
fi
# update before tagging
git fetch --all
for remote in $(git remote); do
git pull "$remote" "$current_branch"
done
for dir in $(find . -name 'go.mod' -exec dirname {} \; | sort -u); do
pkg=$(echo "$dir" | sed 's/\.\///')
latest_version=$(git tag --sort=-v:refname -l "$pkg/v*" | head -1 | sed 's/.*\///')
current_version=$latest_version
major_version=0
minor_version=0
patch_version=1
latest_version="${latest_version#[vV]}"
fields=$(echo "$latest_version" | grep -o "\." | wc -l)
if [ "$fields" -eq 2 ]; then
major_version="${latest_version%%\.*}"
minor_version="${latest_version#*.}"
minor_version="${minor_version%.*}"
patch_version="${latest_version##*.}"
fi
changes=0
first_version=false
if [[ -n "${current_version// /}" ]]; then
changes=$(git diff --name-only "$pkg/$current_version"..HEAD "$dir" | wc -l)
else
changes=$(find "$dir" -exec printf %c {} + | wc -c)
first_version=true
fi
if [ "$changes" -gt 0 ]; then
[[ $update_patch_version == true ]] && patch_version=$((patch_version + 1))
[[ $update_minor_version == true ]] && minor_version=$((minor_version + 1)) && patch_version=0
[[ $update_major_version == true ]] && major_version=$((major_version + 1)) && minor_version=0 && patch_version=0
[[ $first_version == true ]] && major_version=0 && minor_version=1 && patch_version=0
new_version=v$major_version.$minor_version.$patch_version
tag=$pkg/$new_version
if [[ $first_version == true ]]; then
echo "tagging package $pkg with version $new_version"
else
echo "tagging package $pkg with version $new_version from $current_version"
fi
git tag "$tag"
for remote in $(git remote); do
remote_repo=$(git remote get-url "$remote")
echo "pushing $tag to $remote_repo"
git push --no-verify "$remote" "$tag"
done
else
echo "no changes in $pkg"
fi
done