repository.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # repository.py -- For dealing wih git repositories.
  2. # Copyright (C) 2007 James Westby <jw+debian@jameswestby.net>
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; version 2
  7. # of the License.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17. # MA 02110-1301, USA.
  18. import os
  19. from errors import MissingCommitError
  20. from objects import (ShaFile,
  21. Commit,
  22. Tree,
  23. Blob,
  24. )
  25. objectdir = 'objects'
  26. symref = 'ref: '
  27. class Repository(object):
  28. ref_locs = ['', 'refs', 'refs/tags', 'refs/heads', 'refs/remotes']
  29. def __init__(self, root):
  30. controldir = os.path.join(root, ".git")
  31. if os.path.exists(os.path.join(controldir, "objects")):
  32. self.bare = False
  33. self._basedir = controldir
  34. else:
  35. self.bare = True
  36. self._basedir = root
  37. def basedir(self):
  38. return self._basedir
  39. def object_dir(self):
  40. return os.path.join(self.basedir(), objectdir)
  41. def _get_ref(self, file):
  42. f = open(file, 'rb')
  43. try:
  44. contents = f.read()
  45. if contents.startswith(symref):
  46. ref = contents[len(symref):]
  47. if ref[-1] == '\n':
  48. ref = ref[:-1]
  49. return self.ref(ref)
  50. assert len(contents) == 41, 'Invalid ref'
  51. return contents[:-1]
  52. finally:
  53. f.close()
  54. def ref(self, name):
  55. for dir in self.ref_locs:
  56. file = os.path.join(self.basedir(), dir, name)
  57. if os.path.exists(file):
  58. return self._get_ref(file)
  59. def head(self):
  60. return self.ref('HEAD')
  61. def _get_object(self, sha, cls):
  62. assert len(sha) == 40, "Incorrect length sha: %s" % str(sha)
  63. dir = sha[:2]
  64. file = sha[2:]
  65. path = os.path.join(self.object_dir(), dir, file)
  66. if not os.path.exists(path):
  67. # Should this raise instead?
  68. return None
  69. return cls.from_file(path)
  70. def get_object(self, sha):
  71. return self._get_object(sha, ShaFile)
  72. def get_commit(self, sha):
  73. return self._get_object(sha, Commit)
  74. def get_tree(self, sha):
  75. return self._get_object(sha, Tree)
  76. def get_blob(self, sha):
  77. return self._get_object(sha, Blob)
  78. def revision_history(self, head):
  79. """Returns a list of the commits reachable from head.
  80. Returns a list of commit objects. the first of which will be the commit
  81. of head, then following theat will be the parents.
  82. Raises NotCommitError if any no commits are referenced, including if the
  83. head parameter isn't the sha of a commit.
  84. XXX: work out how to handle merges.
  85. """
  86. # We build the list backwards, as parents are more likely to be older
  87. # than children
  88. pending_commits = [head]
  89. history = []
  90. while pending_commits != []:
  91. head = pending_commits.pop(0)
  92. commit = self.get_commit(head)
  93. if commit is None:
  94. raise MissingCommitError(head)
  95. if commit in history:
  96. continue
  97. i = 0
  98. for known_commit in history:
  99. if known_commit.commit_time() > commit.commit_time():
  100. break
  101. i += 1
  102. history.insert(i, commit)
  103. parents = commit.parents()
  104. pending_commits += parents
  105. history.reverse()
  106. return history