nixvim-config/plugins/gitsigns.nix
2025-03-27 23:44:25 -07:00

59 lines
1.8 KiB
Nix

{ ... }:
{
programs.nixvim = {
extraConfigLua = ''
local function get_base_branch()
-- Try to get the default branch from git config
local default_branch_cmd = "git config --get init.defaultbranch || " ..
"git symbolic-ref refs/remotes/origin/HEAD | sed 's!refs/remotes/origin/!!'"
local default_branch = vim.fn.system(default_branch_cmd):gsub("%s+", "")
-- If default branch is empty or fails, fall back to common defaults
if not default_branch or default_branch == "" then
local common_defaults = {"main", "master", "develop", "development"}
for _, branch in ipairs(common_defaults) do
local branch_exists = vim.fn.system(string.format("git branch --list %s", branch)):match("%s*" .. branch .. "%s*")
if branch_exists and branch_exists ~= "" then
default_branch = branch
break
end
end
end
-- If still no default branch, use current branch
if not default_branch or default_branch == "" then
default_branch = vim.fn.system("git rev-parse --abbrev-ref HEAD"):gsub("%s+", "")
end
return default_branch
end
'';
plugins.gitsigns = {
enable = true;
};
autoGroups = {
DynamicGitsignsBase = {
clear = true;
};
};
autoCmd = [
{
event = [ "BufEnter" "DirChanged" ];
group = "DynamicGitsignsBase";
callback.__raw = ''
function()
local ok, gs = pcall(require, 'gitsigns')
if not ok then return end
local new_base = get_base_branch()
-- Use change_base command if available
pcall(gs.change_base, new_base)
end
'';
}
];
};
}