Debian Codegraph @

lib/diff_arg.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2008 Gary van der Merwe <[email protected]>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
20 """Diff Arg Provider classes."""
21
22 from breezy.revision import NULL_REVISION, CURRENT_REVISION
23
24
25 # These classes were extracted from diff.py to avoid dependency on PyQt4
26 # in commands.py where base provider class is used, but PyQt4 is not required
27 # for overriden merge command.
28
29 class DiffArgProvider (object):
30 """Contract class to pass arguments to either builtin diff window, or
31 external diffs"""
32
33 def get_diff_window_args(self, processEvents, es):
34 """Returns the arguments for the builtin diff window.
35
36 :return: {"old_tree": old_tree,
37 "new_tree": new_tree,
38 "old_branch": old_branch, (optional)
39 "new_branch": new_branch, (optional)
40 "specific_files": specific_files, (optional)
41 "ignore_whitespace": True or False} (optional)
42 """
43 raise NotImplementedError()
44
45 def get_ext_diff_args(self, processEvents):
46 """Returns the command line arguments for running an ext diff window.
47
48 :return: (dir, List of command line arguments).
49 """
50 raise NotImplementedError()
51
52
53 class InternalDiffArgProvider(DiffArgProvider):
54 """Use for passing arguments from internal source."""
55
56 def __init__(self,
57 old_revid, new_revid,
58 old_branch, new_branch,
59 old_tree=None, new_tree=None,
60 specific_files=None, specific_file_ids=None):
61 self.old_revid = old_revid
62 self.new_revid = new_revid
63 self.old_branch = old_branch
64 self.new_branch = new_branch
65 self.specific_files = specific_files
66 self.specific_file_ids = specific_file_ids
67
68 self.old_tree = old_tree
69 self.new_tree = new_tree
70
71 def need_to_load_paths(self):
72 return self.specific_file_ids is not None \
73 and self.specific_files is None
74
75 def load_old_tree(self):
76 if not self.old_tree:
77 self.old_tree = \
78 self.old_branch.repository.revision_tree(self.old_revid)
79
80 def load_new_tree_and_paths(self):
81 if not self.new_tree:
82 self.new_tree = \
83 self.new_branch.repository.revision_tree(self.new_revid)
84
85 if self.need_to_load_paths():
86 with self.new_tree.lock_read():
87 self.specific_files = [self.new_tree.id2path(id) \
88 for id in self.specific_file_ids]
89
90 def get_diff_window_args(self, processEvents, es):
91 self.load_old_tree()
92 processEvents()
93 self.load_new_tree_and_paths()
94 processEvents()
95
96 return {"old_tree": self.old_tree,
97 "new_tree": self.new_tree,
98 "old_branch": self.old_branch,
99 "new_branch": self.new_branch,
100 "specific_files": self.specific_files}
101
102 def get_revspec(self):
103 def get_revspec_part(revid):
104 if revid.startswith(CURRENT_REVISION):
105 return ''
106 return 'revid:%s' % revid
107
108 return "-r%s..%s" % (
109 get_revspec_part(self.old_revid),
110 get_revspec_part(self.new_revid))
111
112 def get_ext_diff_args(self, processEvents):
113 from breezy import urlutils
114 from breezy import errors
115
116 args = []
117 revspec = self.get_revspec()
118 if revspec:
119 args.append(revspec)
120
121 from breezy.workingtree import WorkingTree
122 # RJLRJL: TODO: check this (see rev 1074)
123 def get_base(branch, tree):
124 if tree and isinstance(tree, WorkingTree):
125 return urlutils.local_path_to_url(tree.basedir)
126 return branch.base
127
128 old_base = get_base(self.old_branch, self.old_tree)
129 new_base = get_base(self.new_branch, self.new_tree)
130
131 # We need to avoid using --new and --old because diff tools
132 # does not support it. There are however some cases where
133 # this is not possilble.
134 need_old = False
135 if not self.old_branch.base == self.new_branch.base:
136 need_old = True
137
138 try:
139 dir = urlutils.local_path_from_url(new_base)
140 except errors.InvalidURL:
141 dir = ""
142 args.append("--new=%s" % new_base)
143 need_old = True
144
145 if need_old:
146 args.append("--old=%s" % old_base)
147
148 if self.need_to_load_paths():
149 self.load_new_tree_and_paths()
150 processEvents()
151 if self.specific_files:
152 args.extend(self.specific_files)
153
154 return dir, args
155
156
157 class InternalWTDiffArgProvider(InternalDiffArgProvider):
158 """Use for passing arguments from internal source where the new tree is
159 the working tree."""
160
161 def __init__(self, old_revid, new_tree, old_branch, new_branch,
162 specific_files=None):
163 self.old_revid = old_revid
164 self.new_tree = new_tree
165 self.old_branch = old_branch
166 self.new_branch = new_branch
167 self.specific_files = specific_files
168
169 self.old_tree = None
170
171 def load_old_tree(self):
172 if self.old_revid is None and self.old_tree is None:
173 self.old_tree = self.new_tree.basis_tree()
174 self.old_revid = self.old_tree.get_revision_id()
175 else:
176 InternalDiffArgProvider.load_old_tree(self)
177
178 def get_diff_window_args(self, processEvents, es):
179 self.load_old_tree()
180 processEvents()
181
182 return {"old_tree": self.old_tree,
183 "new_tree": self.new_tree,
184 "old_branch": self.old_branch,
185 "new_branch": self.new_branch,
186 "specific_files": self.specific_files}
187
188 def get_revspec(self):
189 if self.old_revid is not None:
190 return "-rrevid:%s" % (self.old_revid,)
191 else:
192 return None
193
194 def need_to_load_paths(self):
195 return False