2

There is a git command that I am using

git log --format=%H 3c2232a5583711aa5f37d0f21014934f67913202

Here the long string at the end is the commit id. This command gives the list of previously commit ids of the branch. The output is like,

3c2232a5583711aa5f37d0f21014934f67913202
9i45e2a5583711aa5f37d0f21014934f679132de

I am trying to issue the same command in python and trying to store the output in a string as the following,

import subprocess

result = subprocess.run(
    [
        "cd",
        "/Users/XYZ/Desktop/gitrepo",
        "git",
        "log",
        "3c2232a5583711aa5f37d0f21014934f67913202",
    ],
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
)
print(result.stdout.decode("utf-8"), type(result.stdout.decode("utf-8")))

But the output of the print is empty! I tried subprocess.run with ["-ls", "-l"] and it worked well. The git command works on command line but I am not able to capture it in a string. When I print the result alone,

CompletedProcess(args=['cd', '/Users/XYZ/Desktop/gitrepo', 'git', 'log', '3c2232a5583711aa5f37d0f21014934f67913202'], returncode=0, stdout=b'')

How can I save the git command's output in a string? I am issuing two commands in one line. Should I issue the commands separately? If I should then, how can I (a) navigate to git folder and (b) issue git command there?

6
  • you run two command in one line - cd and git - so it can execute only cd and use git as argument for cd. And cd` doesn't display any text. Commented Jul 4, 2019 at 19:29
  • @furas I see. How to run these two commands? : (1) navigate to git folder and (2) execute the git command? Commented Jul 4, 2019 at 19:31
  • You could create bash script with cd and git and run it in subprocess. You can also try to use && to execute two commands - similar to cd / && ls Commented Jul 4, 2019 at 19:33
  • 3
    You could use the C option in git and skip changing directory. git -C /Users/XYZ/Desktop/gitrepo log 3c2232a5583711aa5f37d0f21014934f67913202 Commented Jul 4, 2019 at 19:35
  • instead of && you can also try ; - similat to cd / ; ls Commented Jul 4, 2019 at 19:36

1 Answer 1

5

Your code runs cd "/Users/XYZ/Desktop/gitrepo" "git" "log" "3c2232a5583711aa5f37d0f21014934f67913202" which is probably not what you intended.

The best way is not to interpret changing the working directory as a separate command but as part of the setup of the environment to run the git command. The subprocess module has the keyword argument cwd for that.

If cwd is not None, the function changes the working directory to cwd before executing the child. In particular, the function looks for executable (or for the first item in args) relative to cwd if the executable path is a relative path.

This is only documented for the Popen constructor but the subprocess.run documentation has this paragraph:

The arguments shown above are merely the most common ones, described below in Frequently Used Arguments (hence the use of keyword-only notation in the abbreviated signature). The full function signature is largely the same as that of the Popen constructor - apart from timeout, input and check, all the arguments to this function are passed through to that interface.

So you can rewrite your code like this:

import subprocess

result = subprocess.run(
    [
        "git",
        "log",
        "3c2232a5583711aa5f37d0f21014934f67913202",
    ],
    cwd="/Users/XYZ/Desktop/gitrepo"
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
)
print(result.stdout.decode("utf-8"), type(result.stdout.decode("utf-8")))
Sign up to request clarification or add additional context in comments.

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.