Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos 将变量传递给git筛选器分支中的命令(通过删除旧二进制文件来减小repo大小时)_Macos_Git_Shell_Github - Fatal编程技术网

Macos 将变量传递给git筛选器分支中的命令(通过删除旧二进制文件来减小repo大小时)

Macos 将变量传递给git筛选器分支中的命令(通过删除旧二进制文件来减小repo大小时),macos,git,shell,github,Macos,Git,Shell,Github,我正在研究通过删除二进制文件来减少回购规模的方法。我写的脚本,将为我做到这一点,基于 看起来我的解决方案正在运行,但不知何故,我无法将参数传递到嵌套的git命令中。 带有硬编码文件的脚本可以工作: git filter-branch --index-filter 'git rm --cached --ignore-unmatch /*selenium-server-standalone-2.16.0.jar' --prune-empty -- --all git push origin mas

我正在研究通过删除二进制文件来减少回购规模的方法。我写的脚本,将为我做到这一点,基于

看起来我的解决方案正在运行,但不知何故,我无法将参数传递到嵌套的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)

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

以下各项应有效(未经测试):


老问题和@jbraeuer在您的问题中涉及到了一个
--索引过滤器
,但我将添加一个详细信息,这会让我感到困惑,可能会对未来的读者有所帮助

TLDR
--提交筛选器
是特殊的。传递到其命令的变量必须导出

对于大多数过滤器,例如,
[--index filter]
,始终使用eval命令在shell上下文中计算
参数

但是,对于
--commit filter
,该命令将在子流程中计算。因此,如果需要将变量传递到commit filter命令中,则必须在调用
git filter branch
之前导出变量

示例:在过滤器分支操作期间,使用提交过滤器生成新旧sha ID的日志文件,该操作会破坏回购的完整历史记录:

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
export\u SHA\u日志=
_src=
_myfilter='echo“${GIT\u COMMIT}、\c”>${u SHA\u LOG};git_commit_non_empty_tree“$@”tee-a${u SHA_LOG}
git滤波器支路\
--树过滤器“cp${u src}/.gittributes.&&cp-r${u src}/.githooks.&&git add--chmod=+x.githooks/*git add.-.renormalized”\
--标签名称过滤器cat\
--提交筛选器“${u myfilter}”---全部
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