I’m trying to write a script that will help me map some old users to new users in a handful of Git repositories. The problem I’m having is with the subprocess module. Simple commands like “git status” seem to work fine, but the more complex “git filter-branch” command is failing on me.
filter_history function
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)
Some example output:
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
Some things I’ve noticed are that the git command is getting a hyphen appended to it, which doesn’t make a ton of sense to me. Also, if I remove the extra hypen from the command that is printed and execute it in repoPath, everything works fine. Any help or direction on this would be greatly appreciated.
This should work: (I am using linux)
Note that “shell=True” in subprocess.Popen function