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
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ the author name.

Opens a Telescope window with a list of local branches

_Grep behaviour_: filter on branch name.

#### _Keymaps_

- `<CR>` opens a diff for the current file with the selected branch
Expand All @@ -40,6 +42,8 @@ Opens a Telescope window with a list of local branches
Opens a Telescope window with a list of previous commit logs with respect to
selected lines

_Grep behaviour_: filter on commit message.

Note: First you have to select the lines in visual mode, then go back to normal
mode and execute this command.
To make this a bit easier, you can wrap it in a user command and define a keybind:
Expand Down Expand Up @@ -68,7 +72,9 @@ vim.api.nvim_set_keymap(
### 3. diff_commit_file

Opens a Telescope window with a list of git commits that changed the
current file (renames included)
current file (renames included).

_Grep behaviour_: filter on commit message.

#### _Keymaps_

Expand All @@ -78,23 +84,36 @@ current file (renames included)

### 4. search_log_content

Opens a Telescope window with a list of previous commit logs filtered on the
`content` of the commits.
Opens a Telescope window with a list of all previous commit.

_Grep behaviour_: filter on added, updated or removed code (log content: `-G` option in git).

#### _Keymaps_

- `<CR>` opens a diff for the current file with the selected commit
- `<C-o>` opens the selected commit in the browser

### 5. search_log_content_file

Opens a Telescope window with a list of git commits that changed the
current file (renames included).

_Grep behaviour_: filter on added, updated or removed code (log content: `-G` option in git).

#### _Keymaps_

- `<CR>` opens a diff for the current file with the selected commit
- `<C-o>` opens the selected commit in the browser

### 5. checkout_reflog
### 6. checkout_reflog

Opens a Telescope window with all reflog entries

#### _Keymaps_

- `<CR>` checkout the reflog entry

### 6. show_custom_functions
### 7. show_custom_functions

A telescope picker for all functions above.

Expand Down
11 changes: 10 additions & 1 deletion lua/advanced_git_search/git_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ local git_log_entry_maker = function(entry)
}
end

M.git_log_grepper_on_content = function()
M.git_log_grepper_on_content = function(opts)
opts = opts or {}

return finders.new_job(function(query)
local command = {
"git",
Expand All @@ -73,6 +75,13 @@ M.git_log_grepper_on_content = function()
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

last_prompt = prompt
Expand Down
62 changes: 59 additions & 3 deletions lua/advanced_git_search/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ M = {}

-- Map a key to both insert and normal modes
local function omnimap(map_func, key, handler)
map_func('i', key, handler)
map_func('n', key, handler)
map_func("i", key, handler)
map_func("n", key, handler)
end

--- Opens a Telescope window with a list of local branches
Expand Down Expand Up @@ -117,7 +117,63 @@ M.search_log_content = function()
.new({
results_title = "Commits",
prompt_title = "Git log content (added, removed or updated text)",
finder = gu.git_log_grepper_on_content(),
finder = gu.git_log_grepper_on_content({}),
-- finder = finders.new_oneshot_job({'git', 'log', location}),
previewer = previewers.new_termopen_previewer({
get_command = function(entry)
local commit_hash = entry.opts.commit_hash
local prompt = entry.opts.prompt
local command = {
"git",
"diff",
string.format("%s~", commit_hash),
commit_hash,
}

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

return command
end,
}),
-- sorter = sorters.highlighter_only(),
attach_mappings = function(_, map)
omnimap(map, "<CR>", function(prompt_bufnr)
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()
local commit_hash = selection.opts.commit_hash

gu.open_diff_view(commit_hash)
end)
omnimap(map, "<C-o>", function(prompt_bufnr)
actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry()

vim.api.nvim_command(":GBrowse " .. selection.opts.commit_hash)
end)

return true
end,
})
:find()
end

--- Same as `search_log_content` but with respect to the current file
---
--- <CR> opens a diff for the current file with the selected commit
--- <C-o> opens a the selected commit in the browser
M.search_log_content_file = function()
-- local file_name = vim.fn.expand("%")
-- local relative_file_name = vim.fn.expand("%:~:.")

-- git log -L741,751:'app/models/patients/patient.rb' --format='%C(auto)%h \t %as \t %C(green)%an _ %Creset %s'
pickers
.new({
results_title = "Commits",
prompt_title = "Git log content (added, removed or updated text in this file)",
finder = gu.git_log_grepper_on_content({ bufnr = vim.fn.bufnr() }),
-- finder = finders.new_oneshot_job({'git', 'log', location}),
previewer = previewers.new_termopen_previewer({
get_command = function(entry)
Expand Down
1 change: 1 addition & 0 deletions lua/telescope/_extensions/advanced_git_search.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ return require("telescope").register_extension({
diff_commit_file = func.diff_commit_file,
diff_commit_line = func.diff_commit_line,
search_log_content = func.search_log_content,
search_log_content_file = func.search_log_content_file,
show_custom_functions = func.show_custom_functions,
},
})