Skip to content

Commit 2c076da

Browse files
committed
Revise scripts to follow the pattern of "payload as a function"
1 parent 6dc3fbd commit 2c076da

File tree

6 files changed

+69
-51
lines changed

6 files changed

+69
-51
lines changed

Diff for: git-branch-alias

+16-13
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@
6161
# expansion "$@" is changed to "$ <at>"), so don't try to use this copy:
6262
# http://permalink.gmane.org/gmane.comp.version-control.git/247581
6363

64+
git_branch_alias() {
6465
#cwd=$(git rev-parse --show-toplevel)
6566
git=$(git rev-parse --git-dir)
6667
if [ $? -ne 0 ]; then
67-
exit 1
68+
return 1
6869
fi
6970

7071
command=$(basename $0)
@@ -129,7 +130,7 @@ if [ -n "${shorthelp}" ]; then
129130
For help, use: ${command} -h
130131
131132
EOF
132-
exit 0
133+
return 0
133134
fi
134135

135136
if [ -n "${help}" ]; then
@@ -169,15 +170,15 @@ this situation by first switching HEAD to <alias>'s target branch
169170
if HEAD was currently set to <alias>.
170171
171172
EOF
172-
exit 0
173+
return 0
173174
fi
174175

175176
# Use the current branch by default.
176177
if [ -z "${branch}" ]; then
177178
branch=$(git symbolic-ref -q HEAD)
178179
if [ $? -ne 0 ]; then
179180
stderr "Could not establish current HEAD."
180-
exit 1
181+
return 1
181182
fi
182183
fi
183184

@@ -191,13 +192,13 @@ symref=${symref##refs/heads/}
191192
if [ -n "${delete}" ]; then
192193
if [ ! -f "${git}/refs/heads/${symref}" ]; then
193194
stderr "Symbolic reference refs/heads/${symref} does not exist."
194-
exit 1
195+
return 1
195196
fi
196197

197198
# Verify that it IS a symbolic reference
198199
if ! git symbolic-ref "refs/heads/${symref}" >/dev/null; then
199200
stderr "Error validating refs/heads/${symref} as symbolic reference."
200-
exit 1
201+
return 1
201202
fi
202203

203204
# If we currently have <symref> checked out, deleting it is bad
@@ -210,7 +211,7 @@ if [ -n "${delete}" ]; then
210211
branch=$(git symbolic-ref -q HEAD)
211212
if [ $? -ne 0 ]; then
212213
stderr "Could not establish current HEAD."
213-
exit 1
214+
return 1
214215
fi
215216
stdout "Switching HEAD to target branch ${branch}"
216217
# By using git symbolic-ref HEAD to find the target ref
@@ -219,7 +220,7 @@ if [ -n "${delete}" ]; then
219220
if ! git symbolic-ref HEAD "${branch}"; then
220221
stderr "Error updating HEAD from ${symref} to ${branch}"
221222
stderr "Aborting."
222-
exit 1
223+
return 1
223224
fi
224225
fi
225226

@@ -229,7 +230,7 @@ if [ -n "${delete}" ]; then
229230
# the backwards-compatible command.
230231
stdout "Deleting symbolic reference refs/heads/${symref}"
231232
git update-ref -d --no-deref "refs/heads/${symref}"
232-
exit $?
233+
return $?
233234
fi
234235

235236
# Creating a new symbolic reference.
@@ -239,7 +240,7 @@ fi
239240
# the wrong way around (treating it like ln -s is a really bad idea).
240241
if [ ! -f "${git}/refs/heads/${branch}" ]; then
241242
stderr "Target refs/heads/${branch} does not exist."
242-
exit 1
243+
return 1
243244
fi
244245
if [ -f "${git}/refs/heads/${symref}" ]; then
245246
target=$(git symbolic-ref "refs/heads/${symref}")
@@ -251,12 +252,12 @@ if [ -f "${git}/refs/heads/${symref}" ]; then
251252
stderr "File refs/heads/${symref} already exists"
252253
stderr "(and is not a symbolic reference!)"
253254
fi
254-
exit 1
255+
return 1
255256
fi
256257
if git show-ref --verify --heads --quiet "refs/heads/${symref}"; then
257258
# n.b. I'm pretty sure this is unreachable, given the previous block.
258259
stderr "refs/heads/${symref} is a valid reference without a file!?"
259-
exit 1
260+
return 1
260261
fi
261262

262263
# The parameters are good.
@@ -266,6 +267,8 @@ if git symbolic-ref "refs/heads/${symref}" "refs/heads/${branch}"; then
266267
stdout " ${symref} -> ${target##refs/heads/}"
267268
else
268269
stderr "Failed to create branch alias."
269-
exit 1
270+
return 1
270271
fi
272+
}
271273

274+
git_branch_alias "$@"

Diff for: git-branchkill

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
### branch and its trackers.
55
### Script Copyright (C) 2015 by Jim Klimov, License: MIT
66

7+
git_branch_kill() {
78
EXEC=""
89
LOCALKILL=-d
910
REPODIRS=""
@@ -22,7 +23,7 @@ while [ $# -gt 0 ]; do
2223
echo " (e.g. from a shell glob or Midnight Commander substitution);"
2324
echo " if no '-R ...' is specified, script works in current dir"
2425
echo " branch(es) List one or more branch name(s) to remove"
25-
exit 0
26+
return 0
2627
;;
2728
-n) EXEC=echo
2829
echo "Info: Running in read-only mode, no branches should get cut" >&2
@@ -44,22 +45,22 @@ while [ $# -gt 0 ]; do
4445
shift 2
4546
else
4647
echo "Error: Repo dir '$2' specified but not found" >&2
47-
exit 1
48+
return 1
4849
fi
4950
;;
5051
*) break ;; # fall through with branch names
5152
esac
5253
done
5354

54-
[ $# = 0 ] && echo "Error: branch name(s) required" >&2 && exit 1
55+
[ $# = 0 ] && echo "Error: branch name(s) required" >&2 && return 1
5556

5657
### List of failed-to-kill objects:
5758
### "[MRL](RESCODE)\tPATH\tBRANCH\tREASON"
5859
FAILED=""
5960

6061
branchkill() {
61-
CURBRANCH="`git branch | egrep '^\* ' | sed 's,^\* ,,'`" || exit
62-
[ -n "$CURBRANCH" ] || exit
62+
CURBRANCH="`git branch | egrep '^\* ' | sed 's,^\* ,,'`" || return
63+
[ -n "$CURBRANCH" ] || return
6364

6465
REMOTES="`git remote -v | egrep 'push' | awk '{print $1}'`" || REMOTES=""
6566

@@ -134,4 +135,7 @@ fi
134135
echo "List of failed items: $FAILED"
135136
# First line is empty by construction process
136137

137-
exit $RRES
138+
return $RRES
139+
}
140+
141+
git_branch_kill "$@"

Diff for: git-merge-um

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
### depends on "--set-upstream-to" configuration of your branch).
1616
### Copyright (C) 2015-2016 by Jim Klimov, License: MIT
1717

18+
do_git_merge_update_master() {
1819
BASE_REPO=upstream
1920
BASE_BRANCH=master
2021
DO_REBASE=no
@@ -48,3 +49,6 @@ fi
4849
echo ""
4950
echo "=== Pushing the changes back..."
5051
git sync
52+
}
53+
54+
do_git_merge_update_master "$@"

Diff for: git-pull-ff

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LANG=C
1212
LC_ALL=C
1313
export PATH LANG LC_ALL
1414

15-
git_pull-ff() {
15+
git_pull_ff() {
1616

1717
currentbranchref="$(git symbolic-ref HEAD 2>&-)"
1818
currentbranch="`git branch | grep '^* ' | sed 's,^* ,,'`" || \
@@ -114,5 +114,5 @@ git pull
114114
mkdir "$repodir/.zfs/snapshot/git-pull-ff--$TS--Z--afterSync" || true
115115
}
116116

117-
git_pull-ff
117+
git_pull_ff
118118

Diff for: git-sync

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### the remote repositories, and so the local sources are most up-to-date.
1010
### Copyright (C) 2014-2016 by Jim Klimov, License: MIT
1111

12+
do_git_sync() {
1213
echo "=== Syncing from repo `pwd` branch `git branch | egrep '^\*' | sed 's,^\* ,,'` to known remotes..."
1314
for R in "" \
1415
`git remote -v | egrep 'push' | awk '{print $1}' | grep -v upstream` \
@@ -21,4 +22,6 @@ done
2122

2223
echo "=== Pulling fast-forward from remotes ..."
2324
git pull-ff
25+
}
2426

27+
do_git_sync "$@"

0 commit comments

Comments
 (0)