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()',
28 self
.add_signals('itemClicked(QListWidgetItem *)',
30 self
.add_signals('released()',
31 view
.createBranchButton
,
32 view
.localBranchRadio
, view
.remoteBranchRadio
,
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 ######################################################################
48 def create_branch(self
):
49 '''This callback is called when the "Create Branch"
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.' ))
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
)
73 # Whether we should prompt the user for lost commits
74 commits
= self
.model
.rev_list_range(revision
, branch
)
75 check_branch
= bool(commits
)
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]):
85 lines
.append('\t' + commit
[0][:8]
88 skip
= len(commits
) - 5
89 lines
.append('\t(%d skipped)' % skip
)
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
)
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
()
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():
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()