Skip to content
Merged
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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Lazier (documentation will not be available until first use):
"GitDevCleanAll",
"GitDevCloseBuffers",
"GitDevOpen",
"GitDevPersist",
"GitDevRecents",
"GitDevToggleUI",
"GitDevXDGHandle",
Expand Down Expand Up @@ -167,6 +168,22 @@ Supports auto-completion.
- `repo` - Same as `open`.
- `ref` - Same as `open`.

### :floppy_disk: Persist Repository
API: `require("git-dev").persist(repo, ref)`

Command: `GitDevPersist`

Make an ephemeral repository persistent. It will not be deleted when Neovim
exits.
If `repo` is omitted, try to determine repository from current buffer.
If `ref` is omitted, assume it is related to current buffer if an explicit
repository was given.
Supports auto-completion.

#### Parameters
- `repo` - Same as `open`.
- `ref` - Same as `open`.

### :toothbrush: Clean
API: `require("git-dev").clean(repo, ref, opts)`

Expand Down
15 changes: 14 additions & 1 deletion lua/git-dev/history.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ end
---@field ref table
---@field opts table

---@return Key
function History:add(repo, ref, opts, parsed_repo)
---@type GitDevHistoryRecord
local record = {
args = { repo = repo, ref = ref, opts = opts },
parsed = parsed_repo,
}
self._store:set(self:key(record), record)
local key = self:key(record)
self._store:set(key, record)
self:trim()
return key
end

function History:get()
Expand All @@ -41,6 +44,16 @@ function History:key(record)
return record.args.repo .. "|" .. vim.json.encode(record.args.ref)
end

function History:update_opts(key, opts)
local record = self._store:get(key)
if not record then
return
end
record.args.opts =
vim.tbl_deep_extend("force", record.args.opts or {}, opts or {})
self._store:set(key, record)
end

---Purges all history records.
function History:purge()
self._store:purge()
Expand Down
36 changes: 35 additions & 1 deletion lua/git-dev/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ M.open = function(repo, ref, opts)
)

-- Add this call to history store.
M.history:add(repo, ref, opts, parsed_repo)
repo_ctx.history_key = M.history:add(repo, ref, opts, parsed_repo)

ui:print "Done."
end
Expand Down Expand Up @@ -366,6 +366,31 @@ M.close_buffers = function(repo, ref)
return deleted
end

---Makes a repository persistent.
---It will remove the autocmd that deletes the repository directory when
---nvim exits.
---If no repository is given, assume it is related to current buffer.
---@param repo? string
---@param ref? GitRef
M.persist = function(repo, ref)
local repo_ctx = get_session_repo(repo, ref)
if not repo_ctx then
vim.notify "Could not determine repository session."
return
end
if repo_ctx.ephemeral_autocmd_id then
vim.api.nvim_del_autocmd(repo_ctx.ephemeral_autocmd_id)
repo_ctx.ephemeral_autocmd_id = nil
M.session:set_repo(repo_ctx)
if repo_ctx.history_key then
M.history:update_opts(repo_ctx.history_key, { ephemeral = false })
end
vim.notify("Repository is now persistent: " .. repo_ctx.repo)
else
vim.notify("Repository is already persistent: " .. repo_ctx.repo)
end
end

---Cleans a repository. It will close all associated buffers and delete the
---repository directory if it was ephemeral.
---If no repository is given, assume it is related to current buffer.
Expand Down Expand Up @@ -541,6 +566,15 @@ M.setup = function(opts)
.. "with custom paths.",
})

vim.api.nvim_create_user_command("GitDevPersist", function(cmd_args)
local repo, ref = U.parse_cmd_args(cmd_args)
require("git-dev").persist(repo, ref)
end, {
desc = "Make repository persistent",
nargs = "*",
complete = complete_from_session,
})

vim.api.nvim_create_user_command("GitDevCloseBuffers", function(cmd_args)
local repo, ref = U.parse_cmd_args(cmd_args)
require("git-dev").close_buffers(repo, ref)
Expand Down
1 change: 1 addition & 0 deletions lua/git-dev/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
---@field ephemeral_autocmd_id? number
---@field read_only_autocmd_id? number
---@field set_session_autocmd_id? number
---@field history_key? Key

---@alias GitDevSessionRepos table<number, GitDevSessionRepo>

Expand Down