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

Added support for git stash list #1280

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
04fbcd4
wip(base): Building foundation for git stash list
AlphabetsAlphabets Apr 13, 2024
17739b3
feat(stash): Link everything together
AlphabetsAlphabets Apr 17, 2024
df55c43
fix(stash_list_view): `M.open` to `M:open`
AlphabetsAlphabets Apr 17, 2024
992cd3a
feat(config): Default from "split" to "tab"
AlphabetsAlphabets Apr 18, 2024
6c00308
feat(stash): Cleaned up `M.list`
AlphabetsAlphabets Apr 18, 2024
c1ce446
feat(M.stash): Removed `after` argument
AlphabetsAlphabets Apr 18, 2024
b185356
wip(M.Stash): Building ui
AlphabetsAlphabets Apr 18, 2024
12124b5
feat(buffer): change name of buffer
AlphabetsAlphabets Apr 19, 2024
4ee97c0
wip(M.Stash): structure of UI elements
AlphabetsAlphabets Apr 19, 2024
3b7fa9c
feat(M.stash): Moved to more appropriate location
AlphabetsAlphabets Apr 19, 2024
ff7c87b
fix(M.stash): Removed erroneous local
AlphabetsAlphabets Apr 19, 2024
a76789c
fix(cli calls): Removed CLI calls.
AlphabetsAlphabets May 6, 2024
b99080a
fix(draw): Updated drawing code
AlphabetsAlphabets May 6, 2024
4098f2e
feat(return): Updated return to have info for stash list
AlphabetsAlphabets May 6, 2024
1ef03c0
feat(clean): Removed unused variables and renamed variables
AlphabetsAlphabets May 11, 2024
35c221e
feat(concat): Used format instead of `..` to concat strings
AlphabetsAlphabets May 11, 2024
3d6c898
wip(view_stash): Passing oid to CommitViewBuffer
AlphabetsAlphabets May 11, 2024
76bc6bb
feat(whitespace): Removed erroenous whitespace causing
AlphabetsAlphabets May 11, 2024
bd37ec2
Merge branch 'NeogitOrg:master' into fork-master
AlphabetsAlphabets May 11, 2024
812a8c4
Merge branch 'master' into fork-master
AlphabetsAlphabets May 23, 2024
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
2 changes: 1 addition & 1 deletion lua/neogit/buffers/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ M.CommitEntry = Component.new(function(commit, args)
}, graph, { text(" ") }, ref, ref_last, { text(commit.subject) }),
{
virtual_text = {
{ " ", "Constant" },
{ " ", "Constant" },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{ " ", "Constant" },
{ " ", "Constant" },

{
util.str_clamp(commit.author_name, 30 - (#commit.rel_date > 10 and #commit.rel_date or 10)),
"NeogitGraphAuthor",
Expand Down
60 changes: 60 additions & 0 deletions lua/neogit/buffers/stash_list_view/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
local Buffer = require("neogit.lib.buffer")
local config = require("neogit.config")
local CommitViewBuffer = require("neogit.buffers.commit_view")

local git = require("neogit.lib.git")
local ui = require("neogit.buffers.stash_list_view.ui")

---@class StashListBuffer
---@field stashes StashEntry[]
local M = {}
M.__index = M

--- Gets all current stashes
function M.new(stashes)
local instance = {
stashes = stashes,
}

setmetatable(instance, M)
return instance
end

function M:close()
self.buffer:close()
self.buffer = nil
end

--- Creates a buffer populated with output of `git stash list`
--- and supports related operations.
function M:open()
self.buffer = Buffer.create {
name = "NeogitStashView",
filetype = "NeogitStashView",
kind = config.values.stash.kind,
context_higlight = true,
-- Define the available mappings here. `git stash list` has the same options
-- as `git log` refer to git-log(1) for more info.
mappings = {
n = {
["q"] = function()
self:close()
end,
["<esc>"] = function()
self:close()
end,
["<enter>"] = function()
CommitViewBuffer.new(git.rev_parse.oid(self.buffer.ui:get_commit_under_cursor())):open("tab")
end,
}
},
after = function()
vim.cmd([[setlocal nowrap]])
end,
render = function()
return ui.View(self.stashes)
end,
}
end

return M
36 changes: 36 additions & 0 deletions lua/neogit/buffers/stash_list_view/ui.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local Ui = require("neogit.lib.ui")
local Component = require("neogit.lib.ui.component")
local util = require("neogit.lib.util")

local text = Ui.text
local col = Ui.col
local row = Ui.row

local M = {}

---Parses output of `git stash list` and splits elements into table
M.Stash = Component.new(function(stash)
local label = table.concat({"stash@{", stash.idx, "}" }, "")
return col({
row({
text.highlight("Comment")(label),
text(" "),
text(stash.message),
}, {
virtual_text = {
{ " ", "Constant" },
{ stash.rel_date, "Special" },
},
}),
}, { oid = label })
end)

---@param stashes StashEntry[]
---@return table
function M.View(stashes)
return util.map(stashes, function(stash)
return M.Stash(stash)
end)
end

return M
3 changes: 3 additions & 0 deletions lua/neogit/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,9 @@ function M.get_default_values()
popup = {
kind = "split",
},
stash = {
kind = "tab",
},
refs_view = {
kind = "tab",
},
Expand Down
11 changes: 11 additions & 0 deletions lua/neogit/lib/git/stash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ local util = require("neogit.lib.util")
---@class NeogitGitStash
local M = {}

---@class StashEntry
---@field stash_id string the id of the stash i.e. stash@{7}
---@field rel_date string relative timestamp
---@field message string the message associated with each stash.

local function perform_stash(include)
if not include then
return
Expand Down Expand Up @@ -127,7 +132,13 @@ function M.register(meta)
state.stashes.items = util.map(M.list(), function(line)
local idx, message = line:match("stash@{(%d*)}: (.*)")

---@class StashEntry
return {
rel_date = cli.log
.max_count(1)
.format("%cr")
.args(("stash@{%s}"):format(idx))
.call({ hidden = true }).stdout[1],
idx = tonumber(idx),
name = line,
message = message,
Expand Down
6 changes: 6 additions & 0 deletions lua/neogit/popups/stash/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local operation = require("neogit.operations")
local input = require("neogit.lib.input")

local FuzzyFinderBuffer = require("neogit.buffers.fuzzy_finder")
local StashListBuffer = require("neogit.buffers.stash_list_view")

local M = {}

Expand Down Expand Up @@ -64,6 +65,11 @@ function M.drop(popup)
use("drop", popup.state.env.stash, { confirm = true })
end

--- git stash list
function M.list()
StashListBuffer.new(git.repo.state.stashes.items):open()
end

M.rename = operation("stash_rename", function(popup)
use("rename", popup.state.env.stash)
end)
Expand Down
2 changes: 1 addition & 1 deletion lua/neogit/popups/stash/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function M.create(stash)
:action("a", "apply", actions.apply)
:action("d", "drop", actions.drop)
:new_action_group("Inspect")
:action("l", "List")
:action("l", "List", actions.list)
:action("v", "Show")
:new_action_group("Transform")
:action("b", "Branch")
Expand Down