Skip to content

Commit 5d46f70

Browse files
committed
Search log content by file
1 parent b76286d commit 5d46f70

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

lua/advanced_git_search/git_utils.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ local git_log_entry_maker = function(entry)
5555
}
5656
end
5757

58-
M.git_log_grepper_on_content = function()
58+
M.git_log_grepper_on_content = function(opts)
59+
opts = opts or {}
60+
5961
return finders.new_job(function(query)
6062
local command = {
6163
"git",
@@ -73,6 +75,13 @@ M.git_log_grepper_on_content = function()
7375
if prompt and prompt ~= "" then
7476
table.insert(command, "-G" .. prompt)
7577
table.insert(command, "--pickaxe-all")
78+
-- table.insert(command, [[-G']] .. prompt .. [[']])
79+
end
80+
81+
if opts.bufnr then
82+
table.insert(command, "--")
83+
local filename = file.relative_path(opts.bufnr)
84+
table.insert(command, filename)
7685
end
7786

7887
last_prompt = prompt

lua/advanced_git_search/init.lua

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ M = {}
1212

1313
-- Map a key to both insert and normal modes
1414
local function omnimap(map_func, key, handler)
15-
map_func('i', key, handler)
16-
map_func('n', key, handler)
15+
map_func("i", key, handler)
16+
map_func("n", key, handler)
1717
end
1818

1919
--- Opens a Telescope window with a list of local branches
@@ -117,7 +117,63 @@ M.search_log_content = function()
117117
.new({
118118
results_title = "Commits",
119119
prompt_title = "Git log content (added, removed or updated text)",
120-
finder = gu.git_log_grepper_on_content(),
120+
finder = gu.git_log_grepper_on_content({}),
121+
-- finder = finders.new_oneshot_job({'git', 'log', location}),
122+
previewer = previewers.new_termopen_previewer({
123+
get_command = function(entry)
124+
local commit_hash = entry.opts.commit_hash
125+
local prompt = entry.opts.prompt
126+
local command = {
127+
"git",
128+
"diff",
129+
string.format("%s~", commit_hash),
130+
commit_hash,
131+
}
132+
133+
if prompt and prompt ~= "" then
134+
table.insert(command, "-G")
135+
table.insert(command, prompt)
136+
end
137+
138+
return command
139+
end,
140+
}),
141+
-- sorter = sorters.highlighter_only(),
142+
attach_mappings = function(_, map)
143+
omnimap(map, "<CR>", function(prompt_bufnr)
144+
actions.close(prompt_bufnr)
145+
local selection = action_state.get_selected_entry()
146+
local commit_hash = selection.opts.commit_hash
147+
148+
gu.open_diff_view(commit_hash)
149+
end)
150+
omnimap(map, "<C-o>", function(prompt_bufnr)
151+
actions.close(prompt_bufnr)
152+
local selection = action_state.get_selected_entry()
153+
154+
vim.api.nvim_command(":GBrowse " .. selection.opts.commit_hash)
155+
end)
156+
157+
return true
158+
end,
159+
})
160+
:find()
161+
end
162+
163+
--- Same as `search_log_content` but with respect to the current file
164+
---
165+
--- <CR> opens a diff for the current file with the selected commit
166+
--- <C-o> opens a the selected commit in the browser
167+
M.search_log_content_file = function()
168+
-- local file_name = vim.fn.expand("%")
169+
-- local relative_file_name = vim.fn.expand("%:~:.")
170+
171+
-- git log -L741,751:'app/models/patients/patient.rb' --format='%C(auto)%h \t %as \t %C(green)%an _ %Creset %s'
172+
pickers
173+
.new({
174+
results_title = "Commits",
175+
prompt_title = "Git log content (added, removed or updated text in this file)",
176+
finder = gu.git_log_grepper_on_content({ bufnr = vim.fn.bufnr() }),
121177
-- finder = finders.new_oneshot_job({'git', 'log', location}),
122178
previewer = previewers.new_termopen_previewer({
123179
get_command = function(entry)

lua/telescope/_extensions/advanced_git_search.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ return require("telescope").register_extension({
77
diff_commit_file = func.diff_commit_file,
88
diff_commit_line = func.diff_commit_line,
99
search_log_content = func.search_log_content,
10+
search_log_content_file = func.search_log_content_file,
1011
show_custom_functions = func.show_custom_functions,
1112
},
1213
})

0 commit comments

Comments
 (0)