Skip to content

Commit b1dec75

Browse files
Copilotcathyprime
authored andcommitted
Add git_executable configuration parameter to setup() (#1)
Co-authored-by: cathyprime <115636257+cathyprime@users.noreply.github.com>
1 parent 268ddf7 commit b1dec75

File tree

5 files changed

+67
-5
lines changed

5 files changed

+67
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ neogit.setup {
7878
disable_context_highlighting = false,
7979
-- Disables signs for sections/items/hunks
8080
disable_signs = false,
81+
-- Path to git executable. Defaults to "git". Can be used to specify a custom git binary or wrapper script.
82+
git_executable = "git",
8183
-- Offer to force push when branches diverge
8284
prompt_force_push = true,
8385
-- Changes what mode the Commit Editor starts in. `true` will leave nvim in normal mode, `false` will change nvim to

doc/neogit.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ to Neovim users.
9494
disable_context_highlighting = false,
9595
-- Disables signs for sections/items/hunks
9696
disable_signs = false,
97+
-- Path to git executable. Defaults to "git". Can be used to specify a custom git binary or wrapper script.
98+
git_executable = "git",
9799
-- Offer to force push when branches diverge
98100
prompt_force_push = true,
99101
-- Changes what mode the Commit Editor starts in. `true` will leave nvim in normal mode, `false` will change nvim to

lua/neogit/config.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ end
343343
---@class NeogitConfig Neogit configuration settings
344344
---@field filewatcher? NeogitFilewatcherConfig Values for filewatcher
345345
---@field graph_style? NeogitGraphStyle Style for graph
346+
---@field git_executable? string Path to git executable (defaults to "git")
346347
---@field commit_date_format? string Commit date format
347348
---@field log_date_format? string Log date format
348349
---@field disable_hint? boolean Remove the top hint in the Status buffer
@@ -436,6 +437,7 @@ function M.get_default_values()
436437
},
437438
},
438439
highlight = {},
440+
git_executable = "git",
439441
disable_insert_on_commit = "auto",
440442
use_per_project_settings = true,
441443
remember_settings = true,
@@ -1182,6 +1184,7 @@ function M.validate_config()
11821184
validate_type(config.disable_hint, "disable_hint", "boolean")
11831185
validate_type(config.disable_context_highlighting, "disable_context_highlighting", "boolean")
11841186
validate_type(config.disable_signs, "disable_signs", "boolean")
1187+
validate_type(config.git_executable, "git_executable", "string")
11851188
validate_type(config.telescope_sorter, "telescope_sorter", "function")
11861189
validate_type(config.use_per_project_settings, "use_per_project_settings", "boolean")
11871190
validate_type(config.remember_settings, "remember_settings", "boolean")
@@ -1273,6 +1276,12 @@ function M.validate_config()
12731276
return errors
12741277
end
12751278

1279+
---Get the configured git executable path
1280+
---@return string The git executable path
1281+
function M.get_git_executable()
1282+
return M.values.git_executable
1283+
end
1284+
12761285
---@param name string
12771286
---@return boolean
12781287
function M.check_integration(name)

lua/neogit/lib/git/cli.lua

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ local util = require("neogit.lib.util")
44
local Path = require("plenary.path")
55
local runner = require("neogit.runner")
66

7+
---Get the configured git executable path
8+
---@return string
9+
local function get_git_executable()
10+
local config = require("neogit.config")
11+
return config.get_git_executable()
12+
end
13+
714
---@class GitCommandSetup
815
---@field flags table|nil
916
---@field options table|nil
@@ -983,7 +990,7 @@ local configurations = {
983990
---@param dir string
984991
---@return string Absolute path of current worktree
985992
local function worktree_root(dir)
986-
local cmd = { "git", "-C", dir, "rev-parse", "--show-toplevel" }
993+
local cmd = { get_git_executable(), "-C", dir, "rev-parse", "--show-toplevel" }
987994
local result = vim.system(cmd, { text = true }):wait()
988995

989996
return Path:new(vim.trim(result.stdout)):absolute()
@@ -992,7 +999,7 @@ end
992999
---@param dir string
9931000
---@return string Absolute path of `.git/` directory
9941001
local function git_dir(dir)
995-
local cmd = { "git", "-C", dir, "rev-parse", "--git-common-dir" }
1002+
local cmd = { get_git_executable(), "-C", dir, "rev-parse", "--git-common-dir" }
9961003
local result = vim.system(cmd, { text = true }):wait()
9971004

9981005
return Path:new(vim.trim(result.stdout)):absolute()
@@ -1001,7 +1008,7 @@ end
10011008
---@param dir string
10021009
---@return string Absolute path of `.git/` directory
10031010
local function worktree_git_dir(dir)
1004-
local cmd = { "git", "-C", dir, "rev-parse", "--git-dir" }
1011+
local cmd = { get_git_executable(), "-C", dir, "rev-parse", "--git-dir" }
10051012
local result = vim.system(cmd, { text = true }):wait()
10061013

10071014
return Path:new(vim.trim(result.stdout)):absolute()
@@ -1010,7 +1017,7 @@ end
10101017
---@param dir string
10111018
---@return boolean
10121019
local function is_inside_worktree(dir)
1013-
local cmd = { "git", "-C", dir, "rev-parse", "--is-inside-work-tree" }
1020+
local cmd = { get_git_executable(), "-C", dir, "rev-parse", "--is-inside-work-tree" }
10141021
local result = vim.system(cmd):wait()
10151022

10161023
return result.code == 0
@@ -1170,7 +1177,7 @@ local function new_builder(subcommand)
11701177
-- stylua: ignore
11711178
cmd = util.merge(
11721179
{
1173-
"git",
1180+
get_git_executable(),
11741181
"--no-pager",
11751182
"--literal-pathspecs",
11761183
"--no-optional-locks",
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local config = require("neogit.config")
2+
3+
describe("git_executable configuration", function()
4+
before_each(function()
5+
config.values = config.get_default_values()
6+
end)
7+
8+
describe("default configuration", function()
9+
it("should default to 'git'", function()
10+
assert.are.equal("git", config.get_git_executable())
11+
end)
12+
end)
13+
14+
describe("custom git_executable", function()
15+
it("should accept a custom git executable path", function()
16+
config.setup({ git_executable = "/usr/local/bin/git" })
17+
assert.are.equal("/usr/local/bin/git", config.get_git_executable())
18+
end)
19+
20+
it("should accept a git wrapper script", function()
21+
config.setup({ git_executable = "/path/to/custom-git" })
22+
assert.are.equal("/path/to/custom-git", config.get_git_executable())
23+
end)
24+
end)
25+
26+
describe("validation", function()
27+
it("should return invalid when git_executable is not a string", function()
28+
config.values.git_executable = 123
29+
assert.True(vim.tbl_count(config.validate_config()) ~= 0)
30+
end)
31+
32+
it("should return valid when git_executable is a string", function()
33+
config.values.git_executable = "/custom/git"
34+
assert.True(vim.tbl_count(config.validate_config()) == 0)
35+
end)
36+
37+
it("should return valid for default git_executable", function()
38+
config.values.git_executable = "git"
39+
assert.True(vim.tbl_count(config.validate_config()) == 0)
40+
end)
41+
end)
42+
end)

0 commit comments

Comments
 (0)