Skip to content

Commit a8ed692

Browse files
committed
Merge branch 'rnd/fix-binary-blob-detection'
Signed-off-by: Elijah Newren <[email protected]>
2 parents d8e858a + 9932167 commit a8ed692

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

git-filter-repo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3261,8 +3261,10 @@ class RepoFilter(object):
32613261
if blob.original_id in self._args.strip_blobs_with_ids:
32623262
blob.skip()
32633263

3264-
if self._args.replace_text and \
3265-
not any(x == b"0" for x in blob.data[0:8192]):
3264+
if ( self._args.replace_text
3265+
# not (if blob contains zero byte in the first 8Kb, that is, if blob is binary data)
3266+
and not b"\0" in blob.data[0:8192]
3267+
):
32663268
for literal, replacement in self._args.replace_text['literals']:
32673269
blob.data = blob.data.replace(literal, replacement)
32683270
for regex, replacement in self._args.replace_text['regexes']:

t/t9390-filter-repo.sh

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,87 @@ test_expect_success '--replace-text all options' '
850850
)
851851
'
852852

853+
test_expect_success '--replace-text binary zero_byte-0_char' '
854+
(
855+
set -e
856+
set -u
857+
REPO=replace-text-detect-binary
858+
FILE=mangle.bin
859+
OLD_STR=replace-from
860+
NEW_STR=replace-with
861+
# used with printf, contains a zero byte and a "0" character, binary
862+
OLD_CONTENT_FORMAT="${OLD_STR}\\0${OLD_STR}\\n0\\n"
863+
# expect content unchanged due to binary
864+
NEW_CONTENT_FORMAT="${OLD_CONTENT_FORMAT}"
865+
866+
rm -rf "${REPO}"
867+
git init "${REPO}"
868+
cd "${REPO}"
869+
echo "${OLD_STR}==>${NEW_STR}" >../replace-rules
870+
printf "${NEW_CONTENT_FORMAT}" > ../expect
871+
printf "${OLD_CONTENT_FORMAT}" > "${FILE}"
872+
git add "${FILE}"
873+
git commit -m 'test'
874+
git filter-repo --force --replace-text ../replace-rules
875+
876+
test_cmp ../expect "${FILE}"
877+
)
878+
'
879+
880+
test_expect_success '--replace-text binary zero_byte-no_0_char' '
881+
(
882+
set -e
883+
set -u
884+
REPO=replace-text-detect-binary
885+
FILE=mangle.bin
886+
OLD_STR=replace-from
887+
NEW_STR=replace-with
888+
# used with printf, contains a zero byte but no "0" character, binary
889+
OLD_CONTENT_FORMAT="${OLD_STR}\\0${OLD_STR}\\n"
890+
# expect content unchanged due to binary
891+
NEW_CONTENT_FORMAT="${OLD_CONTENT_FORMAT}"
892+
893+
rm -rf "${REPO}"
894+
git init "${REPO}"
895+
cd "${REPO}"
896+
echo "${OLD_STR}==>${NEW_STR}" >../replace-rules
897+
printf "${NEW_CONTENT_FORMAT}" > ../expect
898+
printf "${OLD_CONTENT_FORMAT}" > "${FILE}"
899+
git add "${FILE}"
900+
git commit -m 'test'
901+
git filter-repo --force --replace-text ../replace-rules
902+
903+
test_cmp ../expect "${FILE}"
904+
)
905+
'
906+
907+
test_expect_success '--replace-text text-file no_zero_byte-zero_char' '
908+
(
909+
set -e
910+
set -u
911+
REPO=replace-text-detect-binary
912+
FILE=mangle.bin
913+
OLD_STR=replace-from
914+
NEW_STR=replace-with
915+
# used with printf, contains no zero byte but contains a "0" character, text
916+
OLD_CONTENT_FORMAT="${OLD_STR}0\\n0${OLD_STR}\\n0\\n"
917+
# expect content changed due to text
918+
NEW_CONTENT_FORMAT="${NEW_STR}0\\n0${NEW_STR}\\n0\\n"
919+
920+
rm -rf "${REPO}"
921+
git init "${REPO}"
922+
cd "${REPO}"
923+
echo "${OLD_STR}==>${NEW_STR}" >../replace-rules
924+
printf "${NEW_CONTENT_FORMAT}" > ../expect
925+
printf "${OLD_CONTENT_FORMAT}" > "${FILE}"
926+
git add "${FILE}"
927+
git commit -m 'test'
928+
git filter-repo --force --replace-text ../replace-rules
929+
930+
test_cmp ../expect "${FILE}"
931+
)
932+
'
933+
853934
test_expect_success '--strip-blobs-bigger-than' '
854935
setup_analyze_me &&
855936
(

0 commit comments

Comments
 (0)