Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: inline diff select commit/branch to diff #1855

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Default.sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,15 @@
{ "key": "selector", "operand": "git-savvy.make-commit meta.dropped.git.commit" }
]
},
{
"keys": ["s"],
"command": "gs_commit_view_open_transient_view",
"context": [
{ "key": "setting.command_mode", "operator": "equal", "operand": false },
{ "key": "setting.git_savvy.commit_view", "operator": "equal", "operand": true },
{ "key": "selector", "operand": "git-savvy.make-commit meta.dropped.git.commit" }
]
},


///////////////
Expand Down
18 changes: 18 additions & 0 deletions core/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"gs_commit_view_do_commit",
"gs_commit_view_sign",
"gs_commit_view_close",
"gs_commit_view_open_transient_view",
"gs_commit_log_helper",
"GsPrepareCommitFocusEventListener",
"GsPedanticEnforceEventListener",
Expand Down Expand Up @@ -617,6 +618,23 @@
self.view.close()


class gs_commit_view_open_transient_view(WindowCommand):
def run(self):
window = sublime.active_window()
view = window.active_view()
text = view.substr(sublime.Region(0, view.size()))
text = text.replace(COMMIT_HELP_TEXT, "## To make a commit,\n")
text = text.replace(HELP_WHEN_PATCH_IS_VISIBLE, "")
text = text.replace(HELP_WHEN_UNSTAGING_IS_POSSIBLE, "")
text = text.replace(HELP_WHEN_DISCARDING_IS_POSSIBLE, "")
text = text.replace('diff --git', '\ndiff --git')

window.run_command('set_layout', { "cols": [0.0, 0.5, 1.0], "rows": [0.0, 1.0], "cells": [[0, 0, 1, 1], [1, 0, 2, 1]] })

Check failure on line 632 in core/commands/commit.py

View workflow job for this annotation

GitHub Actions / lint

whitespace after '{'

Check failure on line 632 in core/commands/commit.py

View workflow job for this annotation

GitHub Actions / lint

line too long (128 > 120 characters)

Check failure on line 632 in core/commands/commit.py

View workflow job for this annotation

GitHub Actions / lint

whitespace before '}'
window.run_command('focus_group', { "group": 1 })

Check failure on line 633 in core/commands/commit.py

View workflow job for this annotation

GitHub Actions / lint

whitespace after '{'

Check failure on line 633 in core/commands/commit.py

View workflow job for this annotation

GitHub Actions / lint

whitespace before '}'
view_transient = window.new_file(sublime.NewFileFlags.TRANSIENT, "Packages/GitSavvy/syntax/make_commit.sublime-syntax")

Check failure on line 634 in core/commands/commit.py

View workflow job for this annotation

GitHub Actions / lint

line too long (127 > 120 characters)
view_transient.run_command("append", {"characters": text})


class gs_commit_log_helper(TextCommand, LogHelperMixin):
def run(self, edit, prefix="fixup! ", move_to_eol=True):
view = self.view
Expand Down
82 changes: 80 additions & 2 deletions core/commands/inline_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"gs_inline_diff_navigate_hunk",
"gs_inline_diff_undo",
"GsInlineDiffFocusEventListener",
"SelectInlineDiffCommitCommand",
)


Expand Down Expand Up @@ -131,7 +132,8 @@
hunks or individual lines, and to navigate between hunks.
"""

def run(self, cached=None, match_current_position="<SENTINEL>"):
def run(self, cached=None, match_current_position="<SENTINEL>",
base_commit=None, target_commit=None):
# type: (Optional[bool], object) -> None
if match_current_position != "<SENTINEL>":
print(
Expand Down Expand Up @@ -182,7 +184,9 @@
"file_path": file_path,
"syntax": syntax_file,
"cached": bool(cached),
"match_position": cur_pos
"match_position": cur_pos,
"base_commit": base_commit,
"target_commit": target_commit,
})

def open_from_diff_view(self, view):
Expand Down Expand Up @@ -1212,3 +1216,77 @@
"match_position": cur_pos,
"sync": True
})

class SelectInlineDiffCommitCommand(WindowCommand, GitCommand):

Check failure on line 1220 in core/commands/inline_diff.py

View workflow job for this annotation

GitHub Actions / lint

expected 2 blank lines, found 1
def _on_commits(self):
commit_text_list = self.git('log', '--format="%h %s"', '-n', str(self._last_n_commits)).strip().splitlines()
if not commit_text_list:
return self._on_cancel()
self._commit_desc_list = []
for txt in commit_text_list:
txt = txt.strip('"')
commit_desc = txt.split(' ', 1)
self._commit_desc_list.append(commit_desc)
self.window.show_quick_panel(self._commit_desc_list, self._on_select_commits)

def _on_select_commits(self, _id):
if _id == -1:
return self._on_cancel()
commit_desc = self._commit_desc_list[_id]
commit_id = commit_desc[0]
self._on_done(commit_id)

def _on_branches(self):
branches_text_list = self.git("branch").strip().splitlines()
if not branches_text_list:
return self._on_cancel()
self._branch_list = []
for txt in branches_text_list:
branch = txt.strip(' *')
self._branch_list.append(branch)
self.window.show_quick_panel(self._branch_list, self._on_select_branches)

def _on_select_branches(self, _id):
if _id == -1:
return self._on_cancel()
branch = self._branch_list[_id]
commit_id = self.git("rev-parse", "--short", branch).strip().splitlines()[0]
if not commit_id:
return self._on_cancel()
self._on_done(commit_id)

def _on_done(self, commit_id):
if not commit_id:
return self._on_cancel()
view = self.window.active_view()
settings = view.settings()

Check failure on line 1262 in core/commands/inline_diff.py

View workflow job for this annotation

GitHub Actions / lint

local variable 'settings' is assigned to but never used
base_commit = self.git("rev-parse", "--short", "HEAD").strip().splitlines()[0]
self.window.run_command("gs_inline_diff", {
"cached": self._cached,
"base_commit": base_commit,
"target_commit": commit_id,
})

def _on_change(self, *args, **kwargs):
pass

def _on_cancel(self, *args, **kwargs):
pass

def _on_select_mode(self, _id):
if _id == -1:
return self.on_cancel()
elif _id == 0:
self.window.show_input_panel('commit id', '', self._on_done, self._on_change, self._on_cancel)
return
mode = self._modes[_id]
if mode == 'commits':
self._on_commits()
else: # mode == 'branches'

Check failure on line 1285 in core/commands/inline_diff.py

View workflow job for this annotation

GitHub Actions / lint

at least two spaces before inline comment
self._on_branches()

def run(self, cached=None, last_n_commits=20):
self._cached = cached
self._last_n_commits = last_n_commits
self._modes = ['[commit id]', 'branches', 'commits']
self.window.show_quick_panel(self._modes, self._on_select_mode)
Loading