Skip to content

Commit 9932167

Browse files
author
rndbit
committed
filter-repo: add tests for --replace-text in binary blobs
The --replace-text failed to detect blobs as binary and incorrectly applied to all blobs. Prior to switch from python2 to python3 it incorrectly designated blobs containing 0 character instead of NUL byte as binary and would have been causing text replacements to apply to binary files and not apply to text files containing 0 character. Add regression tests with blobs containing; 0 character, NUL byte, and both 0 character and NUL byte. Signed-off-by: rndbit <[email protected]>
1 parent 9cfe2b4 commit 9932167

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

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)