Merge 0.4.
[jelmer/subvertpy.git] / branchprops.py
1 # Copyright (C) 2006-2007 Jelmer Vernooij <jelmer@samba.org>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17 """Branch property access and caching."""
18
19 from bzrlib.errors import NoSuchRevision
20 from bzrlib.trace import mutter
21
22 import constants
23 from core import SubversionException
24
25
26 class PathPropertyProvider(object):
27     def __init__(self, log):
28         self.log = log
29
30     def get_properties(self, path, revnum):
31         """Obtain all the directory properties set on a path/revnum pair.
32
33         :param path: Subversion path
34         :param revnum: Subversion revision number
35         :return: Dictionary with properties
36         """
37         assert isinstance(path, str)
38         path = path.lstrip("/")
39
40         try:
41             (_, _, props) = self.log._transport.get_dir(path, 
42                 revnum)
43         except SubversionException, (_, num):
44             if num == constants.ERR_FS_NO_SUCH_REVISION:
45                 raise NoSuchRevision(self, revnum)
46             raise
47
48         return props
49
50     def get_changed_properties(self, path, revnum):
51         """Get the contents of a Subversion file property.
52
53         Will use the cache.
54
55         :param path: Subversion path.
56         :param revnum: Subversion revision number.
57         :return: Contents of property or default if property didn't exist.
58         """
59         assert isinstance(revnum, int)
60         assert isinstance(path, str)
61         if not path in self.log.get_revision_paths(revnum):
62             return {}
63         current = self.get_properties(path, revnum)
64         if current == {}:
65             return {}
66         (prev_path, prev_revnum) = self.log.get_previous(path, revnum)
67         if prev_path is None and prev_revnum == -1:
68             previous = {}
69         else:
70             previous = self.get_properties(prev_path.encode("utf-8"), 
71                                            prev_revnum)
72         ret = {}
73         for key, val in current.items():
74             if previous.get(key) != val:
75                 ret[key] = val
76         return ret