@matheusbsbs / matheus-bagliano / commits / 0ea29cce4d
Fork 0

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 <git@randomhacks.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Kidd committed Feb 11, 2009 at 16:10 UTC 0ea29cce4d099d2fa6613ec66c080776cfe869d5
2 files changed +18 -12
git-filter-branch.sh
+14 -12
@@ -221,7 +221,7 @@ die ""
221 trap 'cd ../..; rm -rf "$tempdir"' 0
222
223 # Make sure refs/original is empty
224 -git for-each-ref > "$tempdir"/backup-refs
224 +git for-each-ref > "$tempdir"/backup-refs || exit
225 while read sha1 type name
226 do
227 case "$force,$name" in
@@ -241,8 +241,9 @@ GIT_WORK_TREE=.
241 export GIT_DIR GIT_WORK_TREE
242
243 # 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
247
248 test -s "$tempdir"/heads ||
249 die "Which ref do you want to rewrite?"
@@ -251,8 +252,6 @@ GIT_INDEX_FILE="$(pwd)/../index"
252 export GIT_INDEX_FILE
253 git read-tree || die "Could not seed the index"
254
254 -ret=0
255 -
255 # map old->new commit ids for rewriting parents
256 mkdir ../map || die "Could not create map/ directory"
257
@@ -315,10 +314,11 @@ while read commit parents; do
314 die "tree filter failed: $filter_tree"
315
316 (
318 - git diff-index -r --name-only $commit
317 + git diff-index -r --name-only $commit &&
318 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
322 fi
323
324 eval "$filter_index" < /dev/null ||
@@ -339,7 +339,8 @@ while read commit parents; do
339 eval "$filter_msg" > ../message ||
340 die "msg filter failed: $filter_msg"
341 @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"
344 done <../revs
345
346 # In case of a subdirectory filter, it is possible that a specified head
@@ -407,7 +408,8 @@ do
408 die "Could not rewrite $ref"
409 ;;
410 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
413 done < "$tempdir"/heads
414
415 # TODO: This should possibly go, with the semantics that all positive given
@@ -483,7 +485,7 @@ test -z "$ORIG_GIT_INDEX_FILE" || {
485 }
486
487 if [ "$(is_bare_repository)" = false ]; then
486 - git read-tree -u -m HEAD
488 + git read-tree -u -m HEAD || exit
489 fi
490
489 -exit $ret
491 +exit 0
t/t7003-filter-branch.sh
+4
@@ -48,6 +48,10 @@ test_expect_success 'result is really identical' '
48 test $H = $(git rev-parse HEAD)
49 '
50
51 +test_expect_success 'Fail if commit filter fails' '
52 + test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
53 +'
54 +
55 test_expect_success 'rewrite, renaming a specific file' '
56 git filter-branch -f --tree-filter "mv d doh || :" HEAD
57 '