Skip to content

Commit 376ccb8

Browse files
dschogitster
authored andcommitted
rebase -i: style fixes and minor cleanups
This patch indents ";;" consistently with the rest of git's shell scripts, and makes sure that ";;" are before each "esac". It introduces a helper function "has_action", to make it easier to read the intentions of the code. Errors from "git rev-parse --verify" are no longer ignored. Spaces are quoted using single quotes instead of a backslash, for readability. A "test $preserve=f" (missing spaces) was fixed; hashes are no longer written to "$DOTEST"/rewritten/ unnecessarily. We used to quote the message for a squash, only to have "echo" unquote it. Now we use "printf" and do not need to quote to start with. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent be6ff20 commit 376ccb8

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

git-rebase--interactive.sh

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ output () {
4141
test $status != 0 &&
4242
cat "$DOTEST"/output
4343
return $status
44-
;;
44+
;;
4545
*)
4646
"$@"
47+
;;
4748
esac
4849
}
4950

@@ -63,6 +64,7 @@ comment_for_reflog () {
6364
''|rebase*)
6465
GIT_REFLOG_ACTION="rebase -i ($1)"
6566
export GIT_REFLOG_ACTION
67+
;;
6668
esac
6769
}
6870

@@ -96,13 +98,18 @@ die_abort () {
9698
die "$1"
9799
}
98100

101+
has_action () {
102+
grep -vqe '^$' -e '^#' "$1"
103+
}
104+
99105
pick_one () {
100106
no_ff=
101107
case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
102108
output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
103109
test -d "$REWRITTEN" &&
104110
pick_one_preserving_merges "$@" && return
105-
parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
111+
parent_sha1=$(git rev-parse --verify $sha1^) ||
112+
die "Could not get the parent of $sha1"
106113
current_sha1=$(git rev-parse --verify HEAD)
107114
if test $no_ff$current_sha1 = $parent_sha1; then
108115
output git reset --hard $sha1
@@ -130,7 +137,7 @@ pick_one_preserving_merges () {
130137
fast_forward=t
131138
preserve=t
132139
new_parents=
133-
for p in $(git rev-list --parents -1 $sha1 | cut -d\ -f2-)
140+
for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
134141
do
135142
if test -f "$REWRITTEN"/$p
136143
then
@@ -142,41 +149,43 @@ pick_one_preserving_merges () {
142149
;; # do nothing; that parent is already there
143150
*)
144151
new_parents="$new_parents $new_p"
152+
;;
145153
esac
146154
fi
147155
done
148156
case $fast_forward in
149157
t)
150158
output warn "Fast forward to $sha1"
151-
test $preserve=f && echo $sha1 > "$REWRITTEN"/$sha1
159+
test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
152160
;;
153161
f)
154162
test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
155163

156-
first_parent=$(expr "$new_parents" : " \([^ ]*\)")
164+
first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
157165
# detach HEAD to current parent
158166
output git checkout $first_parent 2> /dev/null ||
159167
die "Cannot move HEAD to $first_parent"
160168

161169
echo $sha1 > "$DOTEST"/current-commit
162170
case "$new_parents" in
163-
\ *\ *)
171+
' '*' '*)
164172
# redo merge
165173
author_script=$(get_author_ident_from_commit $sha1)
166174
eval "$author_script"
167-
msg="$(git cat-file commit $sha1 | \
168-
sed -e '1,/^$/d' -e "s/[\"\\]/\\\\&/g")"
175+
msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
169176
# NEEDSWORK: give rerere a chance
170177
if ! output git merge $STRATEGY -m "$msg" $new_parents
171178
then
172-
echo "$msg" > "$GIT_DIR"/MERGE_MSG
179+
printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
173180
die Error redoing merge $sha1
174181
fi
175182
;;
176183
*)
177184
output git cherry-pick $STRATEGY "$@" ||
178185
die_with_patch $sha1 "Could not pick $sha1"
186+
;;
179187
esac
188+
;;
180189
esac
181190
}
182191

@@ -213,12 +222,11 @@ peek_next_command () {
213222
}
214223

215224
do_next () {
216-
test -f "$DOTEST"/message && rm "$DOTEST"/message
217-
test -f "$DOTEST"/author-script && rm "$DOTEST"/author-script
218-
test -f "$DOTEST"/amend && rm "$DOTEST"/amend
225+
rm -f "$DOTEST"/message "$DOTEST"/author-script \
226+
"$DOTEST"/amend || exit
219227
read command sha1 rest < "$TODO"
220228
case "$command" in
221-
\#|'')
229+
'#'*|'')
222230
mark_action_done
223231
;;
224232
pick)
@@ -246,7 +254,7 @@ do_next () {
246254
squash)
247255
comment_for_reflog squash
248256

249-
test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
257+
has_action "$DONE" ||
250258
die "Cannot 'squash' without a previous commit"
251259

252260
mark_action_done
@@ -256,11 +264,12 @@ do_next () {
256264
EDIT_COMMIT=
257265
USE_OUTPUT=output
258266
cp "$MSG" "$SQUASH_MSG"
259-
;;
267+
;;
260268
*)
261269
EDIT_COMMIT=-e
262270
USE_OUTPUT=
263-
test -f "$SQUASH_MSG" && rm "$SQUASH_MSG"
271+
rm -f "$SQUASH_MSG" || exit
272+
;;
264273
esac
265274

266275
failed=f
@@ -280,11 +289,13 @@ do_next () {
280289
warn
281290
warn "Could not apply $sha1... $rest"
282291
die_with_patch $sha1 ""
292+
;;
283293
esac
284294
;;
285295
*)
286296
warn "Unknown command: $command $sha1 $rest"
287297
die_with_patch $sha1 "Please fix this in the file $TODO."
298+
;;
288299
esac
289300
test -s "$TODO" && return
290301

@@ -473,17 +484,18 @@ EOF
473484
$UPSTREAM...$HEAD | \
474485
sed -n "s/^>/pick /p" >> "$TODO"
475486

476-
test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
487+
has_action "$TODO" ||
477488
die_abort "Nothing to do"
478489

479490
cp "$TODO" "$TODO".backup
480491
git_editor "$TODO" ||
481492
die "Could not execute editor"
482493

483-
test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
494+
has_action "$TODO" ||
484495
die_abort "Nothing to do"
485496

486497
output git checkout $ONTO && do_rest
498+
;;
487499
esac
488500
shift
489501
done

0 commit comments

Comments
 (0)