Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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/5/ruby-on-rails-4/2.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
在Python子流程模块中使用git筛选器分支_Python_Git - Fatal编程技术网

在Python子流程模块中使用git筛选器分支

在Python子流程模块中使用git筛选器分支,python,git,Python,Git,我正试图编写一个脚本,帮助我将一些老用户映射到一些Git存储库中的新用户。我遇到的问题是子流程模块。像“git status”这样的简单命令似乎工作正常,但更复杂的“git filter branch”命令在我身上失败了 过滤历史记录功能 def filter_history(old, new, name, repoPath): command = """ filter-branch --env-filter ' an="$GIT_AUTHOR_NAME" am

我正试图编写一个脚本,帮助我将一些老用户映射到一些Git存储库中的新用户。我遇到的问题是子流程模块。像“git status”这样的简单命令似乎工作正常,但更复杂的“git filter branch”命令在我身上失败了

过滤历史记录功能

def filter_history(old, new, name, repoPath):

command = """ filter-branch --env-filter '
        an="$GIT_AUTHOR_NAME"
        am="$GIT_AUTHOR_EMAIL"
        cn="$GIT_COMMITTER_NAME"
        cm="$GIT_COMMITTER_EMAIL"

        if [[ "$GIT_COMMITTER_EMAIL" == |old|* ]]
        then
            cn="|name|"
            cm="|new|"
        fi
        if [[ "$GIT_AUTHOR_EMAIL" == |old|* ]]
        then
            an="|name|"
            am="|new|"
        fi

        export GIT_AUTHOR_NAME="$an"
        export GIT_AUTHOR_EMAIL="$am"
        export GIT_COMMITTER_NAME="$cn"
        export GIT_COMMITTER_EMAIL="$cm"
    '
"""

#Do string replace
command = command.replace("|old|", old)
command = command.replace("|new|", new)
command = command.replace("|name|", name)

subprocess.Popen(['/usr/bin/git', command], cwd=os.path.dirname(repoPath), shell=False)
一些示例输出:

fatal: cannot exec 'git- filter-branch --env-filter '
        an="$GIT_AUTHOR_NAME"
        am="$GIT_AUTHOR_EMAIL"
        cn="$GIT_COMMITTER_NAME"
        cm="$GIT_COMMITTER_EMAIL"

        if [[ "$GIT_COMMITTER_EMAIL" == jacks* ]]
        then
            cn="Jack Slingerland"
            cm="jacks-teamddm"
        fi
        if [[ "$GIT_AUTHOR_EMAIL" == jacks* ]]
        then
            an="Jack Slingerland"
            am="jacks-teamddm"
        fi

        export GIT_AUTHOR_NAME="$an"
        export GIT_AUTHOR_EMAIL="$am"
        export GIT_COMMITTER_NAME="$cn"
        export GIT_COMMITTER_EMAIL="$cm"
    '
': File name too long
我注意到git命令后面加了一个连字符,这对我来说毫无意义。此外,如果我从打印的命令中删除额外的hypen并在repoPath中执行它,那么一切都会正常工作。如果您在这方面有任何帮助或指导,我们将不胜感激。

这应该可以:(我正在使用linux)


请注意,subprocess.Popen函数中的“shell=True”

作为任何人的参考,@xueyymusic是最接近的。我最终使用了以下方法:

def filter_history(old, new, name, repoPath):

command = """--env-filter '
        an="$GIT_AUTHOR_NAME"
        am="$GIT_AUTHOR_EMAIL"
        cn="$GIT_COMMITTER_NAME"
        cm="$GIT_COMMITTER_EMAIL"

        if [[ "$GIT_COMMITTER_EMAIL" == |old|* ]]
        then
            cn="|name|"
            cm="|new|"
        fi

        if [[ "$GIT_AUTHOR_EMAIL" == |old|* ]]
        then
            an="|name|"
            am="|new|"
        fi

        export GIT_AUTHOR_NAME="$an"
        export GIT_AUTHOR_EMAIL="$am"
        export GIT_COMMITTER_NAME="$cn"
        export GIT_COMMITTER_EMAIL="$cm"
'
"""

#DO string replace
command = command.replace("|old|", old)
command = command.replace("|new|", new)
command = command.replace("|name|", name)

process = subprocess.Popen(['git filter-branch', command],cwd=os.path.dirname(repoPath), shell=True)

您不应该在--env filter`脚本的每一行末尾都放一个“
`”吗?有点像你在中看到的例子。一个
对于包含在
--env filter
参数中的脚本部分,它不足以保证一个多行命令。@VonC我更改了命令,使其在每行之后都包含反勾号,但这似乎没有改变任何东西。backtick()?我考虑的是反斜杠(\:),而不是反勾号。@VonC“照我想的做,不像我打字的那样。“添加反斜杠没有帮助。相反,它只是使命令折叠到一行上,这是bash不喜欢的。@Right。。。我应该在执行其他任务时键入注释,抱歉。我没有看到我最初的评论。既然问题依然存在,你是否尝试过其他途径?是否将外部脚本中的
--env filter
参数外部化?(和/或大大简化脚本以查看错误消息是否仍然存在?)+1.
def filter_history(old, new, name, repoPath):

command = """--env-filter '
        an="$GIT_AUTHOR_NAME"
        am="$GIT_AUTHOR_EMAIL"
        cn="$GIT_COMMITTER_NAME"
        cm="$GIT_COMMITTER_EMAIL"

        if [[ "$GIT_COMMITTER_EMAIL" == |old|* ]]
        then
            cn="|name|"
            cm="|new|"
        fi

        if [[ "$GIT_AUTHOR_EMAIL" == |old|* ]]
        then
            an="|name|"
            am="|new|"
        fi

        export GIT_AUTHOR_NAME="$an"
        export GIT_AUTHOR_EMAIL="$am"
        export GIT_COMMITTER_NAME="$cn"
        export GIT_COMMITTER_EMAIL="$cm"
'
"""

#DO string replace
command = command.replace("|old|", old)
command = command.replace("|new|", new)
command = command.replace("|name|", name)

process = subprocess.Popen(['git filter-branch', command],cwd=os.path.dirname(repoPath), shell=True)