【问题标题】:Passing variable to command in git filter-branch (when reducing repo size by removing old binaries)将变量传递给 git filter-branch 中的命令(通过删除旧二进制文件来减少存储库大小时)
【发布时间】:2012-02-15 03:16:29
【问题描述】:

我正在研究通过删除二进制文件来减少 repo 大小的方法。我正在编写基于http://help.github.com/remove-sensitive-data/ 的脚本

似乎我有解决方案,但不知何故我无法将参数传递给嵌套的 git 命令。 带有硬编码文件的脚本有效:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch /*selenium-server-standalone-2.16.0.jar' --prune-empty -- --all 
git push origin master --force 
rm -rf .git/refs/original/ 
git reflog expire --expire=now --all 
git gc --prune=now 
git gc --aggressive --prune=now 
git status
git pull

这给了我输出

Rewrite f9a33e41dd6da4630773272ec18d194d14935a83 (10/10)rm 'bin/selenium-server-standalone-2.16.0.jar'
rm 'selenium-server-standalone-2.16.0.jar'

Ref 'refs/heads/master' was rewritten
Ref 'refs/remotes/origin/master' was rewritten
WARNING: Ref 'refs/remotes/origin/master' is unchanged

但是当我尝试对其进行参数化时,如下所示,整个过程不起作用

fileOLD=selenium-server-standalone-2.16.0.jar
git filter-branch --index-filter 'git rm --cached --ignore-unmatch /*${fileOLD}' --prune-empty -- --all 
git push origin master --force 
rm -rf .git/refs/original/ 
git reflog expire --expire=now --all 
git gc --prune=now 
git gc --aggressive --prune=now 
git status
git pull

输出

Rewrite 0491bf3461726c2ebd595ebc480010b5bf722302 (1/10)fatal: '/About Administration Tools.app' is outside repository
index filter failed: git rm --cached --ignore-unmatch /*${fileOLD} 

它不会删除文件。

问题是如何在将变量传递到嵌套的 git 命令时正确传递/转义变量? (MAC OS x 10.7.2)

【问题讨论】:

    标签: macos git shell github


    【解决方案1】:

    老问题和@jbraeuer 是您涉及--index-filter 的问题,但我将添加一个吸引我的细节,并可能对未来的读者有所帮助。

    TLDR; --commit-filter 很特别。传递给其命令的变量必须导出

    对于大多数过滤器,例如 [--index-filter <command>]<command> 参数始终在 shell 上下文中使用 eval 命令进行评估。

    但是,对于--commit-filter,命令在子进程中进行评估。因此,如果您需要将变量传递到提交过滤器命令中,则在调用 git filter-branch 之前必须 export

    在过滤器分支操作期间使用提交过滤器生成新旧 sha id 的日志文件的示例,该操作与您的 repo 的完整历史记录相混淆:

    export _SHA_LOG=<location of log file to create>
    _src=<location of .gitattributes and commit hooks to place in your repo history>
    _myfilter='echo "${GIT_COMMIT},\c" >> ${_SHA_LOG}; git_commit_non_empty_tree "$@" | tee -a ${_SHA_LOG}'
    
    git filter-branch \
    --tree-filter "cp ${_src}/.gitattributes . && cp -r ${_src}/.githooks . && git add --chmod=+x .githooks/* git add . --renormalized" \
    --tag-name-filter cat \
    --commit-filter "${_myfilter}" -- --all
    

    【讨论】:

      【解决方案2】:

      问题是您在 git 过滤器中使用的单引号 (')。 Bash 不会替换单引号内的变量。

      以下应该可以工作(未经测试):

      fileOLD=selenium-server-standalone-2.16.0.jar
      git filter-branch --index-filter "git rm --cached --ignore-unmatch /*${fileOLD}" --prune-empty -- --all
      

      【讨论】:

        猜你喜欢
        • 2011-01-08
        • 2012-08-16
        • 2019-11-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-02
        相关资源
        最近更新 更多