forked from ceejatec/git-gerrit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-asf
executable file
·104 lines (88 loc) · 2.23 KB
/
git-asf
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
#!/bin/bash
usage()
{
echo "Usage: git asf commit <SHA>"
exit
}
# Returns non-0 if the remote doesn't exist
check_remote()
{
git config remote.$1.url 2>&1 > /dev/null
}
# Clean up temp stuff created by script
finish()
{
# Delete _asf remote if exists
if check_remote _asf
then
git remote rm _asf
fi
}
trap finish EXIT
cd_to_root()
{
cd `git rev-parse --git-dir`/..
}
asf_commit()
{
cd_to_root
branch=master
while getopts "b:" OPTION
do
case $OPTION in
b)
branch=$OPTARG
;;
esac
done
shift $((OPTIND-1))
# Fetch latest from gerrit
if ! check_remote gerrit
then
echo "Remote 'gerrit' does not exist - please run 'git gerrit init'"
exit 2
fi
echo "Fetching latest state from Gerrit"
git fetch gerrit || exit 5
# Add temporary remote to ASF, including tracking only the target branch and fetching
if ! check_remote _asf
then
project=`basename $(git ls-remote --get-url gerrit)`
git remote add _asf -t $branch "https://git-wip-us.apache.org/repos/asf/$project"
fi
echo "Fetching latest state from `git ls-remote --get-url _asf`"
git fetch _asf || exit 5
# Ensure that the SHA argument exists
sha=`git rev-parse --quiet --verify $1`
if [ -z "$sha" ]
then
echo "Commit '$1' does not exist!"
exit 3
fi
# Ensure that the commit we've been given has already been submitted to Gerrit
if ! git branch --quiet -r --contains $1 | grep -q gerrit/$branch > /dev/null
then
echo "Commit '$1' has not been submitted to branch $branch in Gerrit."
exit 10
fi
# Ensure that the tip of the _asf branch is a parent of the SHA
asf_tip=`git rev-parse --quiet --verify remotes/_asf/$branch`
if ! git log -1 --pretty=%P $sha | grep $asf_tip 2>&1 > /dev/null
then
echo "SHA '$sha' is not a direct child of the ASF tip!"
exit 10
fi
# Finally, all's well - push
echo "Pushing $sha to ASF"
git push _asf $sha:refs/heads/$branch
}
# First argument is sub-command
case "$1" in
"commit" )
shift
asf_commit $@
;;
* )
usage
;;
esac