repository.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. self._basedir = root
  31. def basedir(self):
  32. return self._basedir
  33. def object_dir(self):
  34. return os.path.join(self.basedir(), objectdir)
  35. def _get_ref(self, file):
  36. f = open(file, 'rb')
  37. try:
  38. contents = f.read()
  39. if contents.startswith(symref):
  40. ref = contents[len(symref):]
  41. if ref[-1] == '\n':
  42. ref = ref[:-1]
  43. return self.ref(ref)
  44. assert len(contents) == 41, 'Invalid ref'
  45. return contents[:-1]
  46. finally:
  47. f.close()
  48. def ref(self, name):
  49. for dir in self.ref_locs:
  50. file = os.path.join(self.basedir(), dir, name)
  51. if os.path.exists(file):
  52. return self._get_ref(file)
  53. def head(self):
  54. return self.ref('HEAD')
  55. def _get_object(self, sha, cls):
  56. assert len(sha) == 40, "Incorrect length sha: %s" % str(sha)
  57. dir = sha[:2]
  58. file = sha[2:]
  59. path = os.path.join(self.object_dir(), dir, file)
  60. if not os.path.exists(path):
  61. # Should this raise instead?
  62. return None
  63. return cls.from_file(path)
  64. def get_object(self, sha):
  65. return self._get_object(sha, ShaFile)
  66. def get_commit(self, sha):
  67. return self._get_object(sha, Commit)
  68. def get_tree(self, sha):
  69. return self._get_object(sha, Tree)
  70. def get_blob(self, sha):
  71. return self._get_object(sha, Blob)
  72. def revision_history(self, head):
  73. """Returns a list of the commits reachable from head.
  74. Returns a list of commit objects. the first of which will be the commit
  75. of head, then following theat will be the parents.
  76. Raises NotCommitError if any no commits are referenced, including if the
  77. head parameter isn't the sha of a commit.
  78. XXX: work out how to handle merges.
  79. """
  80. # We build the list backwards, as parents are more likely to be older
  81. # than children
  82. pending_commits = [head]
  83. history = []
  84. while pending_commits != []:
  85. head = pending_commits.pop(0)
  86. commit = self.get_commit(head)
  87. if commit is None:
  88. raise MissingCommitError(head)
  89. if commit in history:
  90. continue
  91. i = 0
  92. for known_commit in history:
  93. if known_commit.commit_time() > commit.commit_time():
  94. break
  95. i += 1
  96. history.insert(i, commit)
  97. parents = commit.parents()
  98. pending_commits += parents
  99. history.reverse()
  100. return history