Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The git_files picker falls back to find_files when outside of a git repository #1342

Closed
wants to merge 7 commits into from
Closed
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
24 changes: 14 additions & 10 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -838,16 +838,20 @@ builtin.git_files({opts}) *builtin.git_files()*
{opts} (table) options to pass to the picker

Options: ~
{cwd} (string) specify the path of the repo
{use_git_root} (boolean) if we should use git root as cwd or
the cwd (important for submodule)
(default: true)
{show_untracked} (boolean) if true, adds `--others` flag to
command and shows untracked files
(default: true)
{recurse_submodules} (boolean) if true, adds the
`--recurse-submodules` flag to command
(default: false)
{cwd} (string) specify the path of the repo
{use_git_root} (boolean) if we should use git root as cwd or
the cwd (important for submodule)
(default: true)
{show_untracked} (boolean) if true, adds `--others` flag to
command and shows untracked files
(default: true)
{recurse_submodules} (boolean) if true, adds the
` --recurse-submodules` flag to command
(default: false)
{find_files_fallback} (boolean) if true, the find_files picker is run
instead when the picker is used
outside of a git repository
(default: true)


builtin.git_commits({opts}) *builtin.git_commits()*
Expand Down
30 changes: 27 additions & 3 deletions lua/telescope/builtin/git.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local actions = require "telescope.actions"
local action_state = require "telescope.actions.state"
local command = require "telescope.command"
local finders = require "telescope.finders"
local make_entry = require "telescope.make_entry"
local pickers = require "telescope.pickers"
Expand All @@ -16,6 +17,17 @@ local git = {}
git.files = function(opts)
local show_untracked = utils.get_default(opts.show_untracked, true)
local recurse_submodules = utils.get_default(opts.recurse_submodules, false)
local find_files_fallback = utils.get_default(opts.find_files_fallback, true)

if opts.not_in_git_repo then
if find_files_fallback then
command.load_command(opts.start_line, opts.end_line, opts.count, 'find_files')
return
else
error(opts.cwd .. " is not a git directory")
end
end

if show_untracked and recurse_submodules then
error "Git does not support both --others and --recurse-submodules"
end
Expand All @@ -42,7 +54,15 @@ git.files = function(opts)
}):find()
end

local function fail_if_not_in_git_repo(opts)
local find_files_fallback = utils.get_default(opts.find_files_fallback, true)
if find_files_fallback and opts.not_in_git_repo then
error(opts.cwd .. " is not a git directory")
end
end

git.commits = function(opts)
fail_if_not_in_git_repo(opts)
opts.entry_maker = opts.entry_maker or make_entry.gen_from_git_commits(opts)

pickers.new(opts, {
Expand Down Expand Up @@ -80,6 +100,7 @@ git.commits = function(opts)
end

git.stash = function(opts)
fail_if_not_in_git_repo(opts)
opts.entry_maker = opts.entry_maker or make_entry.gen_from_git_stash()

pickers.new(opts, {
Expand Down Expand Up @@ -108,6 +129,7 @@ local get_current_buf_line = function(winnr)
end

git.bcommits = function(opts)
fail_if_not_in_git_repo(opts)
opts.current_line = not opts.current_file and get_current_buf_line(0) or nil
opts.current_file = opts.current_file or vim.fn.expand "%:p"

Expand Down Expand Up @@ -148,12 +170,12 @@ git.bcommits = function(opts)
return bufnr
end

local vimdiff = function(selection, command)
local vimdiff = function(selection, position)
local ft = vim.bo.filetype
vim.cmd "diffthis"

local bufnr = get_buffer_of_orig(selection)
vim.cmd(string.format("%s %s", command, bufnr))
vim.cmd(string.format("%s %s", position, bufnr))
vim.bo.filetype = ft
vim.cmd "diffthis"

Expand Down Expand Up @@ -190,6 +212,7 @@ git.bcommits = function(opts)
end

git.branches = function(opts)
fail_if_not_in_git_repo(opts)
local format = "%(HEAD)"
.. "%(refname)"
.. "%(authorname)"
Expand Down Expand Up @@ -309,6 +332,7 @@ git.branches = function(opts)
end

git.status = function(opts)
fail_if_not_in_git_repo(opts)
local gen_new_finder = function()
local expand_dir = utils.if_nil(opts.expand_dir, true, opts.expand_dir)
local git_cmd = { "git", "status", "-s", "--", "." }
Expand Down Expand Up @@ -368,7 +392,7 @@ local set_opts_cwd = function(opts)
if ret ~= 0 then
local output = utils.get_os_command_output({ "git", "rev-parse", "--is-inside-work-tree" }, opts.cwd)
if output[1] ~= "true" then
error(opts.cwd .. " is not a git directory")
opts.not_in_git_repo = true
end
else
if use_git_root then
Expand Down
2 changes: 2 additions & 0 deletions lua/telescope/builtin/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ builtin.current_buffer_tags = require_on_exported_call("telescope.builtin.files"
---@field use_git_root boolean: if we should use git root as cwd or the cwd (important for submodule) (default: true)
---@field show_untracked boolean: if true, adds `--others` flag to command and shows untracked files (default: true)
---@field recurse_submodules boolean: if true, adds the `--recurse-submodules` flag to command (default: false)
---@field find_files_fallback boolean: if true, the find_files picker is run instead when the picker is used
-- outside of a git repository (default: true)
Comment on lines +149 to +150
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can the docs be split this way?

builtin.git_files = require_on_exported_call("telescope.builtin.git").files

--- Lists commits for current directory with diff preview
Expand Down