-
Notifications
You must be signed in to change notification settings - Fork 11
/
git-upstream
executable file
·178 lines (152 loc) · 4.6 KB
/
git-upstream
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/bin/bash
# Figure out ref of local branch's upstream remote/branch.
me=`basename $0`
usage () {
# Call as: usage [EXITCODE] [USAGE MESSAGE]
exit_code=1
if [[ "$1" == [0-9] ]]; then
exit_code="$1"
shift
fi
if [ -n "$1" ]; then
echo "$*" >&2
echo
fi
cat <<EOF >&2
Usage: $me [<local branch>]
Outputs the upstream remote/branch for a given local branch,
if it can be calculated.
The local branch defaults to HEAD.
EOF
exit "$exit_code"
}
get_svn_upstream () {
if ! [ -d "$root/.git/svn" ]; then
return 1
fi
if [ `ls "$root/.git/svn" | wc -l` == 1 ]; then
if upstream=`cd $root/.git/svn && ls`; then
return 0
else
echo "WARNING: cd $root/.git/svn && ls failed" >&2
fi
fi
if ! svn_info=$( git svn info 2>&1 ); then
return 1
fi
if echo "$svn_info" | grep -q 'Unable to determine upstream SVN info'; then
return 1
fi
svn_branch=$(
echo "$svn_info" |
perl -lne '
/^URL: (.+)/ and $u = $1;
if (/^Repository Root: (.+)/) {
$r = $1;
$u =~ s!\Q$r/!!;
# This bit required since git svn info changed to including
# the subpath of the cwd in the URL (somewhere in between 1.6.0
# and 1.7.1):
($subdir = `git prefix`) =~ s!/\n$!!;
$u =~ s!/$subdir$!!;
print $u
}'
)
# Need to apply refspec transformation;
#svn_branches_map=$( git config svn-remote.svn.branches )
# for now we just assume standard layout and cheat.
upstream="remotes/${svn_branch#branches/}"
return 0
}
get_upstream () {
# If the upstream is explicitly specified in config, we honor that.
local_branch="$1"
upstream_remote_conf="branch.$local_branch.remote"
upstream_remote=$(git config "$upstream_remote_conf")
if [ -n "$upstream_remote" ]; then
upstream_branch_conf="branch.$local_branch.merge"
upstream_branch=$(git config "$upstream_branch_conf")
if [ -n "$upstream_branch" ]; then
upstream_branch="${upstream_branch#refs/heads/}"
if [ "$upstream_remote" = '.' ]; then
upstream="$upstream_branch"
else
upstream="$upstream_remote/$upstream_branch"
fi
if git rev-parse "$upstream" >/dev/null 2>&1; then
return
fi
else
handle_missing_upstream_branch
fi
fi
get_svn_upstream
}
handle_missing_upstream_branch () {
# We know which upstream remote to use, but not which
# branch in that remote, so try using the local branch
# name.
upstream="$upstream_remote/$local_branch"
if git rev-parse "$upstream" >/dev/null 2>&1; then
if [ -t 0 ]; then
# Running interactively
cat <<EOF
Remote for $local_branch was set to $upstream_remote but no upstream
branch was set. However, $upstream exists.
Would you like me to set the upstream branch to $local_branch?
EOF
echo -n "Type yes to proceed or anything else to abort > "
read answer
if [ "$answer" = 'yes' ]; then
if git config "$upstream_branch_conf" "$local_branch"; then
echo
echo "Set $upstream_branch_conf=$local_branch"
exit 0
fi
else
echo
echo "$upstream_branch_conf still not set; aborting."
exit 1
fi
else
cat <<EOF >&2
$upstream_branch_conf was not set but should probably be set to
$local_branch to match the local branch name. Please set manually via
git set-upstream. Aborting.
EOF
exit 1
fi
else
cat <<EOF >&2
Couldn't guess which branch of upstream '$upstream_remote' to use :-(
Please set manually via git set-upstream. Aborting.
EOF
exit 1
fi
}
root="`git root`"
if ! [ -d "$root" ]; then
exit 1
fi
if [ "$1" == '-h' ] || [ "$1" == '--help' ] || [ $# -gt 1 ]; then
usage 0
fi
local_branch="$1"
if [ $# -eq 0 ]; then
local_branch="`git head`"
fi
upstream=
get_upstream "$local_branch"
if [ -n "$upstream" ] && git rev-parse "$upstream" >/dev/null 2>&1; then
echo "$upstream"
exit 0
fi
if [ -n "$upstream" ]; then
echo "Best guess '$upstream' was invalid."
fi
cat <<EOF >&2
Maybe you should run:
git set-upstream $local_branch \$remote/\$branch
Couldn't figure out upstream ref; aborting.
EOF
exit 1