git-filter-branch to remove strings, but where strings contain $ ' \ and other characters

后端 未结 4 2046
渐次进展
渐次进展 2021-01-07 09:12

I\'m trying to rewrite history, using:

git filter-branch --tree-filter \'git ls-files -z \"*.php\" |xargs -0 perl -p -i -e \"s#(PASSWORD1|PASSWORD2|PASSWORD3)#

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 09:25

    Building on the brilliant help given by konsolebox which really helped me solve this, the solution I ended up using in terms of doing it via the shell was:

    Define the strings in a file, strings.txt

    string1
    another$string
    yet! @nother string
    some more stuff to re\move
    

    Create a Perl script perl-escape-strings.pl which will be used to escape the strings, where xXxXxXxXxXx is the string they will all be replaced with

    #!/usr/bin/perl
    
    use strict;
    use warnings;
    
    while (<>)
    {
            chomp;
            my $passwd = quotemeta($_);
            print qq|s/$passwd/xXxXxXxXxXx/g;\n|;
    }
    
    exit 0;
    

    Bash script:

    # Pre-process the strings
    ./perl-escape-strings.pl strings.txt > strings-perl-escaped.txt
    
    # Change directory to the repo
    cd repo/
    
    # Define the filter command
    FILTER="git ls-files -z '*.html' '*.php' | xargs -0 perl -p -i ../strings-perl-escaped.txt"
    
    # Run the filter
    git filter-branch --tree-filter "$FILTER" -- --all
    

    However, because the number of strings is large, and my repository is large and with many thousand commits, the filter-branch method is taking a long time. So I'm going to try The BFG mentioned in another answer also in parallel, to see if it completes quicker.

提交回复
热议问题