sequenceditor: ensure that tasks are stopped in all situations
[git-cola.git] / cola / cmds.py
blob5e754f16bf2d08bb6bd8696653248d35f2b62690
1 """Editor commands"""
2 # pylint: disable=too-many-lines
3 import os
4 import re
5 import sys
6 from fnmatch import fnmatch
7 from io import StringIO
9 try:
10 from send2trash import send2trash
11 except ImportError:
12 send2trash = None
14 from . import compat
15 from . import core
16 from . import gitcmds
17 from . import icons
18 from . import resources
19 from . import textwrap
20 from . import utils
21 from . import version
22 from .cmd import ContextCommand
23 from .git import STDOUT
24 from .git import MISSING_BLOB_OID
25 from .i18n import N_
26 from .interaction import Interaction
27 from .models import main
28 from .models import prefs
31 class UsageError(Exception):
32 """Exception class for usage errors."""
34 def __init__(self, title, message):
35 Exception.__init__(self, message)
36 self.title = title
37 self.msg = message
40 class EditModel(ContextCommand):
41 """Commands that mutate the main model diff data"""
43 UNDOABLE = True
45 def __init__(self, context):
46 """Common edit operations on the main model"""
47 super().__init__(context)
49 self.old_diff_text = self.model.diff_text
50 self.old_filename = self.model.filename
51 self.old_mode = self.model.mode
52 self.old_diff_type = self.model.diff_type
53 self.old_file_type = self.model.file_type
55 self.new_diff_text = self.old_diff_text
56 self.new_filename = self.old_filename
57 self.new_mode = self.old_mode
58 self.new_diff_type = self.old_diff_type
59 self.new_file_type = self.old_file_type
61 def do(self):
62 """Perform the operation."""
63 self.model.filename = self.new_filename
64 self.model.set_mode(self.new_mode)
65 self.model.set_diff_text(self.new_diff_text)
66 self.model.set_diff_type(self.new_diff_type)
67 self.model.set_file_type(self.new_file_type)
69 def undo(self):
70 """Undo the operation."""
71 self.model.filename = self.old_filename
72 self.model.set_mode(self.old_mode)
73 self.model.set_diff_text(self.old_diff_text)
74 self.model.set_diff_type(self.old_diff_type)
75 self.model.set_file_type(self.old_file_type)
78 class ConfirmAction(ContextCommand):
79 """Confirm an action before running it"""
81 def ok_to_run(self):
82 """Return True when the command is okay to run"""
83 return True
85 def confirm(self):
86 """Prompt for confirmation"""
87 return True
89 def action(self):
90 """Run the command and return (status, out, err)"""
91 return (-1, '', '')
93 def success(self):
94 """Callback run on success"""
95 return
97 def command(self):
98 """Command name, for error messages"""
99 return 'git'
101 def error_message(self):
102 """Command error message"""
103 return ''
105 def do(self):
106 """Prompt for confirmation before running a command"""
107 status = -1
108 out = err = ''
109 ok = self.ok_to_run() and self.confirm()
110 if ok:
111 status, out, err = self.action()
112 if status == 0:
113 self.success()
114 title = self.error_message()
115 cmd = self.command()
116 Interaction.command(title, cmd, status, out, err)
118 return ok, status, out, err
121 class AbortApplyPatch(ConfirmAction):
122 """Reset an in-progress "git am" patch application"""
124 def confirm(self):
125 title = N_('Abort Applying Patch...')
126 question = N_('Aborting applying the current patch?')
127 info = N_(
128 'Aborting a patch can cause uncommitted changes to be lost.\n'
129 'Recovering uncommitted changes is not possible.'
131 ok_txt = N_('Abort Applying Patch')
132 return Interaction.confirm(
133 title, question, info, ok_txt, default=False, icon=icons.undo()
136 def action(self):
137 status, out, err = gitcmds.abort_apply_patch(self.context)
138 self.model.update_file_merge_status()
139 return status, out, err
141 def success(self):
142 self.model.set_commitmsg('')
144 def error_message(self):
145 return N_('Error')
147 def command(self):
148 return 'git am --abort'
151 class AbortCherryPick(ConfirmAction):
152 """Reset an in-progress cherry-pick"""
154 def confirm(self):
155 title = N_('Abort Cherry-Pick...')
156 question = N_('Aborting the current cherry-pick?')
157 info = N_(
158 'Aborting a cherry-pick can cause uncommitted changes to be lost.\n'
159 'Recovering uncommitted changes is not possible.'
161 ok_txt = N_('Abort Cherry-Pick')
162 return Interaction.confirm(
163 title, question, info, ok_txt, default=False, icon=icons.undo()
166 def action(self):
167 status, out, err = gitcmds.abort_cherry_pick(self.context)
168 self.model.update_file_merge_status()
169 return status, out, err
171 def success(self):
172 self.model.set_commitmsg('')
174 def error_message(self):
175 return N_('Error')
177 def command(self):
178 return 'git cherry-pick --abort'
181 class AbortMerge(ConfirmAction):
182 """Reset an in-progress merge back to HEAD"""
184 def confirm(self):
185 title = N_('Abort Merge...')
186 question = N_('Aborting the current merge?')
187 info = N_(
188 'Aborting the current merge will cause '
189 '*ALL* uncommitted changes to be lost.\n'
190 'Recovering uncommitted changes is not possible.'
192 ok_txt = N_('Abort Merge')
193 return Interaction.confirm(
194 title, question, info, ok_txt, default=False, icon=icons.undo()
197 def action(self):
198 status, out, err = gitcmds.abort_merge(self.context)
199 self.model.update_file_merge_status()
200 return status, out, err
202 def success(self):
203 self.model.set_commitmsg('')
205 def error_message(self):
206 return N_('Error')
208 def command(self):
209 return 'git merge'
212 class AmendMode(EditModel):
213 """Try to amend a commit."""
215 UNDOABLE = True
216 LAST_MESSAGE = None
218 @staticmethod
219 def name():
220 return N_('Amend')
222 def __init__(self, context, amend=True):
223 super().__init__(context)
224 self.skip = False
225 self.amending = amend
226 self.old_commitmsg = self.model.commitmsg
227 self.old_mode = self.model.mode
229 if self.amending:
230 self.new_mode = self.model.mode_amend
231 self.new_commitmsg = gitcmds.prev_commitmsg(context)
232 AmendMode.LAST_MESSAGE = self.model.commitmsg
233 return
234 # else, amend unchecked, regular commit
235 self.new_mode = self.model.mode_none
236 self.new_diff_text = ''
237 self.new_commitmsg = self.model.commitmsg
238 # If we're going back into new-commit-mode then search the
239 # undo stack for a previous amend-commit-mode and grab the
240 # commit message at that point in time.
241 if AmendMode.LAST_MESSAGE is not None:
242 self.new_commitmsg = AmendMode.LAST_MESSAGE
243 AmendMode.LAST_MESSAGE = None
245 def do(self):
246 """Leave/enter amend mode."""
247 # Attempt to enter amend mode. Do not allow this when merging.
248 if self.amending:
249 if self.model.is_merging:
250 self.skip = True
251 self.model.set_mode(self.old_mode)
252 Interaction.information(
253 N_('Cannot Amend'),
255 'You are in the middle of a merge.\n'
256 'Cannot amend while merging.'
259 return
260 self.skip = False
261 super().do()
262 self.model.set_commitmsg(self.new_commitmsg)
263 self.model.update_file_status()
264 self.context.selection.reset(emit=True)
266 def undo(self):
267 if self.skip:
268 return
269 self.model.set_commitmsg(self.old_commitmsg)
270 super().undo()
271 self.model.update_file_status()
272 self.context.selection.reset(emit=True)
275 class AnnexAdd(ContextCommand):
276 """Add to Git Annex"""
278 def __init__(self, context):
279 super().__init__(context)
280 self.filename = self.selection.filename()
282 def do(self):
283 status, out, err = self.git.annex('add', self.filename)
284 Interaction.command(N_('Error'), 'git annex add', status, out, err)
285 self.model.update_status()
288 class AnnexInit(ContextCommand):
289 """Initialize Git Annex"""
291 def do(self):
292 status, out, err = self.git.annex('init')
293 Interaction.command(N_('Error'), 'git annex init', status, out, err)
294 self.model.cfg.reset()
295 self.model.emit_updated()
298 class LFSTrack(ContextCommand):
299 """Add a file to git lfs"""
301 def __init__(self, context):
302 super().__init__(context)
303 self.filename = self.selection.filename()
304 self.stage_cmd = Stage(context, [self.filename])
306 def do(self):
307 status, out, err = self.git.lfs('track', self.filename)
308 Interaction.command(N_('Error'), 'git lfs track', status, out, err)
309 if status == 0:
310 self.stage_cmd.do()
313 class LFSInstall(ContextCommand):
314 """Initialize git lfs"""
316 def do(self):
317 status, out, err = self.git.lfs('install')
318 Interaction.command(N_('Error'), 'git lfs install', status, out, err)
319 self.model.update_config(reset=True, emit=True)
322 class ApplyPatch(ContextCommand):
323 """Apply the specified patch to the worktree or index"""
325 def __init__(
326 self,
327 context,
328 patch,
329 encoding,
330 apply_to_worktree,
332 super().__init__(context)
333 self.patch = patch
334 self.encoding = encoding
335 self.apply_to_worktree = apply_to_worktree
337 def do(self):
338 context = self.context
340 tmp_file = utils.tmp_filename('apply', suffix='.patch')
341 try:
342 core.write(tmp_file, self.patch.as_text(), encoding=self.encoding)
343 if self.apply_to_worktree:
344 status, out, err = gitcmds.apply_diff_to_worktree(context, tmp_file)
345 else:
346 status, out, err = gitcmds.apply_diff(context, tmp_file)
347 finally:
348 core.unlink(tmp_file)
350 Interaction.log_status(status, out, err)
351 self.model.update_file_status(update_index=True)
354 class ApplyPatches(ContextCommand):
355 """Apply patches using the "git am" command"""
357 def __init__(self, context, patches):
358 super().__init__(context)
359 self.patches = patches
361 def do(self):
362 status, output, err = self.git.am('-3', *self.patches)
363 out = f'# git am -3 {core.list2cmdline(self.patches)}\n\n{output}'
364 Interaction.command(N_('Patch failed to apply'), 'git am -3', status, out, err)
365 # Display a diffstat
366 self.model.update_file_status()
368 patch_basenames = [os.path.basename(p) for p in self.patches]
369 if len(patch_basenames) > 25:
370 patch_basenames = patch_basenames[:25]
371 patch_basenames.append('...')
373 basenames = '\n'.join(patch_basenames)
374 if status == 0:
375 Interaction.information(
376 N_('Patch(es) Applied'),
377 (N_('%d patch(es) applied.') + '\n\n%s')
378 % (len(self.patches), basenames),
382 class ApplyPatchesContinue(ContextCommand):
383 """Run "git am --continue" to continue on the next patch in a "git am" session"""
385 def do(self):
386 status, out, err = self.git.am('--continue')
387 Interaction.command(
388 N_('Failed to commit and continue applying patches'),
389 'git am --continue',
390 status,
391 out,
392 err,
394 self.model.update_status()
395 return status, out, err
398 class ApplyPatchesSkip(ContextCommand):
399 """Run "git am --skip" to continue on the next patch in a "git am" session"""
401 def do(self):
402 status, out, err = self.git.am(skip=True)
403 Interaction.command(
404 N_('Failed to continue applying patches after skipping the current patch'),
405 'git am --skip',
406 status,
407 out,
408 err,
410 self.model.update_status()
411 return status, out, err
414 class Archive(ContextCommand):
415 """ "Export archives using the "git archive" command"""
417 def __init__(self, context, ref, fmt, prefix, filename):
418 super().__init__(context)
419 self.ref = ref
420 self.fmt = fmt
421 self.prefix = prefix
422 self.filename = filename
424 def do(self):
425 fp = core.xopen(self.filename, 'wb')
426 cmd = ['git', 'archive', '--format=' + self.fmt]
427 if self.fmt in ('tgz', 'tar.gz'):
428 cmd.append('-9')
429 if self.prefix:
430 cmd.append('--prefix=' + self.prefix)
431 cmd.append(self.ref)
432 proc = core.start_command(cmd, stdout=fp)
433 out, err = proc.communicate()
434 fp.close()
435 status = proc.returncode
436 Interaction.log_status(status, out or '', err or '')
439 class Checkout(EditModel):
440 """A command object for git-checkout.
442 The argv list is forwarded directly to git.
445 def __init__(self, context, argv, checkout_branch=False):
446 super().__init__(context)
447 self.argv = argv
448 self.checkout_branch = checkout_branch
449 self.new_diff_text = ''
450 self.new_diff_type = main.Types.TEXT
451 self.new_file_type = main.Types.TEXT
453 def do(self):
454 super().do()
455 status, out, err = self.git.checkout(*self.argv)
456 if self.checkout_branch:
457 self.model.update_status()
458 else:
459 self.model.update_file_status()
460 Interaction.command(N_('Error'), 'git checkout', status, out, err)
461 return status, out, err
464 class CheckoutTheirs(ConfirmAction):
465 """Checkout "their" version of a file when performing a merge"""
467 @staticmethod
468 def name():
469 return N_('Checkout files from their branch (MERGE_HEAD)')
471 def confirm(self):
472 title = self.name()
473 question = N_('Checkout files from their branch?')
474 info = N_(
475 'This operation will replace the selected unmerged files with content '
476 'from the branch being merged using "git checkout --theirs".\n'
477 '*ALL* uncommitted changes will be lost.\n'
478 'Recovering uncommitted changes is not possible.'
480 ok_txt = N_('Checkout Files')
481 return Interaction.confirm(
482 title, question, info, ok_txt, default=True, icon=icons.merge()
485 def action(self):
486 selection = self.selection.selection()
487 paths = selection.unmerged
488 if not paths:
489 return 0, '', ''
491 argv = ['--theirs', '--'] + paths
492 cmd = Checkout(self.context, argv)
493 return cmd.do()
495 def error_message(self):
496 return N_('Error')
498 def command(self):
499 return 'git checkout --theirs'
502 class CheckoutOurs(ConfirmAction):
503 """Checkout "our" version of a file when performing a merge"""
505 @staticmethod
506 def name():
507 return N_('Checkout files from our branch (HEAD)')
509 def confirm(self):
510 title = self.name()
511 question = N_('Checkout files from our branch?')
512 info = N_(
513 'This operation will replace the selected unmerged files with content '
514 'from your current branch using "git checkout --ours".\n'
515 '*ALL* uncommitted changes will be lost.\n'
516 'Recovering uncommitted changes is not possible.'
518 ok_txt = N_('Checkout Files')
519 return Interaction.confirm(
520 title, question, info, ok_txt, default=True, icon=icons.merge()
523 def action(self):
524 selection = self.selection.selection()
525 paths = selection.unmerged
526 if not paths:
527 return 0, '', ''
529 argv = ['--ours', '--'] + paths
530 cmd = Checkout(self.context, argv)
531 return cmd.do()
533 def error_message(self):
534 return N_('Error')
536 def command(self):
537 return 'git checkout --ours'
540 class BlamePaths(ContextCommand):
541 """Blame view for paths."""
543 @staticmethod
544 def name():
545 return N_('Blame...')
547 def __init__(self, context, paths=None):
548 super().__init__(context)
549 if not paths:
550 paths = context.selection.union()
551 viewer = utils.shell_split(prefs.blame_viewer(context))
552 self.argv = viewer + list(paths)
554 def do(self):
555 try:
556 core.fork(self.argv)
557 except OSError as e:
558 _, details = utils.format_exception(e)
559 title = N_('Error Launching Blame Viewer')
560 msg = N_('Cannot exec "%s": please configure a blame viewer') % ' '.join(
561 self.argv
563 Interaction.critical(title, message=msg, details=details)
566 class CheckoutBranch(Checkout):
567 """Checkout a branch."""
569 def __init__(self, context, branch):
570 args = [branch]
571 super().__init__(context, args, checkout_branch=True)
574 class CherryPick(ContextCommand):
575 """Cherry pick commits into the current branch."""
577 def __init__(self, context, commits):
578 super().__init__(context)
579 self.commits = commits
581 def do(self):
582 status, out, err = gitcmds.cherry_pick(self.context, self.commits)
583 self.model.update_file_merge_status()
584 title = N_('Cherry-pick failed')
585 Interaction.command(title, 'git cherry-pick', status, out, err)
588 class Revert(ContextCommand):
589 """Revert a commit"""
591 def __init__(self, context, oid):
592 super().__init__(context)
593 self.oid = oid
595 def do(self):
596 status, out, err = self.git.revert(self.oid, no_edit=True)
597 self.model.update_file_status()
598 title = N_('Revert failed')
599 out = '# git revert %s\n\n' % self.oid
600 Interaction.command(title, 'git revert', status, out, err)
603 class ResetMode(EditModel):
604 """Reset the mode and clear the model's diff text."""
606 def __init__(self, context):
607 super().__init__(context)
608 self.new_mode = self.model.mode_none
609 self.new_diff_text = ''
610 self.new_diff_type = main.Types.TEXT
611 self.new_file_type = main.Types.TEXT
612 self.new_filename = ''
614 def do(self):
615 super().do()
616 self.model.update_file_status()
617 self.context.selection.reset(emit=True)
620 class ResetCommand(ConfirmAction):
621 """Reset state using the "git reset" command"""
623 def __init__(self, context, ref):
624 super().__init__(context)
625 self.ref = ref
627 def action(self):
628 return self.reset()
630 def command(self):
631 return 'git reset'
633 def error_message(self):
634 return N_('Error')
636 def success(self):
637 self.model.update_file_status()
639 def confirm(self):
640 raise NotImplementedError('confirm() must be overridden')
642 def reset(self):
643 raise NotImplementedError('reset() must be overridden')
646 class ResetMixed(ResetCommand):
647 @staticmethod
648 def tooltip(ref):
649 tooltip = N_('The branch will be reset using "git reset --mixed %s"')
650 return tooltip % ref
652 def confirm(self):
653 title = N_('Reset Branch and Stage (Mixed)')
654 question = N_('Point the current branch head to a new commit?')
655 info = self.tooltip(self.ref)
656 ok_text = N_('Reset Branch')
657 return Interaction.confirm(title, question, info, ok_text)
659 def reset(self):
660 return self.git.reset(self.ref, '--', mixed=True)
663 class ResetKeep(ResetCommand):
664 @staticmethod
665 def tooltip(ref):
666 tooltip = N_('The repository will be reset using "git reset --keep %s"')
667 return tooltip % ref
669 def confirm(self):
670 title = N_('Restore Worktree and Reset All (Keep Unstaged Changes)')
671 question = N_('Restore worktree, reset, and preserve unstaged edits?')
672 info = self.tooltip(self.ref)
673 ok_text = N_('Reset and Restore')
674 return Interaction.confirm(title, question, info, ok_text)
676 def reset(self):
677 return self.git.reset(self.ref, '--', keep=True)
680 class ResetMerge(ResetCommand):
681 @staticmethod
682 def tooltip(ref):
683 tooltip = N_('The repository will be reset using "git reset --merge %s"')
684 return tooltip % ref
686 def confirm(self):
687 title = N_('Restore Worktree and Reset All (Merge)')
688 question = N_('Reset Worktree and Reset All?')
689 info = self.tooltip(self.ref)
690 ok_text = N_('Reset and Restore')
691 return Interaction.confirm(title, question, info, ok_text)
693 def reset(self):
694 return self.git.reset(self.ref, '--', merge=True)
697 class ResetSoft(ResetCommand):
698 @staticmethod
699 def tooltip(ref):
700 tooltip = N_('The branch will be reset using "git reset --soft %s"')
701 return tooltip % ref
703 def confirm(self):
704 title = N_('Reset Branch (Soft)')
705 question = N_('Reset branch?')
706 info = self.tooltip(self.ref)
707 ok_text = N_('Reset Branch')
708 return Interaction.confirm(title, question, info, ok_text)
710 def reset(self):
711 return self.git.reset(self.ref, '--', soft=True)
714 class ResetHard(ResetCommand):
715 @staticmethod
716 def tooltip(ref):
717 tooltip = N_('The repository will be reset using "git reset --hard %s"')
718 return tooltip % ref
720 def confirm(self):
721 title = N_('Restore Worktree and Reset All (Hard)')
722 question = N_('Restore Worktree and Reset All?')
723 info = self.tooltip(self.ref)
724 ok_text = N_('Reset and Restore')
725 return Interaction.confirm(title, question, info, ok_text)
727 def reset(self):
728 return self.git.reset(self.ref, '--', hard=True)
731 class RestoreWorktree(ConfirmAction):
732 """Reset the worktree using the "git read-tree" command"""
734 @staticmethod
735 def tooltip(ref):
736 tooltip = N_(
737 'The worktree will be restored using "git read-tree --reset -u %s"'
739 return tooltip % ref
741 def __init__(self, context, ref):
742 super().__init__(context)
743 self.ref = ref
745 def action(self):
746 return self.git.read_tree(self.ref, reset=True, u=True)
748 def command(self):
749 return 'git read-tree --reset -u %s' % self.ref
751 def error_message(self):
752 return N_('Error')
754 def success(self):
755 self.model.update_file_status()
757 def confirm(self):
758 title = N_('Restore Worktree')
759 question = N_('Restore Worktree to %s?') % self.ref
760 info = self.tooltip(self.ref)
761 ok_text = N_('Restore Worktree')
762 return Interaction.confirm(title, question, info, ok_text)
765 class UndoLastCommit(ResetCommand):
766 """Undo the last commit"""
768 # NOTE: this is the similar to ResetSoft() with an additional check for
769 # published commits and different messages.
770 def __init__(self, context):
771 super().__init__(context, 'HEAD^')
773 def confirm(self):
774 check_published = prefs.check_published_commits(self.context)
775 if check_published and self.model.is_commit_published():
776 return Interaction.confirm(
777 N_('Rewrite Published Commit?'),
779 'This commit has already been published.\n'
780 'This operation will rewrite published history.\n'
781 "You probably don't want to do this."
783 N_('Undo the published commit?'),
784 N_('Undo Last Commit'),
785 default=False,
786 icon=icons.save(),
789 title = N_('Undo Last Commit')
790 question = N_('Undo last commit?')
791 info = N_('The branch will be reset using "git reset --soft %s"')
792 ok_text = N_('Undo Last Commit')
793 info_text = info % self.ref
794 return Interaction.confirm(title, question, info_text, ok_text)
796 def reset(self):
797 return self.git.reset('HEAD^', '--', soft=True)
800 class Commit(ResetMode):
801 """Attempt to create a new commit."""
803 def __init__(self, context, amend, msg, sign, no_verify=False):
804 super().__init__(context)
805 self.amend = amend
806 self.msg = msg
807 self.sign = sign
808 self.no_verify = no_verify
809 self.old_commitmsg = self.model.commitmsg
810 self.new_commitmsg = ''
812 def do(self):
813 # Create the commit message file
814 context = self.context
815 msg = self.msg
816 tmp_file = utils.tmp_filename('commit-message')
817 try:
818 core.write(tmp_file, msg)
819 # Run 'git commit'
820 status, out, err = self.git.commit(
821 F=tmp_file,
822 v=True,
823 gpg_sign=self.sign,
824 amend=self.amend,
825 no_verify=self.no_verify,
827 finally:
828 core.unlink(tmp_file)
829 if status == 0:
830 super().do()
831 if context.cfg.get(prefs.AUTOTEMPLATE):
832 template_loader = LoadCommitMessageFromTemplate(context)
833 template_loader.do()
834 else:
835 self.model.set_commitmsg(self.new_commitmsg)
837 return status, out, err
839 @staticmethod
840 def strip_comments(msg, comment_char='#'):
841 # Strip off comments
842 message_lines = [
843 line for line in msg.split('\n') if not line.startswith(comment_char)
845 msg = '\n'.join(message_lines)
846 if not msg.endswith('\n'):
847 msg += '\n'
849 return msg
852 class CycleReferenceSort(ContextCommand):
853 """Choose the next reference sort type"""
855 def do(self):
856 self.model.cycle_ref_sort()
859 class Ignore(ContextCommand):
860 """Add files to an exclusion file"""
862 def __init__(self, context, filenames, local=False):
863 super().__init__(context)
864 self.filenames = list(filenames)
865 self.local = local
867 def do(self):
868 if not self.filenames:
869 return
870 new_additions = '\n'.join(self.filenames) + '\n'
871 for_status = new_additions
872 if self.local:
873 filename = os.path.join('.git', 'info', 'exclude')
874 else:
875 filename = '.gitignore'
876 if core.exists(filename):
877 current_list = core.read(filename)
878 new_additions = current_list.rstrip() + '\n' + new_additions
879 core.write(filename, new_additions)
880 Interaction.log_status(0, f'Added to {filename}:\n{for_status}', '')
881 self.model.update_file_status()
884 def file_summary(files):
885 txt = core.list2cmdline(files)
886 if len(txt) > 768:
887 txt = txt[:768].rstrip() + '...'
888 wrap = textwrap.TextWrapper()
889 return '\n'.join(wrap.wrap(txt))
892 class RemoteCommand(ConfirmAction):
893 def __init__(self, context, remote):
894 super().__init__(context)
895 self.remote = remote
897 def success(self):
898 self.cfg.reset()
899 self.model.update_remotes()
902 class RemoteAdd(RemoteCommand):
903 def __init__(self, context, remote, url):
904 super().__init__(context, remote)
905 self.url = url
907 def action(self):
908 return self.git.remote('add', self.remote, self.url)
910 def error_message(self):
911 return N_('Error creating remote "%s"') % self.remote
913 def command(self):
914 return f'git remote add "{self.remote}" "{self.url}"'
917 class RemoteRemove(RemoteCommand):
918 def confirm(self):
919 title = N_('Delete Remote')
920 question = N_('Delete remote?')
921 info = N_('Delete remote "%s"') % self.remote
922 ok_text = N_('Delete')
923 return Interaction.confirm(title, question, info, ok_text)
925 def action(self):
926 return self.git.remote('rm', self.remote)
928 def error_message(self):
929 return N_('Error deleting remote "%s"') % self.remote
931 def command(self):
932 return 'git remote rm "%s"' % self.remote
935 class RemoteRename(RemoteCommand):
936 def __init__(self, context, remote, new_name):
937 super().__init__(context, remote)
938 self.new_name = new_name
940 def confirm(self):
941 title = N_('Rename Remote')
942 text = N_('Rename remote "%(current)s" to "%(new)s"?') % {
943 'current': self.remote,
944 'new': self.new_name,
946 info_text = ''
947 ok_text = title
948 return Interaction.confirm(title, text, info_text, ok_text)
950 def action(self):
951 return self.git.remote('rename', self.remote, self.new_name)
953 def error_message(self):
954 return N_('Error renaming "%(name)s" to "%(new_name)s"') % {
955 'name': self.remote,
956 'new_name': self.new_name,
959 def command(self):
960 return f'git remote rename "{self.remote}" "{self.new_name}"'
963 class RemoteSetURL(RemoteCommand):
964 def __init__(self, context, remote, url):
965 super().__init__(context, remote)
966 self.url = url
968 def action(self):
969 return self.git.remote('set-url', self.remote, self.url)
971 def error_message(self):
972 return N_('Unable to set URL for "%(name)s" to "%(url)s"') % {
973 'name': self.remote,
974 'url': self.url,
977 def command(self):
978 return f'git remote set-url "{self.remote}" "{self.url}"'
981 class RemoteEdit(ContextCommand):
982 """Combine RemoteRename and RemoteSetURL"""
984 def __init__(self, context, old_name, remote, url):
985 super().__init__(context)
986 self.rename = RemoteRename(context, old_name, remote)
987 self.set_url = RemoteSetURL(context, remote, url)
989 def do(self):
990 result = self.rename.do()
991 name_ok = result[0]
992 url_ok = False
993 if name_ok:
994 result = self.set_url.do()
995 url_ok = result[0]
996 return name_ok, url_ok
999 class RemoveFromSettings(ConfirmAction):
1000 def __init__(self, context, repo, entry, icon=None):
1001 super().__init__(context)
1002 self.context = context
1003 self.repo = repo
1004 self.entry = entry
1005 self.icon = icon
1007 def success(self):
1008 self.context.settings.save()
1011 class RemoveBookmark(RemoveFromSettings):
1012 def confirm(self):
1013 entry = self.entry
1014 title = msg = N_('Delete Bookmark?')
1015 info = N_('%s will be removed from your bookmarks.') % entry
1016 ok_text = N_('Delete Bookmark')
1017 return Interaction.confirm(title, msg, info, ok_text, icon=self.icon)
1019 def action(self):
1020 self.context.settings.remove_bookmark(self.repo, self.entry)
1021 return (0, '', '')
1024 class RemoveRecent(RemoveFromSettings):
1025 def confirm(self):
1026 repo = self.repo
1027 title = msg = N_('Remove %s from the recent list?') % repo
1028 info = N_('%s will be removed from your recent repositories.') % repo
1029 ok_text = N_('Remove')
1030 return Interaction.confirm(title, msg, info, ok_text, icon=self.icon)
1032 def action(self):
1033 self.context.settings.remove_recent(self.repo)
1034 return (0, '', '')
1037 class RemoveFiles(ContextCommand):
1038 """Removes files"""
1040 def __init__(self, context, remover, filenames):
1041 super().__init__(context)
1042 if remover is None:
1043 remover = os.remove
1044 self.remover = remover
1045 self.filenames = filenames
1046 # We could git-hash-object stuff and provide undo-ability
1047 # as an option. Heh.
1049 def do(self):
1050 files = self.filenames
1051 if not files:
1052 return
1054 rescan = False
1055 bad_filenames = []
1056 remove = self.remover
1057 for filename in files:
1058 if filename:
1059 try:
1060 remove(filename)
1061 rescan = True
1062 except OSError:
1063 bad_filenames.append(filename)
1065 if bad_filenames:
1066 Interaction.information(
1067 N_('Error'), N_('Deleting "%s" failed') % file_summary(bad_filenames)
1070 if rescan:
1071 self.model.update_file_status()
1074 class Delete(RemoveFiles):
1075 """Delete files."""
1077 def __init__(self, context, filenames):
1078 super().__init__(context, os.remove, filenames)
1080 def do(self):
1081 files = self.filenames
1082 if not files:
1083 return
1085 title = N_('Delete Files?')
1086 msg = N_('The following files will be deleted:') + '\n\n'
1087 msg += file_summary(files)
1088 info_txt = N_('Delete %d file(s)?') % len(files)
1089 ok_txt = N_('Delete Files')
1091 if Interaction.confirm(
1092 title, msg, info_txt, ok_txt, default=True, icon=icons.remove()
1094 super().do()
1097 class MoveToTrash(RemoveFiles):
1098 """Move files to the trash using send2trash"""
1100 AVAILABLE = send2trash is not None
1102 def __init__(self, context, filenames):
1103 super().__init__(context, send2trash, filenames)
1106 class DeleteBranch(ConfirmAction):
1107 """Delete a git branch."""
1109 def __init__(self, context, branch):
1110 super().__init__(context)
1111 self.branch = branch
1113 def confirm(self):
1114 title = N_('Delete Branch')
1115 question = N_('Delete branch "%s"?') % self.branch
1116 info = N_('The branch will be no longer available.')
1117 ok_txt = N_('Delete Branch')
1118 return Interaction.confirm(
1119 title, question, info, ok_txt, default=True, icon=icons.discard()
1122 def action(self):
1123 return self.model.delete_branch(self.branch)
1125 def error_message(self):
1126 return N_('Error deleting branch "%s"' % self.branch)
1128 def command(self):
1129 command = 'git branch -D %s'
1130 return command % self.branch
1133 class Rename(ContextCommand):
1134 """Rename a set of paths."""
1136 def __init__(self, context, paths):
1137 super().__init__(context)
1138 self.paths = paths
1140 def do(self):
1141 msg = N_('Untracking: %s') % (', '.join(self.paths))
1142 Interaction.log(msg)
1144 for path in self.paths:
1145 ok = self.rename(path)
1146 if not ok:
1147 return
1149 self.model.update_status()
1151 def rename(self, path):
1152 git = self.git
1153 title = N_('Rename "%s"') % path
1155 if os.path.isdir(path):
1156 base_path = os.path.dirname(path)
1157 else:
1158 base_path = path
1159 new_path = Interaction.save_as(base_path, title)
1160 if not new_path:
1161 return False
1163 status, out, err = git.mv(path, new_path, force=True, verbose=True)
1164 Interaction.command(N_('Error'), 'git mv', status, out, err)
1165 return status == 0
1168 class RenameBranch(ContextCommand):
1169 """Rename a git branch."""
1171 def __init__(self, context, branch, new_branch):
1172 super().__init__(context)
1173 self.branch = branch
1174 self.new_branch = new_branch
1176 def do(self):
1177 branch = self.branch
1178 new_branch = self.new_branch
1179 status, out, err = self.model.rename_branch(branch, new_branch)
1180 Interaction.log_status(status, out, err)
1183 class DeleteRemoteBranch(DeleteBranch):
1184 """Delete a remote git branch."""
1186 def __init__(self, context, remote, branch):
1187 super().__init__(context, branch)
1188 self.remote = remote
1190 def action(self):
1191 return self.git.push(self.remote, self.branch, delete=True)
1193 def success(self):
1194 self.model.update_status()
1195 Interaction.information(
1196 N_('Remote Branch Deleted'),
1197 N_('"%(branch)s" has been deleted from "%(remote)s".')
1199 'branch': self.branch,
1200 'remote': self.remote,
1204 def error_message(self):
1205 return N_('Error Deleting Remote Branch')
1207 def command(self):
1208 command = 'git push --delete %s %s'
1209 return command % (self.remote, self.branch)
1212 def get_mode(context, filename, staged, modified, unmerged, untracked):
1213 model = context.model
1214 if staged:
1215 mode = model.mode_index
1216 elif modified or unmerged:
1217 mode = model.mode_worktree
1218 elif untracked:
1219 if gitcmds.is_binary(context, filename):
1220 mode = model.mode_untracked
1221 else:
1222 mode = model.mode_untracked_diff
1223 else:
1224 mode = model.mode
1225 return mode
1228 class DiffAgainstCommitMode(ContextCommand):
1229 """Diff against arbitrary commits"""
1231 def __init__(self, context, oid):
1232 super().__init__(context)
1233 self.oid = oid
1235 def do(self):
1236 self.model.set_mode(self.model.mode_diff, head=self.oid)
1237 self.model.update_file_status()
1240 class DiffText(EditModel):
1241 """Set the diff type to text"""
1243 def __init__(self, context):
1244 super().__init__(context)
1245 self.new_file_type = main.Types.TEXT
1246 self.new_diff_type = main.Types.TEXT
1249 class ToggleDiffType(ContextCommand):
1250 """Toggle the diff type between image and text"""
1252 def __init__(self, context):
1253 super().__init__(context)
1254 if self.model.diff_type == main.Types.IMAGE:
1255 self.new_diff_type = main.Types.TEXT
1256 self.new_value = False
1257 else:
1258 self.new_diff_type = main.Types.IMAGE
1259 self.new_value = True
1261 def do(self):
1262 diff_type = self.new_diff_type
1263 value = self.new_value
1265 self.model.set_diff_type(diff_type)
1267 filename = self.model.filename
1268 _, ext = os.path.splitext(filename)
1269 if ext.startswith('.'):
1270 cfg = 'cola.imagediff' + ext
1271 self.cfg.set_repo(cfg, value)
1274 class DiffImage(EditModel):
1275 def __init__(
1276 self, context, filename, deleted, staged, modified, unmerged, untracked
1278 super().__init__(context)
1280 self.new_filename = filename
1281 self.new_diff_type = self.get_diff_type(filename)
1282 self.new_file_type = main.Types.IMAGE
1283 self.new_mode = get_mode(
1284 context, filename, staged, modified, unmerged, untracked
1286 self.staged = staged
1287 self.modified = modified
1288 self.unmerged = unmerged
1289 self.untracked = untracked
1290 self.deleted = deleted
1291 self.annex = self.cfg.is_annex()
1293 def get_diff_type(self, filename):
1294 """Query the diff type to use based on cola.imagediff.<extension>"""
1295 _, ext = os.path.splitext(filename)
1296 if ext.startswith('.'):
1297 # Check e.g. "cola.imagediff.svg" to see if we should imagediff.
1298 cfg = 'cola.imagediff' + ext
1299 if self.cfg.get(cfg, True):
1300 result = main.Types.IMAGE
1301 else:
1302 result = main.Types.TEXT
1303 else:
1304 result = main.Types.IMAGE
1305 return result
1307 def do(self):
1308 filename = self.new_filename
1310 if self.staged:
1311 images = self.staged_images()
1312 elif self.modified:
1313 images = self.modified_images()
1314 elif self.unmerged:
1315 images = self.unmerged_images()
1316 elif self.untracked:
1317 images = [(filename, False)]
1318 else:
1319 images = []
1321 self.model.set_images(images)
1322 super().do()
1324 def staged_images(self):
1325 context = self.context
1326 git = self.git
1327 head = self.model.head
1328 filename = self.new_filename
1329 annex = self.annex
1331 images = []
1332 index = git.diff_index(head, '--', filename, cached=True)[STDOUT]
1333 if index:
1334 # Example:
1335 # :100644 100644 fabadb8... 4866510... M describe.c
1336 parts = index.split(' ')
1337 if len(parts) > 3:
1338 old_oid = parts[2]
1339 new_oid = parts[3]
1341 if old_oid != MISSING_BLOB_OID:
1342 # First, check if we can get a pre-image from git-annex
1343 annex_image = None
1344 if annex:
1345 annex_image = gitcmds.annex_path(context, head, filename)
1346 if annex_image:
1347 images.append((annex_image, False)) # git annex HEAD
1348 else:
1349 image = gitcmds.write_blob_path(context, head, old_oid, filename)
1350 if image:
1351 images.append((image, True))
1353 if new_oid != MISSING_BLOB_OID:
1354 found_in_annex = False
1355 if annex and core.islink(filename):
1356 status, out, _ = git.annex('status', '--', filename)
1357 if status == 0:
1358 details = out.split(' ')
1359 if details and details[0] == 'A': # newly added file
1360 images.append((filename, False))
1361 found_in_annex = True
1363 if not found_in_annex:
1364 image = gitcmds.write_blob(context, new_oid, filename)
1365 if image:
1366 images.append((image, True))
1368 return images
1370 def unmerged_images(self):
1371 context = self.context
1372 git = self.git
1373 head = self.model.head
1374 filename = self.new_filename
1375 annex = self.annex
1377 candidate_merge_heads = ('HEAD', 'CHERRY_HEAD', 'MERGE_HEAD')
1378 merge_heads = [
1379 merge_head
1380 for merge_head in candidate_merge_heads
1381 if core.exists(git.git_path(merge_head))
1384 if annex: # Attempt to find files in git-annex
1385 annex_images = []
1386 for merge_head in merge_heads:
1387 image = gitcmds.annex_path(context, merge_head, filename)
1388 if image:
1389 annex_images.append((image, False))
1390 if annex_images:
1391 annex_images.append((filename, False))
1392 return annex_images
1394 # DIFF FORMAT FOR MERGES
1395 # "git-diff-tree", "git-diff-files" and "git-diff --raw"
1396 # can take -c or --cc option to generate diff output also
1397 # for merge commits. The output differs from the format
1398 # described above in the following way:
1400 # 1. there is a colon for each parent
1401 # 2. there are more "src" modes and "src" sha1
1402 # 3. status is concatenated status characters for each parent
1403 # 4. no optional "score" number
1404 # 5. single path, only for "dst"
1405 # Example:
1406 # ::100644 100644 100644 fabadb8... cc95eb0... 4866510... \
1407 # MM describe.c
1408 images = []
1409 index = git.diff_index(head, '--', filename, cached=True, cc=True)[STDOUT]
1410 if index:
1411 parts = index.split(' ')
1412 if len(parts) > 3:
1413 first_mode = parts[0]
1414 num_parents = first_mode.count(':')
1415 # colon for each parent, but for the index, the "parents"
1416 # are really entries in stages 1,2,3 (head, base, remote)
1417 # remote, base, head
1418 for i in range(num_parents):
1419 offset = num_parents + i + 1
1420 oid = parts[offset]
1421 try:
1422 merge_head = merge_heads[i]
1423 except IndexError:
1424 merge_head = 'HEAD'
1425 if oid != MISSING_BLOB_OID:
1426 image = gitcmds.write_blob_path(
1427 context, merge_head, oid, filename
1429 if image:
1430 images.append((image, True))
1432 images.append((filename, False))
1433 return images
1435 def modified_images(self):
1436 context = self.context
1437 git = self.git
1438 head = self.model.head
1439 filename = self.new_filename
1440 annex = self.annex
1442 images = []
1443 annex_image = None
1444 if annex: # Check for a pre-image from git-annex
1445 annex_image = gitcmds.annex_path(context, head, filename)
1446 if annex_image:
1447 images.append((annex_image, False)) # git annex HEAD
1448 else:
1449 worktree = git.diff_files('--', filename)[STDOUT]
1450 parts = worktree.split(' ')
1451 if len(parts) > 3:
1452 oid = parts[2]
1453 if oid != MISSING_BLOB_OID:
1454 image = gitcmds.write_blob_path(context, head, oid, filename)
1455 if image:
1456 images.append((image, True)) # HEAD
1458 images.append((filename, False)) # worktree
1459 return images
1462 class Diff(EditModel):
1463 """Perform a diff and set the model's current text."""
1465 def __init__(self, context, filename, cached=False, deleted=False):
1466 super().__init__(context)
1467 opts = {}
1468 if cached and gitcmds.is_valid_ref(context, self.model.head):
1469 opts['ref'] = self.model.head
1470 self.new_filename = filename
1471 self.new_mode = self.model.mode_worktree
1472 self.new_diff_text = gitcmds.diff_helper(
1473 self.context, filename=filename, cached=cached, deleted=deleted, **opts
1477 class Diffstat(EditModel):
1478 """Perform a diffstat and set the model's diff text."""
1480 def __init__(self, context):
1481 super().__init__(context)
1482 cfg = self.cfg
1483 diff_context = cfg.get('diff.context', 3)
1484 diff = self.git.diff(
1485 self.model.head,
1486 unified=diff_context,
1487 no_ext_diff=True,
1488 no_color=True,
1489 M=True,
1490 stat=True,
1491 )[STDOUT]
1492 self.new_diff_text = diff
1493 self.new_diff_type = main.Types.TEXT
1494 self.new_file_type = main.Types.TEXT
1495 self.new_mode = self.model.mode_diffstat
1498 class DiffStaged(Diff):
1499 """Perform a staged diff on a file."""
1501 def __init__(self, context, filename, deleted=None):
1502 super().__init__(context, filename, cached=True, deleted=deleted)
1503 self.new_mode = self.model.mode_index
1506 class DiffStagedSummary(EditModel):
1507 def __init__(self, context):
1508 super().__init__(context)
1509 diff = self.git.diff(
1510 self.model.head,
1511 cached=True,
1512 no_color=True,
1513 no_ext_diff=True,
1514 patch_with_stat=True,
1515 M=True,
1516 )[STDOUT]
1517 self.new_diff_text = diff
1518 self.new_diff_type = main.Types.TEXT
1519 self.new_file_type = main.Types.TEXT
1520 self.new_mode = self.model.mode_index
1523 class Edit(ContextCommand):
1524 """Edit a file using the configured gui.editor."""
1526 @staticmethod
1527 def name():
1528 return N_('Launch Editor')
1530 def __init__(self, context, filenames, line_number=None, background_editor=False):
1531 super().__init__(context)
1532 self.filenames = filenames
1533 self.line_number = line_number
1534 self.background_editor = background_editor
1536 def do(self):
1537 context = self.context
1538 if not self.filenames:
1539 return
1540 filename = self.filenames[0]
1541 if not core.exists(filename):
1542 return
1543 if self.background_editor:
1544 editor = prefs.background_editor(context)
1545 else:
1546 editor = prefs.editor(context)
1547 opts = []
1549 if self.line_number is None:
1550 opts = self.filenames
1551 else:
1552 # Single-file w/ line-numbers (likely from grep)
1553 editor_opts = {
1554 '*vim*': [filename, '+%s' % self.line_number],
1555 '*emacs*': ['+%s' % self.line_number, filename],
1556 '*textpad*': [f'{filename}({self.line_number},0)'],
1557 '*notepad++*': ['-n%s' % self.line_number, filename],
1558 '*subl*': [f'{filename}:{self.line_number}'],
1561 opts = self.filenames
1562 for pattern, opt in editor_opts.items():
1563 if fnmatch(editor, pattern):
1564 opts = opt
1565 break
1567 try:
1568 core.fork(utils.shell_split(editor) + opts)
1569 except (OSError, ValueError) as e:
1570 message = N_('Cannot exec "%s": please configure your editor') % editor
1571 _, details = utils.format_exception(e)
1572 Interaction.critical(N_('Error Editing File'), message, details)
1575 class FormatPatch(ContextCommand):
1576 """Output a patch series given all revisions and a selected subset."""
1578 def __init__(self, context, to_export, revs, output='patches'):
1579 super().__init__(context)
1580 self.to_export = list(to_export)
1581 self.revs = list(revs)
1582 self.output = output
1584 def do(self):
1585 context = self.context
1586 status, out, err = gitcmds.format_patchsets(
1587 context, self.to_export, self.revs, self.output
1589 Interaction.log_status(status, out, err)
1592 class LaunchTerminal(ContextCommand):
1593 @staticmethod
1594 def name():
1595 return N_('Launch Terminal')
1597 @staticmethod
1598 def is_available(context):
1599 return context.cfg.terminal() is not None
1601 def __init__(self, context, path):
1602 super().__init__(context)
1603 self.path = path
1605 def do(self):
1606 cmd = self.context.cfg.terminal()
1607 if cmd is None:
1608 return
1609 if utils.is_win32():
1610 argv = ['start', '', cmd, '--login']
1611 shell = True
1612 else:
1613 argv = utils.shell_split(cmd)
1614 command = '/bin/sh'
1615 shells = ('zsh', 'fish', 'bash', 'sh')
1616 for basename in shells:
1617 executable = core.find_executable(basename)
1618 if executable:
1619 command = executable
1620 break
1621 argv.append(os.getenv('SHELL', command))
1622 shell = False
1624 core.fork(argv, cwd=self.path, shell=shell)
1627 class LaunchEditor(Edit):
1628 @staticmethod
1629 def name():
1630 return N_('Launch Editor')
1632 def __init__(self, context):
1633 s = context.selection.selection()
1634 filenames = s.staged + s.unmerged + s.modified + s.untracked
1635 super().__init__(context, filenames, background_editor=True)
1638 class LaunchEditorAtLine(LaunchEditor):
1639 """Launch an editor at the specified line"""
1641 def __init__(self, context):
1642 super().__init__(context)
1643 self.line_number = context.selection.line_number
1646 class LoadCommitMessageFromFile(ContextCommand):
1647 """Loads a commit message from a path."""
1649 UNDOABLE = True
1651 def __init__(self, context, path):
1652 super().__init__(context)
1653 self.path = path
1654 self.old_commitmsg = self.model.commitmsg
1655 self.old_directory = self.model.directory
1657 def do(self):
1658 path = os.path.expanduser(self.path)
1659 if not path or not core.isfile(path):
1660 raise UsageError(
1661 N_('Error: Cannot find commit template'),
1662 N_('%s: No such file or directory.') % path,
1664 self.model.set_directory(os.path.dirname(path))
1665 self.model.set_commitmsg(core.read(path))
1667 def undo(self):
1668 self.model.set_commitmsg(self.old_commitmsg)
1669 self.model.set_directory(self.old_directory)
1672 class LoadCommitMessageFromTemplate(LoadCommitMessageFromFile):
1673 """Loads the commit message template specified by commit.template."""
1675 def __init__(self, context):
1676 cfg = context.cfg
1677 template = cfg.get('commit.template')
1678 super().__init__(context, template)
1680 def do(self):
1681 if self.path is None:
1682 raise UsageError(
1683 N_('Error: Unconfigured commit template'),
1685 'A commit template has not been configured.\n'
1686 'Use "git config" to define "commit.template"\n'
1687 'so that it points to a commit template.'
1690 return LoadCommitMessageFromFile.do(self)
1693 class LoadCommitMessageFromOID(ContextCommand):
1694 """Load a previous commit message"""
1696 UNDOABLE = True
1698 def __init__(self, context, oid, prefix=''):
1699 super().__init__(context)
1700 self.oid = oid
1701 self.old_commitmsg = self.model.commitmsg
1702 self.new_commitmsg = prefix + gitcmds.prev_commitmsg(context, oid)
1704 def do(self):
1705 self.model.set_commitmsg(self.new_commitmsg)
1707 def undo(self):
1708 self.model.set_commitmsg(self.old_commitmsg)
1711 class PrepareCommitMessageHook(ContextCommand):
1712 """Use the cola-prepare-commit-msg hook to prepare the commit message"""
1714 UNDOABLE = True
1716 def __init__(self, context):
1717 super().__init__(context)
1718 self.old_commitmsg = self.model.commitmsg
1720 def get_message(self):
1721 title = N_('Error running prepare-commitmsg hook')
1722 hook = gitcmds.prepare_commit_message_hook(self.context)
1724 if os.path.exists(hook):
1725 filename = self.model.save_commitmsg()
1726 status, out, err = core.run_command([hook, filename])
1728 if status == 0:
1729 result = core.read(filename)
1730 else:
1731 result = self.old_commitmsg
1732 Interaction.command_error(title, hook, status, out, err)
1733 else:
1734 message = N_('A hook must be provided at "%s"') % hook
1735 Interaction.critical(title, message=message)
1736 result = self.old_commitmsg
1738 return result
1740 def do(self):
1741 msg = self.get_message()
1742 self.model.set_commitmsg(msg)
1744 def undo(self):
1745 self.model.set_commitmsg(self.old_commitmsg)
1748 class LoadFixupMessage(LoadCommitMessageFromOID):
1749 """Load a fixup message"""
1751 def __init__(self, context, oid):
1752 super().__init__(context, oid, prefix='fixup! ')
1753 if self.new_commitmsg:
1754 self.new_commitmsg = self.new_commitmsg.splitlines()[0]
1757 class Merge(ContextCommand):
1758 """Merge commits"""
1760 def __init__(self, context, revision, no_commit, squash, no_ff, sign):
1761 super().__init__(context)
1762 self.revision = revision
1763 self.no_ff = no_ff
1764 self.no_commit = no_commit
1765 self.squash = squash
1766 self.sign = sign
1768 def do(self):
1769 squash = self.squash
1770 revision = self.revision
1771 no_ff = self.no_ff
1772 no_commit = self.no_commit
1773 sign = self.sign
1775 status, out, err = self.git.merge(
1776 revision, gpg_sign=sign, no_ff=no_ff, no_commit=no_commit, squash=squash
1778 self.model.update_status()
1779 title = N_('Merge failed. Conflict resolution is required.')
1780 Interaction.command(title, 'git merge', status, out, err)
1782 return status, out, err
1785 class OpenDefaultApp(ContextCommand):
1786 """Open a file using the OS default."""
1788 @staticmethod
1789 def name():
1790 return N_('Open Using Default Application')
1792 def __init__(self, context, filenames):
1793 super().__init__(context)
1794 self.filenames = filenames
1796 def do(self):
1797 if not self.filenames:
1798 return
1799 utils.launch_default_app(self.filenames)
1802 class OpenDir(OpenDefaultApp):
1803 """Open directories using the OS default."""
1805 @staticmethod
1806 def name():
1807 return N_('Open Directory')
1809 @property
1810 def _dirnames(self):
1811 return self.filenames
1813 def do(self):
1814 dirnames = self._dirnames
1815 if not dirnames:
1816 return
1817 # An empty dirname defaults to to the current directory.
1818 dirs = [(dirname or core.getcwd()) for dirname in dirnames]
1819 utils.launch_default_app(dirs)
1822 class OpenParentDir(OpenDir):
1823 """Open parent directories using the OS default."""
1825 @staticmethod
1826 def name():
1827 return N_('Open Parent Directory')
1829 @property
1830 def _dirnames(self):
1831 dirnames = list({os.path.dirname(x) for x in self.filenames})
1832 return dirnames
1835 class OpenWorktree(OpenDir):
1836 """Open worktree directory using the OS default."""
1838 @staticmethod
1839 def name():
1840 return N_('Open Worktree')
1842 # The _unused parameter is needed by worktree_dir_action() -> common.cmd_action().
1843 def __init__(self, context, _unused=None):
1844 dirnames = [context.git.worktree()]
1845 super().__init__(context, dirnames)
1848 class OpenNewRepo(ContextCommand):
1849 """Launches git-cola on a repo."""
1851 def __init__(self, context, repo_path):
1852 super().__init__(context)
1853 self.repo_path = repo_path
1855 def do(self):
1856 self.model.set_directory(self.repo_path)
1857 core.fork([sys.executable, sys.argv[0], '--repo', self.repo_path])
1860 class OpenRepo(EditModel):
1861 def __init__(self, context, repo_path):
1862 super().__init__(context)
1863 self.repo_path = repo_path
1864 self.new_mode = self.model.mode_none
1865 self.new_diff_text = ''
1866 self.new_diff_type = main.Types.TEXT
1867 self.new_file_type = main.Types.TEXT
1868 self.new_commitmsg = ''
1869 self.new_filename = ''
1871 def do(self):
1872 old_repo = self.git.getcwd()
1873 if self.model.set_worktree(self.repo_path):
1874 self.fsmonitor.stop()
1875 self.fsmonitor.start()
1876 self.model.update_status(reset=True)
1877 # Check if template should be loaded
1878 if self.context.cfg.get(prefs.AUTOTEMPLATE):
1879 template_loader = LoadCommitMessageFromTemplate(self.context)
1880 template_loader.do()
1881 else:
1882 self.model.set_commitmsg(self.new_commitmsg)
1883 settings = self.context.settings
1884 settings.load()
1885 settings.add_recent(self.repo_path, prefs.maxrecent(self.context))
1886 settings.save()
1887 super().do()
1888 else:
1889 self.model.set_worktree(old_repo)
1892 class OpenParentRepo(OpenRepo):
1893 def __init__(self, context):
1894 path = ''
1895 if version.check_git(context, 'show-superproject-working-tree'):
1896 status, out, _ = context.git.rev_parse(show_superproject_working_tree=True)
1897 if status == 0:
1898 path = out
1899 if not path:
1900 path = os.path.dirname(core.getcwd())
1901 super().__init__(context, path)
1904 class Clone(ContextCommand):
1905 """Clones a repository and optionally spawns a new cola session."""
1907 def __init__(
1908 self, context, url, new_directory, submodules=False, shallow=False, spawn=True
1910 super().__init__(context)
1911 self.url = url
1912 self.new_directory = new_directory
1913 self.submodules = submodules
1914 self.shallow = shallow
1915 self.spawn = spawn
1916 self.status = -1
1917 self.out = ''
1918 self.err = ''
1920 def do(self):
1921 kwargs = {}
1922 if self.shallow:
1923 kwargs['depth'] = 1
1924 recurse_submodules = self.submodules
1925 shallow_submodules = self.submodules and self.shallow
1927 status, out, err = self.git.clone(
1928 self.url,
1929 self.new_directory,
1930 recurse_submodules=recurse_submodules,
1931 shallow_submodules=shallow_submodules,
1932 **kwargs,
1935 self.status = status
1936 self.out = out
1937 self.err = err
1938 if status == 0 and self.spawn:
1939 executable = sys.executable
1940 core.fork([executable, sys.argv[0], '--repo', self.new_directory])
1941 return self
1944 class NewBareRepo(ContextCommand):
1945 """Create a new shared bare repository"""
1947 def __init__(self, context, path):
1948 super().__init__(context)
1949 self.path = path
1951 def do(self):
1952 path = self.path
1953 status, out, err = self.git.init(path, bare=True, shared=True)
1954 Interaction.command(
1955 N_('Error'), 'git init --bare --shared "%s"' % path, status, out, err
1957 return status == 0
1960 def unix_path(path, is_win32=utils.is_win32):
1961 """Git for Windows requires Unix paths, so force them here"""
1962 if is_win32():
1963 path = path.replace('\\', '/')
1964 first = path[0]
1965 second = path[1]
1966 if second == ':': # sanity check, this better be a Windows-style path
1967 path = '/' + first + path[2:]
1969 return path
1972 def sequence_editor():
1973 """Set GIT_SEQUENCE_EDITOR for running git-cola-sequence-editor"""
1974 xbase = unix_path(resources.command('git-cola-sequence-editor'))
1975 if utils.is_win32():
1976 editor = core.list2cmdline([unix_path(sys.executable), xbase])
1977 else:
1978 editor = core.list2cmdline([xbase])
1979 return editor
1982 class SequenceEditorEnvironment:
1983 """Set environment variables to enable git-cola-sequence-editor"""
1985 def __init__(self, context, **kwargs):
1986 self.env = {
1987 'GIT_EDITOR': prefs.editor(context),
1988 'GIT_SEQUENCE_EDITOR': sequence_editor(),
1990 self.env.update(kwargs)
1992 def __enter__(self):
1993 for var, value in self.env.items():
1994 compat.setenv(var, value)
1995 return self
1997 def __exit__(self, exc_type, exc_val, exc_tb):
1998 for var in self.env:
1999 compat.unsetenv(var)
2002 class Rebase(ContextCommand):
2003 def __init__(self, context, upstream=None, branch=None, **kwargs):
2004 """Start an interactive rebase session
2006 :param upstream: upstream branch
2007 :param branch: optional branch to checkout
2008 :param kwargs: forwarded directly to `git.rebase()`
2011 super().__init__(context)
2013 self.upstream = upstream
2014 self.branch = branch
2015 self.kwargs = kwargs
2017 def prepare_arguments(self, upstream):
2018 args = []
2019 kwargs = {}
2021 # Rebase actions must be the only option specified
2022 for action in ('continue', 'abort', 'skip', 'edit_todo'):
2023 if self.kwargs.get(action, False):
2024 kwargs[action] = self.kwargs[action]
2025 return args, kwargs
2027 kwargs['interactive'] = True
2028 kwargs['autosquash'] = self.kwargs.get('autosquash', True)
2029 kwargs.update(self.kwargs)
2031 # Prompt to determine whether or not to use "git rebase --update-refs".
2032 has_update_refs = version.check_git(self.context, 'rebase-update-refs')
2033 if has_update_refs and not kwargs.get('update_refs', False):
2034 title = N_('Update stacked branches when rebasing?')
2035 text = N_(
2036 '"git rebase --update-refs" automatically force-updates any\n'
2037 'branches that point to commits that are being rebased.\n\n'
2038 'Any branches that are checked out in a worktree are not updated.\n\n'
2039 'Using this feature is helpful for "stacked" branch workflows.'
2041 info = N_('Update stacked branches when rebasing?')
2042 ok_text = N_('Update stacked branches')
2043 cancel_text = N_('Do not update stacked branches')
2044 update_refs = Interaction.confirm(
2045 title,
2046 text,
2047 info,
2048 ok_text,
2049 default=True,
2050 cancel_text=cancel_text,
2052 if update_refs:
2053 kwargs['update_refs'] = True
2055 if upstream:
2056 args.append(upstream)
2057 if self.branch:
2058 args.append(self.branch)
2060 return args, kwargs
2062 def do(self):
2063 (status, out, err) = (1, '', '')
2064 context = self.context
2065 cfg = self.cfg
2066 model = self.model
2068 if not cfg.get('rebase.autostash', False):
2069 if model.staged or model.unmerged or model.modified:
2070 Interaction.information(
2071 N_('Unable to rebase'),
2072 N_('You cannot rebase with uncommitted changes.'),
2074 return status, out, err
2076 upstream = self.upstream or Interaction.choose_ref(
2077 context,
2078 N_('Select New Upstream'),
2079 N_('Interactive Rebase'),
2080 default='@{upstream}',
2082 if not upstream:
2083 return status, out, err
2085 self.model.is_rebasing = True
2086 self.model.emit_updated()
2088 args, kwargs = self.prepare_arguments(upstream)
2089 upstream_title = upstream or '@{upstream}'
2090 with SequenceEditorEnvironment(
2091 self.context,
2092 GIT_COLA_SEQ_EDITOR_TITLE=N_('Rebase onto %s') % upstream_title,
2093 GIT_COLA_SEQ_EDITOR_ACTION=N_('Rebase'),
2095 # This blocks the user interface window for the duration
2096 # of git-cola-sequence-editor. We would need to run the command
2097 # in a QRunnable task to avoid blocking the main thread.
2098 # Alternatively, we can hide the main window while rebasing,
2099 # which doesn't require as much effort.
2100 status, out, err = self.git.rebase(
2101 *args, _no_win32_startupinfo=True, **kwargs
2103 self.model.update_status()
2104 if err.strip() != 'Nothing to do':
2105 title = N_('Rebase stopped')
2106 Interaction.command(title, 'git rebase', status, out, err)
2107 return status, out, err
2110 class RebaseEditTodo(ContextCommand):
2111 def do(self):
2112 (status, out, err) = (1, '', '')
2113 with SequenceEditorEnvironment(
2114 self.context,
2115 GIT_COLA_SEQ_EDITOR_TITLE=N_('Edit Rebase'),
2116 GIT_COLA_SEQ_EDITOR_ACTION=N_('Save'),
2118 status, out, err = self.git.rebase(edit_todo=True)
2119 Interaction.log_status(status, out, err)
2120 self.model.update_status()
2121 return status, out, err
2124 class RebaseContinue(ContextCommand):
2125 def do(self):
2126 (status, out, err) = (1, '', '')
2127 with SequenceEditorEnvironment(
2128 self.context,
2129 GIT_COLA_SEQ_EDITOR_TITLE=N_('Rebase'),
2130 GIT_COLA_SEQ_EDITOR_ACTION=N_('Rebase'),
2132 status, out, err = self.git.rebase('--continue')
2133 Interaction.log_status(status, out, err)
2134 self.model.update_status()
2135 return status, out, err
2138 class RebaseSkip(ContextCommand):
2139 def do(self):
2140 (status, out, err) = (1, '', '')
2141 with SequenceEditorEnvironment(
2142 self.context,
2143 GIT_COLA_SEQ_EDITOR_TITLE=N_('Rebase'),
2144 GIT_COLA_SEQ_EDITOR_ACTION=N_('Rebase'),
2146 status, out, err = self.git.rebase(skip=True)
2147 Interaction.log_status(status, out, err)
2148 self.model.update_status()
2149 return status, out, err
2152 class RebaseAbort(ContextCommand):
2153 def do(self):
2154 status, out, err = self.git.rebase(abort=True)
2155 Interaction.log_status(status, out, err)
2156 self.model.update_status()
2159 class Rescan(ContextCommand):
2160 """Rescan for changes"""
2162 def do(self):
2163 self.model.update_status()
2166 class Refresh(ContextCommand):
2167 """Update refs, refresh the index, and update config"""
2169 @staticmethod
2170 def name():
2171 return N_('Refresh')
2173 def do(self):
2174 self.model.update_status(update_index=True)
2175 self.cfg.update()
2176 self.fsmonitor.refresh()
2179 class RefreshConfig(ContextCommand):
2180 """Refresh the git config cache"""
2182 def do(self):
2183 self.cfg.update()
2186 class RevertEditsCommand(ConfirmAction):
2187 def __init__(self, context):
2188 super().__init__(context)
2189 self.icon = icons.undo()
2191 def ok_to_run(self):
2192 return self.model.is_undoable()
2194 def checkout_from_head(self):
2195 return False
2197 def checkout_args(self):
2198 args = []
2199 s = self.selection.selection()
2200 if self.checkout_from_head():
2201 args.append(self.model.head)
2202 args.append('--')
2204 if s.staged:
2205 items = s.staged
2206 else:
2207 items = s.modified
2208 args.extend(items)
2210 return args
2212 def action(self):
2213 checkout_args = self.checkout_args()
2214 return self.git.checkout(*checkout_args)
2216 def success(self):
2217 self.model.set_diff_type(main.Types.TEXT)
2218 self.model.update_file_status()
2221 class RevertUnstagedEdits(RevertEditsCommand):
2222 @staticmethod
2223 def name():
2224 return N_('Revert Unstaged Edits...')
2226 def checkout_from_head(self):
2227 # Being in amend mode should not affect the behavior of this command.
2228 # The only sensible thing to do is to checkout from the index.
2229 return False
2231 def confirm(self):
2232 title = N_('Revert Unstaged Changes?')
2233 text = N_(
2234 'This operation removes unstaged edits from selected files.\n'
2235 'These changes cannot be recovered.'
2237 info = N_('Revert the unstaged changes?')
2238 ok_text = N_('Revert Unstaged Changes')
2239 return Interaction.confirm(
2240 title, text, info, ok_text, default=True, icon=self.icon
2244 class RevertUncommittedEdits(RevertEditsCommand):
2245 @staticmethod
2246 def name():
2247 return N_('Revert Uncommitted Edits...')
2249 def checkout_from_head(self):
2250 return True
2252 def confirm(self):
2253 """Prompt for reverting changes"""
2254 title = N_('Revert Uncommitted Changes?')
2255 text = N_(
2256 'This operation removes uncommitted edits from selected files.\n'
2257 'These changes cannot be recovered.'
2259 info = N_('Revert the uncommitted changes?')
2260 ok_text = N_('Revert Uncommitted Changes')
2261 return Interaction.confirm(
2262 title, text, info, ok_text, default=True, icon=self.icon
2266 class RunConfigAction(ContextCommand):
2267 """Run a user-configured action, typically from the "Tools" menu"""
2269 def __init__(self, context, action_name):
2270 super().__init__(context)
2271 self.action_name = action_name
2273 def do(self):
2274 """Run the user-configured action"""
2275 for env in ('ARGS', 'DIRNAME', 'FILENAME', 'REVISION'):
2276 try:
2277 compat.unsetenv(env)
2278 except KeyError:
2279 pass
2280 rev = None
2281 args = None
2282 context = self.context
2283 cfg = self.cfg
2284 opts = cfg.get_guitool_opts(self.action_name)
2285 cmd = opts.get('cmd')
2286 if 'title' not in opts:
2287 opts['title'] = cmd
2289 if 'prompt' not in opts or opts.get('prompt') is True:
2290 prompt = N_('Run "%s"?') % cmd
2291 opts['prompt'] = prompt
2293 if opts.get('needsfile'):
2294 filename = self.selection.filename()
2295 if not filename:
2296 Interaction.information(
2297 N_('Please select a file'),
2298 N_('"%s" requires a selected file.') % cmd,
2300 return False
2301 dirname = utils.dirname(filename, current_dir='.')
2302 compat.setenv('FILENAME', filename)
2303 compat.setenv('DIRNAME', dirname)
2305 if opts.get('revprompt') or opts.get('argprompt'):
2306 while True:
2307 ok = Interaction.confirm_config_action(context, cmd, opts)
2308 if not ok:
2309 return False
2310 rev = opts.get('revision')
2311 args = opts.get('args')
2312 if opts.get('revprompt') and not rev:
2313 title = N_('Invalid Revision')
2314 msg = N_('The revision expression cannot be empty.')
2315 Interaction.critical(title, msg)
2316 continue
2317 break
2319 elif opts.get('confirm'):
2320 title = os.path.expandvars(opts.get('title'))
2321 prompt = os.path.expandvars(opts.get('prompt'))
2322 if not Interaction.question(title, prompt):
2323 return False
2324 if rev:
2325 compat.setenv('REVISION', rev)
2326 if args:
2327 compat.setenv('ARGS', args)
2328 title = os.path.expandvars(cmd)
2329 Interaction.log(N_('Running command: %s') % title)
2330 cmd = ['sh', '-c', cmd]
2332 if opts.get('background'):
2333 core.fork(cmd)
2334 status, out, err = (0, '', '')
2335 elif opts.get('noconsole'):
2336 status, out, err = core.run_command(cmd)
2337 else:
2338 status, out, err = Interaction.run_command(title, cmd)
2340 if not opts.get('background') and not opts.get('norescan'):
2341 self.model.update_status()
2343 title = N_('Error')
2344 Interaction.command(title, cmd, status, out, err)
2346 return status == 0
2349 class SetDefaultRepo(ContextCommand):
2350 """Set the default repository"""
2352 def __init__(self, context, repo):
2353 super().__init__(context)
2354 self.repo = repo
2356 def do(self):
2357 self.cfg.set_user('cola.defaultrepo', self.repo)
2360 class SetDiffText(EditModel):
2361 """Set the diff text"""
2363 UNDOABLE = True
2365 def __init__(self, context, text):
2366 super().__init__(context)
2367 self.new_diff_text = text
2368 self.new_diff_type = main.Types.TEXT
2369 self.new_file_type = main.Types.TEXT
2372 class SetUpstreamBranch(ContextCommand):
2373 """Set the upstream branch"""
2375 def __init__(self, context, branch, remote, remote_branch):
2376 super().__init__(context)
2377 self.branch = branch
2378 self.remote = remote
2379 self.remote_branch = remote_branch
2381 def do(self):
2382 cfg = self.cfg
2383 remote = self.remote
2384 branch = self.branch
2385 remote_branch = self.remote_branch
2386 cfg.set_repo('branch.%s.remote' % branch, remote)
2387 cfg.set_repo('branch.%s.merge' % branch, 'refs/heads/' + remote_branch)
2390 def format_hex(data):
2391 """Translate binary data into a hex dump"""
2392 hexdigits = '0123456789ABCDEF'
2393 result = ''
2394 offset = 0
2395 byte_offset_to_int = compat.byte_offset_to_int_converter()
2396 while offset < len(data):
2397 result += '%04u |' % offset
2398 textpart = ''
2399 for i in range(0, 16):
2400 if i > 0 and i % 4 == 0:
2401 result += ' '
2402 if offset < len(data):
2403 v = byte_offset_to_int(data[offset])
2404 result += ' ' + hexdigits[v >> 4] + hexdigits[v & 0xF]
2405 textpart += chr(v) if 32 <= v < 127 else '.'
2406 offset += 1
2407 else:
2408 result += ' '
2409 textpart += ' '
2410 result += ' | ' + textpart + ' |\n'
2412 return result
2415 class ShowUntracked(EditModel):
2416 """Show an untracked file."""
2418 def __init__(self, context, filename):
2419 super().__init__(context)
2420 self.new_filename = filename
2421 if gitcmds.is_binary(context, filename):
2422 self.new_mode = self.model.mode_untracked
2423 self.new_diff_text = self.read(filename)
2424 else:
2425 self.new_mode = self.model.mode_untracked_diff
2426 self.new_diff_text = gitcmds.diff_helper(
2427 self.context, filename=filename, cached=False, untracked=True
2429 self.new_diff_type = main.Types.TEXT
2430 self.new_file_type = main.Types.TEXT
2432 def read(self, filename):
2433 """Read file contents"""
2434 cfg = self.cfg
2435 size = cfg.get('cola.readsize', 2048)
2436 try:
2437 result = core.read(filename, size=size, encoding='bytes')
2438 except OSError:
2439 result = ''
2441 truncated = len(result) == size
2443 encoding = cfg.file_encoding(filename) or core.ENCODING
2444 try:
2445 text_result = core.decode_maybe(result, encoding)
2446 except UnicodeError:
2447 text_result = format_hex(result)
2449 if truncated:
2450 text_result += '...'
2451 return text_result
2454 class SignOff(ContextCommand):
2455 """Append a sign-off to the commit message"""
2457 UNDOABLE = True
2459 @staticmethod
2460 def name():
2461 return N_('Sign Off')
2463 def __init__(self, context):
2464 super().__init__(context)
2465 self.old_commitmsg = self.model.commitmsg
2467 def do(self):
2468 """Add a sign-off to the commit message"""
2469 signoff = self.signoff()
2470 if signoff in self.model.commitmsg:
2471 return
2472 msg = self.model.commitmsg.rstrip()
2473 self.model.set_commitmsg(msg + '\n' + signoff)
2475 def undo(self):
2476 """Restore the commit message"""
2477 self.model.set_commitmsg(self.old_commitmsg)
2479 def signoff(self):
2480 """Generate the sign-off string"""
2481 name, email = self.cfg.get_author()
2482 return f'\nSigned-off-by: {name} <{email}>'
2485 def check_conflicts(context, unmerged):
2486 """Check paths for conflicts
2488 Conflicting files can be filtered out one-by-one.
2491 if prefs.check_conflicts(context):
2492 unmerged = [path for path in unmerged if is_conflict_free(path)]
2493 return unmerged
2496 def is_conflict_free(path):
2497 """Return True if `path` contains no conflict markers"""
2498 rgx = re.compile(r'^(<<<<<<<|\|\|\|\|\|\|\||>>>>>>>) ')
2499 try:
2500 with core.xopen(path, 'rb') as f:
2501 for line in f:
2502 line = core.decode(line, errors='ignore')
2503 if rgx.match(line):
2504 return should_stage_conflicts(path)
2505 except OSError:
2506 # We can't read this file ~ we may be staging a removal
2507 pass
2508 return True
2511 def should_stage_conflicts(path):
2512 """Inform the user that a file contains merge conflicts
2514 Return `True` if we should stage the path nonetheless.
2517 title = msg = N_('Stage conflicts?')
2518 info = (
2520 '%s appears to contain merge conflicts.\n\n'
2521 'You should probably skip this file.\n'
2522 'Stage it anyways?'
2524 % path
2526 ok_text = N_('Stage conflicts')
2527 cancel_text = N_('Skip')
2528 return Interaction.confirm(
2529 title, msg, info, ok_text, default=False, cancel_text=cancel_text
2533 class Stage(ContextCommand):
2534 """Stage a set of paths."""
2536 @staticmethod
2537 def name():
2538 return N_('Stage')
2540 def __init__(self, context, paths):
2541 super().__init__(context)
2542 self.paths = paths
2544 def do(self):
2545 msg = N_('Staging: %s') % (', '.join(self.paths))
2546 Interaction.log(msg)
2547 return self.stage_paths()
2549 def stage_paths(self):
2550 """Stages add/removals to git."""
2551 context = self.context
2552 paths = self.paths
2553 if not paths:
2554 if self.model.cfg.get('cola.safemode', False):
2555 return (0, '', '')
2556 return self.stage_all()
2558 add = []
2559 remove = []
2560 status = 0
2561 out = ''
2562 err = ''
2564 for path in set(paths):
2565 if core.exists(path) or core.islink(path):
2566 if path.endswith('/'):
2567 path = path.rstrip('/')
2568 add.append(path)
2569 else:
2570 remove.append(path)
2572 self.model.emit_about_to_update()
2574 # `git add -u` doesn't work on untracked files
2575 if add:
2576 status, out, err = gitcmds.add(context, add)
2577 Interaction.command(N_('Error'), 'git add', status, out, err)
2579 # If a path doesn't exist then that means it should be removed
2580 # from the index. We use `git add -u` for that.
2581 if remove:
2582 status, out, err = gitcmds.add(context, remove, u=True)
2583 Interaction.command(N_('Error'), 'git add -u', status, out, err)
2585 self.model.update_files(emit=True)
2586 return status, out, err
2588 def stage_all(self):
2589 """Stage all files"""
2590 status, out, err = self.git.add(v=True, u=True)
2591 Interaction.command(N_('Error'), 'git add -u', status, out, err)
2592 self.model.update_file_status()
2593 return (status, out, err)
2596 class StageCarefully(Stage):
2597 """Only stage when the path list is non-empty
2599 We use "git add -u -- <pathspec>" to stage, and it stages everything by
2600 default when no pathspec is specified, so this class ensures that paths
2601 are specified before calling git.
2603 When no paths are specified, the command does nothing.
2607 def __init__(self, context):
2608 super().__init__(context, None)
2609 self.init_paths()
2611 def init_paths(self):
2612 """Initialize path data"""
2613 return
2615 def ok_to_run(self):
2616 """Prevent catch-all "git add -u" from adding unmerged files"""
2617 return self.paths or not self.model.unmerged
2619 def do(self):
2620 """Stage files when ok_to_run() return True"""
2621 if self.ok_to_run():
2622 return super().do()
2623 return (0, '', '')
2626 class StageModified(StageCarefully):
2627 """Stage all modified files."""
2629 @staticmethod
2630 def name():
2631 return N_('Stage Modified')
2633 def init_paths(self):
2634 self.paths = self.model.modified
2637 class StageUnmerged(StageCarefully):
2638 """Stage unmerged files."""
2640 @staticmethod
2641 def name():
2642 return N_('Stage Unmerged')
2644 def init_paths(self):
2645 self.paths = check_conflicts(self.context, self.model.unmerged)
2648 class StageUntracked(StageCarefully):
2649 """Stage all untracked files."""
2651 @staticmethod
2652 def name():
2653 return N_('Stage Untracked')
2655 def init_paths(self):
2656 self.paths = self.model.untracked
2658 def stage_all(self):
2659 """Disable the stage_all() behavior for untracked files"""
2660 return (0, '', '')
2663 class StageModifiedAndUntracked(StageCarefully):
2664 """Stage all untracked files."""
2666 @staticmethod
2667 def name():
2668 return N_('Stage Modified and Untracked')
2670 def init_paths(self):
2671 self.paths = self.model.modified + self.model.untracked
2674 class StageOrUnstageAll(ContextCommand):
2675 """If the selection is staged, unstage it, otherwise stage"""
2677 @staticmethod
2678 def name():
2679 return N_('Stage / Unstage All')
2681 def do(self):
2682 if self.model.staged:
2683 do(Unstage, self.context, self.model.staged)
2684 else:
2685 if self.cfg.get('cola.safemode', False):
2686 unstaged = self.model.modified
2687 else:
2688 unstaged = self.model.modified + self.model.untracked
2689 do(Stage, self.context, unstaged)
2692 class StageOrUnstage(ContextCommand):
2693 """If the selection is staged, unstage it, otherwise stage"""
2695 @staticmethod
2696 def name():
2697 return N_('Stage / Unstage')
2699 def do(self):
2700 s = self.selection.selection()
2701 if s.staged:
2702 do(Unstage, self.context, s.staged)
2704 unstaged = []
2705 unmerged = check_conflicts(self.context, s.unmerged)
2706 if unmerged:
2707 unstaged.extend(unmerged)
2708 if s.modified:
2709 unstaged.extend(s.modified)
2710 if s.untracked:
2711 unstaged.extend(s.untracked)
2712 if unstaged:
2713 do(Stage, self.context, unstaged)
2716 class Tag(ContextCommand):
2717 """Create a tag object."""
2719 def __init__(self, context, name, revision, sign=False, message=''):
2720 super().__init__(context)
2721 self._name = name
2722 self._message = message
2723 self._revision = revision
2724 self._sign = sign
2726 def do(self):
2727 result = False
2728 git = self.git
2729 revision = self._revision
2730 tag_name = self._name
2731 tag_message = self._message
2733 if not revision:
2734 Interaction.critical(
2735 N_('Missing Revision'), N_('Please specify a revision to tag.')
2737 return result
2739 if not tag_name:
2740 Interaction.critical(
2741 N_('Missing Name'), N_('Please specify a name for the new tag.')
2743 return result
2745 title = N_('Missing Tag Message')
2746 message = N_('Tag-signing was requested but the tag message is empty.')
2747 info = N_(
2748 'An unsigned, lightweight tag will be created instead.\n'
2749 'Create an unsigned tag?'
2751 ok_text = N_('Create Unsigned Tag')
2752 sign = self._sign
2753 if sign and not tag_message:
2754 # We require a message in order to sign the tag, so if they
2755 # choose to create an unsigned tag we have to clear the sign flag.
2756 if not Interaction.confirm(
2757 title, message, info, ok_text, default=False, icon=icons.save()
2759 return result
2760 sign = False
2762 opts = {}
2763 tmp_file = None
2764 try:
2765 if tag_message:
2766 tmp_file = utils.tmp_filename('tag-message')
2767 opts['file'] = tmp_file
2768 core.write(tmp_file, tag_message)
2770 if sign:
2771 opts['sign'] = True
2772 if tag_message:
2773 opts['annotate'] = True
2774 status, out, err = git.tag(tag_name, revision, **opts)
2775 finally:
2776 if tmp_file:
2777 core.unlink(tmp_file)
2779 title = N_('Error: could not create tag "%s"') % tag_name
2780 Interaction.command(title, 'git tag', status, out, err)
2782 if status == 0:
2783 result = True
2784 self.model.update_status()
2785 Interaction.information(
2786 N_('Tag Created'),
2787 N_('Created a new tag named "%s"') % tag_name,
2788 details=tag_message or None,
2791 return result
2794 class Unstage(ContextCommand):
2795 """Unstage a set of paths."""
2797 @staticmethod
2798 def name():
2799 return N_('Unstage')
2801 def __init__(self, context, paths):
2802 super().__init__(context)
2803 self.paths = paths
2805 def do(self):
2806 """Unstage paths"""
2807 context = self.context
2808 head = self.model.head
2809 paths = self.paths
2811 msg = N_('Unstaging: %s') % (', '.join(paths))
2812 Interaction.log(msg)
2813 if not paths:
2814 return unstage_all(context)
2815 status, out, err = gitcmds.unstage_paths(context, paths, head=head)
2816 Interaction.command(N_('Error'), 'git reset', status, out, err)
2817 self.model.update_file_status()
2818 return (status, out, err)
2821 class UnstageAll(ContextCommand):
2822 """Unstage all files; resets the index."""
2824 def do(self):
2825 return unstage_all(self.context)
2828 def unstage_all(context):
2829 """Unstage all files, even while amending"""
2830 model = context.model
2831 git = context.git
2832 head = model.head
2833 status, out, err = git.reset(head, '--', '.')
2834 Interaction.command(N_('Error'), 'git reset', status, out, err)
2835 model.update_file_status()
2836 return (status, out, err)
2839 class StageSelected(ContextCommand):
2840 """Stage selected files, or all files if no selection exists."""
2842 def do(self):
2843 context = self.context
2844 paths = self.selection.unstaged
2845 if paths:
2846 do(Stage, context, paths)
2847 elif self.cfg.get('cola.safemode', False):
2848 do(StageModified, context)
2851 class UnstageSelected(Unstage):
2852 """Unstage selected files."""
2854 def __init__(self, context):
2855 staged = context.selection.staged
2856 super().__init__(context, staged)
2859 class Untrack(ContextCommand):
2860 """Unstage a set of paths."""
2862 def __init__(self, context, paths):
2863 super().__init__(context)
2864 self.paths = paths
2866 def do(self):
2867 msg = N_('Untracking: %s') % (', '.join(self.paths))
2868 Interaction.log(msg)
2869 status, out, err = self.model.untrack_paths(self.paths)
2870 Interaction.log_status(status, out, err)
2873 class UnmergedSummary(EditModel):
2874 """List unmerged files in the diff text."""
2876 def __init__(self, context):
2877 super().__init__(context)
2878 unmerged = self.model.unmerged
2879 io = StringIO()
2880 io.write('# %s unmerged file(s)\n' % len(unmerged))
2881 if unmerged:
2882 io.write('\n'.join(unmerged) + '\n')
2883 self.new_diff_text = io.getvalue()
2884 self.new_diff_type = main.Types.TEXT
2885 self.new_file_type = main.Types.TEXT
2886 self.new_mode = self.model.mode_display
2889 class UntrackedSummary(EditModel):
2890 """List possible .gitignore rules as the diff text."""
2892 def __init__(self, context):
2893 super().__init__(context)
2894 untracked = self.model.untracked
2895 io = StringIO()
2896 io.write('# %s untracked file(s)\n' % len(untracked))
2897 if untracked:
2898 io.write('# Add these lines to ".gitignore" to ignore these files:\n')
2899 io.write('\n'.join('/' + filename for filename in untracked) + '\n')
2900 self.new_diff_text = io.getvalue()
2901 self.new_diff_type = main.Types.TEXT
2902 self.new_file_type = main.Types.TEXT
2903 self.new_mode = self.model.mode_display
2906 class VisualizeAll(ContextCommand):
2907 """Visualize all branches."""
2909 def do(self):
2910 context = self.context
2911 browser = utils.shell_split(prefs.history_browser(context))
2912 launch_history_browser(browser + ['--all'])
2915 class VisualizeCurrent(ContextCommand):
2916 """Visualize all branches."""
2918 def do(self):
2919 context = self.context
2920 browser = utils.shell_split(prefs.history_browser(context))
2921 launch_history_browser(browser + [self.model.currentbranch] + ['--'])
2924 class VisualizePaths(ContextCommand):
2925 """Path-limited visualization."""
2927 def __init__(self, context, paths):
2928 super().__init__(context)
2929 context = self.context
2930 browser = utils.shell_split(prefs.history_browser(context))
2931 if paths:
2932 self.argv = browser + ['--'] + list(paths)
2933 else:
2934 self.argv = browser
2936 def do(self):
2937 launch_history_browser(self.argv)
2940 class VisualizeRevision(ContextCommand):
2941 """Visualize a specific revision."""
2943 def __init__(self, context, revision, paths=None):
2944 super().__init__(context)
2945 self.revision = revision
2946 self.paths = paths
2948 def do(self):
2949 context = self.context
2950 argv = utils.shell_split(prefs.history_browser(context))
2951 if self.revision:
2952 argv.append(self.revision)
2953 if self.paths:
2954 argv.append('--')
2955 argv.extend(self.paths)
2956 launch_history_browser(argv)
2959 class SubmoduleAdd(ConfirmAction):
2960 """Add specified submodules"""
2962 def __init__(self, context, url, path, branch, depth, reference):
2963 super().__init__(context)
2964 self.url = url
2965 self.path = path
2966 self.branch = branch
2967 self.depth = depth
2968 self.reference = reference
2970 def confirm(self):
2971 title = N_('Add Submodule...')
2972 question = N_('Add this submodule?')
2973 info = N_('The submodule will be added using\n' '"%s"' % self.command())
2974 ok_txt = N_('Add Submodule')
2975 return Interaction.confirm(title, question, info, ok_txt, icon=icons.ok())
2977 def action(self):
2978 context = self.context
2979 args = self.get_args()
2980 return context.git.submodule('add', *args)
2982 def success(self):
2983 self.model.update_file_status()
2984 self.model.update_submodules_list()
2986 def error_message(self):
2987 return N_('Error updating submodule %s' % self.path)
2989 def command(self):
2990 cmd = ['git', 'submodule', 'add']
2991 cmd.extend(self.get_args())
2992 return core.list2cmdline(cmd)
2994 def get_args(self):
2995 args = []
2996 if self.branch:
2997 args.extend(['--branch', self.branch])
2998 if self.reference:
2999 args.extend(['--reference', self.reference])
3000 if self.depth:
3001 args.extend(['--depth', '%d' % self.depth])
3002 args.extend(['--', self.url])
3003 if self.path:
3004 args.append(self.path)
3005 return args
3008 class SubmoduleUpdate(ConfirmAction):
3009 """Update specified submodule"""
3011 def __init__(self, context, path):
3012 super().__init__(context)
3013 self.path = path
3015 def confirm(self):
3016 title = N_('Update Submodule...')
3017 question = N_('Update this submodule?')
3018 info = N_('The submodule will be updated using\n' '"%s"' % self.command())
3019 ok_txt = N_('Update Submodule')
3020 return Interaction.confirm(
3021 title, question, info, ok_txt, default=False, icon=icons.pull()
3024 def action(self):
3025 context = self.context
3026 args = self.get_args()
3027 return context.git.submodule(*args)
3029 def success(self):
3030 self.model.update_file_status()
3032 def error_message(self):
3033 return N_('Error updating submodule %s' % self.path)
3035 def command(self):
3036 cmd = ['git', 'submodule']
3037 cmd.extend(self.get_args())
3038 return core.list2cmdline(cmd)
3040 def get_args(self):
3041 cmd = ['update']
3042 if version.check_git(self.context, 'submodule-update-recursive'):
3043 cmd.append('--recursive')
3044 cmd.extend(['--', self.path])
3045 return cmd
3048 class SubmodulesUpdate(ConfirmAction):
3049 """Update all submodules"""
3051 def confirm(self):
3052 title = N_('Update submodules...')
3053 question = N_('Update all submodules?')
3054 info = N_('All submodules will be updated using\n' '"%s"' % self.command())
3055 ok_txt = N_('Update Submodules')
3056 return Interaction.confirm(
3057 title, question, info, ok_txt, default=False, icon=icons.pull()
3060 def action(self):
3061 context = self.context
3062 args = self.get_args()
3063 return context.git.submodule(*args)
3065 def success(self):
3066 self.model.update_file_status()
3068 def error_message(self):
3069 return N_('Error updating submodules')
3071 def command(self):
3072 cmd = ['git', 'submodule']
3073 cmd.extend(self.get_args())
3074 return core.list2cmdline(cmd)
3076 def get_args(self):
3077 cmd = ['update']
3078 if version.check_git(self.context, 'submodule-update-recursive'):
3079 cmd.append('--recursive')
3080 return cmd
3083 def launch_history_browser(argv):
3084 """Launch the configured history browser"""
3085 try:
3086 core.fork(argv)
3087 except OSError as e:
3088 _, details = utils.format_exception(e)
3089 title = N_('Error Launching History Browser')
3090 msg = N_('Cannot exec "%s": please configure a history browser') % ' '.join(
3091 argv
3093 Interaction.critical(title, message=msg, details=details)
3096 def run(cls, *args, **opts):
3098 Returns a callback that runs a command
3100 If the caller of run() provides args or opts then those are
3101 used instead of the ones provided by the invoker of the callback.
3105 def runner(*local_args, **local_opts):
3106 """Closure return by run() which runs the command"""
3107 if args or opts:
3108 return do(cls, *args, **opts)
3109 return do(cls, *local_args, **local_opts)
3111 return runner
3114 def do(cls, *args, **opts):
3115 """Run a command in-place"""
3116 try:
3117 cmd = cls(*args, **opts)
3118 return cmd.do()
3119 except Exception as e: # pylint: disable=broad-except
3120 msg, details = utils.format_exception(e)
3121 if hasattr(cls, '__name__'):
3122 msg = f'{cls.__name__} exception:\n{msg}'
3123 Interaction.critical(N_('Error'), message=msg, details=details)
3124 return None