Iterate commits b/w 2 specified commits in GitPython
Asked Answered
F

3

6
import git
repo = git.Repo(repo_dir)
ref_name = 'master'
for commit in repo.iter_commits(rev=ref_name):
     <some code here>

This code iterates through all the commits. I want to iterate b/w 2 commits. Just like git log commit1...commit2

How can I do the same using GitPython's iter_commits() method.

Flashcube answered 15/3, 2019 at 6:10 Comment(4)
But do you know the code to execute the git command on terminal? If yes, you can create a function using subprocess. Then you can call the function to run the git command.Bantu
Yes, I can do that. But I don't want go that way. I want to use this GitPython LibFlashcube
gitpython.readthedocs.io/en/stable/… . From Commit.iter_items() accepts a revision specifier, for which I believe Revision Range is also part of it. Just pass 'commit1...commit2' should do the work.Postulate
If your question is exclusively about "how do I do thing X in gitpython", the gitpython tag is appropriate, but none of the other tags are appropriate as you'll get answers like the one from @Yusufsn. Note that commit is about database transactions, not about Git commits.Newsman
R
6

repo.iter_commits(rev='1234abc..5678def') works for me in GitPython==2.1.11

Example:

repo = git.Repo(repo_dir)
for commit in repo.iter_commits(rev='master..HEAD'):
     <some code here>
Rochellrochella answered 21/1, 2020 at 20:36 Comment(0)
B
3

You can use pure gitpython for that.

If you want to able to traverse certain commit (assuming the first commit is HEAD), just use max_count. See The Commit object

two_commits = list(repo.iter_commits('master', max_count=2))
assert len(two_commits) == 2

if you want similar ability to git log commit1...commit2 as you mentioned:

logs = repo.git.log("--oneline", "f5035ce..f63d26b")

will give you:

>>> logs
'f63d26b Fix urxvt name to match debian repo\n571f449 Add more key for helm-org-rifle\nbea2697 Drop bm package'

You can also use logs = repo.git.log("f5035ce..f63d26b") but it will give you all info (just like you use git log without --oneline)

if you want nice output, use pretty print:

from pprint import pprint as pp
>>> pp(logs)
('f63d26b Fix urxvt name to match debian repo\n'
 '571f449 Add more key for helm-org-rifle\n'
 'bea2697 Drop bm package')

For more explanation about repo.git.log, see https://mcmap.net/q/1776120/-how-to-use-git-log-oneline-in-gitpython

Bainbrudge answered 7/4, 2019 at 2:27 Comment(0)
B
-2

First, make a function to run the git command.

from git import *
from subprocess import Popen, PIPE

def execute_gitcmd(cmd, repo):
    pipe = subprocess.Popen(cmd, shell=True, cwd=repo, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    (out, error) = pipe.communicate()
    return out, error
    pipe.wait()

Then write any git command as you use on terminal, for example:

gitcmd = "git log -n1 --oneline"

Finally, call your function:

log = (execute_gitcmd(gitcmd, your_repository))

Hope this can help.

Bantu answered 15/3, 2019 at 7:58 Comment(1)
1. The question was about GitPython. 2. The function has a major bug — return before pipe.wait().Vitrification

© 2022 - 2024 — McMap. All rights reserved.