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 model
.init_branch_data()
40 self
.__display
_model
(model
)
42 ######################################################################
44 ######################################################################
46 def cb_create_branch(self
, model
):
47 '''This callback is called when the "Create Branch"
50 revision
= model
.get_revision()
51 branch
= model
.get_branch()
52 existing_branches
= cmds
.git_branch()
54 if not branch
or not revision
:
55 qtutils
.information(self
.view
,
57 ('Please provide both a branch name and '
58 + 'revision expression.' ))
62 if branch
in existing_branches
:
64 if self
.view
.noUpdateRadio
.isChecked():
65 qtutils
.information(self
.view
,
66 'Warning: Branch Already Exists...',
67 ('The "' + branch
+ '"'
68 + ' branch already exists and '
69 + '"Update Existing Branch?" = "No."'))
72 # Whether we should prompt the user for lost commits
73 commits
= cmds
.git_rev_list_range(revision
, branch
)
74 check_branch
= bool(commits
)
78 for commit
in commits
:
79 lines
.append(commit
[0][:8] +'\t'+ commit
[1])
81 lost_commits
= '\n\t'.join(lines
)
83 result
= qtutils
.question(self
.view
,
84 'Warning: Commits Will Be Lost...',
86 + branch
+ ' branch will lose the '
87 + 'following commits:\n\n\t'
88 + lost_commits
+ '\n\n'
89 + 'Continue anyways?'))
93 # TODO: Settings for git branch
94 track
= self
.view
.remoteBranchRadio
.isChecked()
95 fetch
= self
.view
.fetchCheckBox
.isChecked()
96 ffwd
= self
.view
.fastForwardUpdateRadio
.isChecked()
97 reset
= self
.view
.resetRadio
.isChecked()
99 output
= cmds
.git_create_branch(branch
, revision
, track
=track
)
100 qtutils
.show_command(self
.view
, output
)
103 def cb_item_changed(self
, model
):
104 '''This callback is called when the item selection changes
105 in the branchRootList.'''
107 qlist
= self
.view
.branchRootList
108 ( row
, selected
) = qtutils
.get_selected_row(qlist
)
109 if not selected
: return
111 sources
= self
.__get
_branch
_sources
(model
)
114 # Update the model with the selection
115 model
.set_revision(rev
)
117 # Only set the branch name field if we're
118 # branching from a remote branch.
119 # Assume that we only want to use
120 # the last part of the branch name so that "origin/master"
121 # becomes just "master." Avoid creating branches named HEAD.
122 if not self
.view
.remoteBranchRadio
.isChecked():
125 base_regex
= re
.compile('(.*?/)?([^/]+)$')
126 match
= base_regex
.match(rev
)
128 branch
= match
.group(2)
129 #branch = os.path.basename(rev)
130 if branch
== 'HEAD': return
131 model
.set_branch(branch
)
133 ######################################################################
134 # PRIVATE HELPER METHODS
135 ######################################################################
137 def __display_model(self
, model
):
138 '''Visualize the current state of the model.'''
139 branch_sources
= self
.__get
_branch
_sources
(model
)
140 self
.view
.branchRootList
.clear()
141 for branch_source
in branch_sources
:
142 self
.view
.branchRootList
.addItem(branch_source
)
144 def __get_branch_sources(self
, model
):
145 '''Get the list of items for populating the branch root list.'''
147 if self
.view
.localBranchRadio
.isChecked():
148 return model
.get_local_branches()
150 elif self
.view
.remoteBranchRadio
.isChecked():
151 return model
.get_remote_branches()
153 elif self
.view
.tagRadio
.isChecked():
154 return model
.get_tags()