Skip to content

Commit

Permalink
Add "pickaxe" command
Browse files Browse the repository at this point in the history
  • Loading branch information
kaste committed Jun 17, 2024
1 parent ae23d92 commit bc8e890
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
4 changes: 4 additions & 0 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
"caption": "git: Line History",
"command": "gs_line_history"
},
{
"caption": "git: Pick-axe selection",
"command": "gs_graph_pickaxe"
},
{
"caption": "github: open file on remote",
"command": "gs_github_open_file_on_remote",
Expand Down
52 changes: 50 additions & 2 deletions core/commands/log_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import re
import shlex
import subprocess
import textwrap
import time
import threading
from typing import NamedTuple
Expand Down Expand Up @@ -42,14 +43,15 @@
)
from ..ui_mixins.input_panel import show_single_line_input_panel
from ..ui_mixins.quick_panel import show_branch_panel
from ..utils import add_selection_to_jump_history, focus_view, show_toast, Cache, SEPARATOR
from ..utils import add_selection_to_jump_history, flash, focus_view, show_toast, Cache, SEPARATOR
from ...common import util
from ...common.theme_generator import ThemeGenerator


__all__ = (
"gs_graph",
"gs_graph_current_file",
"gs_graph_pickaxe",
"gs_log_graph_refresh",
"gs_log_graph",
"gs_log_graph_tab_out",
Expand Down Expand Up @@ -247,6 +249,28 @@ def run(self, **kwargs):
self.window.status_message("View has no filename to track.")


class gs_graph_pickaxe(TextCommand, GitCommand):
def run(self, edit):
# type: (sublime.Edit) -> None
view = self.view
window = view.window()
if not window:
return
repo_path = self.repo_path
frozen_sel = list(view.sel())
filters = " ".join(
shlex.quote("-S{}".format(s))
for r in frozen_sel
if (s := view.substr(r))
if (s.strip())
)
if not filters:
flash(view, "Nothing selected.")
return

window.run_command("gs_graph", {"repo_path": repo_path, "filters": filters})


def augment_color_scheme(view):
# type: (sublime.View) -> None
settings = GitSavvySettings()
Expand Down Expand Up @@ -1317,6 +1341,30 @@ def prelude(view):
elif repo_path:
prelude += " REPO: {}\n".format(repo_path)

if apply_filters:
pickaxes, normal_ones = [], []
for arg in shlex.split(filters):
if arg.startswith("-S") or arg.startswith("-G"):
if "\n" in arg:
pickaxes.append(
"\n {}'''\n{}\n '''".format(
arg[:2],
textwrap.indent(textwrap.dedent(arg[2:].rstrip()), " ")
)
)
else:
normal_ones.append(
"{}'{}'".format(
arg[:2],
arg[3:-1] if (arg[2], arg[-1]) == ("'", "'") else arg[2:]
)
)
else:
normal_ones.append(arg)
formatted_filters = "\n".join(filter_((" ".join(normal_ones), "".join(pickaxes))))
else:
formatted_filters = None

prelude += (
" "
+ " ".join(filter_((
Expand All @@ -1326,7 +1374,7 @@ def prelude(view):
else '[a]ll: true' if all_branches else '[a]ll: false'
),
" ".join(branches) if not all_branches and not overview else None,
filters if apply_filters else None
formatted_filters
)))
)
return prelude + "\n\n"
Expand Down
21 changes: 14 additions & 7 deletions syntax/graph.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ variables:

contexts:
main:
- match: ^
push:
- meta_scope: meta.prelude.git_savvy.graph
- match: '^(?=\S)'
set: graph
- match: .*
scope: comment.prelude.git_savvy.graph
- match: '^$\n'
set: prelude

prelude:
- meta_scope: meta.prelude.git_savvy.graph
- match: .*
scope: comment.prelude.git_savvy.graph
- match: '^$\n'
set: prelude_end

prelude_end:
- meta_scope: meta.prelude.git_savvy.graph
- match: '^(?=\S)'
set: graph

graph:
- meta_scope: meta.content.git_savvy.graph
Expand Down
7 changes: 7 additions & 0 deletions syntax/test/syntax_test_graph.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# SYNTAX TEST "Packages/GitSavvy/syntax/graph.sublime-syntax"

# <- meta.prelude
REPO: C:\Users\c-flo\AppData\Roaming\Sublime Text\Packages\SublimeLinter
[a]ll: true

# <- meta.prelude
* 2b17192 (HEAD -> develop, origin/master, origin/HEAD, master) replace all references to .tmLanguage to .sublime-syntax
# <- meta.content
# <- keyword.graph.commit
# ^ constant.numeric.graph.commit-hash
# ^^^^ constant.other.git.head
Expand Down
27 changes: 27 additions & 0 deletions syntax/test/syntax_test_graph_pick_axe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SYNTAX TEST "Packages/GitSavvy/syntax/graph.sublime-syntax"

REPO: C:\Users\c-flo\AppData\Roaming\Sublime Text\Packages\GitSavvy
[a]ll: false
-S'''
class gs_revert_commit(LogMixin, WindowCommand, GitCommand):
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ git-savvy.graph meta.prelude.git_savvy.graph comment.prelude.git_savvy.graph
@on_worker
# ^^^^^^^^^^^^^^^^ git-savvy.graph meta.prelude.git_savvy.graph comment.prelude.git_savvy.graph
def do_action(self, commit_hash, **kwargs):
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ git-savvy.graph meta.prelude.git_savvy.graph comment.prelude.git_savvy.graph
try:
# ^^^^^^^^^^^^^^ git-savvy.graph meta.prelude.git_savvy.graph comment.prelude.git_savvy.graph
self.git("revert", *(commit_hash if isinstance(commit_hash, list) else [commit_hash]))
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ git-savvy.graph meta.prelude.git_savvy.graph comment.prelude.git_savvy.graph
finally:
# ^^^^^^^^^^^^^^^^^^ git-savvy.graph meta.prelude.git_savvy.graph comment.prelude.git_savvy.graph
util.view.refresh_gitsavvy(self.window.active_view(), refresh_sidebar=True)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ git-savvy.graph meta.prelude.git_savvy.graph comment.prelude.git_savvy.graph
'''
# ^^^ git-savvy.graph meta.prelude.git_savvy.graph comment.prelude.git_savvy.graph

#< git-savvy.graph meta.prelude.git_savvy.graph
...
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ git-savvy.graph meta.content.git_savvy.graph
● 96c5f826 Allow to revert multiple commits in one invocation ​ Thu May 2 21:44, herr kaste
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ git-savvy.graph meta.content.git_savvy.graph

0 comments on commit bc8e890

Please sign in to comment.