3

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.

5
  • Shouldn't you put a '`' at the end of each line of your --env-filter` script? A bit like in the examples that you see in linux.die.net/man/1/git-filter-branch. a """ isn't enough to warrant a multiline command when it come to the script part contained in that --env-filter parameter. Commented Feb 2, 2012 at 12:31
  • @VonC I changed the command to include back ticks after each line, but that didn't seem to change anything. Commented Feb 2, 2012 at 14:01
  • backtick (en.wikipedia.org/wiki/Backtick)? I was thinking about backslash (\: en.wikipedia.org/wiki/Backslash), not backtick. Commented Feb 2, 2012 at 14:06
  • @VonC "Do as I think, not as I type." Adding backslashes didn't help. Instead it just made the command collapse onto one line, which bash wasn't fond of. Commented Feb 2, 2012 at 14:53
  • @Right... I should type comment while doing other tasks, sorry. I didn't see my initial comment. So since the issue remains, did you try other avenues? Like externalize the --env-filter parameter in an external script ? (And / or greatly simplify the script in order to see if the error message persists?) Commented Feb 2, 2012 at 16:33

2 Answers 2

2

This should work: (I am using 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)

Note that "shell=True" in subprocess.Popen function

Sign up to request clarification or add additional context in comments.

Comments

1

As an FYI for anyone that comes around, @xueyymusic was the closest. I ended up using the following:

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.