Skip to content

Commit

Permalink
Merge pull request #1821 from timbrel/w-and-e-shortcuts-on-show-commi…
Browse files Browse the repository at this point in the history
…t-view
  • Loading branch information
kaste authored Dec 8, 2023
2 parents d02bc0a + a862938 commit 7926c58
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 1 deletion.
32 changes: 32 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,22 @@
{ "key": "setting.git_savvy.show_commit_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["W"],
"command": "gs_show_commit_reword_commit",
"context": [
{ "key": "setting.command_mode", "operator": "equal", "operand": false },
{ "key": "setting.git_savvy.show_commit_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["E"],
"command": "gs_show_commit_edit_commit",
"context": [
{ "key": "setting.command_mode", "operator": "equal", "operand": false },
{ "key": "setting.git_savvy.show_commit_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["g"],
"command": "gs_show_commit_open_graph_context",
Expand Down Expand Up @@ -1951,6 +1967,22 @@
{ "key": "setting.git_savvy.line_history_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["W"],
"command": "gs_line_history_reword_commit",
"context": [
{ "key": "setting.command_mode", "operator": "equal", "operand": false },
{ "key": "setting.git_savvy.line_history_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["E"],
"command": "gs_show_commit_edit_commit",
"context": [
{ "key": "setting.command_mode", "operator": "equal", "operand": false },
{ "key": "setting.git_savvy.line_history_view", "operator": "equal", "operand": true }
]
},
{
"keys": ["."],
"command": "gs_line_history_navigate",
Expand Down
4 changes: 4 additions & 0 deletions core/commands/_help_popups.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ class gs_show_commit_help_tooltip(GsAbstractHelpPopup):
[n]/[p] show next/previous commit
[h] open commit on GitHub (if available)
[f] initiate fixup commit
[W] reWord commit message
[E] Edit commit
[g] show in graph
[,]/[.] go to next/previous hunk (also: [j]/[k] in vintageous mode)
Expand Down Expand Up @@ -232,6 +234,8 @@ class gs_line_history_help_tooltip(GsAbstractHelpPopup):
[O] open file revision at hunk
[g] show in graph
[f] make fixup commit
[W] reWord commit message
[E] Edit commit
[,]/[.] go to next/previous hunk (also: [j]/[k] in vintageous mode)
### Other ###
Expand Down
63 changes: 62 additions & 1 deletion core/commands/show_commit.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from contextlib import contextmanager
import os
import re
from webbrowser import open as open_in_browser

import sublime
from sublime_plugin import WindowCommand, TextCommand

from . import diff
from . import intra_line_colorizer
from . import log_graph_rebase_actions
from . import show_file_at_commit
from ..fns import filter_, unique
from ..fns import filter_, flatten, unique
from ..git_command import GitCommand
from ..utils import flash, focus_view, Cache
from ..parse_diff import SplittedDiff
Expand All @@ -32,12 +34,16 @@
"gs_show_commit_show_hunk_on_working_dir",
"gs_show_commit_open_graph_context",
"gs_show_commit_initiate_fixup_commit",
"gs_show_commit_reword_commit",
"gs_line_history_reword_commit",
"gs_show_commit_edit_commit",
)

MYPY = False
if MYPY:
from typing import Dict, Optional, Tuple
from ..types import LineNo, ColNo
from GitSavvy.core.base_commands import GsCommand, Args, Kont

SHOW_COMMIT_TITLE = "SHOW-COMMIT: {}"

Expand Down Expand Up @@ -218,6 +224,61 @@ def run(self, edit):
flash(view, "Could not extract commit message subject")


def extract_commit_hash(self, args, done):
# type: (GsCommand, Args, Kont) -> None
view = log_graph_rebase_actions.get_view_for_command(self)
if not view:
return

diff = SplittedDiff.from_view(view)
commit_hashes = set(filter_(
diff.commit_hash_before_pt(pt)
for pt in unique(flatten(view.sel()))
))

if not commit_hashes:
flash(view, "No commit header found around the cursor.")
return
elif len(commit_hashes) > 1:
flash(view, "Multiple commits are selected.")
return

commit_hash = self.get_short_hash(commit_hashes.pop())
done(commit_hash)


class gs_show_commit_reword_commit(log_graph_rebase_actions.gs_rebase_reword_commit):
defaults = {
"commit_hash": extract_commit_hash,
}

def rebase(self, *args, **kwargs):
rv = super().rebase(*args, **kwargs)
match = re.search(r"^\[detached HEAD (\w+)]", rv, re.M)
if match is not None:
view = self.view
settings = view.settings()
new_commit_hash = match.group(1)

settings.set("git_savvy.show_commit_view.commit", new_commit_hash)
view.run_command("gs_show_commit_refresh")
flash(view, "Now on commit {}".format(new_commit_hash))

return rv


class gs_line_history_reword_commit(log_graph_rebase_actions.gs_rebase_reword_commit):
defaults = {
"commit_hash": extract_commit_hash,
}


class gs_show_commit_edit_commit(log_graph_rebase_actions.gs_rebase_edit_commit):
defaults = {
"commit_hash": extract_commit_hash,
}


class gs_show_commit_toggle_setting(TextCommand):

"""
Expand Down

0 comments on commit 7926c58

Please sign in to comment.