From 6b06d7d7e1fdd84964eb9ada0064bc6744467673 Mon Sep 17 00:00:00 2001 From: Scott Little Date: Mon, 8 Jul 2019 12:06:16 -0400 Subject: [PATCH] filter_git_history.sh passes broken parent-filter to git filter-branch Problem: filter_git_history.sh, when passed a list if directories to filter, sometimes passes a bad arguement to git filter-branch. e.g. filter_git_history.sh results in ... git filter-branch ... --parent-filter '' ... resulting in logs .... Rewrite 1a32385367efab7a2bccf63981993b322e0d4b4c (1/866)/usr/lib/git-core/git-filter-branch: line 340: [: missing `]' /usr/lib/git-core/git-filter-branch: line 341: -o: command not found Rewrite 3cd12006bb6e7c9157f6b0f82a10d0c8cd63e334 (2/866)/usr/lib/git-core/git-filter-branch: line 340: [: missing `]' /usr/lib/git-core/git-filter-branch: line 341: -o: command not found The issue appears to be carriage returns in variable 'set_roots'. Specifically the expresion within the '[' ']' pair seems to be incompatible with carriage returns. If I run with bash debugging turned on, I see the for loop at line 45 iterated twice, producing the following output. + set_roots=' if [ 1 -eq 0 -o "$GIT_COMMIT" = '\''1232ac58be82479ddf1af7f7eb335103cb883356'\'' -o "$GIT_COMMIT" = '\''ffe029ef99c501a0076ad6106a364bbb9c0d80ea'\'' ]; then echo '\'''\''; else cat; fi' Note that there is a carriage return prior to the second -o clause. My theory is that within git-filter-branch, bash is being invoked twice. First call: if [ 1 -eq 0 -o "$GIT_COMMIT" = '\''1232ac58be82479ddf1af7f7eb335103cb883356'\'' resulting in: git-filter-branch: line 340: [: missing `]' Second call: -o "$GIT_COMMIT" = '\''ffe029ef99c501a0076ad6106a364bbb9c0d80ea'\'' ]; then echo '\'''\''; else cat; fi resulting in: git-filter-branch: line 341: -o: command not found Other details: bash 4.3-7ubuntu1.7 git 1:1.9.1-1ubuntu0.10 Solution Use 'echo -n' rather than 'echo' when constructing 'set_roots'. Which results in ... + set_roots=' if [ 1 -eq 0 -o "$GIT_COMMIT" = '\''1232ac58be82479ddf1af7f7eb335103cb883356'\'' -o "$GIT_COMMIT" = '\''ffe029ef99c501a0076ad6106a364bbb9c0d80ea'\'' ]; then echo '\'''\''; else cat; fi' The entire '[' ']' expression is on the same line, and the git-filter-branch go away. Change-Id: I3a21fd0841f093495cf4727a9f5633a6f80c1690 Signed-off-by: Scott Little --- filter_git_history.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/filter_git_history.sh b/filter_git_history.sh index 28407a6..710ae6b 100755 --- a/filter_git_history.sh +++ b/filter_git_history.sh @@ -42,7 +42,7 @@ done # Purge all parents for those commits set_roots=" -if [ 1 -eq 0 $(for root in $roots; do echo " -o \"\$GIT_COMMIT\" = '$root' "; done) ]; then +if [ 1 -eq 0 $(for root in $roots; do echo -n " -o \"\$GIT_COMMIT\" = '$root' "; done) ]; then echo ''; else cat;