Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Git().shortlog() not working #1434

Answered by Byron
lcnittl asked this question in Q&A
Discussion options

For some reason I cannot get output from Git().shortlog() anymore. Running on Windows 11 with Python 3.9.6 (same behavior on Ubuntu 20.04 (WSL2) with Python 3.8.10.

In [1]: import git
In [2]: git.__version__
Out[2]: '3.1.27'
In [3]: g = git.Git()
In [4]: g.status()
Out[4]: 'On branch master\nYour branch is up to date with \'origin/master\'.\n\nChanges not staged for commit:\n (use "git add <file>..." to update what will be committed)\n (use "git restore <file>..." to discard changes in working directory)\n\tmodified: src/sphinxcontrib/scm/__init__.py\n\tmodified: src/sphinxcontrib/scm/directives.py\n\tmodified: src/sphinxcontrib/scm/roles.py\n\tmodified: src/sphinxcontrib/scm/util.py\n\nUntracked files:\n (use "git add <file>..." to include in what will be committed)\n\tpublish.ps1\n\nno changes added to commit (use "git add" and/or "git commit -a")'
In [5]: g.shortlog()
Out[5]: ''

git shortlog is working in the CLI:

PS scm> git shortlog
Christian Knittl-Frank (32):
 Initial commit
 ...

Any idea where this problem could originate from? Any more info I can provide to track down the issue?

Cheers!

You must be logged in to vote

That looks a little strange indeed.

The docs about debugging git-python command invocations might be helpful to get some more information on what's happening. git.shortlog() does indeed only run git shortlog, but with redirections. Maybe those affect it in some way.

You could also try it with a plain Popen(...) call and see if the issue is similar.

Replies: 2 comments 1 reply

Comment options

That looks a little strange indeed.

The docs about debugging git-python command invocations might be helpful to get some more information on what's happening. git.shortlog() does indeed only run git shortlog, but with redirections. Maybe those affect it in some way.

You could also try it with a plain Popen(...) call and see if the issue is similar.

You must be logged in to vote
1 reply
Comment options

shortlog.py:

import logging
import os
import subprocess
import shlex
import time
os.environ["GIT_PYTHON_TRACE"] = "full"
cmd = "git shortlog" # this gives truncated output
# cmd = "git --no-pager shortlog" # this gives full output
ret_val = subprocess.Popen(shlex.split(cmd))
print("## subprocess.Popen() ##")
print(ret_val)
time.sleep(1)
ret_val = Git().shortlog()
print("## Git().shortlog() ##")
print(ret_val)
time.sleep(1)

Executing this file gives:

(venv-py39) PS C:\Users\lcnittl\repos\github.com\sphinxcontrib\scm> & c:/Users/lcnittl/repos/github.com/sphinxcontrib/scm/.venv-py39/Scripts/python.exe c:/Users/lcnittl/repos/github.com/sphinxcontrib/scm/shortlog.py
## subprocess.Popen() ##
<Popen: returncode: None args: ['git', 'shortlog']>
Christian Knittl-Frank (36):
 Initial commit
 v0.1.0
 Fix README
 Add missing metadata
 Update trove classifiers
 Update setup aliases
 Remove superfluous encoding declaration
 Redefine version handling
 Fix for Python 3.7
 Bump version to v0.1.1
 Add 'git_dir'
 Bump version to v0.1.2
 Update .pre-commit-config.yaml
 pre-commit run --all
 Update .pre-commit-config.yaml
DEBUG:git.cmd:Popen(['git', 'shortlog'], cwd=C:\Users\lcnittl\repos\github.com\sphinxcontrib\scm, universal_newlines=False, shell=None, istream=None)
INFO:git.cmd:git shortlog -> 0
## Git().shortlog() ##
(.venv-py39) PS C:\Users\lcnittl\repos\github.com\sphinxcontrib\scm> 

What I can say is that not using the --no-pager flag in this script leaves the shell in an unstable state (still in the pager, after exiting the script). Yet, adding this flag to the git.cmd.Git:execute() (inserting it between ́git ́ and ́shortlog ́ in the command list) did not solve the problem.

I will try to dig into this a little on the coming weekend, including the herein mentioned environment variables, as I seemingly cannot get this to work properly – i.e. not seeing

If set to full, the executed git command and its entire output on stdout and stderr will be shown as they happen

– at this very moment).

Cheers, lcnittl

Answer selected by Byron
Comment options

I found the same issue with python 3.10 and gitpython 3.1.32.

Using --no-pager didn't help, but there is an explanation and a simple fix.
I'm posting the solution in case anyone else has the same problem.

Fix:

Use:

g.shortlog("HEAD")

instead of

g.shortlog()

Explanation:

From git shortlog --help:

If no revisions are passed on the command line and either standard input is not a terminal or there is no current branch, git shortlog will output a summary of the log read from standard input, without reference to the current repository.

Since GitPython uses Popen(stdin=subprocess.DEVNULL), we need to specify the revision on the command line, or the log is read from the standard input (that's /dev/null).

To reproduce the wrong behaviour with subprocess:

from subprocess import Popen, DEVNULL, PIPE
command = ['git', 'shortlog']
proc = Popen(command, stdin=DEVNULL, stderr=PIPE, stdout=PIPE)
print(proc.communicate())
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #1433 on April 18, 2022 23:18.

AltStyle によって変換されたページ (->オリジナル) /