diff --git a/core/commands/commit.py b/core/commands/commit.py index d4fa3ed77..26ed0d3fd 100644 --- a/core/commands/commit.py +++ b/core/commands/commit.py @@ -1,4 +1,4 @@ -from itertools import takewhile +from itertools import chain, takewhile import os import sublime @@ -8,6 +8,7 @@ from .diff import DECODE_ERROR_MESSAGE from . import intra_line_colorizer from ..git_command import GitCommand, GitSavvyError +from ..fns import head from ..runtime import enqueue_on_worker, text_command from ..settings import SettingsMixin from ..ui_mixins.quick_panel import LogHelperMixin @@ -651,7 +652,21 @@ def action(entry): view.sel().clear() view.sel().add(len(text)) - self.show_log_panel(action, preselected_commit_message=clean_subject) + def preselected_commit(items): + # type: (List[LogEntry]) -> int + return next(chain( + head(idx for idx, item in enumerate(items) if item.summary == clean_subject), + head( + idx for idx, item in enumerate(items) + if ( + not item.summary.startswith("fixup! ") + and not item.summary.startswith("squash! ") + ) + ) if prefix else [], + [-1] + )) + + self.show_log_panel(action, preselected_commit=preselected_commit) def cleanup_subject(subject): diff --git a/core/commands/diff.py b/core/commands/diff.py index 783b814b7..1e2031dd9 100644 --- a/core/commands/diff.py +++ b/core/commands/diff.py @@ -15,7 +15,7 @@ from . import intra_line_colorizer from . import stage_hunk from .navigate import GsNavigate -from ..fns import filter_, flatten, pairwise, unique +from ..fns import head, filter_, flatten, pairwise, unique from ..parse_diff import SplittedDiff from ..git_command import GitCommand from ..runtime import ensure_on_ui, enqueue_on_worker @@ -794,7 +794,20 @@ def action(entry): "initial_text": "fixup! {}".format(commit_message) }) - self.show_log_panel(action) + def preselected_commit(items): + # type: (List[LogEntry]) -> int + return next(chain( + head( + idx for idx, item in enumerate(items) + if ( + not item.summary.startswith("fixup! ") + and not item.summary.startswith("squash! ") + ) + ), + [-1] + )) + + self.show_log_panel(action, preselected_commit=preselected_commit) MYPY = False diff --git a/core/fns.py b/core/fns.py index a4d9426d1..c22ac4b03 100644 --- a/core/fns.py +++ b/core/fns.py @@ -61,6 +61,11 @@ def drop(n, iterable): return islice(iterable, n, None) +def head(iterable): + # type: (Iterable[T]) -> List[T] + return take(1, iterable) + + def tail(iterable): # type: (Iterable[T]) -> Iterator[T] return drop(1, iterable) diff --git a/core/ui_mixins/quick_panel.py b/core/ui_mixins/quick_panel.py index 323439452..7c63cdccc 100644 --- a/core/ui_mixins/quick_panel.py +++ b/core/ui_mixins/quick_panel.py @@ -593,8 +593,8 @@ def on_selection(self, commit): class LogHelperMixin(GitCommand): - def show_log_panel(self, action, preselected_commit_message=None): - # type: (Callable[[LogEntry], None], str) -> None + def show_log_panel(self, action, preselected_commit=lambda items: -1): + # type: (Callable[[LogEntry], None], Callable[[List[LogEntry]], int]) -> None window = self._current_window() if not window: return @@ -622,11 +622,7 @@ def format_item(entry: LogEntry) -> str: entry.summary ))) - preselected_idx = next( - (idx for idx, item in enumerate(items) if item.summary == preselected_commit_message), - -1 - ) if preselected_commit_message else -1 - + preselected_idx = preselected_commit(items) show_panel( window, map(format_item, items),