Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
feat: add git log flags
  • Loading branch information
aaronhallaert committed Nov 27, 2024
commit 2ff59f7714deb81e01c796892e246a07e898714d
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Enable `show_builtin_git_pickers` to additionally show builtin git pickers.
-- customize git diff in previewer
-- e.g. flags such as { "--raw" }
git_diff_flags = {},
git_log_flags = {},
-- Show builtin git pickers when executing "show_custom_functions" or :AdvancedGitSearch
show_builtin_git_pickers = false,
entry_default_author_or_date = "author", -- one of "author" or "date"
Expand Down
7 changes: 7 additions & 0 deletions lua/advanced_git_search/commands/find.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local file = require("advanced_git_search.utils.file")
local git_utils = require("advanced_git_search.utils.git")
local tbl = require("advanced_git_search.utils.table")
local command_utils = require("advanced_git_search.commands.utils")

-- Specify shell commands for each finders in table format
local M = {}
Expand Down Expand Up @@ -41,6 +42,8 @@ M.git_log_content = function(prompt, author, bufnr)
"--format='%h %as %an _ %s'",
}

command = command_utils.format_git_log_command(command)

if author and author ~= "" and author ~= '""' then
table.insert(command, "--author=" .. author)
end
Expand Down Expand Up @@ -72,6 +75,8 @@ M.git_log_file = function(prompt, author, bufnr)
"--format='%h %as %an _ %s'",
}

command = command_utils.format_git_log_command(command)

if author and author ~= "" and author ~= '""' then
table.insert(command, "--author=" .. author)
end
Expand Down Expand Up @@ -105,6 +110,8 @@ M.git_log_location = function(prompt, author, bufnr, start_line, end_line)
"--format='%h %as %an _ %s'",
}

command = command_utils.format_git_log_command(command)

if author and author ~= "" and author ~= '""' then
table.insert(command, "--author=" .. author)
end
Expand Down
33 changes: 33 additions & 0 deletions lua/advanced_git_search/commands/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ M.format_git_diff_command = function(command, git_flags_ix, git_diff_flags_ix)
return command
end

--- @param command table
--- @param git_flags_ix number|nil
--- @param git_log_flags_ix number|nil
--- @return table Command with configured git log flags
M.format_git_log_command = function(command, git_flags_ix, git_log_flags_ix)
git_flags_ix = git_flags_ix or 2
git_log_flags_ix = git_log_flags_ix or 3

local git_log_flags = config.git_log_flags()
local git_flags = config.git_flags()

if git_flags_ix > git_log_flags_ix then
vim.notify(
"git_flags must be inserted before git_log_flags",
vim.log.levels.ERROR
)
end

if git_log_flags ~= nil and #git_log_flags > 0 then
for i, flag in ipairs(git_log_flags) do
table.insert(command, git_log_flags_ix + i - 1, flag)
end
end

if git_flags ~= nil and #git_flags > 0 then
for i, flag in ipairs(git_flags) do
table.insert(command, git_flags_ix + i - 1, flag)
end
end

return command
end

M.split_query_from_author = function(query)
local author = nil
local prompt = nil
Expand Down
16 changes: 16 additions & 0 deletions lua/advanced_git_search/utils/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ M.setup = function(ext_config)
or "GBrowse {commit_hash}"
ext_config.diff_plugin = ext_config.diff_plugin or "fugitive"
ext_config.git_diff_flags = ext_config.git_diff_flags or {}
ext_config.git_log_flags = ext_config.git_log_flags or {}
ext_config.show_builtin_git_pickers = ext_config.show_builtin_git_pickers
or false

Expand All @@ -34,6 +35,21 @@ M.git_diff_flags = function()
return git_diff_flags
end

M.git_log_flags = function()
local git_log_flags = config["git_log_flags"] or {}

if type(git_log_flags) ~= "table" then
vim.notify(
"git_log_flags must be a table",
vim.log.levels.ERROR,
{ title = "Advanced Git Search" }
)
return nil
end

return git_log_flags
end

local function get_keymaps()
local keymaps = {
toggle_date_author = "<C-w>",
Expand Down
16 changes: 14 additions & 2 deletions lua/advanced_git_search/utils/git.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local utils = require("advanced_git_search.utils")
local command_utils = require("advanced_git_search.commands.utils")
local file = require("advanced_git_search.utils.file")
local command_util = require("advanced_git_search.utils.command")

Expand All @@ -12,10 +13,21 @@ local all_commit_hashes = function()
end

local all_commit_hashes_touching_file = function(git_relative_file_path)
local git_log_cmd = {
"git",
"log",
"--follow",
"--pretty=format:'%H'",
"--",
git_relative_file_path,
}

git_log_cmd = command_utils.format_git_log_command(git_log_cmd)

local command = "cd "
.. file.git_dir()
.. " && git log --follow --pretty=format:'%H' -- "
.. git_relative_file_path
.. " && "
.. table.concat(git_log_cmd, " ")

local output = command_util.execute(command)
return utils.split_string(output, "\n")
Expand Down
1 change: 1 addition & 0 deletions lua/spec/minimal_init_fzf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function M.setup()
require("advanced_git_search.fzf").setup({
git_flags = { "-c", "delta.side-by-side=false" },
git_diff_flags = {},
git_log_flags = {},
show_builtin_git_pickers = true,
diff_plugin = "diffview",
entry_default_author_or_date = "author",
Expand Down
1 change: 1 addition & 0 deletions lua/spec/minimal_init_telescope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function M.setup()
advanced_git_search = {
git_flags = { "-c", "delta.side-by-side=false" },
git_diff_flags = {},
git_log_flags = {},
show_builtin_git_pickers = true,
diff_plugin = "diffview",
},
Expand Down