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
120 changes: 70 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
:Telescope advanced_git_search {function_name}
```

or in lua
#### or in lua

```lua
require('telescope').extensions.advanced_git_search.{function_name}()
```

#### or through another Telescope picker

execute `:AdvancedGitSearch`, choose your picker and press `<CR>`

### 🔎 Enter a query

Your usual telescope experience. See the individual commands for the grep behaviour.
Expand All @@ -27,26 +31,66 @@ the author name.

## ⚡️Commands

### 1. diff_branch_file
### 1. search_log_content -- Search in repo log content

Opens a Telescope window with a list of local branches
Opens a Telescope window with a list of all previous commit.

_Grep behaviour_: filter on branch name.
_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 branch
- `<CR>` Opens a diff of the current file with the selected commit
- `<C-e>` show the entire commit for all files in neovim with diff plugin
- `<C-o>` Open the selected commit in the browser
- `<C-y>` copy the commit hash to clipboard

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

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 of the current file with the selected commit
- `<C-e>` show the entire commit for all files in neovim with diff plugin
- `<C-o>` Open the selected commit in the browser
- `<C-y>` copy the commit hash to clipboard

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

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

_Grep behaviour_: filter on commit message.

### 2. diff_commit_line
#### _Keymaps_

- `<CR>` Opens a diff of the current file with the selected commit
- `<C-e>` show the entire commit for all files in neovim with diff plugin
- `<C-o>` Open the selected commit in the browser
- `<C-y>` copy the commit hash to clipboard

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

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
#### How to use

_The following only applies when you use one of the commands below._

```vim
:Telescope advanced_git_search diff_commit_line
:lua require('telescope').extensions.advanced_git_search.diff_commit_line()
```

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:
To make a bit easier, you can wrap it in a user command and define a keybind:

```lua
vim.api.nvim_create_user_command(
Expand All @@ -63,55 +107,26 @@ vim.api.nvim_set_keymap(
)
```

No extra setup is needed when you use `:AdvancedGitSearch`.

#### _Keymaps_

- `<CR>` opens a diff for the current file with the corresponding file on the selected commit
- `<C-e>` show the entire commit for all files in neovim with diff plugin
- `<C-o>` opens a the selected commit in the browser
- `<C-y>` copy the commit hash to clipboard

### 3. diff_commit_file

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

_Grep behaviour_: filter on commit message.

#### _Keymaps_

- `<CR>` Opens a diff of the current file with the selected commit
- `<C-e>` show the entire commit for all files in neovim with diff plugin
- `<C-o>` Open the selected commit in the browser
- `<C-y>` copy the commit hash to clipboard

### 4. search_log_content

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).
### 5. diff_branch_file -- Diff file with branch

#### _Keymaps_

- `<CR>` Opens a diff of the current file with the selected commit
- `<C-e>` show the entire commit for all files in neovim with diff plugin
- `<C-o>` Open the selected commit in the browser
- `<C-y>` copy the commit hash to clipboard

### 5. search_log_content_file

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

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

#### _Keymaps_

- `<CR>` Opens a diff of the current file with the selected commit
- `<C-e>` show the entire commit for all files in neovim with diff plugin
- `<C-o>` Open the selected commit in the browser
- `<C-y>` copy the commit hash to clipboard
- `<CR>` opens a diff for the current file with the selected branch

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

Opens a Telescope window with a list of changed files on the current branch (including staged files).
The fork point of the current branch is determined with the following command:
Expand All @@ -133,7 +148,7 @@ _Grep behaviour_: filter on filename.

- `<CR>` opens the selected file.

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

Opens a Telescope window with all reflog entries

Expand All @@ -144,6 +159,7 @@ Opens a Telescope window with all reflog entries
### 8. show_custom_functions

A telescope picker for all functions above.
Enable `show_builtin_git_pickers` to additionally show Telescopes builtin git pickers.

## ⚙️ Installation

Expand All @@ -160,12 +176,14 @@ With Lazy
advanced_git_search = {
-- fugitive or diffview
diff_plugin = "fugitive",
-- customize git in previewer
-- customize git in previewer
-- e.g. flags such as { "--no-pager" }, or { "-c", "delta.side-by-side=false" }
git_flags = {},
-- customize git diff in previewer
-- customize git diff in previewer
-- e.g. flags such as { "--raw" }
git_diff_flags = {},
-- Show builtin git pickers when executing "show_custom_functions" or :AdvancedGitSearch
show_builtin_git_pickers = false,
}
}
}
Expand Down Expand Up @@ -196,14 +214,16 @@ With Packer
-- move this to the place where you call the telescope setup function
extensions = {
advanced_git_search = {
-- fugitive or diffview
-- Fugitive or diffview
diff_plugin = "fugitive",
-- customize git in previewer
-- Customize git in previewer
-- e.g. flags such as { "--no-pager" }, or { "-c", "delta.side-by-side=false" }
git_flags = {},
-- customize git diff in previewer
-- Customize git diff in previewer
-- e.g. flags such as { "--raw" }
git_diff_flags = {},
-- Show builtin git pickers when executing "show_custom_functions" or :AdvancedGitSearch
show_builtin_git_pickers = false,
}
}
}
Expand Down
87 changes: 74 additions & 13 deletions lua/advanced_git_search/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local git_utils = require("advanced_git_search.utils.git")
local config = require("advanced_git_search.utils.config")

local pickers = require("telescope.pickers")
local sorters = require("telescope.sorters")
Expand Down Expand Up @@ -163,17 +164,54 @@ M.checkout_reflog = function()
:find()
end

local git_functions = {
{ value = "Changed on current branch", func = M.changed_on_branch },
{ value = "Find in repo log content", func = M.search_log_content },
{ value = "Find in file log content", func = M.search_log_content_file },
{ value = "Diff file with branch", func = M.diff_branch_file },
{ value = "Diff file with previous commit", func = M.diff_commit_file },
local custom_git_functions = {
{
value = "Diff file with selected line history",
value = "Search in repo log content",
func = M.search_log_content,
},
{
value = "Search in file log content",
func = M.search_log_content_file,
},
{
value = "Diff current file with commit",
func = M.diff_commit_file,
},
{
value = "Diff current file with selected line history",
func = M.diff_commit_line,
},
{ value = "Checkout from reflog", func = M.checkout_reflog },
{
value = "Diff file with branch",
func = M.diff_branch_file,
},
{
value = "Changed on current branch (experimental)",
func = M.changed_on_branch,
},
{
value = "Checkout from reflog",
func = M.checkout_reflog,
},
}

local builtin_git_functions = {
{
value = "Git commits [telescope.builtin]",
func = require("telescope.builtin").git_commits,
},
{
value = "Git branches [telescope.builtin]",
func = require("telescope.builtin").git_branches,
},
{
value = "Git status [telescope.builtin]",
func = require("telescope.builtin").git_status,
},
{
value = "Git stash [telescope.builtin]",
func = require("telescope.builtin").git_stash,
},
}

local function map_item(git_functions_table, f)
Expand All @@ -184,8 +222,23 @@ local function map_item(git_functions_table, f)
return t
end

local git_functions_table = function()
local t = {}
for _, v in pairs(custom_git_functions) do
t[#t + 1] = v
end

if config.show_builtin_git_pickers() then
for _, v in pairs(builtin_git_functions) do
t[#t + 1] = v
end
end

return t
end

local function execute_git_function(value)
for _, v in pairs(git_functions) do
for _, v in pairs(git_functions_table()) do
if v["value"] == value then
v["func"]()
return
Expand All @@ -195,12 +248,14 @@ end

--- Opens all a selector for all advanced git search functions
M.show_custom_functions = function()
local keys = map_item(git_functions_table(), function(item)
return item["value"]
end)

pickers
.new({
results_title = "Git action",
finder = finders.new_table(map_item(git_functions, function(item)
return item["value"]
end)),
prompt_title = "Git actions",
finder = finders.new_table(keys),
sorter = sorters.get_fuzzy_file(),
attach_mappings = function(_, map)
ags_mappings.omnimap(map, "<CR>", function(prompt_bufnr)
Expand All @@ -215,4 +270,10 @@ M.show_custom_functions = function()
:find()
end

vim.api.nvim_create_user_command(
"AdvancedGitSearch",
"lua require('telescope').extensions.advanced_git_search.show_custom_functions()",
{ range = true }
)

return M
6 changes: 6 additions & 0 deletions lua/advanced_git_search/utils/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ M.setup = function(ext_config)

ext_config.diff_plugin = ext_config.diff_plugin or "fugitive"
ext_config.git_diff_flags = ext_config.git_diff_flags or {}
ext_config.show_builtin_git_pickers = ext_config.show_builtin_git_pickers
or false

config = ext_config
end
Expand Down Expand Up @@ -76,4 +78,8 @@ M.diff_plugin = function()
return diff_plugin
end

M.show_builtin_git_pickers = function()
return config["show_builtin_git_pickers"]
end

return M