-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgit-tagger.sh
executable file
·116 lines (99 loc) · 2.67 KB
/
git-tagger.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/sh
#
# git-tagger.sh
#
# automatically add a tag to the given git repositories
# input: a list of target git repositories that is generated by meta-debian
#
# (example)
# input: git.list.git.server.com,
# usage: ./git-tagger.sh git.list.git.server.com git.server.com tagname
#
WORKDIR=$(pwd)
LOG=$WORKDIR/git-tagger.log
GITLIST_SORT=$WORKDIR/git.list.sort
GITDIR=${WORKDIR}/git
usage() {
echo "
usage: $0 <git.list> <server> <tag> [-d]
arguments:
git.list : a list of git repositories generated by meta-debian
server : the git server name that provides all repositories in git.list
tag : net tag name
options:
-d : delete mode; delete given tag in all repositories
example:
$0 git.list.git.server.com git.server.com tagname
"
exit 2
}
die() {
echo "ERROR: $@" | tee -a $LOG
exit 1
}
# arguments
GITLIST="$1"
SERVER="$2"
TAG="$3"
[ -n "$TAG" ] || usage
[ -f $GITLIST ] || die $GITLIST not found
# options
OPT="$4"
# tagging
rm -rf $LOG $GITDIR
if [ "$OPT" = "-d" ]; then
echo "deleting tag \"$TAG\" in each repository..."
else
echo "adding tag \"$TAG\" to each repository..."
fi
grep -v "^#" $GITLIST | sort | uniq > $GITLIST_SORT
while read line; do
path=$(echo "$line" | cut -d " " -f 1)
commit=$(echo "$line" | cut -d " " -f 2)
# uri=git://$SERVER/$path
uri=gitosis@$SERVER:$path
if [ "$OPT" = "-d" ]; then
echo "deleting $TAG in $uri" | tee -a $LOG
else
echo "adding $TAG to $uri $commit" | tee -a $LOG
fi
if [ "$OPT" = "-d" ]; then
# confirm the target tag exists
if [ -z "$(git ls-remote --tags $uri $TAG)" ]; then
die "tag $TAG not found in $uri"
fi
# delete the tag
mkdir $GITDIR || die "failed to create $GITDIR"
cd $GITDIR
git init >> $LOG 2>&1 || die "failed to initialize $GITDIR"
echo "git push $uri :refs/tags/$TAG" >> $LOG
git push $uri :refs/tags/$TAG >> $LOG 2>&1 || \
die "failed to delete tag"
cd $WORKDIR
else
# clone
echo "git clone --bare $uri $GITDIR" >> $LOG
git clone --bare $uri $GITDIR >> $LOG 2>&1 || \
die "git clone failed"
cd $GITDIR
# confirm the target commit exists
if ! git show -s --format=oneline $commit >/dev/null 2>&1; then
die "commit $commit not found in $uri"
fi
# confirm the tag name is not used
if git tag | grep -q '^'"$TAG"'$'; then
die "tag $TAG is already used in $uri"
fi
# add a tag
echo "git tag $TAG $commit" >> $LOG
git tag $TAG $commit >> $LOG 2>&1 || die "git tag failed"
# push
echo "git push --tags" >> $LOG
git push --tags >> $LOG 2>&1 || die "git push failed"
cd $WORKDIR
fi
echo "----------------------------------------------------------------------" >> $LOG
rm -rf $GITDIR
done < $GITLIST_SORT
rm -f $GITLIST_SORT
echo "done"