我正在尝试编写一个脚本,帮助我将一些旧用户映射到少数Git存储库中的新用户。我遇到的问题是子进程模块。像“git status”这样的简单命令似乎运行正常,但更复杂的“git filter-branch”命令对我失败了。
filter_history函数
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中执行它,一切正常。任何帮助或指导都将非常感激。
答案 0 :(得分:2)
这应该有效:(我正在使用linux)
def filter_history(old, new, name, repoPath):
command = """'
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(['git filter-branch --env-filter', command],cwd=os.path.dirname(repoPath), shell=True)
请注意subprocess.Popen函数中的“shell = True”
答案 1 :(得分:1)
作为任何人的一个FYI,@ 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)