3 from PyQt4
.QtGui
import QDialog
6 from ugit
import qtutils
7 from ugit
.views
import CreateBranchView
8 from ugit
.qobserver
import QObserver
10 def create_new_branch(model
,parent
):
12 view
= CreateBranchView(parent
)
13 ctl
= CreateBranchController(model
, view
)
15 return view
.exec_() == QDialog
.Accepted
17 class CreateBranchController(QObserver
):
18 def init(self
, model
, view
):
19 self
.add_observables('revision', 'local_branch')
21 branch_list
= self
.item_changed
,
22 create_button
= self
.create_branch
,
23 local_radio
= self
.display_model
,
24 remote_radio
= self
.display_model
,
25 tag_radio
= self
.display_model
,
29 #+--------------------------------------------------------------------
31 def create_branch(self
):
32 '''This callback is called when the "Create Branch"
35 revision
= self
.model
.get_revision()
36 branch
= self
.model
.get_local_branch()
37 existing_branches
= self
.model
.get_local_branches()
39 if not branch
or not revision
:
41 self
.tr('Missing Data'),
42 self
.tr('Please provide both a branch'
43 + ' name and revision expression.' ))
47 if branch
in existing_branches
:
49 if self
.view
.no_update_radio
.isChecked():
50 msg
= self
.tr("Branch '%s' already exists.")
51 msg
= unicode(msg
) % branch
52 qtutils
.information( self
.tr('warning'), msg
)
55 # Whether we should prompt the user for lost commits
56 commits
= self
.model
.rev_list_range(revision
, branch
)
57 check_branch
= bool(commits
)
60 msg
= self
.tr("Resetting '%s' to '%s' will "
61 "lose the following commits:")
62 lines
= [ unicode(msg
) % (branch
, revision
) ]
64 for idx
, commit
in enumerate(commits
):
65 subject
= commit
[1][0:min(len(commit
[1]),16)]
66 if len(subject
) < len(commit
[1]):
68 lines
.append('\t' + commit
[0][:8]
71 skip
= len(commits
) - 5
72 lines
.append('\t(%d skipped)' % skip
)
76 unicode(self
.tr("Recovering lost commits may "
78 unicode(self
.tr("Reset '%s'?")) % branch
81 result
= qtutils
.question(self
.view
,
82 self
.tr('warning'), '\n'.join(lines
))
86 # TODO: Settings for git branch
87 track
= self
.view
.remote_radio
.isChecked()
88 fetch
= self
.view
.fetch_checkbox
.isChecked()
89 ffwd
= self
.view
.ffwd_only_radio
.isChecked()
90 reset
= self
.view
.reset_radio
.isChecked()
92 output
= self
.model
.create_branch(branch
, revision
, track
=track
)
93 qtutils
.show_output(output
)
96 def item_changed(self
, *rest
):
97 '''This callback is called when the item selection changes
98 in the branch_list.'''
100 qlist
= self
.view
.branch_list
101 ( row
, selected
) = qtutils
.get_selected_row(qlist
)
102 if not selected
: return
104 sources
= self
.__get
_branch
_sources
()
107 # Update the model with the selection
108 self
.model
.set_revision(rev
)
110 # Only set the branch name field if we're
111 # branching from a remote branch.
112 if not self
.view
.remote_radio
.isChecked():
115 branch
= utils
.basename(rev
)
116 if branch
== 'HEAD': return
118 self
.model
.set_local_branch(branch
)
120 ######################################################################
122 def display_model(self
):
123 branches
= self
.__get
_branch
_sources
()
124 qtutils
.set_items(self
.view
.branch_list
, branches
)
126 def __get_branch_sources(self
):
127 '''Get the list of items for populating the branch root list.'''
128 if self
.view
.local_radio
.isChecked():
129 return self
.model
.get_local_branches()
130 elif self
.view
.remote_radio
.isChecked():
131 return self
.model
.get_remote_branches()
132 elif self
.view
.tag_radio
.isChecked():
133 return self
.model
.get_tags()