-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgit-remotes
More file actions
executable file
·178 lines (166 loc) · 5.61 KB
/
git-remotes
File metadata and controls
executable file
·178 lines (166 loc) · 5.61 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
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
#
# Prints the fork and upstream remotes.
# Finds the remotes from the following sources:
# 1. The existing remote for main (git config --get branch.main.remote)
# This is required.
# 2. origin (git remote get-url origin)
# 3. upstream (git remote get-url upstream)
# 4. for github, your github username (git-hub-user)
DEBUG=0
QUIET=0
FORK_ONLY=0
UPSTREAM_ONLY=0
# Parse arguments
while (( $# > 0 )); do
if [[ "$1" == "-d" || "$1" == "--debug" ]]; then
DEBUG=1
elif [[ "$1" == "-q" || "$1" == "--quiet" ]]; then
QUIET=1
elif [[ "$1" == "--fork" ]]; then
FORK_ONLY=1
elif [[ "$1" == "--upstream" ]]; then
UPSTREAM_ONLY=1
fi
shift
done
set -e # stop on error
SCRIPTS="$(dirname "$0")"
main=$("$SCRIPTS"/git-default-branch)
if (( $DEBUG )); then
>&2 echo "Using branch \"$main\" from: git default-branch"
fi
remote_main=$(git config --get branch.${main}.remote || echo)
if (( $DEBUG )); then
>&2 echo "Remote for branch \"$main\": $remote_main"
fi
remote_main_url=$(git remote get-url "$remote_main" 2>/dev/null || echo)
if (( $DEBUG )); then
>&2 echo "URL for branch \"$main\": $remote_main_url"
fi
if [[ ! "$remote_main_url" ]]; then
>&2 echo "Error: no remote found for the $main branch"
>&2 echo
>&2 echo "To set one, run the following (choose origin or upstream):"
>&2 echo " git branch --set-upstream-to=(origin | upstream) $main"
exit 1
fi
# Default to using the remote for main.
fork="$remote_main"
fork_url="$remote_main_url"
upstream="$remote_main"
upstream_url="$remote_main_url"
if (( $DEBUG )); then
>&2 echo "Default:"
>&2 printf " fork: $fork\t$fork_url\n"
>&2 printf " upstream: $upstream\t$upstream_url\n"
fi
# Check for origin/upstream fork configuration.
if [[ "$remote_main" == "origin" ]]; then
remote_origin_url=$remote_main_url
else
remote_origin_url=$(git remote get-url "origin" 2>/dev/null || echo)
fi
if (( $DEBUG )); then
>&2 echo "URL for remote origin: $remote_origin_url"
fi
remote_upstream_url=$(git remote get-url "upstream" 2>/dev/null || echo)
if (( $DEBUG )); then
>&2 echo "URL for remote upstream: $remote_upstream_url"
fi
if [[ "$remote_origin_url" && "$remote_upstream_url" ]]; then
fork="origin"
fork_url="$remote_origin_url"
upstream="upstream"
upstream_url="$remote_upstream_url"
if (( $DEBUG )); then
>&2 echo "Using origin and upstream:"
>&2 printf " fork: $fork\t$fork_url\n"
>&2 printf " upstream: $upstream\t$upstream_url\n"
fi
else
# Check for origin/<user> and upstream/<user> fork configuration.
if [[ "$remote_origin_url" =~ [./@]github[./] ]] ||
[[ "$remote_upstream_url" =~ [./@]github[./] ]]; then
if (( $DEBUG )); then
>&2 echo Found github URL
fi
hub_user=$("$SCRIPTS"/git-hub-user --no-input)
if ! [[ "$hub_user" ]]; then
if ! (( $QUIET )); then
>&2 echo "Warning: no github user found, but a github remote is being used"
>&2 echo
>&2 echo "To set a github user, run the following:"
>&2 echo " git hub-user"
fi
else
remote_user=$hub_user
if (( $DEBUG )); then
>&2 echo "Using remote user \"$remote_user\" from: git hub-user"
fi
fi
elif [[ "$remote_origin_url" =~ [./@]gitlab[./] ]] ||
[[ "$remote_upstream_url" =~ [./@]gitlab[./] ]]; then
if (( $DEBUG )); then
>&2 echo Found gitlab URL
fi
lab_user=$("$SCRIPTS"/git-lab-user)
if ! [[ "$lab_user" ]]; then
if ! (( $QUIET )); then
>&2 echo "Warning: no gitlab user found, but a gitlab remote is being used"
>&2 echo
>&2 echo "To set a gitlab user, run the following:"
>&2 echo " git lab-user"
fi
else
remote_user=$lab_user
if (( $DEBUG )); then
>&2 echo "Using remote user \"$remote_user\" from: git lab-user"
fi
fi
fi
if [[ -z "$remote_user" ]]; then
remote_user=$USER
if (( $DEBUG )); then
>&2 echo "Using remote user \"$remote_user\" from: \$USER"
fi
fi
remote_user_url=$(git remote get-url "$remote_user" 2>/dev/null || echo)
if (( $DEBUG )); then
>&2 echo "URL for remote $remote_user: $remote_user_url"
fi
if [[ "$remote_user_url" ]]; then
if [[ "$remote_origin_url" ]]; then
fork="$remote_user"
fork_url="$remote_user_url"
upstream="origin"
upstream_url="$remote_origin_url"
if (( $DEBUG )); then
>&2 echo "Using $remote_user and origin:"
>&2 printf " fork: $fork\t$fork_url\n"
>&2 printf " upstream: $upstream\t$upstream_url\n"
fi
elif [[ "$remote_upstream_url" ]]; then
fork="$remote_user"
fork_url="$remote_user_url"
upstream="upstream"
upstream_url="$remote_upstream_url"
if (( $DEBUG )); then
>&2 echo "Using $remote_user and upstream:"
>&2 printf " fork: $fork\t$fork_url\n"
>&2 printf " upstream: $upstream\t$upstream_url\n"
fi
fi
fi
fi
if (( $DEBUG )); then
>&2 echo
fi
if (( $FORK_ONLY )); then
printf "$fork\t$fork_url\n"
elif (( $UPSTREAM_ONLY )); then
printf "$upstream\t$upstream_url\n"
else
printf "fork: $fork\t$fork_url\n"
printf "upstream: $upstream\t$upstream_url\n"
fi