~jelmer/qbrz/relative

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# -*- coding: utf-8 -*-
#
# QBzr - Qt frontend to Bazaar commands
# Copyright (C) 2007 Lukáš Lalinský <lalinsky@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

import os

from PyQt4 import QtCore, QtGui

from breezy import (
    errors,
    osutils,
    urlutils,
    )
from breezy.commands import get_cmd_object

from breezy.plugins.qbrz.lib.i18n import gettext
from breezy.plugins.qbrz.lib.subprocess import SubProcessDialog
from breezy.plugins.qbrz.lib.ui_pull import Ui_PullForm
from breezy.plugins.qbrz.lib.ui_push import Ui_PushForm
from breezy.plugins.qbrz.lib.ui_merge import Ui_MergeForm
from breezy.plugins.qbrz.lib.util import (
    iter_branch_related_locations,
    save_pull_location,
    fill_pull_combo,
    fill_combo_with,
    hookup_directory_picker,
    DIRECTORYPICKER_SOURCE,
    DIRECTORYPICKER_TARGET,
    url_for_display,
    )


class QBzrPullWindow(SubProcessDialog):

    NAME = "pull"

    def __init__(self, branch, tree=None, location=None, revision=None, remember=None,
                 overwrite=None, ui_mode=True, parent=None):
        self.branch = branch
        self.tree = tree
        super(QBzrPullWindow, self).__init__(name = self.NAME,
                                             ui_mode = ui_mode,
                                             parent = parent)
        self.ui = Ui_PullForm()
        self.setupUi(self.ui)
        # add the subprocess widgets.
        for w in self.make_default_layout_widgets():
            self.layout().addWidget(w)

        self.default_location = fill_pull_combo(self.ui.location, self.branch)
        if location:
            self.ui.location.setEditText(location)
        else:
            self.ui.location.setFocus()

        if remember:
            self.ui.remember.setCheckState(QtCore.Qt.Checked)
        if overwrite:
            self.ui.overwrite.setCheckState(QtCore.Qt.Checked)
        if revision:
            self.ui.revision.setText(revision)

        # One directory picker for the pull location.
        hookup_directory_picker(self,
                                self.ui.location_picker,
                                self.ui.location,
                                DIRECTORYPICKER_SOURCE)

    def do_start(self):
        if self.tree:
            dest = self.tree.basedir
        else:
            dest = self.branch.base
        args = []
        if dest != osutils.getcwd():
            args.extend(('--directory', dest))
        if self.ui.overwrite.isChecked():
            args.append('--overwrite')
        if self.ui.remember.isChecked():
            args.append('--remember')
        revision = str(self.ui.revision.text())
        if revision:
            args.append('--revision')
            args.append(revision)
        location = str(self.ui.location.currentText())
        if location and location != self.default_location:
            args.insert(0, location)
        self.process_widget.do_start(None, 'pull', *args)
        save_pull_location(self.branch, location)


class QBzrPushWindow(SubProcessDialog):

    NAME = "push"

    def __init__(self, branch, tree=None, location=None,
                 create_prefix=None, use_existing_dir=None,
                 remember=None, overwrite=None, ui_mode=True, parent=None):

        self.branch = branch
        self.tree = tree
        self._no_strict = None
        super(QBzrPushWindow, self).__init__(name = self.NAME,
                                             ui_mode = ui_mode,
                                             parent = parent)

        self.ui = Ui_PushForm()
        self.setupUi(self.ui)
        # and add the subprocess widgets.
        for w in self.make_default_layout_widgets():
            self.layout().addWidget(w)

        self.default_location = self.branch.get_push_location()
        df = url_for_display(self.default_location or
            self._suggested_push_location())
        fill_combo_with(self.ui.location, df,
                        iter_branch_related_locations(self.branch))
        if location:
            self.ui.location.setEditText(location)
        else:
            self.ui.location.setFocus()

        if remember:
            self.ui.remember.setCheckState(QtCore.Qt.Checked)
        if overwrite:
            self.ui.overwrite.setCheckState(QtCore.Qt.Checked)
        if create_prefix:
            self.ui.create_prefix.setCheckState(QtCore.Qt.Checked)
        if use_existing_dir:
            self.ui.use_existing_dir.setCheckState(QtCore.Qt.Checked)

        # One directory picker for the push location.
        hookup_directory_picker(self,
                                self.ui.location_picker,
                                self.ui.location,
                                DIRECTORYPICKER_TARGET)

    def _suggested_push_location(self):
        """Suggest a push location when one is not already defined.

        @return: a sensible location as a string or '' if none.
        """
        # If this is a feature branch and its parent exists locally,
        # its grandparent is likely to be the hosted master branch.
        # If so, suggest a push location, otherwise don't.
        parent_url = self.branch.get_parent()
        if parent_url and parent_url.startswith("file://"):
            from breezy.branch import Branch
            try:
                parent_branch = Branch.open(parent_url)
            except errors.NotBranchError:
                return ''
            master_url = (parent_branch.get_parent() or
                parent_branch.get_bound_location())
            if master_url and not master_url.startswith("file://"):
                if master_url.find("launchpad") >= 0:
                    suggest_url = self._build_lp_push_suggestion(master_url)
                    if suggest_url:
                        return suggest_url
                # XXX we can hook in there even more specific suggesters
                # XXX maybe we need registry?
                suggest_url = self._build_generic_push_suggestion(master_url)
                if suggest_url:
                    return suggest_url
        return ''

    def _build_lp_push_suggestion(self, master_url):
        try:
            from breezy.plugins.launchpad import account
        except ImportError:
            # yes, ImportError is possible with bzr.exe,
            # because user has option to not install launchpad plugin at all
            return ''
        from breezy.plugins.qbrz.lib.util import launchpad_project_from_url
        user_name = account.get_lp_login()
        project_name = launchpad_project_from_url(master_url)
        branch_name = urlutils.basename(self.branch.base)
        if user_name and project_name and branch_name:
            return "lp:~%s/%s/%s" % (user_name, project_name, branch_name)
        else:
            return ''

    def _build_generic_push_suggestion(self, master_url):
        master_parent = urlutils.dirname(master_url)
        branch_name = urlutils.basename(self.branch.base)
        return urlutils.join(master_parent, branch_name)

    def do_start(self):
        if self.tree:
            dest = self.tree.basedir
        else:
            dest = self.branch.base
        args = []
        if dest != osutils.getcwd():
            args.extend(('--directory', dest))
        if self.ui.overwrite.isChecked():
            args.append('--overwrite')
        if self.ui.remember.isChecked():
            args.append('--remember')
        if self.ui.create_prefix.isChecked():
            args.append('--create-prefix')
        if self.ui.use_existing_dir.isChecked():
            args.append('--use-existing-dir')
        if 'strict' in get_cmd_object('push').options() and self._no_strict:
            # force --no-strict because we checking blocking conditions
            # in validate method (see below).
            args.append('--no-strict')
        location = str(self.ui.location.currentText())
        if location and location != self.default_location:
            args.insert(0, location)
        self.process_widget.do_start(None, 'push', *args)

    def validate(self):
        """Check working tree for blocking conditions (such as uncommitted
        changes or out of date) and return True if we can push anyway
        or False if push operation should be aborted.
        """
        if self._no_strict:
            return True
        # check blocking conditions in the tree
        if self.tree is None:
            return True     # no tree - no check
        cfg = self.branch.get_config()
        strict = cfg.get_user_option('push_strict')
        if strict is not None:
            bools = dict(yes=True, no=False, on=True, off=False, true=True, false=False)
            strict = bools.get(strict.lower(), None)
        if strict == False:
            return True     # don't check blocking conditions
        # the code below based on check in from breezy/builtins.py: cmd_push
        tree = self.tree
        blocker = None
        if (tree.has_changes(tree.basis_tree()) or len(tree.get_parent_ids()) > 1):
            blocker = gettext('Working tree has uncommitted changes.')
        if tree.last_revision() != tree.branch.last_revision():
            # The tree has lost sync with its branch, there is little
            # chance that the user is aware of it but he can still force
            # the push with --no-strict
            blocker = gettext("Working tree is out of date, please run 'brz update'.")
        #
        if blocker is None:
            return True
        if self.ask_confirmation(blocker + "\n\n" +
            gettext("Do you want to continue anyway?"),
            type='warning'):
                self._no_strict = True
                return True
        return False


class QBzrMergeWindow(SubProcessDialog):

    NAME = "merge"

    def __init__(self, branch, tree=None, location=None, revision=None, remember=None,
                 force=None, uncommitted=None, ui_mode=True, parent=None):
        super(QBzrMergeWindow, self).__init__(name = self.NAME,
                                             ui_mode = ui_mode,
                                             parent = parent)
        self.branch = branch
        self.tree = tree
        self.ui = Ui_MergeForm()
        self.setupUi(self.ui)
        # and add the subprocess widgets.
        for w in self.make_default_layout_widgets():
            self.layout().addWidget(w)

        fill_pull_combo(self.ui.location, self.branch)
        if location:
            self.ui.location.setEditText(location)
        else:
            self.ui.location.setFocus()

        if remember:
            self.ui.remember.setCheckState(QtCore.Qt.Checked)
        if force:
            self.ui.force.setCheckState(QtCore.Qt.Checked)
        if uncommitted:
            self.ui.uncommitted.setCheckState(QtCore.Qt.Checked)
        if revision:
            self.ui.revision.setText(revision)

        # One directory picker for the pull location.
        hookup_directory_picker(self,
                                self.ui.location_picker,
                                self.ui.location,
                                DIRECTORYPICKER_SOURCE)

    def do_start(self):
        if self.tree:
            dest = self.tree.basedir
        else:
            dest = self.branch.base
        if dest == os.getcwd():
            args = []
        else:
            args = ['--directory', dest]
        if self.ui.remember.isChecked():
            args.append('--remember')
        if self.ui.force.isChecked():
            args.append('--force')
        if self.ui.uncommitted.isChecked():
            args.append('--uncommitted')
        rev = str(self.ui.revision.text()).strip()
        if rev:
            args.extend(['--revision', rev])
        location = str(self.ui.location.currentText())
        self.process_widget.do_start(None, 'merge', location, *args)
        save_pull_location(None, location)