From 74845541232eb8ff1144d8eadbf96829575c7174 Mon Sep 17 00:00:00 2001 From: DeathAxe Date: Sat, 20 Mar 2021 10:50:18 +0100 Subject: [PATCH] Fix: Empty compare target Fixes #560 Setting compare target to `origin` may cause a `None` target to be applied. GitGutter no longer knows that to compare against and raises exceptions. This commit fixes `on_branch_name()` to set a new compare target if the given value is empty and ensures the handler to use sane fallback values in case compare target values are empty. --- modules/compare.py | 7 ++++--- modules/handler.py | 11 +++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/modules/compare.py b/modules/compare.py index 7d06e060..43c97e4b 100644 --- a/modules/compare.py +++ b/modules/compare.py @@ -174,9 +174,10 @@ def set_against_origin(git_gutter, **kwargs): by the GitGutterCommand object. """ def on_branch_name(status): - if not status or not status.get('remote'): - sublime.message_dialog('Current branch has not tracking remote!') - git_gutter.git_handler.set_compare_against(status.get('remote'), True) + remote = status.get('remote') if status else None + if remote: + git_gutter.git_handler.set_compare_against(remote, True) + sublime.message_dialog('Current branch has no tracking remote!') git_gutter.git_handler.git_branch_status().then(on_branch_name) diff --git a/modules/handler.py b/modules/handler.py index c33b7463..e81f5f06 100644 --- a/modules/handler.py +++ b/modules/handler.py @@ -220,10 +220,11 @@ def get_compare_against(self): The reference to compare the view against. """ # Interactively specified compare target overrides settings. - if self._git_tree in self._compare_against_mapping: - return self._compare_against_mapping[self._git_tree] - # Project settings and Preferences override plugin settings if set. - return self.settings.get('compare_against', 'HEAD') + result = self._compare_against_mapping.get(self._git_tree) + if not result: + # Project settings and Preferences override plugin settings if set. + result = self.settings.get('compare_against', 'HEAD') + return result def set_compare_against(self, compare_against, refresh=False): """Apply a new branch/commit/tag string the view is compared to. @@ -239,6 +240,8 @@ def set_compare_against(self, compare_against, refresh=False): from 'git show-ref' to compare the view against refresh (bool): True to force git diff and update GUI """ + if not compare_against: + return self._compare_against_mapping[self._git_tree] = compare_against # force refresh if live_mode and focus_change_mode are disabled refresh |= (not self.settings.get('live_mode') and