diff --git a/doc/telescope.txt b/doc/telescope.txt index 14a63e5396..2058265abe 100644 --- a/doc/telescope.txt +++ b/doc/telescope.txt @@ -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()* diff --git a/lua/telescope/builtin/git.lua b/lua/telescope/builtin/git.lua index 0302c7d863..1f9fdb2d62 100644 --- a/lua/telescope/builtin/git.lua +++ b/lua/telescope/builtin/git.lua @@ -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" @@ -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 @@ -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, { @@ -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, { @@ -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" @@ -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" @@ -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)" @@ -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", "--", "." } @@ -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 diff --git a/lua/telescope/builtin/init.lua b/lua/telescope/builtin/init.lua index dd7ac48f8f..f3ba372688 100644 --- a/lua/telescope/builtin/init.lua +++ b/lua/telescope/builtin/init.lua @@ -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) builtin.git_files = require_on_exported_call("telescope.builtin.git").files --- Lists commits for current directory with diff preview