3 from PyQt4
.QtGui
import QDialog
7 from qobserver
import QObserver
8 from views
import CreateBranchGUI
10 def create_new_branch(model
,parent
):
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
)
20 self
.add_observables('revision', 'local_branch')
22 branch_list
= self
.item_changed
,
23 create_button
= self
.create_branch
,
24 local_radio
= self
.display_model
,
25 remote_radio
= self
.display_model
,
26 tag_radio
= self
.display_model
,
30 ######################################################################
33 def create_branch(self
):
34 '''This callback is called when the "Create Branch"
37 revision
= self
.model
.get_revision()
38 branch
= self
.model
.get_local_branch()
39 existing_branches
= self
.model
.get_local_branches()
41 if not branch
or not revision
:
42 qtutils
.information(self
.view
,
43 self
.tr('Missing Data'),
44 self
.tr('Please provide both a branch'
45 + ' name and revision expression.' ))
49 if branch
in existing_branches
:
51 if self
.view
.no_update_radio
.isChecked():
52 msg
= self
.tr("Branch '%s' already exists.")
53 msg
= unicode(msg
) % branch
54 qtutils
.information(self
.view
,
55 self
.tr('warning'), msg
)
58 # Whether we should prompt the user for lost commits
59 commits
= self
.model
.rev_list_range(revision
, branch
)
60 check_branch
= bool(commits
)
63 msg
= self
.tr("Resetting '%s' to '%s' will "
64 "lose the following commits:")
65 lines
= [ unicode(msg
) % (branch
, revision
) ]
67 for idx
, commit
in enumerate(commits
):
68 subject
= commit
[1][0:min(len(commit
[1]),16)]
69 if len(subject
) < len(commit
[1]):
71 lines
.append('\t' + commit
[0][:8]
74 skip
= len(commits
) - 5
75 lines
.append('\t(%d skipped)' % skip
)
79 unicode(self
.tr("Recovering lost commits may "
81 unicode(self
.tr("Reset '%s'?")) % branch
84 result
= qtutils
.question(self
.view
,
85 self
.tr('warning'), '\n'.join(lines
))
89 # TODO: Settings for git branch
90 track
= self
.view
.remote_radio
.isChecked()
91 fetch
= self
.view
.fetch_checkbox
.isChecked()
92 ffwd
= self
.view
.ffwd_only_radio
.isChecked()
93 reset
= self
.view
.reset_radio
.isChecked()
95 output
= self
.model
.create_branch(branch
, revision
, track
=track
)
96 qtutils
.show_output(output
)
99 def item_changed(self
, *rest
):
100 '''This callback is called when the item selection changes
101 in the branch_list.'''
103 qlist
= self
.view
.branch_list
104 ( row
, selected
) = qtutils
.get_selected_row(qlist
)
105 if not selected
: return
107 sources
= self
.__get
_branch
_sources
()
110 # Update the model with the selection
111 self
.model
.set_revision(rev
)
113 # Only set the branch name field if we're
114 # branching from a remote branch.
115 if not self
.view
.remote_radio
.isChecked():
118 branch
= utils
.basename(rev
)
119 if branch
== 'HEAD': return
121 self
.model
.set_local_branch(branch
)
123 ######################################################################
125 def display_model(self
):
126 branches
= self
.__get
_branch
_sources
()
127 qtutils
.set_items(self
.view
.branch_list
, branches
)
129 def __get_branch_sources(self
):
130 '''Get the list of items for populating the branch root list.'''
131 if self
.view
.local_radio
.isChecked():
132 return self
.model
.get_local_branches()
133 elif self
.view
.remote_radio
.isChecked():
134 return self
.model
.get_remote_branches()
135 elif self
.view
.tag_radio
.isChecked():
136 return self
.model
.get_tags()