feat: Add support for multiple lazygit config files
Lazygit supports using multiple config files: https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md?plain=1#L346 This commit adds code to parse vim.g.lazygit_config_file_path as a `string | table`, then passes the files to `lazygit` via the `-ucf` flag
This commit is contained in:
committed by
Dheepak Krishnamurthy
parent
75c920883f
commit
de35012036
@@ -66,6 +66,8 @@ let g:lazygit_use_neovim_remote = 1 " fallback to 0 if neovim-remote is not inst
|
|||||||
|
|
||||||
let g:lazygit_use_custom_config_file_path = 0 " config file path is evaluated if this value is 1
|
let g:lazygit_use_custom_config_file_path = 0 " config file path is evaluated if this value is 1
|
||||||
let g:lazygit_config_file_path = '' " custom config file path
|
let g:lazygit_config_file_path = '' " custom config file path
|
||||||
|
" OR
|
||||||
|
let g:lazygit_config_file_path = [] " list of custom config file paths
|
||||||
```
|
```
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
@@ -77,6 +79,8 @@ vim.g.lazygit_use_neovim_remote = 1 -- fallback to 0 if neovim-remote is not ins
|
|||||||
|
|
||||||
vim.g.lazygit_use_custom_config_file_path = 0 -- config file path is evaluated if this value is 1
|
vim.g.lazygit_use_custom_config_file_path = 0 -- config file path is evaluated if this value is 1
|
||||||
vim.g.lazygit_config_file_path = '' -- custom config file path
|
vim.g.lazygit_config_file_path = '' -- custom config file path
|
||||||
|
-- OR
|
||||||
|
vim.g.lazygit_config_file_path = {} -- table of custom config file paths
|
||||||
```
|
```
|
||||||
|
|
||||||
Call `:LazyGit` to start a floating window with `lazygit` in the current working directory.
|
Call `:LazyGit` to start a floating window with `lazygit` in the current working directory.
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ local project_root_dir = require("lazygit.utils").project_root_dir
|
|||||||
local get_root = require("lazygit.utils").get_root
|
local get_root = require("lazygit.utils").get_root
|
||||||
local is_lazygit_available = require("lazygit.utils").is_lazygit_available
|
local is_lazygit_available = require("lazygit.utils").is_lazygit_available
|
||||||
local is_symlink = require("lazygit.utils").is_symlink
|
local is_symlink = require("lazygit.utils").is_symlink
|
||||||
|
local open_or_create_config = require("lazygit.utils").open_or_create_config
|
||||||
|
|
||||||
local fn = vim.fn
|
local fn = vim.fn
|
||||||
|
|
||||||
@@ -47,23 +48,33 @@ local function exec_lazygit_command(cmd)
|
|||||||
end
|
end
|
||||||
|
|
||||||
local function lazygitdefaultconfigpath()
|
local function lazygitdefaultconfigpath()
|
||||||
return fn.substitute(fn.system("lazygit -cd"), "\n", "", "")
|
-- lazygit -cd gives only the config dir, not the config file, so concat config.yml
|
||||||
|
return fn.substitute(fn.system("lazygit -cd"), "\n", "", "") .. "/config.yml"
|
||||||
end
|
end
|
||||||
|
|
||||||
local function lazygitgetconfigpath()
|
local function lazygitgetconfigpath()
|
||||||
|
local default_config_path = lazygitdefaultconfigpath()
|
||||||
|
-- if vim.g.lazygit_config_file_path is a table, check if all config files exist
|
||||||
if vim.g.lazygit_config_file_path then
|
if vim.g.lazygit_config_file_path then
|
||||||
-- if file exists
|
if type(vim.g.lazygit_config_file_path) == "table" then
|
||||||
if fn.empty(fn.glob(vim.g.lazygit_config_file_path)) == 0 then
|
for _, config_file in ipairs(vim.g.lazygit_config_file_path) do
|
||||||
|
if fn.empty(fn.glob(config_file)) == 1 then
|
||||||
|
print("lazygit: custom config file path: '" .. config_file .. "' could not be found. Returning default config")
|
||||||
|
return default_config_path
|
||||||
|
end
|
||||||
|
end
|
||||||
return vim.g.lazygit_config_file_path
|
return vim.g.lazygit_config_file_path
|
||||||
|
elseif fn.empty(fn.glob(vim.g.lazygit_config_file_path)) == 0 then
|
||||||
|
return vim.g.lazygit_config_file_path
|
||||||
|
else
|
||||||
|
print("lazygit: custom config file path: '" .. vim.g.lazygit_config_file_path .. "' could not be found. Returning default config")
|
||||||
|
return default_config_path
|
||||||
end
|
end
|
||||||
|
|
||||||
print("lazygit: custom config file path: '" .. vim.g.lazygit_config_file_path .. "' could not be found")
|
|
||||||
else
|
else
|
||||||
print("lazygit: custom config file path is not set, option: 'lazygit_config_file_path' is missing")
|
print("lazygit: custom config file path is not set, option: 'lazygit_config_file_path' is missing")
|
||||||
|
-- any issue with the config file we fallback to the default config file path
|
||||||
|
return default_config_path
|
||||||
end
|
end
|
||||||
|
|
||||||
-- any issue with the config file we fallback to the default config file path
|
|
||||||
return lazygitdefaultconfigpath()
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- :LazyGit entry point
|
--- :LazyGit entry point
|
||||||
@@ -83,7 +94,11 @@ local function lazygit(path)
|
|||||||
_ = project_root_dir()
|
_ = project_root_dir()
|
||||||
|
|
||||||
if vim.g.lazygit_use_custom_config_file_path == 1 then
|
if vim.g.lazygit_use_custom_config_file_path == 1 then
|
||||||
cmd = cmd .. " -ucf " .. lazygitgetconfigpath()
|
local config_path = lazygitgetconfigpath()
|
||||||
|
if type(config_path) == "table" then
|
||||||
|
config_path = table.concat(config_path, ",")
|
||||||
|
end
|
||||||
|
cmd = cmd .. " -ucf '" .. config_path .. "'" -- quote config_path to avoid whitespace errors
|
||||||
end
|
end
|
||||||
|
|
||||||
if path == nil then
|
if path == nil then
|
||||||
@@ -131,28 +146,18 @@ end
|
|||||||
local function lazygitconfig()
|
local function lazygitconfig()
|
||||||
local config_file = lazygitgetconfigpath()
|
local config_file = lazygitgetconfigpath()
|
||||||
|
|
||||||
if fn.empty(fn.glob(config_file)) == 1 then
|
if type(config_file) == "table" then
|
||||||
-- file does not exist
|
vim.ui.select(
|
||||||
-- check if user wants to create it
|
config_file,
|
||||||
local answer = fn.confirm(
|
{ prompt = "select config file to edit" },
|
||||||
"File "
|
function (path)
|
||||||
.. config_file
|
open_or_create_config(path)
|
||||||
.. " does not exist.\nDo you want to create the file and populate it with the default configuration?",
|
end
|
||||||
"&Yes\n&No"
|
|
||||||
)
|
)
|
||||||
if answer == 2 then
|
|
||||||
return nil
|
|
||||||
end
|
|
||||||
if fn.isdirectory(fn.fnamemodify(config_file, ":h")) == false then
|
|
||||||
-- directory does not exist
|
|
||||||
fn.mkdir(fn.fnamemodify(config_file, ":h"), "p")
|
|
||||||
end
|
|
||||||
vim.cmd("edit " .. config_file)
|
|
||||||
vim.cmd([[execute "silent! 0read !lazygit -c"]])
|
|
||||||
vim.cmd([[execute "normal 1G"]])
|
|
||||||
else
|
else
|
||||||
vim.cmd("edit " .. config_file)
|
open_or_create_config(config_file)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -92,6 +92,30 @@ local function is_symlink()
|
|||||||
return resolved ~= fn.expand('%:p')
|
return resolved ~= fn.expand('%:p')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function open_or_create_config(path)
|
||||||
|
if fn.empty(fn.glob(path)) == 1 then
|
||||||
|
-- file does not exist
|
||||||
|
-- check if user wants to create it
|
||||||
|
local answer = fn.confirm(
|
||||||
|
"File "
|
||||||
|
.. path
|
||||||
|
.. " does not exist.\nDo you want to create the file and populate it with the default configuration?",
|
||||||
|
"&Yes\n&No"
|
||||||
|
)
|
||||||
|
if answer == 2 then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
if fn.isdirectory(fn.fnamemodify(path, ":h")) == false then
|
||||||
|
-- directory does not exist
|
||||||
|
fn.mkdir(fn.fnamemodify(path, ":h"), "p")
|
||||||
|
end
|
||||||
|
vim.cmd("edit " .. path)
|
||||||
|
vim.cmd([[execute "silent! 0read !lazygit -c"]])
|
||||||
|
vim.cmd([[execute "normal 1G"]])
|
||||||
|
else
|
||||||
|
vim.cmd("edit " .. path)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return {
|
return {
|
||||||
get_root = get_root,
|
get_root = get_root,
|
||||||
@@ -99,4 +123,5 @@ return {
|
|||||||
lazygit_visited_git_repos = lazygit_visited_git_repos,
|
lazygit_visited_git_repos = lazygit_visited_git_repos,
|
||||||
is_lazygit_available = is_lazygit_available,
|
is_lazygit_available = is_lazygit_available,
|
||||||
is_symlink = is_symlink,
|
is_symlink = is_symlink,
|
||||||
|
open_or_create_config = open_or_create_config,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user