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 |
||
21 |
from objects import ShaFile |
|
22 |
||
23 |
objectdir = 'objects' |
|
24 |
symref = 'ref: ' |
|
25 |
||
26 |
class Repository(object): |
|
27 |
||
28 |
ref_locs = ['', 'refs', 'refs/tags', 'refs/heads', 'refs/remotes'] |
|
29 |
||
30 |
def __init__(self, root): |
|
31 |
self._basedir = root |
|
32 |
||
33 |
def basedir(self): |
|
34 |
return self._basedir |
|
35 |
||
36 |
def object_dir(self): |
|
37 |
return os.path.join(self.basedir(), objectdir) |
|
38 |
||
39 |
def _get_ref(self, file): |
|
40 |
f = open(file, 'rb') |
|
41 |
try: |
|
42 |
contents = f.read() |
|
43 |
if contents.startswith(symref): |
|
44 |
ref = contents[len(symref):] |
|
45 |
if ref[-1] == '\n': |
|
46 |
ref = ref[:-1] |
|
47 |
return self.ref(ref) |
|
48 |
assert len(contents) == 41, 'Invalid ref' |
|
49 |
return contents[:-1] |
|
50 |
finally: |
|
51 |
f.close() |
|
52 |
||
53 |
def ref(self, name): |
|
54 |
for dir in self.ref_locs: |
|
55 |
file = os.path.join(self.basedir(), dir, name) |
|
56 |
if os.path.exists(file): |
|
57 |
return self._get_ref(file) |
|
58 |
||
59 |
def head(self): |
|
60 |
return self.ref('HEAD') |
|
61 |
||
62 |
def get_object(self, sha): |
|
63 |
assert len(sha) == 40, "Incorrect sha length" |
|
64 |
dir = sha[:2] |
|
65 |
file = sha[2:] |
|
66 |
path = os.path.join(self.object_dir(), dir, file) |
|
67 |
if not os.path.exists(path): |
|
68 |
return None |
|
69 |
return ShaFile.from_file(path) |
|
70 |