Debian Codegraph @

lib/branch.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 #
3 # QBrz - Qt frontend to Breezy commands
4 # Copyright (C) 2007 Lukáš Lalinský <[email protected]om>
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 import os
21
22 from PyQt5 import QtCore
23
24 from breezy import osutils
25 from breezy.commands import get_cmd_object
26
27 from breezy.plugins.qbrz.lib.subprocess import SubProcessDialog
28 from breezy.plugins.qbrz.lib.ui_branch import Ui_BranchForm
29 from breezy.plugins.qbrz.lib.util import (
30 iter_saved_pull_locations,
31 save_pull_location,
32 fill_combo_with,
33 hookup_directory_picker,
34 DIRECTORYPICKER_SOURCE,
35 DIRECTORYPICKER_TARGET,
36 )
37
38
39 class QBzrBranchWindow(SubProcessDialog):
40
41 NAME = "branch"
42
43 def __init__(self, from_location, to_location=None, revision=None,
44 bind=False, parent_dir=None, ui_mode=True, parent=None):
45 super(QBzrBranchWindow, self).__init__(name = self.NAME,
46 ui_mode = ui_mode,
47 parent = parent)
48
49 # Unless instructed otherwise, use the current directory as
50 # the parent directory.
51 if parent_dir is None:
52 parent_dir = os.getcwd()
53 self.parent_dir = parent_dir
54
55 # Layout the form, adding the subprocess widgets
56 self.ui = Ui_BranchForm()
57 self.setupUi(self.ui)
58 self.cmd_branch_options = get_cmd_object('branch').options()
59 if 'bind' not in self.cmd_branch_options:
60 self.ui.bind.setVisible(False)
61 for w in self.make_default_layout_widgets():
62 self.layout().addWidget(w)
63
64 # Setup smart setting of fields as others are edited.
65 self.ui.from_location.editTextChanged['QString'].connect(self.from_changed)
66
67 # Initialise the fields
68 fill_combo_with(self.ui.from_location,
69 '',
70 iter_saved_pull_locations())
71 if from_location:
72 self.ui.from_location.setEditText(from_location)
73 if to_location:
74 self.ui.to_location.setEditText(to_location)
75 if bind:
76 self.ui.bind.setChecked(True)
77 if revision:
78 self.ui.revision.setText(revision)
79
80 # Hook up our directory pickers
81 hookup_directory_picker(self,
82 self.ui.from_picker,
83 self.ui.from_location,
84 DIRECTORYPICKER_SOURCE)
85 hookup_directory_picker(self,
86 self.ui.to_picker,
87 self.ui.to_location,
88 DIRECTORYPICKER_TARGET)
89
90 # Put the focus on the To location if From is already set
91 if from_location:
92 self.ui.to_location.setFocus()
93 else:
94 self.ui.from_location.setFocus()
95
96 def from_changed(self, from_location):
97 to_location = self._default_to_location(str(from_location))
98 if to_location is not None:
99 self.ui.to_location.setEditText(to_location)
100
101 def _default_to_location(self, from_location):
102 """Work out a good To location give a From location.
103
104 :return: the To location or None if unsure
105 """
106 # We want to avoid opening the from location here so
107 # we 'guess' the basename using some simple heuristics
108 from_location = from_location.replace('\\', '/').rstrip('/')
109 if from_location.find('/') >= 0:
110 basename = osutils.basename(from_location)
111 else:
112 # Handle 'directory services' like lp:
113 ds_sep = from_location.find(':')
114 if ds_sep >= 0:
115 basename = from_location[ds_sep + 1:]
116 else:
117 return None
118
119 # Calculate the To location and check it's not the same as the
120 # From location.
121 to_location = osutils.pathjoin(self.parent_dir, basename)
122 if to_location == from_location:
123 return None
124 else:
125 return to_location
126
127 def get_to_location(self):
128 """The path the branch was created in."""
129 # This is used by explorer to find the location to open on completion
130 return str(self.ui.to_location.currentText())
131
132 def do_start(self):
133 args = []
134 if self.ui.bind.isChecked():
135 args.append('--bind')
136 revision = str(self.ui.revision.text())
137 if revision:
138 args.append('--revision')
139 args.append(revision)
140 from_location = str(self.ui.from_location.currentText())
141 to_location = self.get_to_location()
142 if 'use-existing-dir' in self.cmd_branch_options:
143 # always use this options because it should be mostly harmless
144 args.append('--use-existing-dir')
145 self.process_widget.do_start(None, 'branch', from_location, to_location, *args)
146 save_pull_location(None, from_location)