Skip to content

Commit f78ab35

Browse files
Kuniwakgitster
authored andcommitted
filter-branch: fix errors caused by refs that point at non-committish
"git filter-branch -- --all" prints error messages when processing refs that point at objects that are not committish. Such refs can be created by "git replace" with trees or blobs. And also "git tag" with trees or blobs can create such refs. Filter these problematic refs out early, before they are seen by the logic to see which refs have been modified and which have been left intact (which is where the unwanted error messages come from), and warn that these refs are left unwritten while doing so. Signed-off-by: Yuki Kokubun <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d32eb83 commit f78ab35

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

git-filter-branch.sh

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,18 @@ done < "$tempdir"/backup-refs
251251

252252
# The refs should be updated if their heads were rewritten
253253
git rev-parse --no-flags --revs-only --symbolic-full-name \
254-
--default HEAD "$@" > "$tempdir"/raw-heads || exit
255-
sed -e '/^^/d' "$tempdir"/raw-heads >"$tempdir"/heads
254+
--default HEAD "$@" > "$tempdir"/raw-refs || exit
255+
while read ref
256+
do
257+
case "$ref" in ^?*) continue ;; esac
258+
259+
if git rev-parse --verify "$ref"^0 >/dev/null 2>&1
260+
then
261+
echo "$ref"
262+
else
263+
warn "WARNING: not rewriting '$ref' (not a committish)"
264+
fi
265+
done >"$tempdir"/heads <"$tempdir"/raw-refs
256266

257267
test -s "$tempdir"/heads ||
258268
die "You must specify a ref to rewrite."

t/t7003-filter-branch.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,4 +470,18 @@ test_expect_success 'tree-filter deals with object name vs pathname ambiguity' '
470470
git show HEAD:$ambiguous
471471
'
472472

473+
test_expect_success 'rewrite repository including refs that point at non-commit object' '
474+
test_when_finished "git reset --hard original" &&
475+
tree=$(git rev-parse HEAD^{tree}) &&
476+
test_when_finished "git replace -d $tree" &&
477+
echo A >new &&
478+
git add new &&
479+
new_tree=$(git write-tree) &&
480+
git replace $tree $new_tree &&
481+
git tag -a -m "tag to a tree" treetag $new_tree &&
482+
git reset --hard HEAD &&
483+
git filter-branch -f -- --all >filter-output 2>&1 &&
484+
! fgrep fatal filter-output
485+
'
486+
473487
test_done

0 commit comments

Comments
 (0)