Changed the create_new_branch method's signature to be consistent
[ugit.git] / ugitlibs / createbranchcontroller.py
blobf9022f11a0760d8e1f13c895e9f947853bc52e82
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()', view.branchRootList)
27 self.add_signals('released()',
28 view.createBranchButton,
29 view.localBranchRadio, view.remoteBranchRadio,
30 view.tagRadio)
32 self.add_callbacks({
33 'branchRootList': self.item_changed,
34 'createBranchButton': self.create_branch,
35 'localBranchRadio': self.__display_model,
36 'remoteBranchRadio': self.__display_model,
37 'tagRadio': self.__display_model,
40 self.__display_model()
42 ######################################################################
43 # Qt callbacks
45 def create_branch(self):
46 '''This callback is called when the "Create Branch"
47 button is called.'''
49 revision = self.model.get_revision()
50 branch = self.model.get_local_branch()
51 existing_branches = self.model.get_local_branches()
53 if not branch or not revision:
54 qtutils.information(self.view,
55 self.tr('Missing Data'),
56 self.tr('Please provide both a branch'
57 + ' name and revision expression.' ))
58 return
60 check_branch = False
61 if branch in existing_branches:
63 if self.view.noUpdateRadio.isChecked():
64 msg = self.tr("Branch '%s' already exists.")
65 msg = unicode(msg) % branch
66 qtutils.information(self.view,
67 self.tr('warning'), msg)
68 return
70 # Whether we should prompt the user for lost commits
71 commits = self.model.rev_list_range(revision, branch)
72 check_branch = bool(commits)
74 if check_branch:
75 msg = self.tr("Resetting '%s' to '%s' will lose the following commits:")
76 lines = [ unicode(msg) % (branch, revision) ]
78 for idx, commit in enumerate(commits):
79 subject = commit[1][0:min(len(commit[1]),16)]
80 if len(subject) < len(commit[1]):
81 subject += '...'
82 lines.append('\t' + commit[0][:8]
83 +'\t' + subject)
84 if idx >= 5:
85 skip = len(commits) - 5
86 lines.append('\t(%d skipped)' % skip)
87 break
89 lines.extend([
90 unicode(self.tr("Recovering lost commits may not be easy.")),
91 unicode(self.tr("Reset '%s'?")) % branch
94 result = qtutils.question(self.view,
95 self.tr('warning'), '\n'.join(lines))
97 if not result: return
99 # TODO: Settings for git branch
100 track = self.view.remoteBranchRadio.isChecked()
101 fetch = self.view.fetchCheckBox.isChecked()
102 ffwd = self.view.fastForwardUpdateRadio.isChecked()
103 reset = self.view.resetRadio.isChecked()
105 output = self.model.create_branch(branch, revision, track=track)
106 qtutils.show_output(self.view, output)
107 self.view.accept()
109 def item_changed(self):
110 '''This callback is called when the item selection changes
111 in the branchRootList.'''
113 qlist = self.view.branchRootList
114 ( row, selected ) = qtutils.get_selected_row(qlist)
115 if not selected: return
117 sources = self.__get_branch_sources()
118 rev = sources[row]
120 # Update the model with the selection
121 self.model.set_revision(rev)
123 # Only set the branch name field if we're
124 # branching from a remote branch.
125 if not self.view.remoteBranchRadio.isChecked():
126 return
128 branch = utils.basename(rev)
129 if branch == 'HEAD': return
131 self.model.set_local_branch(branch)
133 ######################################################################
135 def __display_model(self):
136 branches = self.__get_branch_sources()
137 qtutils.set_items(self.view.branchRootList, branches)
139 def __get_branch_sources(self):
140 '''Get the list of items for populating the branch root list.'''
142 if self.view.localBranchRadio.isChecked():
143 return self.model.get_local_branches()
145 elif self.view.remoteBranchRadio.isChecked():
146 return self.model.get_remote_branches()
148 elif self.view.tagRadio.isChecked():
149 return self.model.get_tags()