getting difference of commits between two branches using gitpython

1.9k Views Asked by At

What I want to achieve can be done using command line git as follows

git log stage..develop

commit ea31b4f01f02af91f31.....b34895e1c825 (origin/develop, develop)
Author: author <[email protected]>
Date:   Tue Oct 13 11:59:01 2020 +0400

    chore: 15th commit

I want to do the same thing using gitpython

I am unable to find a suitable documentation for the same. I checked the docs but no luck.

import git
import shutil
import os

from git import Git

ssh_key_identify_file_path = ""


class SCMRepo(object):
    repo_url = ""
    repo_name = ""
    repo_obj = None

    def __init__(self, r_url, r_name):
        self.repo_url = r_url
        self.repo_name = r_name

    def git_clone(self, r_clone_dir):
        git.Git(r_clone_dir).clone(self.repo_url)
        self.repo_obj = git.Repo.init(r_clone_dir + self.repo_name)

    def git_checkout(self, r_branch_name):
        self.repo_obj.git.checkout(r_branch_name)

    def git_reset(self, reset_to_branch_name):
        self.repo_obj.git.reset('--hard', reset_to_branch_name)

    def git_push(self):
        origin = self.repo_obj.remote(name='origin')
        origin.push()

    def git_log(self):
        if os.path.isdir("/tmp/" + self.repo_name):
            repository_path = "/tmp/" + self.repo_name
            gitObj = git.Git(repository_path)
            loginfo = gitObj.log()
            print(type(loginfo))
            print(loginfo)
        else:
            print("Repo path does not exist")



def get_scm_object(repository_url):
    repository_name = repository_url.split("/")[1].replace(".git", "")
    if os.path.isdir("/tmp/" + repository_name):
        shutil.rmtree("/tmp/" + repository_name.replace(".git", ""))
    scm_object = SCMRepo(repository_url, repository_name)
    scm_object.git_clone("/tmp/")
    return scm_object



def get_git_log_of_branch(scm_object, branch_name):
    scm_object.git_checkout(branch_name)
    scm_object.git_log()


scm_obj = get_scm_object("[email protected]/test-ff.git")
get_git_log_of_branch(scm_obj, "stage")
print("*********************************")
1

There are 1 best solutions below

0
On BEST ANSWER

Refer this. Seems to be the same. It will print the diff between the develop and stage branch.