Skip to content

Commit 8430f04

Browse files
committed
Add fzf-lua support
Port search_log_content to fzf Signed-off-by: Aaron Hallaert <[email protected]> Port search_log_content_file to fzf Signed-off-by: Aaron Hallaert <[email protected]> Port diff_commit_line to fzf Signed-off-by: Aaron Hallaert <[email protected]> Port diff_commit_file to fzf Signed-off-by: Aaron Hallaert <[email protected]> Transform entries fzf Signed-off-by: Aaron Hallaert <[email protected]> Add actions fzf Signed-off-by: Aaron Hallaert <[email protected]> Add global picker fzf Signed-off-by: Aaron Hallaert <[email protected]> Add changed on branch fzf Signed-off-by: Aaron Hallaert <[email protected]> Add checkout reflog fzf Signed-off-by: Aaron Hallaert <[email protected]> Rename requires Signed-off-by: Aaron Hallaert <[email protected]> Update readme Signed-off-by: Aaron Hallaert <[email protected]> Fix git_diff_file Signed-off-by: Aaron Hallaert <[email protected]> Fix query inputs and fallbacks for prompt and author Signed-off-by: Aaron Hallaert <[email protected]>
1 parent 670f8e7 commit 8430f04

File tree

18 files changed

+909
-251
lines changed

18 files changed

+909
-251
lines changed

README.md

Lines changed: 106 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
1-
# Telescope Advanced Git Search
1+
# Advanced Git Search
2+
3+
An advanced git search extension for `Telescope` and `fzf-lua`.
4+
5+
Search your git history by commit message, content and author in Neovim
26

37
## 🖥️ Usage
48

5-
[![Demo](https://img.youtube.com/vi/bO0uYLlHtYo/0.jpg)](https://www.youtube.com/watch?v=bO0uYLlHtYo)
9+
- [Demo](https://www.youtube.com/watch?v=bO0uYLlHtYo)
610

711
### 📖 Open a picker
812

13+
#### 🔭 Telescope
14+
915
```vim
1016
:Telescope advanced_git_search {function_name}
1117
```
1218

13-
#### or in lua
19+
> or in lua
1420
1521
```lua
1622
require('telescope').extensions.advanced_git_search.{function_name}()
1723
```
1824

19-
#### or through another Telescope picker
25+
> or through another Telescope picker
26+
27+
execute `:AdvancedGitSearch`, choose your picker and press `<CR>`
28+
29+
#### 🧎 fzf-lua
30+
31+
```lua
32+
require('advanced-git-search.fzf').{function_name}()
33+
```
34+
35+
> or through another picker
2036
2137
execute `:AdvancedGitSearch`, choose your picker and press `<CR>`
2238

2339
### 🔎 Enter a query
2440

25-
Your usual telescope experience. See the individual commands for the grep behaviour.
41+
Your usual search experience. See the individual commands for the grep behaviour.
2642

2743
### ✏️ Further search on commit author with `@`
2844

@@ -33,7 +49,7 @@ the author name.
3349

3450
### 1. search_log_content -- Search in repo log content
3551

36-
Opens a Telescope window with a list of all previous commit.
52+
Opens a window with a list of all previous commit.
3753

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

@@ -46,7 +62,7 @@ _Grep behaviour_: filter on added, updated or removed code (log content: `-G` op
4662

4763
### 2. search_log_content_file -- Search in file log content
4864

49-
Opens a Telescope window with a list of git commits that changed the
65+
Opens a window with a list of git commits that changed the
5066
current file (renames included).
5167

5268
_Grep behaviour_: filter on added, updated or removed code (log content: `-G` option in git).
@@ -60,7 +76,7 @@ _Grep behaviour_: filter on added, updated or removed code (log content: `-G` op
6076

6177
### 3. diff_commit_file -- Diff current file with commit
6278

63-
Opens a Telescope window with a list of git commits that changed the
79+
Opens a window with a list of git commits that changed the
6480
current file (renames included).
6581

6682
_Grep behaviour_: filter on commit message.
@@ -74,19 +90,18 @@ _Grep behaviour_: filter on commit message.
7490

7591
### 4. diff_commit_line -- Diff current file with selected line history
7692

77-
Opens a Telescope window with a list of previous commit logs with respect to
93+
Opens a window with a list of previous commit logs with respect to
7894
selected lines
7995

8096
_Grep behaviour_: filter on commit message.
8197

8298
#### How to use
8399

84-
_The following only applies when you use one of the commands below._
85-
86-
```vim
87-
:Telescope advanced_git_search diff_commit_line
88-
:lua require('telescope').extensions.advanced_git_search.diff_commit_line()
89-
```
100+
> _This workaround only applies when you use the following command. (Telescope)_
101+
>
102+
> ```vim
103+
> :Telescope advanced_git_search diff_commit_line
104+
> ```
90105
91106
First you have to select the lines in visual mode, then go back to normal
92107
mode and execute this command.
@@ -107,7 +122,7 @@ vim.api.nvim_set_keymap(
107122
)
108123
```
109124
110-
No extra setup is needed when you use `:AdvancedGitSearch`.
125+
> No extra setup is needed when you use `:AdvancedGitSearch`.
111126
112127
#### _Keymaps_
113128

@@ -118,7 +133,7 @@ No extra setup is needed when you use `:AdvancedGitSearch`.
118133

119134
### 5. diff_branch_file -- Diff file with branch
120135

121-
Opens a Telescope window with a list of local branches
136+
Opens a window with a list of local branches
122137

123138
_Grep behaviour_: filter on branch name.
124139

@@ -128,7 +143,7 @@ _Grep behaviour_: filter on branch name.
128143

129144
### 6. changed_on_branch -- Changed on current branch (experimental)
130145

131-
Opens a Telescope window with a list of changed files on the current branch (including staged files).
146+
Opens a window with a list of changed files on the current branch (including staged files).
132147
The fork point of the current branch is determined with the following command:
133148

134149
```sh
@@ -150,7 +165,7 @@ _Grep behaviour_: filter on filename.
150165

151166
### 7. checkout_reflog -- Checkout from reflog
152167

153-
Opens a Telescope window with all reflog entries
168+
Opens a window with all reflog entries
154169

155170
#### _Keymaps_
156171

@@ -159,10 +174,12 @@ Opens a Telescope window with all reflog entries
159174
### 8. show_custom_functions
160175

161176
A telescope picker for all functions above.
162-
Enable `show_builtin_git_pickers` to additionally show Telescopes builtin git pickers.
177+
Enable `show_builtin_git_pickers` to additionally show builtin git pickers.
163178

164179
## ⚙️ Installation
165180

181+
### Telescope
182+
166183
With Lazy
167184

168185
```lua
@@ -243,6 +260,75 @@ With Packer
243260
})
244261
```
245262

263+
### Fzf-lua
264+
265+
With Lazy
266+
267+
```lua
268+
{
269+
"aaronhallaert/advanced-git-search.nvim",
270+
config = function()
271+
-- optional: setup telescope before loading the extension
272+
require("advanced-git-search.fzf").setup{
273+
-- fugitive or diffview
274+
diff_plugin = "fugitive",
275+
-- customize git in previewer
276+
-- e.g. flags such as { "--no-pager" }, or { "-c", "delta.side-by-side=false" }
277+
git_flags = {},
278+
-- customize git diff in previewer
279+
-- e.g. flags such as { "--raw" }
280+
git_diff_flags = {},
281+
-- Show builtin git pickers when executing "show_custom_functions" or :AdvancedGitSearch
282+
show_builtin_git_pickers = false,
283+
}
284+
end,
285+
dependencies = {
286+
"ibhagwan/fzf-lua",
287+
-- to show diff splits and open commits in browser
288+
"tpope/vim-fugitive",
289+
-- to open commits in browser with fugitive
290+
"tpope/vim-rhubarb",
291+
-- OPTIONAL: to replace the diff from fugitive with diffview.nvim
292+
-- (fugitive is still needed to open in browser)
293+
-- "sindrets/diffview.nvim",
294+
},
295+
}
296+
```
297+
298+
With Packer
299+
300+
```lua
301+
use({
302+
"aaronhallaert/advanced-git-search.nvim",
303+
config = function()
304+
-- optional: setup telescope before loading the extension
305+
require("advanced-git-search.fzf").setup{
306+
-- Fugitive or diffview
307+
diff_plugin = "fugitive",
308+
-- Customize git in previewer
309+
-- e.g. flags such as { "--no-pager" }, or { "-c", "delta.side-by-side=false" }
310+
git_flags = {},
311+
-- Customize git diff in previewer
312+
-- e.g. flags such as { "--raw" }
313+
git_diff_flags = {},
314+
-- Show builtin git pickers when executing "show_custom_functions" or :AdvancedGitSearch
315+
show_builtin_git_pickers = false,
316+
}
317+
}
318+
end,
319+
requires = {
320+
"ibhagwan/fzf-lua",
321+
-- to show diff splits and open commits in browser
322+
"tpope/vim-fugitive",
323+
-- to open commits in browser with fugitive
324+
"tpope/vim-rhubarb",
325+
-- optional: to replace the diff from fugitive with diffview.nvim
326+
-- (fugitive is still needed to open in browser)
327+
-- "sindrets/diffview.nvim",
328+
},
329+
})
330+
```
331+
246332
### Prerequisites
247333

248334
- git

lua/advanced_git_search/actions.lua

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,23 @@ local config = require("advanced_git_search.utils.config")
22

33
local M = {}
44

5-
--- open diff for current file
6-
--- @param commit string commit or branch to diff with
7-
--- @param file_name string|nil file name to diff
5+
---General action: Open entire commit with fugitive or diffview
6+
---@param commit_hash string
7+
M.open_commit = function(commit_hash)
8+
local diff_plugin = config.diff_plugin()
9+
10+
if diff_plugin == "diffview" then
11+
vim.api.nvim_command(
12+
":DiffviewOpen -uno " .. commit_hash .. "~.." .. commit_hash
13+
)
14+
elseif diff_plugin == "fugitive" then
15+
vim.api.nvim_command(":Gedit " .. commit_hash)
16+
end
17+
end
18+
19+
---General action: open diff for current file
20+
---@param commit string commit or branch to diff with
21+
---@param file_name string|nil file name to diff
822
M.open_diff_view = function(commit, file_name)
923
local diff_plugin = config.diff_plugin()
1024

@@ -25,4 +39,29 @@ M.open_diff_view = function(commit, file_name)
2539
end
2640
end
2741

42+
---General action: Copy commit hash to system clipboard
43+
---@param commit_hash string
44+
M.copy_to_clipboard = function(commit_hash)
45+
vim.notify(
46+
"Copied commit hash " .. commit_hash .. " to clipboard",
47+
vim.log.levels.INFO,
48+
{ title = "Advanced Git Search" }
49+
)
50+
51+
vim.fn.setreg("+", commit_hash)
52+
vim.fn.setreg("*", commit_hash)
53+
end
54+
55+
---General action: Open commit in browser
56+
---@param commit_hash string
57+
M.open_in_browser = function(commit_hash)
58+
vim.api.nvim_command(":GBrowse " .. commit_hash)
59+
end
60+
61+
---General action: Checkout commit
62+
---@param commit_hash string
63+
M.checkout_commit = function(commit_hash)
64+
vim.api.nvim_command(":!git checkout " .. commit_hash)
65+
end
66+
2867
return M

lua/advanced_git_search/commands/find.lua

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ M.git_branches = function()
1212
}
1313
end
1414

15+
M.reflog = function()
16+
return {
17+
"git",
18+
"reflog",
19+
"--date=iso",
20+
}
21+
end
22+
1523
---@param prompt string|nil
1624
---@param author string|nil
1725
---@param bufnr number|nil
@@ -23,12 +31,13 @@ M.git_log_content = function(prompt, author, bufnr)
2331
"--format='%h %as %an _ %s'",
2432
}
2533

26-
if author and author ~= "" then
34+
if author and author ~= "" and author ~= '""' then
2735
table.insert(command, "--author=" .. author)
2836
end
2937

30-
if prompt and prompt ~= "" then
31-
table.insert(command, "-G" .. prompt)
38+
if prompt and prompt ~= "" and prompt ~= '""' then
39+
table.insert(command, "-G")
40+
table.insert(command, prompt)
3241
table.insert(command, "--pickaxe-all")
3342
end
3443

@@ -53,11 +62,11 @@ M.git_log_file = function(prompt, author, bufnr)
5362
"--format='%h %as %an _ %s'",
5463
}
5564

56-
if author and author ~= "" then
65+
if author and author ~= "" and author ~= '""' then
5766
table.insert(command, "--author=" .. author)
5867
end
5968

60-
if prompt and prompt ~= "" then
69+
if prompt and prompt ~= "" and prompt ~= '""' then
6170
table.insert(command, "-s")
6271
table.insert(command, "-i")
6372
table.insert(command, "--grep=" .. prompt)
@@ -86,11 +95,11 @@ M.git_log_location = function(prompt, author, bufnr, start_line, end_line)
8695
"--format='%h %as %an _ %s'",
8796
}
8897

89-
if author and author ~= "" then
98+
if author and author ~= "" and author ~= '""' then
9099
table.insert(command, "--author=" .. author)
91100
end
92101

93-
if prompt and prompt ~= "" then
102+
if prompt and prompt ~= "" and prompt ~= '""' then
94103
table.insert(command, "-s")
95104
table.insert(command, "-i")
96105
table.insert(command, "--grep=" .. prompt)
@@ -100,7 +109,7 @@ M.git_log_location = function(prompt, author, bufnr, start_line, end_line)
100109
end
101110

102111
M.changed_on_branch = function()
103-
vim.tbl_flatten({
112+
return vim.tbl_flatten({
104113
"git",
105114
"--no-pager",
106115
"diff",

0 commit comments

Comments
 (0)