6 from qobserver
import QObserver
8 class GitCreateBranchController (QObserver
):
9 def __init__ (self
, model
, view
):
10 QObserver
.__init
__ (self
, model
, view
)
12 self
.model_to_view (model
, 'revision', 'revisionLine')
13 self
.model_to_view (model
, 'branch', 'branchNameLine')
15 self
.add_signals ('textChanged (const QString&)',
19 self
.add_signals ('itemSelectionChanged()',
22 self
.add_signals ('released()',
23 view
.createBranchButton
,
24 view
.localBranchRadio
,
25 view
.remoteBranchRadio
,
28 self
.add_callbacks (model
, {
29 'branchRootList': self
.cb_item_changed
,
30 'createBranchButton': self
.cb_create_branch
,
32 lambda(m
): self
.__display
_model
(m
),
34 lambda(m
): self
.__display
_model
(m
),
36 lambda(m
): self
.__display
_model
(m
),
39 self
.__display
_model
(model
)
41 ######################################################################
43 ######################################################################
45 def cb_create_branch (self
, model
):
46 '''This callback is called when the "Create Branch"
49 revision
= model
.get_revision()
50 branch
= model
.get_branch()
51 existing_branches
= cmds
.git_branch()
53 if not branch
or not revision
:
54 qtutils
.information (self
.view
,
56 ('Please provide both a branch name and '
57 + 'revision expression.' ))
61 if branch
in existing_branches
:
63 if self
.view
.noUpdateRadio
.isChecked():
64 qtutils
.information (self
.view
,
65 'Warning: Branch Already Exists...',
66 ('The "' + branch
+ '"'
67 + ' branch already exists and '
68 + '"Update Existing Branch?" = "No."'))
71 # Whether we should prompt the user for lost commits
72 commits
= cmds
.git_rev_list_range (revision
, branch
)
73 check_branch
= bool (commits
)
77 for commit
in commits
:
78 lines
.append (commit
[0][:8] +'\t'+ commit
[1])
80 lost_commits
= '\n\t'.join (lines
)
82 result
= qtutils
.question (self
.view
,
83 'Warning: Commits Will Be Lost...',
85 + branch
+ ' branch will lose the '
86 + 'following commits:\n\n\t'
87 + lost_commits
+ '\n\n'
88 + 'Continue anyways?'))
92 # TODO: Settings for git branch
93 track
= self
.view
.remoteBranchRadio
.isChecked()
94 fetch
= self
.view
.fetchCheckBox
.isChecked()
95 ffwd
= self
.view
.fastForwardUpdateRadio
.isChecked()
96 reset
= self
.view
.resetRadio
.isChecked()
98 output
= cmds
.git_create_branch (branch
, revision
, track
=track
)
99 qtutils
.show_command (self
.view
, output
)
102 def cb_item_changed (self
, model
):
103 '''This callback is called when the item selection changes
104 in the branchRootList.'''
106 qlist
= self
.view
.branchRootList
107 ( row
, selected
) = qtutils
.get_selected_row (qlist
)
108 if not selected
: return
110 sources
= self
.__get
_branch
_sources
(model
)
113 # Update the model with the selection
114 model
.set_revision (rev
)
116 # Only set the branch name field if we're
117 # branching from a remote branch.
118 # Assume that we only want to use
119 # the last part of the branch name so that "origin/master"
120 # becomes just "master." Avoid creating branches named HEAD.
121 if not self
.view
.remoteBranchRadio
.isChecked():
124 base_regex
= re
.compile ('(.*?/)?([^/]+)$')
125 match
= base_regex
.match (rev
)
127 branch
= match
.group (2)
128 #branch = os.path.basename (rev)
129 if branch
== 'HEAD': return
130 model
.set_branch (branch
)
132 ######################################################################
133 # PRIVATE HELPER METHODS
134 ######################################################################
136 def __display_model (self
, model
):
137 '''Visualize the current state of the model.'''
138 branch_sources
= self
.__get
_branch
_sources
(model
)
139 self
.view
.branchRootList
.clear()
140 for branch_source
in branch_sources
:
141 self
.view
.branchRootList
.addItem (branch_source
)
143 def __get_branch_sources (self
, model
):
144 '''Get the list of items for populating the branch root list.'''
146 if self
.view
.localBranchRadio
.isChecked():
147 return model
.get_local_branches()
149 elif self
.view
.remoteBranchRadio
.isChecked():
150 return model
.get_remote_branches()
152 elif self
.view
.tagRadio
.isChecked():
153 return model
.get_tags()