0

The cmd.split() does not work:

cmd = f'git log --all --grep="fixed bug" --pretty="%H|%an"'
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Generate this error:

enter image description here

With this code instead it works.

cmd = f'git log --reverse --pretty=format:"%H|%an|%at"'

It is not a problem of "bugs" because leaving only "fixed" does not generate anything. In addition, these 2 words are present in commits. I'm afraid it's a "--grepp" problem.

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
Marriv99
  • 3
  • 2
  • 1
    Of course it doesn't; it simply splits on whitespace. Use `shlex.split()` (and don't use `Popen` for cases that `run` et al. can handle, like the `subprocess` documentation tells you). – tripleee Feb 08 '23 at 19:53
  • 1
    Duplicate of https://stackoverflow.com/questions/27533176/how-to-split-up-the-command-here-for-using-subprocess-popen – tripleee Feb 08 '23 at 19:55
  • What should I use? Can you write me the full line? – Marriv99 Feb 08 '23 at 19:57
  • `result = subprocess.run(shlex.split('git log --all --grep="fixed bug" --pretty="%H|%an"'), check=True, capture_output=True, text=True)`; perhaps see also https://stackoverflow.com/questions/4256107/running-bash-commands-in-python/51950538#51950538 – tripleee Feb 08 '23 at 19:59
  • 3
    Also, please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) – tripleee Feb 08 '23 at 20:00
  • Before i used process.communicate(), now what should i use? – Marriv99 Feb 08 '23 at 20:09
  • 1
    `result.stdout` contains standard output; `result.stderr` the standard error. In case you hadn't found the documentation, it's at https://docs.python.org/3/library/asyncio-subprocess.html – tripleee Feb 08 '23 at 20:13

1 Answers1

1

Split, splits using spaces as separators.

In [2]: cmd = f'git log --all --grep="fixed bug" --pretty="%H|%an"'

In [3]: cmd.split()
Out[3]: ['git', 'log', '--all', '--grep="fixed', 'bug"', '--pretty="%H|%an"']

So the command that gets executed is something like:

git log --all --grep=fixed bug --pretty="%H|%an"

which is not valid since 'fixed bug' are taken as 2 different arguments

The preferred solution seems to be using shlex.split function instead that takes this into account.

In [5]: shlex.split(cmd)
Out[5]: ['git', 'log', '--all', '--grep=fixed bug', '--pretty=%H|%an']
Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
  • Shouldn't the two methods do the same thing? It formats the text differently and and now i can't go on. Isn't there a way to do "--pretty" differently? What I have to do is find some commits with keyboard. – Marriv99 Feb 08 '23 at 21:33