-
Notifications
You must be signed in to change notification settings - Fork 11
/
git-tag-patchset
executable file
·93 lines (76 loc) · 2.21 KB
/
git-tag-patchset
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
#!/bin/bash
# Quick way of tagging patch sets as they get uploaded to Gerrit, to
# keep track of a review's history. Ideally git-review would do this;
# submitted as https://storyboard.openstack.org/#!/story/2005068
me=`basename $0`
usage () {
if [ -n "$1" ]; then
echo "$*" >&2
echo
fi
cat <<EOF >&2
Usage: $me [-f|--force] [PATCH-SET-NUM] [COMMIT-ISH]
Make a tag for a new or given patch set for the current branch which
points to COMMIT-ISH.
The tag will be named in this format:
\${current_branch}/PS\${num}
Options:
-f, --force Force-update an existing tag to point to COMMIT-ISH
EOF
exit 1
}
find_latest_patch_set () {
set -o pipefail
git show-ref |
awk '$2 ~ /^refs\/tags\/'"${tag_prefix////\\/}"'[0-9]+$/ {print $2}' |
sed "s,^refs/tags/${tag_prefix},," |
sort -n |
tail -n 1
}
main () {
case "$1" in
-f|--force)
force=-f
shift
;;
esac
if [ $# -gt 2 ]; then
usage
fi
if ! branch="`git head`"; then
echo >&2 "Couldn't figure out current branch! Aborting."
exit 1
fi
target="${2:-$branch}"
tag_prefix="$branch/PS"
if [ -n "$1" ]; then
tag="${tag_prefix}$1"
else
latest_patch_set=$( find_latest_patch_set )
if [[ $? != 0 ]]; then
echo >&2 "Couldn't figure out latest existing patch set for $branch; aborting."
exit 1
fi
if [[ -z "$latest_patch_set" ]]; then
echo >&2 "No previous patchset; assuming this is the first."
new_patch_set=1
else
new_patch_set="$(( latest_patch_set + 1 ))"
echo "Latest patch set is $latest_patch_set; new patch set will be $new_patch_set"
fi
tag="${tag_prefix}$new_patch_set"
fi
if git rev-parse --quiet --verify "$tag" >/dev/null; then
current=$( git rev-parse "$tag")
if [[ "$current" == $(git rev-parse "$target") ]]; then
echo "$tag already points to $target"
else
git tag $force "$tag" "$target"
fi
else
if git tag "$tag" "$target"; then
echo "Tagged $target as $tag"
fi
fi
}
main "$@"