Skip to content

Commit 0ea29cc

Browse files
emkgitster
authored andcommitted
filter-branch: Add more error-handling
9273b56 (filter-branch: Fix fatal error on bare repositories, 2009-02-03) fixed a missing check of return status from an underlying command in git-filter-branch, but there still are places that do not check errors. For example, the command does not pay attention to the exit status of the command given by --commit-filter. It should abort in such a case. This attempts to fix all the remaining places that fails to checks errors. In two places, I've had to break apart pipelines in order to check the error code for the first stage of the pipeline, as discussed here: http://kerneltrap.org/mailarchive/git/2009/1/28/4835614 Feedback on this patch was provided by Johannes Sixt, Johannes Schindelin and Junio C Hamano. Thomas Rast helped with pipeline error handling. Signed-off-by: Eric Kidd <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e5f5050 commit 0ea29cc

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

git-filter-branch.sh

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ die ""
221221
trap 'cd ../..; rm -rf "$tempdir"' 0
222222

223223
# Make sure refs/original is empty
224-
git for-each-ref > "$tempdir"/backup-refs
224+
git for-each-ref > "$tempdir"/backup-refs || exit
225225
while read sha1 type name
226226
do
227227
case "$force,$name" in
@@ -241,8 +241,9 @@ GIT_WORK_TREE=.
241241
export GIT_DIR GIT_WORK_TREE
242242

243243
# The refs should be updated if their heads were rewritten
244-
git rev-parse --no-flags --revs-only --symbolic-full-name --default HEAD "$@" |
245-
sed -e '/^^/d' >"$tempdir"/heads
244+
git rev-parse --no-flags --revs-only --symbolic-full-name \
245+
--default HEAD "$@" > "$tempdir"/raw-heads || exit
246+
sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
246247

247248
test -s "$tempdir"/heads ||
248249
die "Which ref do you want to rewrite?"
@@ -251,8 +252,6 @@ GIT_INDEX_FILE="$(pwd)/../index"
251252
export GIT_INDEX_FILE
252253
git read-tree || die "Could not seed the index"
253254

254-
ret=0
255-
256255
# map old->new commit ids for rewriting parents
257256
mkdir ../map || die "Could not create map/ directory"
258257

@@ -315,10 +314,11 @@ while read commit parents; do
315314
die "tree filter failed: $filter_tree"
316315

317316
(
318-
git diff-index -r --name-only $commit
317+
git diff-index -r --name-only $commit &&
319318
git ls-files --others
320-
) |
321-
git update-index --add --replace --remove --stdin
319+
) > "$tempdir"/tree-state || exit
320+
git update-index --add --replace --remove --stdin \
321+
< "$tempdir"/tree-state || exit
322322
fi
323323

324324
eval "$filter_index" < /dev/null ||
@@ -339,7 +339,8 @@ while read commit parents; do
339339
eval "$filter_msg" > ../message ||
340340
die "msg filter failed: $filter_msg"
341341
@SHELL_PATH@ -c "$filter_commit" "git commit-tree" \
342-
$(git write-tree) $parentstr < ../message > ../map/$commit
342+
$(git write-tree) $parentstr < ../message > ../map/$commit ||
343+
die "could not write rewritten commit"
343344
done <../revs
344345

345346
# In case of a subdirectory filter, it is possible that a specified head
@@ -407,7 +408,8 @@ do
407408
die "Could not rewrite $ref"
408409
;;
409410
esac
410-
git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1
411+
git update-ref -m "filter-branch: backup" "$orig_namespace$ref" $sha1 ||
412+
exit
411413
done < "$tempdir"/heads
412414

413415
# TODO: This should possibly go, with the semantics that all positive given
@@ -483,7 +485,7 @@ test -z "$ORIG_GIT_INDEX_FILE" || {
483485
}
484486

485487
if [ "$(is_bare_repository)" = false ]; then
486-
git read-tree -u -m HEAD
488+
git read-tree -u -m HEAD || exit
487489
fi
488490

489-
exit $ret
491+
exit 0

t/t7003-filter-branch.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ test_expect_success 'result is really identical' '
4848
test $H = $(git rev-parse HEAD)
4949
'
5050

51+
test_expect_success 'Fail if commit filter fails' '
52+
test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
53+
'
54+
5155
test_expect_success 'rewrite, renaming a specific file' '
5256
git filter-branch -f --tree-filter "mv d doh || :" HEAD
5357
'

0 commit comments

Comments
 (0)