tag.txt 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. .. _tutorial-tag:
  2. Tagging
  3. =======
  4. This tutorial will demonstrate how to add a tag to a commit via dulwich.
  5. First let's initialize the repository:
  6. >>> from dulwich.repo import Repo
  7. >>> _repo = Repo("myrepo", mkdir=True)
  8. Next we build the commit object and add it to the object store:
  9. >>> from dulwich.objects import Blob, Tree, Commit, parse_timezone
  10. >>> permissions = 0100644
  11. >>> author = "John Smith"
  12. >>> blob = Blob.from_string("empty")
  13. >>> tree = Tree()
  14. >>> tree.add(tag, permissions, blob.id)
  15. >>> commit = Commit()
  16. >>> commit.tree = tree.id
  17. >>> commit.author = commit.committer = author
  18. >>> commit.commit_time = commit.author_time = int(time())
  19. >>> tz = parse_timezone('-0200')[0]
  20. >>> commit.commit_timezone = commit.author_timezone = tz
  21. >>> commit.encoding = "UTF-8"
  22. >>> commit.message = 'Tagging repo: ' + message
  23. Add objects to the repo store instance:
  24. >>> object_store = _repo.object_store
  25. >>> object_store.add_object(blob)
  26. >>> object_store.add_object(tree)
  27. >>> object_store.add_object(commit)
  28. >>> master_branch = 'master'
  29. >>> _repo.refs['refs/heads/' + master_branch] = commit.id
  30. Finally, add the tag top the repo:
  31. >>> _repo['refs/tags/' + commit] = commit.id
  32. Alternatively, we can use the tag object if we'd like to annotate the tag:
  33. >>> from dulwich.objects import Blob, Tree, Commit, parse_timezone, Tag
  34. >>> tag_message = "Tag Annotation"
  35. >>> tag = Tag()
  36. >>> tag.tagger = author
  37. >>> tag.message = message
  38. >>> tag.name = "v0.1"
  39. >>> tag.object = (Commit, commit.id)
  40. >>> tag.tag_time = commit.author_time
  41. >>> tag.tag_timezone = tz
  42. >>> object_store.add_object(tag)
  43. >>> _repo['refs/tags/' + tag] = tag.id