Skip to content

Commit b6a35f8

Browse files
committed
filter-repo: implement --strip-blobs-with-ids
Add a flag allowing for specifying a file filled with blob-ids which will be stripped from the repository. Signed-off-by: Elijah Newren <[email protected]>
1 parent d958b03 commit b6a35f8

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

git-filter-repo

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,8 @@ EXAMPLES
16391639
dest='max_blob_size', default=0,
16401640
help=_("Strip blobs (files) bigger than specified size (e.g. '5M', "
16411641
"'2G', etc)"))
1642+
contents.add_argument('--strip-blobs-with-ids', metavar='BLOB-ID-FILENAME',
1643+
help=_("Strip blob with the specified git object ids (hashes)"))
16421644

16431645
refrename = parser.add_argument_group(title=_("Renaming of refs "
16441646
"(see also --refname-callback)"))
@@ -1926,6 +1928,11 @@ EXAMPLES
19261928
args.mailmap = MailmapInfo(args.mailmap)
19271929
if args.replace_text:
19281930
args.replace_text = FilteringOptions.get_replace_text(args.replace_text)
1931+
if args.strip_blobs_with_ids:
1932+
with open(args.strip_blobs_with_ids, 'br') as f:
1933+
args.strip_blobs_with_ids = set(f.read().split())
1934+
else:
1935+
args.strip_blobs_with_ids = set()
19291936
return args
19301937

19311938
class RepoAnalyze(object):
@@ -2919,6 +2926,9 @@ class RepoFilter(object):
29192926
if self._args.max_blob_size and len(blob.data) > self._args.max_blob_size:
29202927
blob.skip()
29212928

2929+
if blob.original_id in self._args.strip_blobs_with_ids:
2930+
blob.skip()
2931+
29222932
if self._args.replace_text:
29232933
for literal, replacement in self._args.replace_text['literals']:
29242934
blob.data = blob.data.replace(literal, replacement)
@@ -3051,6 +3061,10 @@ class RepoFilter(object):
30513061
if self._args.max_blob_size and \
30523062
self._unpacked_size.get(change.blob_id, 0) > self._args.max_blob_size:
30533063
continue
3064+
if self._args.strip_blobs_with_ids and \
3065+
change.blob_id in self._args.strip_blobs_with_ids:
3066+
continue
3067+
# Otherwise, record the change
30543068
new_file_changes[change.filename] = change
30553069
commit.file_changes = [v for k,v in sorted(new_file_changes.items())]
30563070

t/t9390-filter-repo.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,51 @@ test_expect_success '--strip-blobs-bigger-than' '
710710
)
711711
'
712712

713+
test_expect_success '--strip-blobs-with-ids' '
714+
(
715+
git clone file://"$(pwd)"/analyze_me strip_blobs_with_ids &&
716+
cd strip_blobs_with_ids &&
717+
718+
# Verify certain files are present initially
719+
git log --format=%n --name-only | sort | uniq >../filenames &&
720+
test_line_count = 11 ../filenames &&
721+
grep fake_submodule ../filenames &&
722+
723+
# Strip "a certain file" files
724+
git filter-repo --strip-blobs-with-ids <(echo deadbeefdeadbeefdeadbeefdeadbeefdeadbeef) &&
725+
726+
git log --format=%n --name-only | sort | uniq >../filenames &&
727+
test_line_count = 10 ../filenames &&
728+
# Make sure fake_submodule was removed
729+
! grep fake_submodule ../filenames &&
730+
731+
# Do it again, this time with --replace-text since that means
732+
# we are operating without --no-data and have to go through
733+
# a different codepath. (The search/replace terms are bogus)
734+
cat >../bad-ids <<-\EOF &&
735+
34b6a0c9d02cb6ef7f409f248c0c1224ce9dd373
736+
51b95456de9274c9a95f756742808dfd480b9b35
737+
EOF
738+
cat >../replace-rules <<-\EOF &&
739+
not found==>was found
740+
EOF
741+
git filter-repo --strip-blobs-with-ids ../bad-ids --replace-text ../replace-rules &&
742+
743+
git log --format=%n --name-only | sort | uniq >../filenames &&
744+
test_line_count = 5 ../filenames &&
745+
! grep sequence/to ../filenames &&
746+
! grep words/to ../filenames &&
747+
! grep capricious ../filenames &&
748+
! grep fickle ../filenames &&
749+
! grep mercurial ../filenames
750+
751+
# Remove the temporary auxiliary files
752+
rm ../bad-ids &&
753+
rm ../replace-rules &&
754+
rm ../filenames
755+
)
756+
'
757+
713758
test_expect_success 'setup commit message rewriting' '
714759
test_create_repo commit_msg &&
715760
(

0 commit comments

Comments
 (0)