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

后端 未结 4 2044
渐次进展
渐次进展 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:30

    Using a wrapper script:

    #!/bin/bash
    
    readarray -t PASSWORDS < list_file
    
    REPLACEMENT='xXxXxXxXxXx'
    SEP=$'\xFF'
    
    EXPR=${PASSWORDS[0]}
    for (( I = 1; I < ${#PASSWORDS[@]}; ++I )); do
        EXPR+="|${PASSWORDS[I]}"
    done
    EXPR="s${SEP}(${EXPR})${SEP}$REPLACEMENT${SEP}g"
    EXPR=${EXPR//'\'/'\\\\'}; EXPR=${EXPR//'$'/'\\\$'}
    EXPR=${EXPR//'"'/'\"'};   EXPR=${EXPR//'`','\`'}
    EXPR=${EXPR//'^','\\^'};  EXPR=${EXPR//'[','\\['}
    EXPR=${EXPR//']','\\]'};  EXPR=${EXPR//'+','\\+'}
    EXPR=${EXPR//'?','\\?'};  EXPR=${EXPR//'.','\\.'}
    EXPR=${EXPR//'*','\\*'};  EXPR=${EXPR//'{','\\{'}
    EXPR=${EXPR//'}','\\}'};  EXPR=${EXPR//'(','\\('}
    EXPR=${EXPR//')','\\)'}
    
    FILTER="git ls-files -z '*.php' | xargs -0 perl -p -i -e \"$EXPR\""
    
    echo "Number of passwords: ${#PASSWORDS[@]}"    
    echo "Passwords:" "${PASSWORDS[@]}"
    echo "EXPR: $EXPR"
    echo "FILTER: $FILTER"
    
    git filter-branch --tree-filter "$FILTER" -- --all
    

提交回复
热议问题