~jelmer/brz/tarmac

« back to all changes in this revision

Viewing changes to tarmac/branch.py

  • Committer: Jelmer Vernooij
  • Date: 2022-10-19 10:56:02 UTC
  • Revision ID: jelmer@jelmer.uk-20221019105602-liq636ud5fvaelz7
Simplify some locking logic.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
# along with Tarmac.  If not, see <http://www.gnu.org/licenses/>.
16
16
 
17
17
'''Tarmac branch tools.'''
 
18
from contextlib import ExitStack
18
19
import logging
19
20
import os
20
21
import shutil
71
72
 
72
73
    def create_tree(self):
73
74
        '''Create the dir and working tree.'''
74
 
        try:
75
 
            self.logger.debug(
76
 
                'Using tree in %(tree_dir)s' % {
77
 
                    'tree_dir': self.config.tree_dir})
78
 
            if os.path.exists(self.config.tree_dir):
79
 
                self.tree = WorkingTree.open(self.config.tree_dir)
 
75
        self.logger.debug(
 
76
            'Using tree in %(tree_dir)s' % {
 
77
                'tree_dir': self.config.tree_dir})
 
78
        if os.path.exists(self.config.tree_dir):
 
79
            self.tree = WorkingTree.open(self.config.tree_dir)
80
80
 
81
 
                if self.tree.branch.user_url != self.bzr_branch.user_url:
82
 
                    self.logger.debug('Tree URLs do not match: %s - %s' % (
83
 
                        self.bzr_branch.user_url, self.tree.branch.user_url))
84
 
                    raise InvalidWorkingTree(
85
 
                        'The `tree_dir` option for the target branch is not a '
86
 
                        'lightweight checkout. Please ask a project '
87
 
                        'administrator to resolve the issue, and try again.')
88
 
            else:
89
 
                self.logger.debug('Tree does not exist.  Creating dir')
90
 
                # Create the path up to but not including tree_dir if it does
91
 
                # not exist.
92
 
                parent_dir = os.path.dirname(self.config.tree_dir)
93
 
                if not os.path.exists(parent_dir):
94
 
                    os.makedirs(parent_dir)
95
 
                self.tree = self.bzr_branch.create_checkout(
96
 
                    self.config.tree_dir, lightweight=True)
97
 
        except AttributeError:
98
 
            # Store this so we can rmtree later
99
 
            self.temp_tree_dir = tempfile.mkdtemp()
100
 
            self.logger.debug(
101
 
                'Using temp dir at %(tree_dir)s' % {
102
 
                    'tree_dir': self.temp_tree_dir})
103
 
            self.tree = self.bzr_branch.create_checkout(self.temp_tree_dir)
 
81
            if self.tree.branch.user_url != self.bzr_branch.user_url:
 
82
                self.logger.debug('Tree URLs do not match: %s - %s' % (
 
83
                    self.bzr_branch.user_url, self.tree.branch.user_url))
 
84
                raise InvalidWorkingTree(
 
85
                    'The `tree_dir` option for the target branch is not a '
 
86
                    'lightweight checkout. Please ask a project '
 
87
                    'administrator to resolve the issue, and try again.')
 
88
        else:
 
89
            self.logger.debug('Tree does not exist.  Creating dir')
 
90
            # Create the path up to but not including tree_dir if it does
 
91
            # not exist.
 
92
            parent_dir = os.path.dirname(self.config.tree_dir)
 
93
            if not os.path.exists(parent_dir):
 
94
                os.makedirs(parent_dir)
 
95
            self.tree = self.bzr_branch.create_checkout(
 
96
                self.config.tree_dir, lightweight=True)
104
97
 
105
98
        self.cleanup()
106
99
 
138
131
    def unmanaged_files(self):
139
132
        """Get the list of ignored and unknown files in the tree."""
140
133
        unmanaged = []
141
 
        try:
142
 
            self.tree.lock_read()
 
134
        with self.tree.lock_read():
143
135
            unmanaged = [x for x in self.tree.unknowns()]
144
136
            unmanaged.extend([x[0] for x in self.tree.ignored_files()])
145
 
        finally:
146
 
            self.tree.unlock()
147
137
        return unmanaged
148
138
 
149
139
    @property
156
146
                '%s in %s' % (conflict.typestring, conflict.path))
157
147
        return '\n'.join(conflicts)
158
148
 
159
 
    def commit(self, commit_message, revprops=None, **kwargs):
 
149
    def commit(self, commit_message, revprops=None, dry_run=False, **kwargs):
160
150
        '''Commit changes.'''
161
151
        if not revprops:
162
152
            revprops = {}
179
169
        else:
180
170
            committer = 'Tarmac'
181
171
 
182
 
        try:
 
172
        if not dry_run:
183
173
            self.tree.commit(commit_message, committer=committer,
184
174
                             revprops=revprops, authors=authors)
185
 
        except AssertionError as error:
186
 
            raise TarmacMergeError(str(error))
187
175
 
188
176
    @property
189
177
    def landing_candidates(self):
195
183
        author_list = []
196
184
 
197
185
        if self.target:
198
 
            try:
199
 
                self.bzr_branch.lock_read()
200
 
                self.target.bzr_branch.lock_read()
 
186
            with ExitStack() as es:
 
187
                es.enter_context(self.bzr_branch.lock_read())
 
188
                es.enter_context(self.target.bzr_branch.lock_read())
201
189
 
202
190
                graph = self.bzr_branch.repository.get_graph(
203
191
                    self.target.bzr_branch.repository)
214
202
                        if author not in author_list:
215
203
                            author_list.append(author)
216
204
 
217
 
            finally:
218
 
                self.target.bzr_branch.unlock()
219
 
                self.bzr_branch.unlock()
220
205
        else:
221
206
            last_rev = self.bzr_branch.last_revision()
222
207
            if last_rev != NULL_REVISION:
232
217
        """Return the list of bugs fixed by the branch."""
233
218
        bugs_list = []
234
219
 
235
 
        try:
236
 
            self.bzr_branch.lock_read()
 
220
        with self.bzr_branch.lock_read():
237
221
            oldrevid = self.bzr_branch.get_rev_id(self.lp_branch.revision_count)
238
222
            for rev_info in self.bzr_branch.iter_merge_sorted_revisions(
239
223
                stop_revision_id=oldrevid):
246
230
                except NoSuchRevision:
247
231
                    continue
248
232
 
249
 
        finally:
250
 
            self.bzr_branch.unlock()
251
233
        return bugs_list
252
234
 
253
235
    @property