-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-remotes.sh
executable file
·254 lines (231 loc) · 6.8 KB
/
git-remotes.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/bin/bash
# Tools for listing/removing branches.
# -Christopher Welborn 03-25-2017
appname="git-remotes"
appversion="0.0.3"
apppath="$(readlink -f "${BASH_SOURCE[0]}")"
appscript="${apppath##*/}"
appdir="${apppath%/*}"
colr_file="$appdir/colr.sh"
if hash colrc &>/dev/null; then
function colr {
# Even wrapped in a function, this is still faster than colr.sh and colr.py.
colrc "$@"
}
elif [[ -e "$colr_file" ]]; then
# shellcheck source=/home/cj/scripts/git-commands/colr.sh
source "$colr_file"
colr_auto_disable
else
logger --id=$$ "git-remotes.sh: missing $colr_file"
function colr {
echo -e "$1"
}
fi
function echo_err {
# Echo to stderr.
echo -e "$@" 1>&2
}
function delete_remote_branch {
# Delete a remote branch.
local originname=$1 branchname=$2
[[ -z "$originname" ]] && {
read -r -p "Enter the origin name [origin]: " originname
}
[[ -z "$originname" ]] && originname="origin"
if [[ "$originname" =~ / ]]; then
branchname="$(cut -f2 -d'/' <<<"$originname")"
originname="$(cut -f1 -d'/' <<<"$originname")"
fi
local originfmt
originfmt="$(colr "$originname" blue)"
[[ -z "$branchname" ]] && {
read -r -p "Enter a remote branch to delete on $originfmt: " branchname;
}
[[ -z "$branchname" ]] && {
echo_err "No remote branch given!"
return 1
}
local branchfmt
branchfmt="$(colr "$branchname" cyan)"
valid_origin_branch "$originname" "$branchname" || {
echo_err "Not a valid remote branch: $originfmt/$branchfmt"
return 1
}
echo -e "\nDeleting remote branch: $originfmt/$branchfmt"
git push "$originname" --delete "$branchname"
}
function fail {
# Print a message to stderr and exit with an error status code.
echo_err "$@"
exit 1
}
function fail_usage {
# Print a usage failure message, and exit with an error status code.
print_usage "$@"
exit 1
}
function git_remote_branch {
local status repo
status="$(git status --porcelain --branch)" || {
fail "No repo name available from \`git status\`."
}
local repo
repo="$(cut -d '.' -f 4 <<<"$status" | cut -d ' ' -f1)"
if [[ -z "$repo" ]] || [[ "$repo" == '##'* ]]; then
fail "No remote repo found from \`git status\`."
fi
printf "%s" "$repo"
}
function git_remote_status {
local statline status
statline="$(git status --porcelain --branch)" || {
fail "No repo name available from \`git status\`."
}
status="$(cut -d '[' -f2 <<<"$statline" | tr -d ']')"
if [[ -z "$status" ]] || [[ "$status" == '##'* ]]; then
fail "No remote repo found from \`git status\`."
fi
printf "%s" "$status"
}
function git_unpushed {
local repo=$1
[[ -z "$repo" ]] && repo="$(git_remote_branch)"
[[ -z "$repo" ]] && return 1
local output
if output="$(git log --pretty=oneline "$repo..HEAD")"; then
if [[ -n "$output" ]]; then
printf "%s\n" "$output"
else
printf "All commits pushed to: %s\n" "$repo"
fi
else
printf "Unable to get unpushed commits for: %s\n" "$repo"
return 1
fi
return 0
}
function git_unpulled {
local repo=$1
[[ -z "$repo" ]] && repo="$(git_remote_branch)"
[[ -z "$repo" ]] && return 1
local repoargs
repoargs="$(echo "$repo" | tr '/' ' ')"
if git fetch "$repoargs" 1>/dev/null 2>/dev/null; then
git log --pretty=oneline HEAD.."$repo"
else
printf "No changes for: %s\n" "$repo"
return 1
fi
return 0
}
function print_usage {
# Show usage reason if first arg is available.
[[ -n "$1" ]] && echo_err "\n$1\n"
echo "$appname v. $appversion
Usage:
$appscript -h | -v
$appscript -b | -B
$appscript (-u | -U) [ORIGIN/BRANCH]
$appscript -d [BRANCH] [ORIGIN]
Options:
BRANCH : Remote branch name.
You may also use the 'origin/branch' format.
ORIGIN : Origin to work with.
-b,--branches : Show remote branches.
-B,--allbranches : Show all branches.
-d,--delete : Delete a remote branch.
-h,--help : Show this message.
-u,--unpushed : Show unpushed commits.
-U,--unpulled : Show unpulled commits.
-v,--version : Show $appname version and exit.
"
}
function show_branches {
# Show branches (remote by default).
declare -a args=("$@")
((${#args[@]})) || args+=("-r")
git branch "${args[@]}"
}
function show_remotes {
# Show all remote names, using `git remote -v`, except colorized.
local origin url method total=0 methodcolor="yellow"
while read -r origin url method; do
((total++))
methodcolor="yellow"
[[ "$method" =~ push ]] && methodcolor="green"
printf "%s %s %s\n" \
"$(colr "$(printf "%-25s" "$origin")" blue)" \
"$(colr "$(printf "%-45s" "$url")" cyan)" \
"$(colr "$method" "$methodcolor")"
done < <(git remote -v)
if ((!total)); then
echo_err "No remote repos.\n"
return 1
fi
return 0
}
function valid_origin_branch {
# Returns a successful exit status if the origin name and branch are
# valid (tested with `git show`)
local originname=$1 branchname=$2
if [[ -z "$originname" ]] || [[ -z "$branchname" ]]; then
echo_err "Both origin name and branch are needed. Got:"
echo_err " origin: '$originname'"
echo_err " branch: '$branchname'"
return 1
fi
git show --no-patch --pretty="format:%h" "${originname}/${branchname}" &>/dev/null
}
declare -a nonflags
do_all_branches=0
do_branches=0
do_remote_delete=0
do_unpushed=0
do_unpulled=0
for arg; do
case "$arg" in
"-B" | "--allbranches")
do_branches=1
do_all_branches=1
;;
"-b" | "--branches")
do_branches=1
;;
"-d" | "--delete")
do_remote_delete=1
;;
"-h" | "--help")
print_usage ""
exit 0
;;
"-u" | "--unpushed")
do_unpushed=1
;;
"-U" | "--unpulled")
do_unpulled=1
;;
"-v" | "--version")
echo -e "$appname v. $appversion\n"
exit 0
;;
-*)
fail_usage "Unknown flag argument: $arg"
;;
*)
nonflags+=("$arg")
esac
done
if ((do_branches)); then
arg="-r"
((do_all_branches)) && arg="-a"
show_branches "$arg"
elif ((do_remote_delete)); then
delete_remote_branch "${nonflags[0]}" "${nonflags[1]}"
elif ((do_unpushed)); then
git_unpushed "${nonflags[@]}"
elif ((do_unpulled)); then
git_unpulled "${nonflags[@]}"
else
show_remotes
fi