Skip to content

Commit 0cd29a0

Browse files
Oblomovgitster
authored andcommitted
git-am: refactor 'cleaning up and aborting'
Introduce a clean_abort function that echoes an optional error message to standard error, removes the dotest directory and exits with status 1. Use it when patch format detection or patch splitting fails early. Signed-off-by: Giuseppe Bilotta <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c574e68 commit 0cd29a0

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

git-am.sh

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,12 @@ It does not apply to blobs recorded in its index."
134134
unset GITHEAD_$his_tree
135135
}
136136

137+
clean_abort () {
138+
test $# = 0 || echo >&2 "$@"
139+
rm -fr "$dotest"
140+
exit 1
141+
}
142+
137143
patch_format=
138144

139145
check_patch_format () {
@@ -180,22 +186,19 @@ check_patch_format () {
180186
esac
181187
;;
182188
esac
183-
} < "$1"
189+
} < "$1" || clean_abort
184190
}
185191

186192
split_patches () {
187193
case "$patch_format" in
188194
mbox)
189-
git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
190-
rm -fr "$dotest"
191-
exit 1
192-
}
195+
git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
196+
clean_abort
193197
;;
194198
stgit-series)
195199
if test $# -ne 1
196200
then
197-
echo "Only one StGIT patch series can be applied at once"
198-
exit 1
201+
clean_abort "Only one StGIT patch series can be applied at once"
199202
fi
200203
series_dir=`dirname "$1"`
201204
series_file="$1"
@@ -210,7 +213,7 @@ split_patches () {
210213
shift
211214
# remove the arg coming from the first-line comment
212215
shift
213-
} < "$series_file"
216+
} < "$series_file" || clean_abort
214217
# set the patch format appropriately
215218
patch_format=stgit
216219
# now handle the actual StGIT patches
@@ -239,18 +242,14 @@ split_patches () {
239242
print "Subject: ", $_ ;
240243
$subject = 1;
241244
}
242-
' < "$stgit" > "$dotest/$msgnum" || {
243-
echo "Failed to import $patch_format patch $stgit"
244-
exit 1
245-
}
245+
' < "$stgit" > "$dotest/$msgnum" || clean_abort
246246
done
247247
echo "$this" > "$dotest/last"
248248
this=
249249
msgnum=
250250
;;
251251
*)
252-
echo "Patch format $patch_format is not supported."
253-
exit 1
252+
clean_abort "Patch format $patch_format is not supported."
254253
;;
255254
esac
256255
}

0 commit comments

Comments
 (0)