38
38
def __init__(self, lp_branch, config=False, target=None):
39
39
self.lp_branch = lp_branch
40
self.bzr_branch = bzr_branch.Branch.open(self.lp_branch.bzr_identity)
42
41
self.config = BranchConfig(lp_branch.bzr_identity, config)
46
45
self.target = target
47
46
self.logger = logging.getLogger('tarmac')
47
self.temp_tree_dir = None
49
def get_bzr_branch(self):
50
return bzr_branch.Branch.open(self.lp_branch.bzr_identity)
52
# For backwards compatibility
53
bzr_branch = property(get_bzr_branch)
56
if self.temp_tree_dir is not None:
57
return WorkingTree.open(self.temp_tree_dir)
58
if os.path.exists(self.config.tree_dir):
59
return WorkingTree.open(self.config.tree_dir)
61
# For backwards compatibility
62
tree = property(get_tree)
50
65
"""Do some potenetially necessary cleanup during deletion."""
52
# If we were using a temp directory, then remove it
66
if self.temp_tree_dir is not None:
53
67
shutil.rmtree(self.temp_tree_dir)
54
except AttributeError:
59
70
def create(cls, lp_branch, config, create_tree=False, target=None):
62
73
clazz.create_tree()
65
77
def create_tree(self):
66
78
'''Create the dir and working tree.'''
79
bzr_branch = self.get_bzr_branch()
69
'Using tree in %(tree_dir)s' % {
70
'tree_dir': self.config.tree_dir})
71
if os.path.exists(self.config.tree_dir):
72
self.tree = WorkingTree.open(self.config.tree_dir)
81
tree = self.get_tree()
74
83
self.logger.debug('Tree does not exist. Creating dir')
75
84
# Create the path up to but not including tree_dir if it does
77
86
parent_dir = os.path.dirname(self.config.tree_dir)
78
87
if not os.path.exists(parent_dir):
79
88
os.makedirs(parent_dir)
80
self.tree = self.bzr_branch.create_checkout(
81
self.config.tree_dir, lightweight=True)
89
tree = bzr_branch.create_checkout(
90
self.config.tree_dir, lightweight=False)
92
'Using tree in %(tree_dir)s' % {
93
'tree_dir': self.config.tree_dir})
82
94
except AttributeError:
83
95
# Store this so we can rmtree later
84
96
self.temp_tree_dir = tempfile.mkdtemp()
86
98
'Using temp dir at %(tree_dir)s' % {
87
99
'tree_dir': self.temp_tree_dir})
88
self.tree = self.bzr_branch.create_checkout(self.temp_tree_dir)
100
tree = bzr_branch.create_checkout(self.temp_tree_dir)
92
104
def cleanup(self):
93
105
'''Remove the working tree from the temp dir.'''
106
tree = self.get_tree()
95
108
self.logger.info("Running cleanup in %s." % (
96
109
self.lp_branch.bzr_identity))
98
111
self.logger.info("Reverted changes in %s." % (
99
112
self.lp_branch.bzr_identity))
100
for filename in [self.tree.abspath(f) for f in self.unmanaged_files]:
113
for filename in [tree.abspath(f) for f in self.unmanaged_files]:
101
114
if os.path.isdir(filename) and not os.path.islink(filename):
102
115
shutil.rmtree(filename)
106
119
self.logger.info("Successfully removed extra files from %s." % (
107
120
self.lp_branch.bzr_identity))
110
123
def merge(self, branch, revid=None):
111
124
'''Merge from another tarmac.branch.Branch instance.'''
113
conflict_list = self.tree.merge_from_branch(
114
branch.bzr_branch, to_revision=revid)
125
tree = self.get_tree()
127
conflict_list = tree.merge_from_branch(
128
branch.get_bzr_branch(), to_revision=revid)
115
129
if conflict_list:
116
130
message = u'Conflicts merging branch.'
125
139
def unmanaged_files(self):
126
140
"""Get the list of ignored and unknown files in the tree."""
127
self.tree.lock_read()
128
unmanaged = [x for x in self.tree.unknowns()]
129
unmanaged.extend([x[0] for x in self.tree.ignored_files()])
141
tree = self.get_tree()
144
unmanaged = [x for x in tree.unknowns()]
145
unmanaged.extend([x[0] for x in tree.ignored_files()])
134
150
def conflicts(self):
135
151
'''Print the conflicts.'''
136
assert self.tree.conflicts()
152
tree = self.get_tree()
138
for conflict in self.tree.conflicts():
155
for conflict in tree.conflicts():
139
156
conflicts.append(
140
157
u'%s in %s' % (conflict.typestring, conflict.path))
141
158
return '\n'.join(conflicts)
143
160
def commit(self, commit_message, revprops=None, **kwargs):
144
161
'''Commit changes.'''
162
tree = self.get_tree()
158
177
'review identity or vote.')
159
178
revprops['reviews'] = '\n'.join(reviews)
161
self.tree.commit(commit_message, committer='Tarmac',
162
revprops=revprops, authors=authors)
180
tree.commit(commit_message, committer='Tarmac',
181
revprops=revprops, authors=authors)
165
184
def landing_candidates(self):
170
189
def authors(self):
192
bzr_branch = self.get_bzr_branch()
174
self.bzr_branch.lock_read()
175
self.target.bzr_branch.lock_read()
194
target_bzr_branch = self.target.get_bzr_branch()
195
bzr_branch.lock_read()
196
target_bzr_branch.lock_read()
177
graph = self.bzr_branch.repository.get_graph(
178
self.target.bzr_branch.repository)
198
graph = bzr_branch.repository.get_graph(
199
target_bzr_branch.repository)
180
201
unique_ids = graph.find_unique_ancestors(
181
self.bzr_branch.last_revision(),
182
[self.target.bzr_branch.last_revision()])
202
bzr_branch.last_revision(),
203
[target_bzr_branch.last_revision()])
184
revs = self.bzr_branch.repository.get_revisions(unique_ids)
205
revs = bzr_branch.repository.get_revisions(unique_ids)
186
207
apparent_authors = rev.get_apparent_authors()
187
208
for author in apparent_authors:
189
210
if author not in author_list:
190
211
author_list.append(author)
192
self.target.bzr_branch.unlock()
193
self.bzr_branch.unlock()
213
target_bzr_branch.unlock()
195
last_rev = self.bzr_branch.last_revision()
216
last_rev = bzr_branch.last_revision()
196
217
if last_rev != 'null:':
197
rev = self.bzr_branch.repository.get_revision(last_rev)
218
rev = bzr_branch.repository.get_revision(last_rev)
198
219
apparent_authors = rev.get_apparent_authors()
199
220
author_list.extend(
200
221
[a.replace('\n', '') for a in apparent_authors])
206
227
"""Return the list of bugs fixed by the branch."""
209
self.bzr_branch.lock_read()
210
oldrevid = self.bzr_branch.get_rev_id(self.lp_branch.revision_count)
211
for rev_info in self.bzr_branch.iter_merge_sorted_revisions(
230
bzr_branch = self.get_bzr_branch()
231
bzr_branch.lock_read()
232
oldrevid = bzr_branch.get_rev_id(self.lp_branch.revision_count)
233
for rev_info in bzr_branch.iter_merge_sorted_revisions(
212
234
stop_revision_id=oldrevid):
214
rev = self.bzr_branch.repository.get_revision(rev_info[0])
236
rev = bzr_branch.repository.get_revision(rev_info[0])
215
237
for bug in rev.iter_bugs():
216
238
if bug[0].startswith('https://launchpad.net/bugs/'):
217
239
bugs_list.append(bug[0].replace(