1 from __future__
import division
, absolute_import
, unicode_literals
3 from qtpy
import QtWidgets
4 from qtpy
.QtCore
import Qt
7 from ..interaction
import Interaction
8 from ..qtutils
import get
11 from .. import qtutils
12 from . import completion
13 from . import standard
17 def local_merge(context
):
18 """Provides a dialog for merging branches"""
19 view
= Merge(context
, qtutils
.active_window())
25 class Merge(standard
.Dialog
):
26 """Provides a dialog for merging branches."""
28 def __init__(self
, context
, parent
=None, ref
=None):
29 standard
.Dialog
.__init
__(self
, parent
=parent
)
30 self
.context
= context
31 self
.cfg
= cfg
= context
.cfg
32 self
.model
= context
.model
33 if parent
is not None:
34 self
.setWindowModality(Qt
.WindowModal
)
37 self
.title_label
= QtWidgets
.QLabel()
38 self
.revision_label
= QtWidgets
.QLabel()
39 self
.revision_label
.setText(N_('Revision to Merge'))
41 self
.revision
= completion
.GitRefLineEdit(context
)
42 self
.revision
.setToolTip(N_('Revision to Merge'))
44 self
.revision
.set_value(ref
)
46 self
.radio_local
= qtutils
.radio(text
=N_('Local Branch'), checked
=True)
47 self
.radio_remote
= qtutils
.radio(text
=N_('Tracking Branch'))
48 self
.radio_tag
= qtutils
.radio(text
=N_('Tag'))
50 self
.revisions
= QtWidgets
.QListWidget()
51 self
.revisions
.setAlternatingRowColors(True)
53 self
.button_viz
= qtutils
.create_button(
54 text
=N_('Visualize'), icon
=icons
.visualize()
57 tooltip
= N_('Squash the merged commits into a single commit')
58 self
.checkbox_squash
= qtutils
.checkbox(text
=N_('Squash'), tooltip
=tooltip
)
61 'Always create a merge commit when enabled, '
62 'even when the merge is a fast-forward update'
64 self
.checkbox_noff
= qtutils
.checkbox(
65 text
=N_('No fast forward'), tooltip
=tooltip
, checked
=False
67 self
.checkbox_noff_state
= False
70 'Commit the merge if there are no conflicts. '
71 'Uncheck to leave the merge uncommitted'
73 self
.checkbox_commit
= qtutils
.checkbox(
74 text
=N_('Commit'), tooltip
=tooltip
, checked
=True
76 self
.checkbox_commit_state
= True
78 text
= N_('Create Signed Commit')
79 checked
= cfg
.get('cola.signcommits', False)
80 tooltip
= N_('GPG-sign the merge commit')
81 self
.checkbox_sign
= qtutils
.checkbox(
82 text
=text
, checked
=checked
, tooltip
=tooltip
84 self
.button_close
= qtutils
.close_button()
87 self
.button_merge
= qtutils
.create_button(
88 text
=N_('Merge'), icon
=icon
, default
=True
92 self
.revlayt
= qtutils
.hbox(
101 self
.radiolayt
= qtutils
.hbox(
109 self
.buttonlayt
= qtutils
.hbox(
113 self
.checkbox_squash
,
115 self
.checkbox_commit
,
122 self
.mainlayt
= qtutils
.vbox(
130 self
.setLayout(self
.mainlayt
)
132 # Signal/slot connections
133 # pylint: disable=no-member
134 self
.revision
.textChanged
.connect(self
.update_title
)
135 self
.revision
.enter
.connect(self
.merge_revision
)
136 self
.revisions
.itemSelectionChanged
.connect(self
.revision_selected
)
138 qtutils
.connect_released(self
.radio_local
, self
.update_revisions
)
139 qtutils
.connect_released(self
.radio_remote
, self
.update_revisions
)
140 qtutils
.connect_released(self
.radio_tag
, self
.update_revisions
)
141 qtutils
.connect_button(self
.button_merge
, self
.merge_revision
)
142 qtutils
.connect_button(self
.checkbox_squash
, self
.toggle_squash
)
143 qtutils
.connect_button(self
.button_viz
, self
.viz_revision
)
144 qtutils
.connect_button(self
.button_close
, self
.reject
)
147 self
.model
.updated
.connect(self
.update_all
)
150 self
.init_size(parent
=parent
)
151 self
.revision
.setFocus()
153 def update_all(self
):
154 """Set the branch name for the window title and label."""
156 self
.update_revisions()
158 def update_title(self
, _txt
=None):
159 branch
= self
.model
.currentbranch
160 revision
= self
.revision
.text()
162 txt
= N_('Merge "%(revision)s" into "%(branch)s"') % dict(
163 revision
=revision
, branch
=branch
166 txt
= N_('Merge into "%s"') % branch
167 self
.button_merge
.setEnabled(bool(revision
))
168 self
.title_label
.setText(txt
)
169 self
.setWindowTitle(txt
)
171 def toggle_squash(self
):
172 """Toggles the commit checkbox based on the squash checkbox."""
173 if get(self
.checkbox_squash
):
174 self
.checkbox_commit_state
= self
.checkbox_commit
.checkState()
175 self
.checkbox_commit
.setCheckState(Qt
.Unchecked
)
176 self
.checkbox_commit
.setDisabled(True)
177 self
.checkbox_noff_state
= self
.checkbox_noff
.checkState()
178 self
.checkbox_noff
.setCheckState(Qt
.Unchecked
)
179 self
.checkbox_noff
.setDisabled(True)
181 self
.checkbox_noff
.setDisabled(False)
182 oldstateff
= self
.checkbox_noff_state
183 self
.checkbox_noff
.setCheckState(oldstateff
)
184 self
.checkbox_commit
.setDisabled(False)
185 oldstate
= self
.checkbox_commit_state
186 self
.checkbox_commit
.setCheckState(oldstate
)
188 def update_revisions(self
):
189 """Update the revision list whenever a radio button is clicked"""
190 self
.revisions
.clear()
191 self
.revisions
.addItems(self
.current_revisions())
193 def revision_selected(self
):
194 """Update the revision field when a list item is selected"""
195 revlist
= self
.current_revisions()
196 widget
= self
.revisions
197 revision
= qtutils
.selected_item(widget
, revlist
)
198 if revision
is not None:
199 self
.revision
.setText(revision
)
201 def current_revisions(self
):
202 """Retrieve candidate items to merge"""
203 if get(self
.radio_local
):
204 return self
.model
.local_branches
205 if get(self
.radio_remote
):
206 return self
.model
.remote_branches
207 if get(self
.radio_tag
):
208 return self
.model
.tags
211 def viz_revision(self
):
212 """Launch a gitk-like viewer on the selection revision"""
213 revision
= self
.revision
.text()
215 Interaction
.information(
216 N_('No Revision Specified'), N_('You must specify a revision to view.')
219 cmds
.do(cmds
.VisualizeRevision
, self
.context
, revision
)
221 def merge_revision(self
):
222 """Merge the selected revision/branch"""
223 revision
= self
.revision
.text()
225 Interaction
.information(
226 N_('No Revision Specified'), N_('You must specify a revision to merge.')
230 noff
= get(self
.checkbox_noff
)
231 no_commit
= not get(self
.checkbox_commit
)
232 squash
= get(self
.checkbox_squash
)
233 sign
= get(self
.checkbox_sign
)
234 context
= self
.context
235 cmds
.do(cmds
.Merge
, context
, revision
, no_commit
, squash
, noff
, sign
)
238 def export_state(self
):
239 """Export persistent settings"""
240 state
= super(Merge
, self
).export_state()
241 state
['no-ff'] = get(self
.checkbox_noff
)
242 state
['sign'] = get(self
.checkbox_sign
)
243 state
['commit'] = get(self
.checkbox_commit
)
246 def apply_state(self
, state
):
247 """Apply persistent settings"""
248 result
= super(Merge
, self
).apply_state(state
)
249 self
.checkbox_noff
.setChecked(state
.get('no-ff', False))
250 self
.checkbox_sign
.setChecked(state
.get('sign', False))
251 self
.checkbox_commit
.setChecked(state
.get('commit', True))