Skip to content

Commit 9d51a90

Browse files
committed
filter-repo: fix pruning of empty commits with blob callbacks
Blob callbacks, either implicit (via e.g. --replace-text) or explicit, can modify blobs in ways that make them match other blobs, which in turn can result in some commits becoming empty. We need to detect such cases and ensure we prune these empty commits when --prune-empty=auto. Reported-by: John Gietzen <[email protected]> Signed-off-by: Elijah Newren <[email protected]>
1 parent 3a3cd3d commit 9d51a90

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

git-filter-repo

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3348,11 +3348,14 @@ class RepoFilter(object):
33483348
self._filter_files(commit)
33493349

33503350
# Find out which files were modified by the callbacks. Such paths could
3351-
# lead to sebsequent commits being empty (e.g. if removed a line containing
3351+
# lead to subsequent commits being empty (e.g. if removing a line containing
33523352
# a password from every version of a file that had the password, and some
33533353
# later commit did nothing more than remove that line)
33543354
final_file_changes = set(commit.file_changes)
3355-
differences = orig_file_changes.symmetric_difference(final_file_changes)
3355+
if self._args.replace_text or self._blob_callback:
3356+
differences = orig_file_changes.union(final_file_changes)
3357+
else:
3358+
differences = orig_file_changes.symmetric_difference(final_file_changes)
33563359
self._files_tweaked.update(x.filename for x in differences)
33573360

33583361
# Call the user-defined callback, if any

t/t9390-filter-repo.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,27 @@ test_expect_success 'degenerate merge with non-matching filenames' '
13721372
)
13731373
'
13741374

1375+
test_expect_success 'Filtering a blob to make it match previous version' '
1376+
test_create_repo remove_unique_bits_of_blob &&
1377+
(
1378+
cd remove_unique_bits_of_blob &&
1379+
1380+
test_write_lines foo baz >metasyntactic_names &&
1381+
git add metasyntactic_names &&
1382+
git commit -m init &&
1383+
1384+
test_write_lines foo bar baz >metasyntactic_names &&
1385+
git add metasyntactic_names &&
1386+
git commit -m second &&
1387+
1388+
git filter-repo --force --blob-callback "blob.data = blob.data.replace(b\"\\nbar\", b\"\")"
1389+
1390+
echo 1 >expect &&
1391+
git rev-list --count HEAD >actual &&
1392+
test_cmp expect actual
1393+
)
1394+
'
1395+
13751396
test_expect_success 'tweaking just a tag' '
13761397
test_create_repo tweaking_just_a_tag &&
13771398
(

0 commit comments

Comments
 (0)