1 """Widgets for Fetch, Push, and Pull"""
2 from __future__
import absolute_import
, division
, print_function
, unicode_literals
6 from qtpy
import QtWidgets
7 from qtpy
.QtCore
import Qt
10 from ..interaction
import Interaction
11 from ..qtutils
import connect_button
12 from ..qtutils
import get
13 from .. import gitcmds
15 from .. import qtutils
18 from . import standard
27 """Fetch from remote repositories"""
28 return run(context
, Fetch
)
32 """Push to remote repositories"""
33 return run(context
, Push
)
37 """Pull from remote repositories"""
38 return run(context
, Pull
)
41 def run(context
, RemoteDialog
):
42 """Launches fetch/push/pull dialogs."""
43 # Copy global stuff over to speedup startup
44 parent
= qtutils
.active_window()
45 view
= RemoteDialog(context
, parent
=parent
)
50 def combine(result
, prev
):
51 """Combine multiple (status, out, err) tuples into a combined tuple
53 The current state is passed in via `prev`.
54 The status code is a max() over all the subprocess status codes.
55 Individual (out, err) strings are sequentially concatenated together.
58 if isinstance(prev
, (tuple, list)):
60 raise AssertionError('combine() with length %d' % len(prev
))
62 max(prev
[0], result
[0]),
63 combine(prev
[1], result
[1]),
64 combine(prev
[2], result
[2]),
67 combined
= prev
+ '\n\n' + result
76 def uncheck(value
, *checkboxes
):
77 """Uncheck the specified checkboxes if value is True"""
79 for checkbox
in checkboxes
:
80 checkbox
.setChecked(False)
83 def strip_remotes(remote_branches
):
84 """Strip the <remote>/ prefixes from branches
86 e.g. "origin/main" becomes "main".
89 branches
= [utils
.strip_one(branch
) for branch
in remote_branches
]
90 return [branch
for branch
in branches
if branch
!= 'HEAD']
93 def get_default_remote(context
):
94 """Get the name of the default remote to use for pushing.
96 This will be the remote the branch is set to track, if it is set. If it
97 is not, remote.pushDefault will be used (or origin if not set)
100 upstream_remote
= gitcmds
.upstream_remote(context
)
101 return upstream_remote
or context
.cfg
.get('remote.pushDefault', default
='origin')
104 class ActionTask(qtutils
.Task
):
105 """Run actions asynchronously"""
107 def __init__(self
, model_action
, remote
, kwargs
):
108 qtutils
.Task
.__init
__(self
)
109 self
.model_action
= model_action
114 """Runs the model action and captures the result"""
115 return self
.model_action(self
.remote
, **self
.kwargs
)
118 class RemoteActionDialog(standard
.Dialog
):
119 """Interface for performing remote operations"""
121 def __init__(self
, context
, action
, title
, parent
=None, icon
=None):
122 """Customize the dialog based on the remote action"""
123 standard
.Dialog
.__init
__(self
, parent
=parent
)
124 self
.setWindowTitle(title
)
125 if parent
is not None:
126 self
.setWindowModality(Qt
.WindowModal
)
128 self
.context
= context
129 self
.model
= model
= context
.model
131 self
.filtered_remote_branches
= []
132 self
.selected_remotes
= []
134 self
.runtask
= qtutils
.RunTask(parent
=self
)
135 self
.progress
= standard
.progress(title
, N_('Updating'), self
)
137 self
.local_label
= QtWidgets
.QLabel()
138 self
.local_label
.setText(N_('Local Branch'))
140 self
.local_branch
= QtWidgets
.QLineEdit()
141 qtutils
.add_completer(self
.local_branch
, model
.local_branches
)
143 self
.local_branches
= QtWidgets
.QListWidget()
144 self
.local_branches
.addItems(model
.local_branches
)
146 self
.remote_label
= QtWidgets
.QLabel()
147 self
.remote_label
.setText(N_('Remote'))
149 self
.remote_name
= QtWidgets
.QLineEdit()
150 qtutils
.add_completer(self
.remote_name
, model
.remotes
)
151 # pylint: disable=no-member
152 self
.remote_name
.editingFinished
.connect(self
.remote_name_edited
)
153 self
.remote_name
.textEdited
.connect(lambda x
: self
.remote_name_edited())
155 self
.remotes
= QtWidgets
.QListWidget()
157 mode
= QtWidgets
.QAbstractItemView
.ExtendedSelection
158 self
.remotes
.setSelectionMode(mode
)
159 self
.remotes
.addItems(model
.remotes
)
161 self
.remote_branch_label
= QtWidgets
.QLabel()
162 self
.remote_branch_label
.setText(N_('Remote Branch'))
164 self
.remote_branch
= QtWidgets
.QLineEdit()
165 remote_branches
= strip_remotes(model
.remote_branches
)
166 qtutils
.add_completer(self
.remote_branch
, remote_branches
)
168 self
.remote_branches
= QtWidgets
.QListWidget()
169 self
.remote_branches
.addItems(model
.remote_branches
)
171 text
= N_('Prompt on creation')
172 tooltip
= N_('Prompt when pushing creates new remote branches')
173 self
.prompt_checkbox
= qtutils
.checkbox(
174 checked
=True, text
=text
, tooltip
=tooltip
177 text
= N_('Fast-forward only')
179 'Refuse to merge unless the current HEAD is already up-'
180 'to-date or the merge can be resolved as a fast-forward'
182 self
.ff_only_checkbox
= qtutils
.checkbox(
183 checked
=True, text
=text
, tooltip
=tooltip
186 text
= N_('No fast-forward')
188 'Create a merge commit even when the merge resolves as a fast-forward'
190 self
.no_ff_checkbox
= qtutils
.checkbox(
191 checked
=False, text
=text
, tooltip
=tooltip
195 'Allow non-fast-forward updates. Using "force" can '
196 'cause the remote repository to lose commits; '
199 self
.force_checkbox
= qtutils
.checkbox(
200 checked
=False, text
=text
, tooltip
=tooltip
203 self
.tags_checkbox
= qtutils
.checkbox(text
=N_('Include tags '))
206 'Remove remote-tracking branches that no longer exist on the remote'
208 self
.prune_checkbox
= qtutils
.checkbox(text
=N_('Prune '), tooltip
=tooltip
)
210 tooltip
= N_('Rebase the current branch instead of merging')
211 self
.rebase_checkbox
= qtutils
.checkbox(text
=N_('Rebase'), tooltip
=tooltip
)
213 text
= N_('Set upstream')
214 tooltip
= N_('Configure the remote branch as the the new upstream')
215 self
.upstream_checkbox
= qtutils
.checkbox(text
=text
, tooltip
=tooltip
)
217 self
.action_button
= qtutils
.ok_button(title
, icon
=icon
)
218 self
.close_button
= qtutils
.close_button()
220 self
.buttons
= utils
.Group(self
.action_button
, self
.close_button
)
222 self
.local_branch_layout
= qtutils
.hbox(
223 defs
.small_margin
, defs
.spacing
, self
.local_label
, self
.local_branch
226 self
.remote_layout
= qtutils
.hbox(
227 defs
.small_margin
, defs
.spacing
, self
.remote_label
, self
.remote_name
230 self
.remote_branch_layout
= qtutils
.hbox(
233 self
.remote_branch_label
,
237 self
.options_layout
= qtutils
.hbox(
241 self
.ff_only_checkbox
,
245 self
.rebase_checkbox
,
246 self
.upstream_checkbox
,
247 self
.prompt_checkbox
,
253 self
.remote_input_layout
= qtutils
.vbox(
254 defs
.no_margin
, defs
.spacing
, self
.remote_layout
, self
.remotes
257 self
.local_branch_input_layout
= qtutils
.vbox(
258 defs
.no_margin
, defs
.spacing
, self
.local_branch_layout
, self
.local_branches
261 self
.remote_branch_input_layout
= qtutils
.vbox(
264 self
.remote_branch_layout
,
265 self
.remote_branches
,
270 self
.remote_input_layout
,
271 self
.local_branch_input_layout
,
272 self
.remote_branch_input_layout
,
274 else: # fetch and pull
276 self
.remote_input_layout
,
277 self
.remote_branch_input_layout
,
278 self
.local_branch_input_layout
,
280 self
.top_layout
= qtutils
.hbox(defs
.no_margin
, defs
.spacing
, *widgets
)
282 self
.main_layout
= qtutils
.vbox(
283 defs
.margin
, defs
.spacing
, self
.top_layout
, self
.options_layout
285 self
.setLayout(self
.main_layout
)
287 default_remote
= get_default_remote(context
)
289 remotes
= model
.remotes
290 if default_remote
in remotes
:
291 idx
= remotes
.index(default_remote
)
292 if self
.select_remote(idx
):
293 self
.set_remote_name(default_remote
)
295 if self
.select_first_remote():
296 self
.set_remote_name(remotes
[0])
298 # Trim the remote list to just the default remote
299 self
.update_remotes()
300 self
.set_field_defaults()
302 # Setup signals and slots
303 # pylint: disable=no-member
304 self
.remotes
.itemSelectionChanged
.connect(self
.update_remotes
)
306 local
= self
.local_branches
307 local
.itemSelectionChanged
.connect(self
.update_local_branches
)
309 remote
= self
.remote_branches
310 remote
.itemSelectionChanged
.connect(self
.update_remote_branches
)
312 self
.no_ff_checkbox
.toggled
.connect(
313 lambda x
: uncheck(x
, self
.ff_only_checkbox
, self
.rebase_checkbox
)
316 self
.ff_only_checkbox
.toggled
.connect(
317 lambda x
: uncheck(x
, self
.no_ff_checkbox
, self
.rebase_checkbox
)
320 self
.rebase_checkbox
.toggled
.connect(
321 lambda x
: uncheck(x
, self
.no_ff_checkbox
, self
.ff_only_checkbox
)
324 connect_button(self
.action_button
, self
.action_callback
)
325 connect_button(self
.close_button
, self
.close
)
328 self
, N_('Close'), self
.close
, QtGui
.QKeySequence
.Close
, 'Esc'
331 self
.prune_checkbox
.hide()
335 self
.upstream_checkbox
.hide()
336 self
.prompt_checkbox
.hide()
339 # Fetch and Push-only options
340 self
.force_checkbox
.hide()
341 self
.tags_checkbox
.hide()
342 self
.local_label
.hide()
343 self
.local_branch
.hide()
344 self
.local_branches
.hide()
347 self
.rebase_checkbox
.hide()
348 self
.no_ff_checkbox
.hide()
349 self
.ff_only_checkbox
.hide()
351 self
.init_size(parent
=parent
)
353 def set_rebase(self
, value
):
354 """Check the rebase checkbox"""
355 self
.rebase_checkbox
.setChecked(value
)
357 def set_field_defaults(self
):
358 """Set sensible initial defaults"""
359 # Default to "git fetch origin main"
361 if action
in (FETCH
, PULL
):
362 self
.local_branch
.setText('')
363 self
.remote_branch
.setText('')
366 # Select the current branch by default for push
368 branch
= self
.model
.currentbranch
370 idx
= self
.model
.local_branches
.index(branch
)
373 if self
.select_local_branch(idx
):
374 self
.set_local_branch(branch
)
375 self
.set_remote_branch('')
377 def set_remote_name(self
, remote_name
):
378 """Set the remote name"""
379 self
.remote_name
.setText(remote_name
)
381 def set_local_branch(self
, branch
):
382 """Set the local branch name"""
383 self
.local_branch
.setText(branch
)
385 self
.local_branch
.selectAll()
387 def set_remote_branch(self
, branch
):
388 """Set the remote branch name"""
389 self
.remote_branch
.setText(branch
)
391 self
.remote_branch
.selectAll()
393 def set_remote_branches(self
, branches
):
394 """Set the list of remote branches"""
395 self
.remote_branches
.clear()
396 self
.remote_branches
.addItems(branches
)
397 self
.filtered_remote_branches
= branches
398 qtutils
.add_completer(self
.remote_branch
, strip_remotes(branches
))
400 def select_first_remote(self
):
401 """Select the first remote in the list view"""
402 return self
.select_remote(0)
404 def select_remote(self
, idx
):
405 """Select a remote by index"""
406 item
= self
.remotes
.item(idx
)
408 item
.setSelected(True)
409 self
.remotes
.setCurrentItem(item
)
410 self
.set_remote_name(item
.text())
416 def select_remote_by_name(self
, remote
):
417 """Select a remote by name"""
418 remotes
= self
.model
.remotes
419 if remote
in remotes
:
420 idx
= remotes
.index(remote
)
421 result
= self
.select_remote(idx
)
426 def set_selected_remotes(self
, remotes
):
427 """Set the list of selected remotes
429 Return True if all remotes were found and selected.
432 # Invalid remote names are ignored.
433 # This handles a remote going away between sessions.
434 # The selection is unchanged when none of the specified remotes exist.
436 for remote
in remotes
:
437 if remote
in self
.model
.remotes
:
441 # Only clear the selection if the specified remotes exist
442 self
.remotes
.clearSelection()
443 found
= all(self
.select_remote_by_name(x
) for x
in remotes
)
446 def select_local_branch(self
, idx
):
447 """Selects a local branch by index in the list view"""
448 item
= self
.local_branches
.item(idx
)
450 item
.setSelected(True)
451 self
.local_branches
.setCurrentItem(item
)
452 self
.local_branch
.setText(item
.text())
458 def display_remotes(self
, widget
):
459 """Display the available remotes in a listwidget"""
461 for remote_name
in self
.model
.remotes
:
462 url
= self
.model
.remote_url(remote_name
, self
.action
)
463 display
= '%s\t(%s)' % (remote_name
, N_('URL: %s') % url
)
464 displayed
.append(display
)
465 qtutils
.set_items(widget
, displayed
)
467 def update_remotes(self
):
468 """Update the remote name when a remote from the list is selected"""
469 widget
= self
.remotes
470 remotes
= self
.model
.remotes
471 selection
= qtutils
.selected_item(widget
, remotes
)
473 self
.selected_remotes
= []
475 self
.set_remote_name(selection
)
476 self
.selected_remotes
= qtutils
.selected_items(self
.remotes
, self
.model
.remotes
)
477 self
.set_remote_to(selection
, self
.selected_remotes
)
479 def set_remote_to(self
, _remote
, selected_remotes
):
480 context
= self
.context
481 all_branches
= gitcmds
.branch_list(context
, remote
=True)
484 for remote_name
in selected_remotes
:
485 patterns
.append(remote_name
+ '/*')
487 for branch
in all_branches
:
489 if fnmatch
.fnmatch(branch
, pat
):
490 branches
.append(branch
)
493 self
.set_remote_branches(branches
)
495 self
.set_remote_branches(all_branches
)
496 self
.set_remote_branch('')
498 def remote_name_edited(self
):
499 """Update the current remote when the remote name is typed manually"""
500 remote
= self
.remote_name
.text()
501 self
.set_remote_to(remote
, [remote
])
503 def update_local_branches(self
):
504 """Update the local/remote branch names when a branch is selected"""
505 branches
= self
.model
.local_branches
506 widget
= self
.local_branches
507 selection
= qtutils
.selected_item(widget
, branches
)
510 self
.set_local_branch(selection
)
511 self
.set_remote_branch(selection
)
513 def update_remote_branches(self
):
514 """Update the remote branch name when a branch is selected"""
515 widget
= self
.remote_branches
516 branches
= self
.filtered_remote_branches
517 selection
= qtutils
.selected_item(widget
, branches
)
520 branch
= utils
.strip_one(selection
)
523 self
.set_remote_branch(branch
)
525 def common_args(self
):
526 """Returns git arguments common to fetch/push/pull"""
527 remote_name
= self
.remote_name
.text()
528 local_branch
= self
.local_branch
.text()
529 remote_branch
= self
.remote_branch
.text()
531 ff_only
= get(self
.ff_only_checkbox
)
532 force
= get(self
.force_checkbox
)
533 no_ff
= get(self
.no_ff_checkbox
)
534 rebase
= get(self
.rebase_checkbox
)
535 set_upstream
= get(self
.upstream_checkbox
)
536 tags
= get(self
.tags_checkbox
)
537 prune
= get(self
.prune_checkbox
)
544 'local_branch': local_branch
,
547 'remote_branch': remote_branch
,
548 'set_upstream': set_upstream
,
556 def push_to_all(self
, _remote
, *args
, **kwargs
):
557 """Push to all selected remotes"""
558 selected_remotes
= self
.selected_remotes
560 for remote
in selected_remotes
:
561 result
= self
.model
.push(remote
, *args
, **kwargs
)
562 all_results
= combine(result
, all_results
)
565 def action_callback(self
):
566 """Perform the actual fetch/push/pull operation"""
569 model_action
= self
.model
.fetch
571 model_action
= self
.push_to_all
572 else: # if action == PULL:
573 model_action
= self
.model
.pull
575 remote_name
= self
.remote_name
.text()
577 errmsg
= N_('No repository selected.')
578 Interaction
.log(errmsg
)
580 remote
, kwargs
= self
.common_args()
581 self
.selected_remotes
= qtutils
.selected_items(self
.remotes
, self
.model
.remotes
)
583 # Check if we're about to create a new branch and warn.
584 remote_branch
= self
.remote_branch
.text()
585 local_branch
= self
.local_branch
.text()
587 if action
== PUSH
and not remote_branch
:
588 branch
= local_branch
589 candidate
= '%s/%s' % (remote
, branch
)
590 prompt
= get(self
.prompt_checkbox
)
592 if prompt
and candidate
not in self
.model
.remote_branches
:
594 args
= dict(branch
=branch
, remote
=remote
)
597 'Branch "%(branch)s" does not exist in "%(remote)s".\n'
598 'A new remote branch will be published.'
602 info_txt
= N_('Create a new remote branch?')
603 ok_text
= N_('Create Remote Branch')
604 if not Interaction
.confirm(
605 title
, msg
, info_txt
, ok_text
, icon
=icons
.cola()
609 if get(self
.force_checkbox
):
611 title
= N_('Force Fetch?')
612 msg
= N_('Non-fast-forward fetch overwrites local history!')
613 info_txt
= N_('Force fetching from %s?') % remote
614 ok_text
= N_('Force Fetch')
616 title
= N_('Force Push?')
618 'Non-fast-forward push overwrites published '
619 'history!\n(Did you pull first?)'
621 info_txt
= N_('Force push to %s?') % remote
622 ok_text
= N_('Force Push')
623 else: # pull: shouldn't happen since the controls are hidden
625 if not Interaction
.confirm(
626 title
, msg
, info_txt
, ok_text
, default
=False, icon
=icons
.discard()
630 # Disable the GUI by default
631 self
.buttons
.setEnabled(False)
633 # Use a thread to update in the background
634 task
= ActionTask(model_action
, remote
, kwargs
)
635 self
.runtask
.start(task
, progress
=self
.progress
, finish
=self
.action_completed
)
637 def action_completed(self
, task
):
638 """Grab the results of the action and finish up"""
639 status
, out
, err
= task
.result
640 self
.buttons
.setEnabled(True)
642 command
= 'git %s' % self
.action
.lower()
643 message
= Interaction
.format_command_status(command
, status
)
644 details
= Interaction
.format_out_err(out
, err
)
646 log_message
= message
648 log_message
+= '\n\n' + details
649 Interaction
.log(log_message
)
655 if self
.action
== PUSH
:
657 message
+= N_('Have you rebased/pulled lately?')
659 Interaction
.critical(self
.windowTitle(), message
=message
, details
=details
)
662 # Use distinct classes so that each saves its own set of preferences
663 class Fetch(RemoteActionDialog
):
664 """Fetch from remote repositories"""
666 def __init__(self
, context
, parent
=None):
667 super(Fetch
, self
).__init
__(
668 context
, FETCH
, N_('Fetch'), parent
=parent
, icon
=icons
.repo()
671 def export_state(self
):
672 """Export persistent settings"""
673 state
= RemoteActionDialog
.export_state(self
)
674 state
['tags'] = get(self
.tags_checkbox
)
675 state
['prune'] = get(self
.prune_checkbox
)
678 def apply_state(self
, state
):
679 """Apply persistent settings"""
680 result
= RemoteActionDialog
.apply_state(self
, state
)
681 tags
= bool(state
.get('tags', False))
682 self
.tags_checkbox
.setChecked(tags
)
683 prune
= bool(state
.get('prune', False))
684 self
.prune_checkbox
.setChecked(prune
)
688 class Push(RemoteActionDialog
):
689 """Push to remote repositories"""
691 def __init__(self
, context
, parent
=None):
692 super(Push
, self
).__init
__(
693 context
, PUSH
, N_('Push'), parent
=parent
, icon
=icons
.push()
696 def export_state(self
):
697 """Export persistent settings"""
698 state
= RemoteActionDialog
.export_state(self
)
699 state
['prompt'] = get(self
.prompt_checkbox
)
700 state
['tags'] = get(self
.tags_checkbox
)
703 def apply_state(self
, state
):
704 """Apply persistent settings"""
705 result
= RemoteActionDialog
.apply_state(self
, state
)
707 # Restore the "prompt on creation" checkbox
708 prompt
= bool(state
.get('prompt', True))
709 self
.prompt_checkbox
.setChecked(prompt
)
711 # Restore the "tags" checkbox
712 tags
= bool(state
.get('tags', False))
713 self
.tags_checkbox
.setChecked(tags
)
718 class Pull(RemoteActionDialog
):
719 """Pull from remote repositories"""
721 def __init__(self
, context
, parent
=None):
722 super(Pull
, self
).__init
__(
723 context
, PULL
, N_('Pull'), parent
=parent
, icon
=icons
.pull()
726 def apply_state(self
, state
):
727 """Apply persistent settings"""
728 result
= RemoteActionDialog
.apply_state(self
, state
)
729 # Rebase has the highest priority
730 rebase
= bool(state
.get('rebase', False))
731 ff_only
= not rebase
and bool(state
.get('ff_only', False))
732 no_ff
= not rebase
and not ff_only
and bool(state
.get('no_ff', False))
734 self
.rebase_checkbox
.setChecked(rebase
)
735 self
.no_ff_checkbox
.setChecked(no_ff
)
737 # Allow users coming from older versions that have rebase=False to
738 # pickup the new ff_only=True default by only setting ff_only False
739 # when it either exists in the config or when rebase=True.
740 if 'ff_only' in state
or rebase
:
741 self
.ff_only_checkbox
.setChecked(ff_only
)
745 def export_state(self
):
746 """Export persistent settings"""
747 state
= RemoteActionDialog
.export_state(self
)
749 state
['ff_only'] = get(self
.ff_only_checkbox
)
750 state
['no_ff'] = get(self
.no_ff_checkbox
)
751 state
['rebase'] = get(self
.rebase_checkbox
)