Skip to content

Commit 4f84a74

Browse files
committed
filter-repo: use more expensive prunability checks when needed
When users are inserting new objects into the stream, we cannot make as many assumptions and need to do more careful checks for whether commits become empty or not. Signed-off-by: Elijah Newren <[email protected]>
1 parent b1fae48 commit 4f84a74

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

git-filter-repo

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,12 @@ class _IDs(object):
372372
# A map of new-ids to every old-id that points to the new-id (1:N map)
373373
self._reverse_translation = {}
374374

375+
def has_renames(self):
376+
"""
377+
Return whether there have been ids remapped to new values
378+
"""
379+
return bool(self._translation)
380+
375381
def new(self):
376382
"""
377383
Should be called whenever a new blob or commit object is created. The
@@ -3121,12 +3127,16 @@ class RepoFilter(object):
31213127
if not self._import_pipes:
31223128
return False
31233129

3124-
# non-merge commits can only be empty if blob/file-change editing caused
3125-
# all file changes in the commit to have the same file contents as
3126-
# the parent.
3127-
changed_files = set(change.filename for change in commit.file_changes)
3128-
if len(orig_parents) < 2 and changed_files - self._files_tweaked:
3129-
return False
3130+
# If there have not been renames/remappings of IDs (due to insertion of
3131+
# new blobs), then we can sometimes know things aren't prunable with a
3132+
# simple check
3133+
if not _IDS.has_renames():
3134+
# non-merge commits can only be empty if blob/file-change editing caused
3135+
# all file changes in the commit to have the same file contents as
3136+
# the parent.
3137+
changed_files = set(change.filename for change in commit.file_changes)
3138+
if len(orig_parents) < 2 and changed_files - self._files_tweaked:
3139+
return False
31303140

31313141
# Finally, the hard case: due to either blob rewriting, or due to pruning
31323142
# of empty commits wiping out the first parent history back to the merge

t/t9391-filter-repo-lib-usage.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ test_description='Usage of git-filter-repo as a library'
77
export PYTHONPATH=$(dirname $TEST_DIRECTORY):$PYTHONPATH
88
# Avoid writing git_filter_repo.pyc file
99
export PYTHONDONTWRITEBYTECODE=1
10+
export CONTRIB_DIR=$TEST_DIRECTORY/../contrib/filter-repo-demos
1011

1112
setup()
1213
{
@@ -163,4 +164,27 @@ test_expect_success 'other error cases' '
163164
)
164165
'
165166

167+
test_expect_success 'lint-history' '
168+
test_create_repo lint-history &&
169+
(
170+
cd lint-history &&
171+
echo initial >content &&
172+
git add content &&
173+
git commit -m "initial" &&
174+
175+
printf "CRLF is stupid\r\n" >content &&
176+
git add content &&
177+
git commit -m "make a statement" &&
178+
179+
printf "CRLF is stupid\n" >content &&
180+
git add content &&
181+
git commit -m "oops, that was embarassing" &&
182+
183+
$CONTRIB_DIR/lint-history --filenames-important dos2unix &&
184+
echo 2 >expect &&
185+
git rev-list --count HEAD >actual &&
186+
test_cmp expect actual
187+
)
188+
'
189+
166190
test_done

0 commit comments

Comments
 (0)