/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.211.2 by James Westby
Make it more like a real project.
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
0.211.1 by James Westby
Start the python-git project.
19
import os
20
0.211.4 by James Westby
Add methods to repo to get objects of a certain type.
21
from objects import (ShaFile,
22
                     Commit,
23
                     Tree,
24
                     Blob,
25
                     )
0.211.1 by James Westby
Start the python-git project.
26
27
objectdir = 'objects'
28
symref = 'ref: '
29
30
class Repository(object):
31
32
  ref_locs = ['', 'refs', 'refs/tags', 'refs/heads', 'refs/remotes']
33
34
  def __init__(self, root):
35
    self._basedir = root
36
37
  def basedir(self):
38
    return self._basedir
39
40
  def object_dir(self):
41
    return os.path.join(self.basedir(), objectdir)
42
43
  def _get_ref(self, file):
44
    f = open(file, 'rb')
45
    try:
46
      contents = f.read()
47
      if contents.startswith(symref):
48
        ref = contents[len(symref):]
49
        if ref[-1] == '\n':
50
          ref = ref[:-1]
51
        return self.ref(ref)
52
      assert len(contents) == 41, 'Invalid ref'
53
      return contents[:-1]
54
    finally:
55
      f.close()
56
57
  def ref(self, name):
58
    for dir in self.ref_locs:
59
      file = os.path.join(self.basedir(), dir, name)
60
      if os.path.exists(file):
61
        return self._get_ref(file)
62
63
  def head(self):
64
    return self.ref('HEAD')
65
0.211.4 by James Westby
Add methods to repo to get objects of a certain type.
66
  def _get_object(self, sha, cls):
67
    assert len(sha) == 40, "Incorrect length sha: %s" % str(sha)
0.211.1 by James Westby
Start the python-git project.
68
    dir = sha[:2]
69
    file = sha[2:]
70
    path = os.path.join(self.object_dir(), dir, file)
71
    if not os.path.exists(path):
72
      return None
0.211.4 by James Westby
Add methods to repo to get objects of a certain type.
73
    return cls.from_file(path)
74
75
  def get_object(self, sha):
76
    return self._get_object(sha, ShaFile)
77
78
  def get_commit(self, sha):
79
    return self._get_object(sha, Commit)
80
81
  def get_tree(self, sha):
82
    return self._get_object(sha, Tree)
83
84
  def get_blob(self, sha):
85
    return self._get_object(sha, Blob)
0.211.1 by James Westby
Start the python-git project.
86