1

I am trying to achieve git tag --contains <commit> in gitpython. Can anyone point me to the documentation. I have found documentation to fetch all tags but not with tags that contain particular commit.

2 Answers 2

1

Update 2019: as Anentropic mentioned in the comments, you can "shell out" commands using GitPython as you would running the git cli. For instance in this case, you would use repo.git.tag("--contains", "<commit>").split("\n").


I've given up on GitPython because of these restrictions. It's annoyingly not very git like. This simple class takes care of all things git (aside from init of a new repo and auth):

class PyGit:
    def __init__(self, repo_directory):
        self.repo_directory = repo_directory
        git_check = subprocess.check_output(['git', 'rev-parse', '--git-dir'],
                                            cwd=self.repo_directory).split("\n")[0]
        if git_check != '.git':
            raise Exception("Invalid git repo directory: '{}'.\n"
                            "repo_directory must be a root repo directory "
                            "of git project.".format(self.repo_directory))

    def __call__(self, *args, **kwargs):
        return self._git(args[0])

    def _git(self, *args):
        arguments = ["git"] + [arg for arg in args]
        return subprocess.check_output(arguments, cwd=self.repo_directory).split("\n")

So now you can do anything you can do in git:

>>> git = PyGit("/path/to/repo/")

>>> git("checkout", "master")
["Switched to branch 'master'",
"Your branch is up-to-date with 'origin/master'."]

>>> git("checkout", "develop")
["Switched to branch 'develop'",
"Your branch is up-to-date with 'origin/develop'."]

>>> git("describe", "--tags")
["1.4.0-rev23"]

>>> git("tag", "--contains", "ex4m9le*c00m1t*h4Sh")
["1.4.0-rev23", "MY-SECOND-TAG-rev1"]
2
  • Added to github since I just started using this a lot. github.com/mijdavis2/PyGit
    – mjd2
    Commented Mar 11, 2016 at 23:07
  • 1
    You can do exactly this (shelling out to git cli) in GitPython already: repo.git.tag("--contains", "ex4m9le*c00m1t*h4Sh") ...but you have to add a .split("\n") to get list of tags. On the other hand in other cases is useful to have an object interface rather than just shelling out all the time.
    – Anentropic
    Commented Apr 12, 2019 at 13:27
-1
tagref = TagReference.list_items(repo)[0]
print tagref.commit.message

From the docs.

1
  • Does not answer the question. This gets the commit message of the first tag in the repo. The question asks how to get a list of all tags which contain a particular commit.
    – Anentropic
    Commented Apr 12, 2019 at 13:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.