Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions lua/neogit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,35 @@ function M.setup(opts)
signs.setup(config.values)
state.setup(config.values)
autocmds.setup()

if config.values.auto_fetch_enabled then
local fetch = require("neogit.lib.git.fetch")
local git = require("neogit.lib.git")

if M.auto_fetch_timer then
M.auto_fetch_timer:close()
end

M.auto_fetch_timer = vim.uv.new_timer()
M.auto_fetch_timer:start(
config.values.auto_fetch_interval,
config.values.auto_fetch_interval,
vim.schedule_wrap(function()
if git.cli.is_inside_worktree(vim.uv.cwd()) then
fetch.fetch("--all")
end
end)
)

if config.values.auto_fetch_on_startup then
if git.cli.is_inside_worktree(vim.uv.cwd()) then
fetch.fetch("--all")
end
end
elseif M.auto_fetch_timer then
M.auto_fetch_timer:close()
M.auto_fetch_timer = nil
end
end

local function construct_opts(opts)
Expand Down
9 changes: 9 additions & 0 deletions lua/neogit/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,9 @@ end
---@field disable_signs? boolean Special signs to draw for sections etc. in Neogit
---@field prompt_force_push? boolean Offer to force push when branches diverge
---@field git_services? table Templartes to use when opening a pull request for a branch
---@field auto_fetch_enabled? boolean Enable/disable automatic fetching
---@field auto_fetch_interval? integer The interval (in milliseconds) for automatic fetching
---@field auto_fetch_on_startup? boolean Perform an initial fetch when Neogit starts
---@field fetch_after_checkout? boolean Perform a fetch if the newly checked out branch has an upstream or pushRemote set
---@field telescope_sorter? function The sorter telescope will use
---@field process_spinner? boolean Hide/Show the process spinner
Expand Down Expand Up @@ -399,6 +402,9 @@ function M.get_default_values()
disable_insert_on_commit = "auto",
use_per_project_settings = true,
remember_settings = true,
auto_fetch_enabled = false,
auto_fetch_interval = 300000, -- 5 minutes in milliseconds
auto_fetch_on_startup = false,
fetch_after_checkout = false,
sort_branches = "-committerdate",
kind = "tab",
Expand Down Expand Up @@ -1132,6 +1138,9 @@ function M.validate_config()
end

if validate_type(config, "base config", "table") then
validate_type(config.auto_fetch_enabled, "auto_fetch_enabled", "boolean")
validate_type(config.auto_fetch_interval, "auto_fetch_interval", "number")
validate_type(config.auto_fetch_on_startup, "auto_fetch_on_startup", "boolean")
validate_type(config.disable_hint, "disable_hint", "boolean")
validate_type(config.disable_context_highlighting, "disable_context_highlighting", "boolean")
validate_type(config.disable_signs, "disable_signs", "boolean")
Expand Down
25 changes: 23 additions & 2 deletions lua/neogit/lib/git/fetch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,32 @@ function M.fetch_interactive(remote, branch, args)
return git.cli.fetch.args(remote or "", branch or "").arg_list(args).call { pty = true }
end

local a = require("plenary.async")
local notification = require("neogit.lib.notification")

---@param remote string
---@param branch string
---@return ProcessResult
function M.fetch(remote, branch)
return git.cli.fetch.args(remote, branch).call { ignore_error = true }
notification.info("Fetching...")
a.void(function()
local result = git.cli.fetch.args(remote, branch).call { ignore_error = true }

if result and result.code == 0 then
notification.info("Fetch complete.")
elseif result then
local error_message = "Fetch failed: "
if type(result.stderr) == "table" then
error_message = error_message .. table.concat(result.stderr, "\n")
elseif type(result.stderr) == "string" then
error_message = error_message .. result.stderr
else
error_message = error_message .. "Unknown error format."
end
notification.error(error_message)
else
notification.error("Fetch failed: An unexpected error occurred and no result was returned.")
end
end)()
end

return M
Loading