3 from PyQt4
.QtGui
import QDialog
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
)
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
,
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 ######################################################################
45 def create_branch(self
):
46 '''This callback is called when the "Create Branch"
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.' ))
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
)
70 # Whether we should prompt the user for lost commits
71 commits
= self
.model
.rev_list_range(revision
, branch
)
72 check_branch
= bool(commits
)
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]):
82 lines
.append('\t' + commit
[0][:8]
85 skip
= len(commits
) - 5
86 lines
.append('\t(%d skipped)' % skip
)
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
))
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
)
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
()
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():
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()