Error trying to save git repository log to txt (Python) file

Asked

Viewed 74 times

0

Hello, I’m trying to write a code in Python to save a git log from a git repository to a file . txt in a different directory using the function subprocess.Popen(). I know the command line syntax is:

git -C [diretorioGit] log --first-parent --pretty="%H" > [diretorio/arquivoDestino]

but when trying to do using the Python function:

subprocess.Popen(["git", "-C", "github_repos/ChainLink-Token", "log", "--first-parent", "--pretty="+ "%H", '>' ,"../../github_commits/ChainLink-Token_commits.txt"], stdout=subprocess.PIPE).communicate()

the following error appears:

fatal: ambiguous argument '>': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

someone would know the error?

Note: why the command line is working

Thank you in advance

  • Tries '--pretty="%H"' .

  • Thanks for trying to help my friend, but it didn’t work

  • If the command works interactively, surely the problem revolves around those quotation marks of %H, because the way it is in the question is clearly wrong (the quotation marks will not be part of the command invoked by Popen()).

1 answer

0


One option, is for you to run git commands on Popen and with the return of the method, write to the file:

import subprocess

proc = subprocess.Popen(["git", "-C", ".", "log", "--first-parent", '--pretty="%H"'], stdout=subprocess.PIPE).communicate()

with open("ChainLink-Token_commits.txt", "w") as log:
    [log.write(data.decode("utf-8")) for data in proc if data]

This even gives the possibility to change the way the data is recorded in the text file... Of course, if necessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.