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

Enable attach() to work with any buffer given the necessary context data #682

Closed
sindrets opened this issue Dec 4, 2022 · 1 comment · Fixed by #683
Closed

Enable attach() to work with any buffer given the necessary context data #682

sindrets opened this issue Dec 4, 2022 · 1 comment · Fixed by #683
Labels
enhancement New feature or request

Comments

@sindrets
Copy link
Contributor

sindrets commented Dec 4, 2022

Could we extend the gitsigns.attach() function to allow for attaching to any arbitrary buffer given the necessary git context data? This would allow plugins to utilize gitsigns to attach to any buffer that represents a real git object.

The motivation for this is that - from time to time - users ask about the possibility to show inline diffs in diffview.nvim. I figured that using gitsigns with toggle_linehl and toggle_deleted is perfect for this. Except that currently I can only do this with the current, working tree version of a file (and fugitive buffers).

Instead of giving gitsigns the capability to parse diffview URI's, I figured it would be a lot more extensible to extend the attach() function such that callers can optionally give it information about:

  • What file the buffer represents.
  • The top-level of it's parent git repository.
  • The path to the git dir.
  • The revision it belongs to (i.e. a commit SHA).
  • The base revision it should be diffed against.

Imagined API

---@param bufnr integer # Buffer number
---@param ctx GitContext
function gitsigns.attach(bufnr, ctx) --[[ ... ]] end

---@class GitContext
---@field toplevel string # Path to the top-level of the repository
---@field gitdir string # Path to the git dir
---@field file string # Path to the file represented by the buffer
---@field commit string # Git revision the file belongs to
---@field base string # Git revision the file should be diffed against

-- Works the same as before when only given a bufnr
gitsigns.attach(12)

-- When given, gitsigns will try to attach to the buffer using the context
-- data, instead of deriving this from the buffer name / path.
gitsigns.attach(12, {
  toplevel = "/path/to/toplevel",
  gitdir = "/path/to/toplevel/.git",
  file = "path/to/file.txt",
  commit = "d076301",
  base = "a4e2275",
})

Is this something you'd be willing to include? If so, I can work on a PR.

@sindrets sindrets added the enhancement New feature or request label Dec 4, 2022
@lewis6991
Copy link
Owner

It's an interesting idea. I'd review a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants