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
28 changes: 28 additions & 0 deletions lua/advanced_git_search/actions/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
local config = require("advanced_git_search.utils.config")

local M = {}

--- open diff for current file
--- @param commit string commit or branch to diff with
--- @param file_name string|nil file name to diff
M.open_diff_view = function(commit, file_name)
local diff_plugin = config.diff_plugin()

if file_name ~= nil and file_name ~= "" then
if diff_plugin == "diffview" then
vim.api.nvim_command(
":DiffviewOpen " .. commit .. " -- " .. file_name
)
elseif diff_plugin == "fugitive" then
vim.api.nvim_command(":Gvdiffsplit " .. commit .. ":" .. file_name)
end
else
if diff_plugin == "diffview" then
vim.api.nvim_command(":DiffviewOpen " .. commit)
elseif diff_plugin == "fugitive" then
vim.api.nvim_command(":Gvdiffsplit " .. commit)
end
end
end

return M
126 changes: 126 additions & 0 deletions lua/advanced_git_search/finders/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
local finders = require("telescope.finders")
local file = require("advanced_git_search.utils.file")
local finder_utils = require("advanced_git_search.finders.utils")
local git_utils = require("advanced_git_search.utils.git")

local M = {}

M.git_branches_finder = function()
return finders.new_oneshot_job({
"git",
"branch",
"--format=%(refname:short)",
})
end

--- Returns all commits that changed the visual selection in the buffer
M.git_log_location_finder = function(bufnr, start_line, end_line)
local filename = file.relative_path(bufnr)
local location = string.format("-L%d,%d:%s", start_line, end_line, filename)

return finders.new_job(function(query)
local command = {
"git",
"log",
location,
"--no-patch",
"--format=%C(auto)%h %as %C(green)%an _ %Creset %s",
}

local prompt, author = finder_utils.split_query_from_author(query)

if author and author ~= "" then
table.insert(command, "--author=" .. author)
end

if prompt and prompt ~= "" then
table.insert(command, "-s")
table.insert(command, "-i")
table.insert(command, "--grep=" .. prompt)
end

finder_utils.set_last_prompt(prompt)
return vim.tbl_flatten(command)
end, finder_utils.git_log_entry_maker)
end

--- Returns all commits that contains the prompt string in the commit content
--- @param opts table with optional key `bufnr` to filter on the file of the buffer
M.git_log_content_finder = function(opts)
opts = opts or {}

return finders.new_job(function(query)
local command = {
"git",
"log",
"--format=%C(auto)%h %as %C(green)%an _ %Creset %s",
}

local prompt, author = finder_utils.split_query_from_author(query)

if author and author ~= "" then
table.insert(command, "--author=" .. author)
end

if prompt and prompt ~= "" then
table.insert(command, "-G" .. prompt)
table.insert(command, "--pickaxe-all")
-- table.insert(command, [[-G']] .. prompt .. [[']])
end

if opts.bufnr then
table.insert(command, "--follow")
local filename = file.relative_path(opts.bufnr)
table.insert(command, filename)
end

finder_utils.set_last_prompt(prompt)
return vim.tbl_flatten(command)
end, finder_utils.git_log_entry_maker)
end

--- Returns all commits that changed the file of the passed buffer
M.git_log_file_finder = function(bufnr)
local filename = file.relative_path(bufnr)
return finders.new_job(function(query)
local command = {
"git",
"log",
"--format=%C(auto)%h %as %C(green)%an _ %Creset %s",
}

local prompt, author = finder_utils.split_query_from_author(query)

if author and author ~= "" then
table.insert(command, "--author=" .. author)
end

if prompt and prompt ~= "" then
table.insert(command, "-s")
table.insert(command, "-i")
table.insert(command, "--grep=" .. prompt)
end

table.insert(command, "--follow")
table.insert(command, filename)

finder_utils.set_last_prompt(prompt)
return vim.tbl_flatten(command)
end, finder_utils.git_log_entry_maker)
end

M.changed_files_on_current_branch_finder = function()
return finders.new_oneshot_job(vim.tbl_flatten({
"git",
"--no-pager",
"diff",
"--name-only",
"--cached",
"--diff-filter=ACMR",
"--merge-base",
git_utils.base_branch(),
"--relative",
}))
end

return M
69 changes: 69 additions & 0 deletions lua/advanced_git_search/finders/utils.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
local utils = require("advanced_git_search.utils")

local M = {}
local last_prompt = nil

M.split_query_from_author = function(query)
local author = nil
local prompt = nil
if query ~= nil and query ~= "" then
-- starts with @
if query:sub(1, 1) == "@" then
author = query:sub(2)
return prompt, author
end

local split = utils.split_string(query, "@")
prompt = split[1]

-- remove last space from prompt
if prompt:sub(-1) == " " then
prompt = prompt:sub(1, -2)
end

author = split[2]
end

return prompt, author
end

--- Parse "--format=%C(auto)%h %as %C(green)%an _ %Creset %s" to table
--- with opts: commit_hash, date, author, message, prompt
--- @param entry string
M.git_log_entry_maker = function(entry)
-- dce3b0743 2022-09-09 author _ message
-- FIXME: will break if author contains _
local split = utils.split_string(entry, "_")
local attrs = utils.split_string(split[1])
local hash = attrs[1]
local date = attrs[2]
local author = attrs[3]
-- join split from second element
local message = split[2]
if #split > 2 then
for i = 3, #split do
message = message .. "_" .. split[i]
end
end

return {
value = entry,
display = date .. " by " .. author .. " --" .. message,
-- display = hash .. " @ " .. date .. " by " .. author .. " --" .. message,
ordinal = author .. " " .. message,
preview_title = hash .. " -- " .. message,
opts = {
commit_hash = hash,
date = date,
author = author,
message = message,
prompt = last_prompt,
},
}
end

M.set_last_prompt = function(prompt)
last_prompt = prompt
end

return M
Loading