The error message does not correspond to your code: the code in question would produce SyntaxError, not TypeError.

You don't need shell=True. To get git's output, you could use subprocess.check_output() function:

from subprocess import check_output

date_string = check_output('git log -1 --pretty=format:"%ci"'.split()).decode()
Answer from jfs on Stack Overflow
🌐
GitHub
gist.github.com › simonw › 091b765a071d1558464371042db3b959
Python function for retrieving all git commits (from "git log" in the current directory) as a useful data structure · GitHub
# Added decode to get this to work for me lines = subprocess.check_output( ['git', 'log'], stderr=subprocess.STDOUT ).decode("utf-8").split("\n")
Discussions

git - Python subprocess yielding a different result than shell? - Stack Overflow
I guess you have to specify your pipes seperately, as subprocess escapes |. ... Can you dump the output of the command without wc -l? I think the culprit is shell=True since the shell that is invoked for running your command is different from the one you use. ... Seems to still be running the same command. Prints out a thousand lines of git log... More on stackoverflow.com
🌐 stackoverflow.com
Why are Git log command outputs different when launched from Windows shell and from Python subprocess? - Stack Overflow
If I execute the command git log --no-walk --tags --reverse in the Windows shell, I get results such as: commit e83dcf42f2a52183cf21642c7b8deb42961663f3 (tag: my_tag) Author: an_author Date: a_date · while if I execute the same Git command from a Python script: subprocess.check_output(['git', ... More on stackoverflow.com
🌐 stackoverflow.com
Python subprocess - git log wrong JSON Format - Stack Overflow
I tried to format git log to json but failed miserabely. I used this command for the formatting, and I don't think this is where my problem lies, but hey you never know. These are my functions. def More on stackoverflow.com
🌐 stackoverflow.com
December 14, 2022
How to use git command 'git log -p' with GitPython
I need use GitPython to do one command, the look like git command " git log -p". I read the tutorial but I still don't know how to do it . I use the command master.log() ,but couldn&#... More on github.com
🌐 github.com
8
May 2, 2014
🌐
Gerrit
gerrit.googlesource.com › git-repo › + › master › git_command.py
git_command.py - git-repo - Git at Google
Sign in · gerrit / git-repo / master / . / git_command.py · blob: 1cb8f1aaff684d001eaeb9caae6c0b3460d3cb46 [file] [log] [blame] · Powered by Gitiles| Privacy| Termstxt json
🌐
Medium
medium.com › @wide4head › interfacing-git-from-python-abb55c548853
Interfacing with git from python. many a time we may need to get the git… | by Preveen P | Medium
August 30, 2024 - import subprocess def get_short_commit_id_with_date(): result = subprocess.run(['git', 'log', '-1', '--pretty=format:%h - �'], stdout=subprocess.PIPE) output = result.stdout.decode().strip() return output.split(' - ')[0], output.split(' - ')[1] short_commit_id, commit_date = get_short_commit_id_with_date() print(f"Short Commit ID: {short_commit_id}") print(f"Commit Date: {commit_date}")
Find elsewhere
🌐
GitHub
github.com › gitpython-developers › GitPython › issues › 157
How to use git command 'git log -p' with GitPython · Issue #157 · gitpython-developers/GitPython
May 2, 2014 - I need use GitPython to do one command, the look like git command " git log -p". I read the tutorial but I still don't know how to do it . I use the command master.log() ,but couldn&#...
Author   fengqiaoye
Top answer
1 of 1
8

You can get all the repository commits using:

import git
repo = git.Repo("/home/user/.emacs.d")
commits = list(repo.iter_commits("master", max_count=5))

Then you can identify yourself what kind of data gitpython offers:

dir(commits[0])

Some of them are:

  • author
  • committed_datetime
  • hexsha
  • message
  • stats

Take an example:

>>> commits[0].author
<git.Actor "azzamsa <[email protected]>">

>>> commits[0].hexsha
'fe4326e94eca2e651bf0081bee02172fedaf0b90'

>>> commits[0].message
'Add ocaml mode\n'

>>> commits[0].committed_datetime
datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=<git.objects.util.tzoffset object at 0x7fb4fcd01790>)

(committed_datetime outputs datetime object with locale object)

If you want to check if a commit contains a file (which is usable if you want to grab all commit from that file). You can use:

def is_exists(filename, sha):
    """Check if a file in current commit exist."""
    files = repo.git.show("--pretty=", "--name-only", sha)
    if filename in files:
        return True

Then to get all commit from a file:

def get_file_commits(filename):
    file_commits = []
    for commit in commits:
        if is_exists(filename, commit.hexsha):
            file_commits.append(commit)

    return file_commits

e.g I want to take all commits from 'init.el' file:

initel_file_commits = get_file_commits('init.el')

>>> initel_file_commits
[<git.Commit "fe4326e94eca2e651bf0081bee02172fedaf0b90">, <git.Commit
"e4f39891fb484a95ea76e8e07244b908e732e7b3">]

See that the function working correctly:

>>> initel_file_commits[0].stats.files
{'init.el': {'insertions': 1, 'deletions': 0, 'lines': 1}, 'modules/aza-ocaml.el': {'insertions': 28, 'deletions': 0, 'lines': 28}}

>>> initel_file_commits[1].stats.files
{'init.el': {'insertions': 1, 'deletions': 0, 'lines': 1}, 'modules/aza-calfw.el': {'insertions': 65, 'deletions': 0, 'lines': 65}, 'modules/aza-home.el': {'insertions': 0, 'deletions': 57, 'lines': 57}}

Hope it helps.

🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
2 days ago - Source code: Lib/subprocess.py The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace seve...
🌐
Reddit
reddit.com › r/git › how to parse the output of "git log" with python
r/git on Reddit: How to parse the output of "git log" with python
February 18, 2012 - Please do not use shell=True with Popen (or other functions from the subprocess module). It sets a bad example. ... Not exactly what you're looking for but why would anyone want to parse git log I believe it would make more sense to implement it in python using git objects and so on.
🌐
Stack Overflow
stackoverflow.com › questions › 65998015 › executing-git-bash-commands-using-python-subprocess-module
Executing Git Bash commands using Python Subprocess Module - Stack Overflow
February 1, 2021 - PythonAnywhere creates log files for you -- you'll find them in /var/log/. Can you specify what you actually need (it's not a common thing to keep log files under version control) or show relevant bits of your code?