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

fix(gitbrowse): open commit not under cursor #393

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mosheavni
Copy link

Description

Open a commit if cword is not a commit and what is "commit"

Related Issue(s)

#373

@mosheavni mosheavni changed the title fix/gitbrowse-commit fix(gitbrowse): open commit not under cursor Jan 6, 2025
@mosheavni
Copy link
Author

@folke any thoughts here?

@dpetka2001
Copy link
Contributor

dpetka2001 commented Jan 6, 2025

Wouldn't it be better to expose a field commit to the user and he can use that to specify the commit he wants? Commits don't seem to have anything to do with branches or am I wrong? Something like

diff --git a/lua/snacks/gitbrowse.lua b/lua/snacks/gitbrowse.lua
index db71825..7f5459a 100644
--- a/lua/snacks/gitbrowse.lua
+++ b/lua/snacks/gitbrowse.lua
@@ -28,6 +28,7 @@ local defaults = {
   ---@type "repo" | "branch" | "file" | "commit"
   what = "file", -- what to open. not all remotes support all types
   branch = nil, ---@type string?
+  commit = nil, ---@type string?
   line_start = nil, ---@type number?
   line_end = nil, ---@type number?
   -- patterns to transform remotes to an actual URL
@@ -142,7 +143,8 @@ function M._open(opts)
   file = file and (uv.fs_stat(file) or {}).type == "file" and vim.fs.normalize(file) or nil
   local cwd = file and vim.fn.fnamemodify(file, ":h") or vim.fn.getcwd()
   local word = vim.fn.expand("<cword>")
-  local is_commit = is_valid_commit_hash(word, cwd)
+  local commit_to_check = opts.commit or word
+  local is_commit = commit_to_check and is_valid_commit_hash(commit_to_check, cwd)
   ---@type snacks.gitbrowse.Fields
   local fields = {
     branch = opts.branch
@@ -150,7 +152,7 @@ function M._open(opts)
     file = file and system({ "git", "-C", cwd, "ls-files", "--full-name", file }, "Failed to get git file path")[1],
     line_start = opts.line_start,
     line_end = opts.line_end,
-    commit = is_commit and word or nil,
+    commit = is_commit and commit_to_check or nil,
   }

maybe? And user can then do lua Snacks.gitbrowse({what = "commit", commit = "ffd7740"}) for example? I've tested this only with Github. Not sure if the rest also work with this as I don't have access.

PS: This is just a suggestion. Feel free to ignore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants