3

Using a Python script, I'd like to get the email of a person who last committed changes to a specific file /path/to/file.py.

Sounds easy, right? I just need to somehow parse the following

git log -n 1 --pretty=format:%ae -- /path/to/file.py

Package sh is my preferred choice. Unfortunately, in Python

import sh
print(str(sh.git.log('-n 1 --pretty=format:%ae -- /path/to/file.py')))
print(str(sh.git.log('-n', '1', '--pretty=format:%ae', '--', /path/to/file.py')))

both print - (press RETURN). So maybe I'm messing something up with the arguments.

Otherwise, str(sh.git.status()) correctly returns On branch master ..., and some other tested commands work as expected.

How to solve this?

4
  • Try the blame git command Commented Feb 8, 2018 at 9:47
  • @voltento Please read the question. I know the working git command but I have troubles parsing it in Python. Commented Feb 8, 2018 at 9:49
  • 1
    The title you chose is quite misleading then. This question is hardly about git at all, because as you said yourself, your git command to get the email address of the last commit is working fine. You are just having trouble doing something with that in Python. I'd recommend refining your question to shift the focus to the real problem. Commented Feb 8, 2018 at 9:51
  • 1
    @anothernode Thanks for the suggestion, I renamed the question to reflect more on the parsing problem. Commented Feb 8, 2018 at 9:58

2 Answers 2

3

The - (press RETURN) output sounds like it's something printed by a pager.

Remember, every Git command may (depending on options, arguments, configuration settings, and other environmental details such as whether stdin is a tty) run its output through a pager. The pager used depends on your personal configuration. How that pager acts depends on the pager used and on the input data.

One simple workaround is to run git --no-pager <git-command> to tell Git not to use a pager, even if the configuration and environment suggest that Git should use a pager.

Sign up to request clarification or add additional context in comments.

1 Comment

This was what I needed. The code is run on multiple machines and I personally don't even configure them.
2

This should work:

print(str(sh.git.log("-n 1", "--pretty=format:%ae", "/path/to/file")))

At least this shows how it works on my machine:

$ git log -n 1 --pretty=format:%ae -- README.md
[email protected]
$ python3
Python 3.6.4 (default, Jan 25 2018, 15:54:40)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sh
>>> print(str(sh.git.log("-n 1", "--pretty=format:%ae", "README.md")))
[email protected]

3 Comments

Why should it work? Anyway, it's still the same - (press RETURN).
Are you sure you are giving the parameters to sh.git.log() exactly as I show here? Because you are giving them differently in your code in the question.
Thanks for your help. It also worked for me on a different machine. To make it work everywhere, I had to add --no-pager.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.