@matheusbsbs / matheus-bagliano / commits / 8c1ce0f46b
Fork 0

filter-branch: fail gracefully when a filter fails

A common mistake is to provide a filter which fails unwantedly. For example, this will stop in the middle: git filter-branch --env-filter ' test $GIT_COMMITTER_EMAIL = xyz && export GIT_COMMITTER_EMAIL = abc' rewritten When $GIT_COMMITTER_EMAIL is not "xyz", the test fails, and consequently the whole filter has a non-zero exit status. However, as demonstrated in this example, filter-branch would just stop, and the user would be none the wiser. Also, a failing msg-filter would not have been caught, as was the case with one of the tests. This patch fixes both issues, by paying attention to the exit status of msg-filter, and by saying what failed before exiting. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Johannes Schindelin committed Jul 4, 2007 at 15:36 UTC 8c1ce0f46b85d40f215084eed7313896300082df
2 files changed +36 -11
git-filter-branch.sh
+29 -10
@@ -28,6 +28,16 @@ map()
28 fi
29 }
30
31 +# override die(): this version puts in an extra line break, so that
32 +# the progress is still visible
33 +
34 +die()
35 +{
36 + echo >&2
37 + echo "$*" >&2
38 + exit 1
39 +}
40 +
41 # When piped a commit, output a script to set the ident of either
42 # "author" or "committer
43
@@ -181,23 +191,29 @@ while read commit parents; do
191 export GIT_COMMIT=$commit
192 git cat-file commit "$commit" >../commit
193
184 - eval "$(set_ident AUTHOR <../commit)"
185 - eval "$(set_ident COMMITTER <../commit)"
186 - eval "$filter_env" < /dev/null
194 + eval "$(set_ident AUTHOR <../commit)" ||
195 + die "setting author failed for commit $commit"
196 + eval "$(set_ident COMMITTER <../commit)" ||
197 + die "setting committer failed for commit $commit"
198 + eval "$filter_env" < /dev/null ||
199 + die "env filter failed: $filter_env"
200
201 if [ "$filter_tree" ]; then
202 git checkout-index -f -u -a
203 # files that $commit removed are now still in the working tree;
204 # remove them, else they would be added again
205 git ls-files -z --others | xargs -0 rm -f
193 - eval "$filter_tree" < /dev/null
206 + eval "$filter_tree" < /dev/null ||
207 + die "tree filter failed: $filter_tree"
208 +
209 git diff-index -r $commit | cut -f 2- | tr '\n' '\0' | \
210 xargs -0 git update-index --add --replace --remove
211 git ls-files -z --others | \
212 xargs -0 git update-index --add --replace --remove
213 fi
214
200 - eval "$filter_index" < /dev/null
215 + eval "$filter_index" < /dev/null ||
216 + die "index filter failed: $filter_index"
217
218 parentstr=
219 for parent in $parents; do
@@ -206,13 +222,15 @@ while read commit parents; do
222 done
223 done
224 if [ "$filter_parent" ]; then
209 - parentstr="$(echo "$parentstr" | eval "$filter_parent")"
225 + parentstr="$(echo "$parentstr" | eval "$filter_parent")" ||
226 + die "parent filter failed: $filter_parent"
227 fi
228
229 sed -e '1,/^$/d' <../commit | \
213 - eval "$filter_msg" | \
214 - sh -c "$filter_commit" "git commit-tree" $(git write-tree) \
215 - $parentstr > ../map/$commit
230 + eval "$filter_msg" > ../message ||
231 + die "msg filter failed: $filter_msg"
232 + sh -c "$filter_commit" "git commit-tree" \
233 + $(git write-tree) $parentstr < ../message > ../map/$commit
234 done <../revs
235
236 src_head=$(tail -n 1 ../revs | sed -e 's/ .*//')
@@ -249,7 +267,8 @@ if [ "$filter_tag_name" ]; then
267 [ -f "../map/$sha1" ] || continue
268 new_sha1="$(cat "../map/$sha1")"
269 export GIT_COMMIT="$sha1"
252 - new_ref="$(echo "$ref" | eval "$filter_tag_name")"
270 + new_ref="$(echo "$ref" | eval "$filter_tag_name")" ||
271 + die "tag name filter failed: $filter_tag_name"
272
273 echo "$ref -> $new_ref ($sha1 -> $new_sha1)"
274
t/t7003-filter-branch.sh
+7 -1
@@ -107,13 +107,19 @@ test_expect_success 'use index-filter to move into a subdirectory' '
107 mv \$GIT_INDEX_FILE.new \$GIT_INDEX_FILE" directorymoved &&
108 test -z "$(git diff HEAD directorymoved:newsubdir)"'
109
110 +test_expect_success 'stops when msg filter fails' '
111 + ! git-filter-branch --msg-filter false nonono &&
112 + rm -rf .git-rewrite &&
113 + ! git rev-parse nonono
114 +'
115 +
116 test_expect_success 'author information is preserved' '
117 : > i &&
118 git add i &&
119 test_tick &&
120 GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
121 git-filter-branch --msg-filter "cat; \
116 - test \$GIT_COMMIT = $(git rev-parse master) && \
122 + test \$GIT_COMMIT != $(git rev-parse master) || \
123 echo Hallo" \
124 preserved-author &&
125 test 1 = $(git rev-list --author="B V Uips" preserved-author | wc -l)