From 5d2035424de2d957c844c888c1af8e7fafb1f476 Mon Sep 17 00:00:00 2001 From: David Aguilar Date: Tue, 7 Mar 2023 01:06:45 -0800 Subject: [PATCH] status: add support for "git checkout --theirs/--ours" on unmerge files Signed-off-by: David Aguilar --- cola/cmds.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ cola/widgets/status.py | 17 +++++++++++ 2 files changed, 94 insertions(+) diff --git a/cola/cmds.py b/cola/cmds.py index 0425664c..9cd8a2fb 100644 --- a/cola/cmds.py +++ b/cola/cmds.py @@ -464,6 +464,83 @@ class Checkout(EditModel): else: self.model.update_file_status() Interaction.command(N_('Error'), 'git checkout', status, out, err) + return status, out, err + + +class CheckoutTheirs(ConfirmAction): + """Checkout "their" version of a file when performing a merge""" + + @staticmethod + def name(): + return N_('Checkout files from "their" branch (MERGE_HEAD)') + + def confirm(self): + title = self.name() + question = N_('Checkout files from their MERGE_HEAD using "git checkout --theirs"?') + info = N_( + 'This operation will replace the selected unmerged files with content ' + 'from the branch being merged.\n' + '*ALL* uncommitted changes will be lost.\n' + 'Recovering uncommitted changes is not possible.' + ) + ok_txt = N_('Checkout Files') + return Interaction.confirm( + title, question, info, ok_txt, default=True, icon=icons.merge() + ) + + def action(self): + selection = self.selection.selection() + paths = selection.unmerged + if not paths: + return 0, '', '' + + argv = ['--theirs', '--'] + paths + cmd = Checkout(self.context, argv) + return cmd.do() + + def error_message(self): + return N_('Error') + + def command(self): + return 'git checkout --theirs' + + +class CheckoutOurs(ConfirmAction): + """Checkout "our" version of a file when performing a merge""" + + @staticmethod + def name(): + return N_('Checkout files from "our" branch (HEAD)') + + def confirm(self): + title = self.name() + question = N_('Checkout files from MERGE_HEAD?') + info = N_( + 'This will replace the selected unmerged files with content ' + 'from the branch being merged into.\n' + '*ALL* uncommitted changes will be lost.\n' + 'Recovering uncommitted changes is not possible.' + ) + ok_txt = N_('Checkout Files') + return Interaction.confirm( + title, question, info, ok_txt, default=True, icon=icons.merge() + ) + + def action(self): + selection = self.selection.selection() + paths = selection.unmerged + if not paths: + return 0, '', '' + + argv = ['--ours', '--'] + paths + cmd = Checkout(self.context, argv) + return cmd.do() + + def error_message(self): + return N_('Error') + + def command(self): + return 'git checkout --ours' class BlamePaths(ContextCommand): diff --git a/cola/widgets/status.py b/cola/widgets/status.py index caca7676..304c9c23 100644 --- a/cola/widgets/status.py +++ b/cola/widgets/status.py @@ -236,6 +236,20 @@ class StatusTreeWidget(QtWidgets.QTreeWidget): hotkeys.MOVE_DOWN_SECONDARY, ) + # Checkout the selected paths using "git checkout --ours". + self.checkout_ours_action = qtutils.add_action( + self, + cmds.CheckoutOurs.name(), + cmds.run(cmds.CheckoutOurs, context) + ) + + # Checkout the selected paths using "git checkout --theirs". + self.checkout_theirs_action = qtutils.add_action( + self, + cmds.CheckoutTheirs.name(), + cmds.run(cmds.CheckoutTheirs, context) + ) + self.copy_path_action = qtutils.add_action( self, N_('Copy Path to Clipboard'), @@ -867,6 +881,9 @@ class StatusTreeWidget(QtWidgets.QTreeWidget): menu.addAction(self.launch_editor_action) menu.addAction(self.view_history_action) menu.addAction(self.view_blame_action) + menu.addSeparator() + menu.addAction(self.checkout_ours_action) + menu.addAction(self.checkout_theirs_action) return menu def _create_unstaged_context_menu(self, menu, s): -- 2.11.4.GIT