Remove some unused data and small fixups
[ugit.git] / ugitlibs / createbranchcontroller.py
blob0fcfb5b444c481101502635562caab09b940cbf0
1 #!/usr/bin/env python
2 import os
3 from PyQt4.QtGui import QDialog
5 import utils
6 import qtutils
7 from qobserver import QObserver
8 from views import CreateBranchGUI
10 def create_new_branch(model,parent):
11 model = model.clone(init=False)
12 view = CreateBranchGUI(parent)
13 ctl = CreateBranchController(model,view)
14 view.show()
15 return view.exec_() == QDialog.Accepted
17 class CreateBranchController(QObserver):
18 def __init__(self, model, view):
19 QObserver.__init__(self, model, view)
21 self.model_to_view('revision', 'revisionLine')
22 self.model_to_view('local_branch', 'branchLine')
23 self.add_signals('textChanged(const QString&)',
24 view.revisionLine, view.branchLine)
26 self.add_signals('itemSelectionChanged()',
27 view.branchRootList)
28 self.add_signals('itemClicked(QListWidgetItem *)',
29 view.branchRootList)
30 self.add_signals('released()',
31 view.createBranchButton,
32 view.localBranchRadio, view.remoteBranchRadio,
33 view.tagRadio)
35 self.add_callbacks(
36 branchRootList = self.item_changed,
37 createBranchButton = self.create_branch,
38 localBranchRadio = self.__display_model,
39 remoteBranchRadio = self.__display_model,
40 tagRadio = self.__display_model,
43 self.__display_model()
45 ######################################################################
46 # Qt callbacks
48 def create_branch(self):
49 '''This callback is called when the "Create Branch"
50 button is called.'''
52 revision = self.model.get_revision()
53 branch = self.model.get_local_branch()
54 existing_branches = self.model.get_local_branches()
56 if not branch or not revision:
57 qtutils.information(self.view,
58 self.tr('Missing Data'),
59 self.tr('Please provide both a branch'
60 + ' name and revision expression.' ))
61 return
63 check_branch = False
64 if branch in existing_branches:
66 if self.view.noUpdateRadio.isChecked():
67 msg = self.tr("Branch '%s' already exists.")
68 msg = unicode(msg) % branch
69 qtutils.information(self.view,
70 self.tr('warning'), msg)
71 return
73 # Whether we should prompt the user for lost commits
74 commits = self.model.rev_list_range(revision, branch)
75 check_branch = bool(commits)
77 if check_branch:
78 msg = self.tr("Resetting '%s' to '%s' will lose the following commits:")
79 lines = [ unicode(msg) % (branch, revision) ]
81 for idx, commit in enumerate(commits):
82 subject = commit[1][0:min(len(commit[1]),16)]
83 if len(subject) < len(commit[1]):
84 subject += '...'
85 lines.append('\t' + commit[0][:8]
86 +'\t' + subject)
87 if idx >= 5:
88 skip = len(commits) - 5
89 lines.append('\t(%d skipped)' % skip)
90 break
92 lines.extend([
93 unicode(self.tr("Recovering lost commits may not be easy.")),
94 unicode(self.tr("Reset '%s'?")) % branch
97 result = qtutils.question(self.view,
98 self.tr('warning'), '\n'.join(lines))
100 if not result: return
102 # TODO: Settings for git branch
103 track = self.view.remoteBranchRadio.isChecked()
104 fetch = self.view.fetchCheckBox.isChecked()
105 ffwd = self.view.fastForwardUpdateRadio.isChecked()
106 reset = self.view.resetRadio.isChecked()
108 output = self.model.create_branch(branch, revision, track=track)
109 qtutils.show_output(self.view, output)
110 self.view.accept()
112 def item_changed(self, *rest):
113 '''This callback is called when the item selection changes
114 in the branchRootList.'''
116 qlist = self.view.branchRootList
117 ( row, selected ) = qtutils.get_selected_row(qlist)
118 if not selected: return
120 sources = self.__get_branch_sources()
121 rev = sources[row]
123 # Update the model with the selection
124 self.model.set_revision(rev)
126 # Only set the branch name field if we're
127 # branching from a remote branch.
128 if not self.view.remoteBranchRadio.isChecked():
129 return
131 branch = utils.basename(rev)
132 if branch == 'HEAD': return
134 self.model.set_local_branch(branch)
136 ######################################################################
138 def __display_model(self):
139 branches = self.__get_branch_sources()
140 qtutils.set_items(self.view.branchRootList, branches)
142 def __get_branch_sources(self):
143 '''Get the list of items for populating the branch root list.'''
144 if self.view.localBranchRadio.isChecked():
145 return self.model.get_local_branches()
146 elif self.view.remoteBranchRadio.isChecked():
147 return self.model.get_remote_branches()
148 elif self.view.tagRadio.isChecked():
149 return self.model.get_tags()