Search code examples
pythonpython-3.xsubprocesspopen

How to build the arguments list for Popen when one of the arguments has the format "-c name=value"?


I'm trying to run a git clone from Python but I need to disable the credentials popup you get on Windows, but only for this clone.

This is what I tried:

p = Popen(
    [
        "C:\\git.exe",
        "-c",
        "credential.helper=\"\"",
        "clone",
        "https://abc:abc@url.git",
        "C:\\path"
    ],
    stderr=PIPE,
    stdout=PIPE,
)

p.wait()
c = p.communicate()
print(c[0].decode("utf-8"))
print(c[1].decode("utf-8"))

But running this code still shows the credentials popup. Once I manually close the window I get the following output:

Cloning into 'C:\path'... git: 'credential-' is not a git command. See 'git --help'.

The most similar command is credential

Running the command directly on the terminal works without issue and joining the arguments string into a single string and using that as the parameter to Popen also works, which tells me issue is probably related to how Popen parses arguments with an equal sign in them..?

How can I build this arguments list?


Solution

  • Your shell is likely removing the quotes in credential.helper=""; try this instead:

    p = Popen(
        [
            "C:\\git.exe",
            "-c",
            "credential.helper=",  # note no extra quotes here
            "clone",
            "https://abc:abc@url.git",
            "C:\\path"
        ],
        stderr=PIPE,
        stdout=PIPE,
    )
    

    That way you set credential.helper to nothing, rather than to the empty string, which presumably git treats differently (I assume if you have credential.helper=foo, git tries to run git credential-foo, so with the empty string specified, it tries to run git credential-, which won't work).