Skip to content

Commit 16f4de4

Browse files
committed
Support config of git (diff) flags
1 parent 6dd2767 commit 16f4de4

File tree

5 files changed

+109
-15
lines changed

5 files changed

+109
-15
lines changed

README.md

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,24 @@ With Lazy
147147
{
148148
"aaronhallaert/advanced-git-search.nvim",
149149
config = function()
150-
require("telescope").load_extension("advanced_git_search")
151-
150+
-- optional: setup telescope before loading the extension
152151
require("telescope").setup{
153152
-- move this to the place where you call the telescope setup function
154153
extensions = {
155154
advanced_git_search = {
156155
-- fugitive or diffview
157156
diff_plugin = "fugitive",
157+
-- customize git in previewer
158+
-- e.g. flags such as { "--no-pager" }, or { "-c", "delta.side-by-side=false" }
159+
git_flags = {},
160+
-- customize git diff in previewer
161+
-- e.g. flags such as { "--raw" }
162+
git_diff_flags = {},
158163
}
159164
}
160165
}
166+
167+
require("telescope").load_extension("advanced_git_search")
161168
end,
162169
dependencies = {
163170
"nvim-telescope/telescope.nvim",
@@ -176,23 +183,30 @@ With Packer
176183
use({
177184
"aaronhallaert/advanced-git-search.nvim",
178185
config = function()
179-
require("telescope").load_extension("advanced_git_search")
180-
186+
-- optional: setup telescope before loading the extension
181187
require("telescope").setup{
182188
-- move this to the place where you call the telescope setup function
183189
extensions = {
184190
advanced_git_search = {
185191
-- fugitive or diffview
186192
diff_plugin = "fugitive",
193+
-- customize git in previewer
194+
-- e.g. flags such as { "--no-pager" }, or { "-c", "delta.side-by-side=false" }
195+
git_flags = {},
196+
-- customize git diff in previewer
197+
-- e.g. flags such as { "--raw" }
198+
git_diff_flags = {},
187199
}
188200
}
189201
}
202+
203+
require("telescope").load_extension("advanced_git_search")
190204
end,
191205
requires = {
192206
"nvim-telescope/telescope.nvim",
193207
-- to show diff splits and open commits in browser
194208
"tpope/vim-fugitive",
195-
-- OPTIONAL: to replace the diff from fugitive with diffview.nvim
209+
-- optional: to replace the diff from fugitive with diffview.nvim
196210
-- (fugitive is still needed to open in browser)
197211
-- "sindrets/diffview.nvim",
198212
},

lua/advanced_git_search/git_utils.lua

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,39 @@ local split_query_from_author = function(query)
3131
return prompt, author
3232
end
3333

34+
--- @param command table
35+
--- @param git_flags_ix number|nil
36+
--- @param git_diff_flags_ix number|nil
37+
--- @return table Command with configured git diff flags
38+
local git_diff_command = function(command, git_flags_ix, git_diff_flags_ix)
39+
git_flags_ix = git_flags_ix or 2
40+
git_diff_flags_ix = git_diff_flags_ix or 3
41+
42+
local git_diff_flags = config.git_diff_flags()
43+
local git_flags = config.git_flags()
44+
45+
if git_flags_ix > git_diff_flags_ix then
46+
vim.notify(
47+
"git_flags must be inserted before git_diff_flags",
48+
vim.log.levels.ERROR
49+
)
50+
end
51+
52+
if git_diff_flags ~= nil and #git_diff_flags > 0 then
53+
for i, flag in ipairs(git_diff_flags) do
54+
table.insert(command, git_diff_flags_ix + i - 1, flag)
55+
end
56+
end
57+
58+
if git_flags ~= nil and #git_flags > 0 then
59+
for i, flag in ipairs(git_flags) do
60+
table.insert(command, git_flags_ix + i - 1, flag)
61+
end
62+
end
63+
64+
return command
65+
end
66+
3467
local git_log_entry_maker = function(entry)
3568
-- dce3b0743 2022-09-09 author _ message
3669
-- FIXME: will break if author contains _
@@ -183,7 +216,7 @@ M.git_diff_previewer_file = function(bufnr)
183216
local commit_hash = entry.opts.commit_hash
184217

185218
local prev_commit = string.format("%s~", commit_hash)
186-
return {
219+
return git_diff_command({
187220
"git",
188221
"diff",
189222
prev_commit
@@ -192,7 +225,7 @@ M.git_diff_previewer_file = function(bufnr)
192225
commit_hash
193226
.. ":"
194227
.. determine_historic_file_name(commit_hash, bufnr),
195-
}
228+
})
196229
end,
197230
})
198231
end
@@ -250,5 +283,6 @@ M.current_branch = function()
250283
end
251284

252285
M.determine_historic_file_name = determine_historic_file_name
286+
M.git_diff_command = git_diff_command
253287

254288
return M

lua/advanced_git_search/init.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ M.changed_on_branch = function()
4242
sorter = sorters.get_fuzzy_file(),
4343
previewer = previewers.new_termopen_previewer({
4444
get_command = function(entry)
45-
return {
45+
return gu.git_diff_command({
4646
"git",
4747
"diff",
4848
"--diff-filter=ACMR",
@@ -51,7 +51,7 @@ M.changed_on_branch = function()
5151
gu.base_branch(),
5252
"--",
5353
entry.value,
54-
}
54+
})
5555
end,
5656
}),
5757
attach_mappings = function(_, map)
@@ -91,13 +91,13 @@ M.diff_branch_file = function()
9191
get_command = function(entry)
9292
local branch = entry.value
9393

94-
return {
94+
return gu.git_diff_command({
9595
"git",
9696
"diff",
9797
branch,
9898
"--",
9999
file_name,
100-
}
100+
})
101101
end,
102102
}),
103103
attach_mappings = function(_, map)
@@ -183,12 +183,12 @@ M.search_log_content = function()
183183
get_command = function(entry)
184184
local commit_hash = entry.opts.commit_hash
185185
local prompt = entry.opts.prompt
186-
local command = {
186+
local command = gu.git_diff_command({
187187
"git",
188188
"diff",
189189
string.format("%s~", commit_hash),
190190
commit_hash,
191-
}
191+
})
192192

193193
if prompt and prompt ~= "" then
194194
table.insert(command, "-G")
@@ -242,12 +242,12 @@ M.search_log_content_file = function()
242242
get_command = function(entry)
243243
local commit_hash = entry.opts.commit_hash
244244
local prompt = entry.opts.prompt
245-
local command = {
245+
local command = gu.git_diff_command({
246246
"git",
247247
"diff",
248248
string.format("%s~", commit_hash),
249249
commit_hash,
250-
}
250+
})
251251

252252
if prompt and prompt ~= "" then
253253
table.insert(command, "-G")

lua/advanced_git_search/utils/config.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ M.setup = function(ext_config)
66
ext_config = ext_config or {}
77

88
ext_config.diff_plugin = ext_config.diff_plugin or "fugitive"
9+
ext_config.git_diff_flags = ext_config.git_diff_flags or {}
910

1011
config = ext_config
1112
end
@@ -14,6 +15,36 @@ M.get_config = function()
1415
return config
1516
end
1617

18+
M.git_diff_flags = function()
19+
local git_diff_flags = config["git_diff_flags"] or {}
20+
21+
if type(git_diff_flags) ~= "table" then
22+
vim.notify(
23+
"git_diff_flags must be a table",
24+
vim.log.levels.ERROR,
25+
{ title = "Advanced Git Search" }
26+
)
27+
return nil
28+
end
29+
30+
return git_diff_flags
31+
end
32+
33+
M.git_flags = function()
34+
local git_flags = config["git_flags"] or {}
35+
36+
if type(git_flags) ~= "table" then
37+
vim.notify(
38+
"git_flags must be a table",
39+
vim.log.levels.ERROR,
40+
{ title = "Advanced Git Search" }
41+
)
42+
return nil
43+
end
44+
45+
return git_flags
46+
end
47+
1748
M.diff_plugin = function()
1849
local diff_plugin = config["diff_plugin"]
1950

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
local M = {}
2+
3+
--- @param array table
4+
--- @param value any pattern to match
5+
--- @return number index of value in array, -1 if not found
6+
M.index_of = function(array, value)
7+
for i, v in ipairs(array) do
8+
if v == value then
9+
return i
10+
end
11+
end
12+
return -1
13+
end
14+
15+
return M

0 commit comments

Comments
 (0)