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('revision', 'revisionLine')
13 self
.model_to_view('local_branch', 'branchLine')
14 self
.add_signals('textChanged(const QString&)',
15 view
.revisionLine
, view
.branchLine
)
17 self
.add_signals('itemSelectionChanged()', view
.branchRootList
)
18 self
.add_signals('released()',
19 view
.createBranchButton
,
20 view
.localBranchRadio
, view
.remoteBranchRadio
,
24 'branchRootList': self
.item_changed
,
25 'createBranchButton': self
.create_branch
,
26 'localBranchRadio': self
.__display
_model
,
27 'remoteBranchRadio': self
.__display
_model
,
28 'tagRadio': self
.__display
_model
,
31 model
.init_branch_data()
32 self
.__display
_model
()
34 ######################################################################
37 def create_branch(self
):
38 '''This callback is called when the "Create Branch"
41 revision
= self
.model
.get_revision()
42 branch
= self
.model
.get_local_branch()
43 existing_branches
= cmds
.git_branch()
45 if not branch
or not revision
:
46 qtutils
.information(self
.view
,
47 self
.tr('Missing Data'),
48 self
.tr('Please provide both a branch'
49 + ' name and revision expression.' ))
53 if branch
in existing_branches
:
55 if self
.view
.noUpdateRadio
.isChecked():
56 msg
= self
.tr("Branch '%s' already exists.")
57 msg
= unicode(msg
) % branch
58 qtutils
.information(self
.view
,
59 self
.tr('warning'), msg
)
62 # Whether we should prompt the user for lost commits
63 commits
= cmds
.git_rev_list_range(revision
, branch
)
64 check_branch
= bool(commits
)
67 msg
= self
.tr("Resetting '%s' to '%s' will lose the following commits:")
68 lines
= [ unicode(msg
) % (branch
, revision
) ]
70 for idx
, commit
in enumerate(commits
):
71 subject
= commit
[1][0:min(len(commit
[1]),16)]
72 if len(subject
) < len(commit
[1]):
74 lines
.append('\t' + commit
[0][:8]
77 skip
= len(commits
) - 5
78 lines
.append('\t(%d skipped)' % skip
)
82 unicode(self
.tr("Recovering lost commits may not be easy.")),
83 unicode(self
.tr("Reset '%s'?")) % branch
86 result
= qtutils
.question(self
.view
,
87 self
.tr('warning'), '\n'.join(lines
))
91 # TODO: Settings for git branch
92 track
= self
.view
.remoteBranchRadio
.isChecked()
93 fetch
= self
.view
.fetchCheckBox
.isChecked()
94 ffwd
= self
.view
.fastForwardUpdateRadio
.isChecked()
95 reset
= self
.view
.resetRadio
.isChecked()
97 output
= cmds
.git_create_branch(branch
, revision
, track
=track
)
98 qtutils
.show_command(self
.view
, output
)
101 def item_changed(self
):
102 '''This callback is called when the item selection changes
103 in the branchRootList.'''
105 qlist
= self
.view
.branchRootList
106 ( row
, selected
) = qtutils
.get_selected_row(qlist
)
107 if not selected
: return
109 sources
= self
.__get
_branch
_sources
()
112 # Update the model with the selection
113 self
.model
.set_revision(rev
)
115 # Only set the branch name field if we're
116 # branching from a remote branch.
117 if not self
.view
.remoteBranchRadio
.isChecked():
120 branch
= utils
.basename(rev
)
121 if branch
== 'HEAD': return
123 self
.model
.set_local_branch(branch
)
125 ######################################################################
127 def __display_model(self
):
128 '''Visualize the current state of the model.'''
129 branch_sources
= self
.__get
_branch
_sources
()
130 self
.view
.branchRootList
.clear()
131 for branch_source
in branch_sources
:
132 self
.view
.branchRootList
.addItem(branch_source
)
134 def __get_branch_sources(self
):
135 '''Get the list of items for populating the branch root list.'''
137 if self
.view
.localBranchRadio
.isChecked():
138 return self
.model
.get_local_branches()
140 elif self
.view
.remoteBranchRadio
.isChecked():
141 return self
.model
.get_remote_branches()
143 elif self
.view
.tagRadio
.isChecked():
144 return self
.model
.get_tags()