5

I'm trying to find the content of the commits that have been checked in between two specific commits.

In git I'd do git rev-list --ancestry-path <older_commit_sha>..<newer_commit_sha>.

In git-python, since it doesn't look there's a direct way of doing it, I resorted to calling the exact command, through repo.git.execute(). The output is a string of commit IDs (hex SHA).

Now, is there a way in git-python to create a valid Commit object starting from the hex SHA as given by execute()?

4 Answers 4

2

It's a tad bit late (like, almost 6 years late), but I somehow figured out how I can do this.

commits = list(repo.iter_commits("{}..{}".format(begin_sha, end_sha), "", ancestry_path=True))

This will return the Commit object list within (begin_sha, end_sha] (with --ancestry-path enabled). I suspected that the kwargs at the end of the argument list of iter_commits are supposed to be redirected to git rev-list, so I just gave it a shot whether I can specify toggle options too other than value options like --after. Seems like it works.

I blame the lack of a proper reference manual for having to figure this out with trial and error. :(

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

Comments

1

I did not find a function in git-python to get the spcific commit object by sha. So I can only think of itering all the commits to get it:

def get_commit_by_sha(repo, sha):
    for c in r.iter_commits():
        if c.hexsha == last_one:
            return c
    return None

As for comparing the difference between two submissions specific commits. diff method is useful, a simple usage:

old_commit.diff(new_comit.hexsha)

The result is a git.diff.DiffIndex object, refer to https://gitpython.readthedocs.io/en/stable/reference.html#git.diff.DiffIndex

1 Comment

You can use Repo.commit method to get specific commit object by sha. Just pass sha string as rev argument. For example: repo.commit('8556c92f96022162da21684194febfa617ead5e1')
0

After much poking, and given this question is not getting much traction, I resorted to using .execute(). Specifically:

commits = repo.git.execute(['git', 'rev-list', '--ancestry-path',
                            '%s..%s' % (oldCommit.hexsha, newCommit.hexsha)]).split()

commits is a list of str.

Of course, oldCommit and newCommit are git.Commit objects.

Comments

0

Posting Przemek Lada's comment as a direct answer to the question "is there a way in git-python to create a valid Commit object starting from the hex SHA as given by execute()?"

You can use the Repo.commit method to get specific commit object by sha. Just pass sha string as rev argument. For example: repo.commit('8556c92f96022162da21684194febfa617ead5e1')

See the Commit object tutorial for more examples.

Comments

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.