3

I have created a simple script which uses GitPython to tag the latest commit on a specific branch which has already been checked out.

from git import Repo

def TagRepo(path, tag):
    repo = Repo(path)
    repo.create_tag(tag)
    repo.remotes.origin.push(tag)


if __name__ == "__main__":

    parser = optparse.OptionParser('usage: %prog [options] ')
    parser.add_option('-p', '--path', dest='path', help='path to repo')
    parser.add_option('-t', '--tag', dest='tag', help='Tag label')

    (options, args) = parser.parse_args()

    TagRepo(options.path, options.tag)

I want to improve it so that I can pass it a specific commit's SHA and tag that instead of the latest commit on the branch but I can't see how to do that with GitPython.

3
  • If you are providing a specific SHA, why do you need the tag for? They can be used interchangeably Commented Jan 5, 2018 at 11:54
  • When i want to locate the commit (which relates to a release) down the line - I would rather be looking for version_1.2.3 than a11bef06a3f659402fe7563abf99ad00de2209e6. Commented Jan 5, 2018 at 13:20
  • Just a side note : do not make use of Optparse it is marked deprecated and is probably not included in py3. Commented Feb 27, 2018 at 4:15

2 Answers 2

1

From the documentation of the module:

Obtain commits at the specified revision

    repo.commit('master')
    repo.commit('v0.8.1')
    repo.commit('HEAD~10')

So to retrieve a specific commit just use repo.commit('SHA-1')

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

1 Comment

Thanks I will try that.
0

From the source code:

def create_tag(self, path, ref='HEAD', message=None, force=False, **kwargs)

it appears you can pass a ref to create_tag which following the comments down the line is described as:

:param ref: A reference to the object you want to tag. It can be a commit, tree or blob.

so just get the commit you want and pass that as the ref. dont foget to push as OP does.

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.