]> arthur.barton.de Git - bup.git/blob - lib/bup/repo.py
25a94b0f66f9cb04765abf1f11436a4d1ceff3f2
[bup.git] / lib / bup / repo.py
1
2 from functools import partial
3
4 from bup import client, git
5
6
7 class LocalRepo:
8     def __init__(self, repo_dir=None):
9         self.repo_dir = repo_dir or git.repo()
10         self._cp = git.cp(repo_dir)
11         self.rev_list = partial(git.rev_list, repo_dir=repo_dir)
12
13     def cat(self, ref):
14         """If ref does not exist, yield (None, None, None).  Otherwise yield
15         (oidx, type, size), and then all of the data associated with
16         ref.
17
18         """
19         it = self._cp.get(ref)
20         oidx, typ, size = info = next(it)
21         yield info
22         if oidx:
23             for data in it:
24                 yield data
25         assert not next(it, None)
26
27     def join(self, ref):
28         return self._cp.join(ref)
29
30     def refs(self, patterns=None, limit_to_heads=False, limit_to_tags=False):
31         for ref in git.list_refs(patterns=patterns,
32                                  limit_to_heads=limit_to_heads,
33                                  limit_to_tags=limit_to_tags,
34                                  repo_dir=self.repo_dir):
35             yield ref
36
37 class RemoteRepo:
38     def __init__(self, address):
39         self.address = address
40         self.client = client.Client(address)
41         self.rev_list = self.client.rev_list
42
43     def cat(self, ref):
44         """If ref does not exist, yield (None, None, None).  Otherwise yield
45         (oidx, type, size), and then all of the data associated with
46         ref.
47
48         """
49         # Yield all the data here so that we don't finish the
50         # cat_batch iterator (triggering its cleanup) until all of the
51         # data has been read.  Otherwise we'd be out of sync with the
52         # server.
53         items = self.client.cat_batch((ref,))
54         oidx, typ, size, it = info = next(items)
55         yield info[:-1]
56         if oidx:
57             for data in it:
58                 yield data
59         assert not next(items, None)
60
61     def join(self, ref):
62         return self.client.join(ref)
63
64     def refs(self, patterns=None, limit_to_heads=False, limit_to_tags=False):
65         for ref in self.client.refs(patterns=patterns,
66                                     limit_to_heads=limit_to_heads,
67                                     limit_to_tags=limit_to_tags):
68             yield ref