-2

How can I run below git command with python?

git_cmd = f'git log --format="" --name-only --since=2021-08-26 | sort -u'
try:
    stdout, stderr = subprocess.Popen(git_cmd, stdout=subprocess.PIPE, shell=True).communicate()
    print(stdout)
except subprocess.CalledProcessError as e:
    print("Exception on process, rc", e.returncode, "stdout=", e.output)

This is throwing error as :

-uThe system cannot find the file specified

And Then I tried:

git_cmd = f'git log --format="" --name-only --since=2021-08-26 | sort -u'
try:
    stdout = subprocess.check_output(git_cmd, stderr=subprocess.STDOUT).decode('utf-8')
    print(stdout)
except subprocess.CalledProcessError as e:
    print("Exception on process, rc", e.returncode, "stdout=", e.output)

This throws below error:

Exception on process, rc=128 stdout=b"fatal: ambiguous argument '|': unknown revision or path not in the working tree.
\nUse '--' to seperate paths from revisions, like this:\n'git <command> [<revision>...] -- [<file>...]'\n"

I cloned repo in my local and created python file inside repo.

Edgar
  • 39
  • 5
  • @JohnKugelman that was typo. I am surprised it worked for you – Edgar Aug 27 '21 at 02:46
  • Why was there a typo; did you not copy and paste the code? I ask because the code looks like it should work, and it does work when I run it, so I suspect there's something going on that we can't see. Like the code on your machine has some typo in it that's not present in this post. – John Kugelman Aug 27 '21 at 02:48
  • Can you copy and paste the precise error message you're getting, the full thing? I can't figure out what would print a message like that. It'd be more understandable if you got something like `bash: -u: No such file or directory" with the name of a command and more punctuation. – John Kugelman Aug 27 '21 at 02:50
  • @JohnKugelman Cant open stackoverflow in corporate pc, had to login from personal pc for this platform and had to type. But I doubled checked, I do not have typo in my code. – Edgar Aug 27 '21 at 02:51
  • 2
    What OS are you on? "The system cannot find the file specified" sounds like something a Windows program would print, but `git ... | sort -u` is UNIX-y syntax. Is this on WSL? – John Kugelman Aug 27 '21 at 02:52
  • Tried both on bash and windows – Edgar Aug 27 '21 at 02:54
  • @JohnKugelman Thanks for your query, made me double check my config. Found some discrepancies on my path. Its working now – Edgar Aug 27 '21 at 03:21
  • I put in the "not reproducable" close vote since it does seem to be a typo-ish thing. I did want to ask though: why is that an f-string, when there's nothing to format inside it? – torek Aug 27 '21 at 08:15

1 Answers1

1

You can create two subprocesses, piping the output of one to another, but it's probably better to simply run the first command and sort it in Python

Additionally, to avoid using a shell (which is largely-considered poor practice: Actual meaning of 'shell=True' in subprocess), split up your input before passing to subprocess.Popen()

You can do this manually (as I have below) or with shlex.split()

import subprocess

# get each part of the command separately
git_cmd = ('git', 'log', '--format=', '--name-only', '--since=2021-08-26')

p = subprocess.Popen(
    git_cmd,
    stdout=subprocess.PIPE, stderr=subprocess.PIPE,
)
out, err = p.communicate()
if p.returncode != 0 or err:
    raise Exception(f"running git failed: {err.decode()[:1000]}")

# assuming you're using UTF-8 and want a lexical sort
# if you have a great amount of output, some generator may be better
result = list(sorted(set(out.decode("utf-8").splitlines())))
ti7
  • 16,375
  • 6
  • 40
  • 68