Skip to content

Commit 1e6feb8

Browse files
committed
msys2 path patches to make git functions work at least
The patch assumes that `plenary.nvim` has the changes I made [in the corresponded PR](nvim-lua/plenary.nvim#663). At least for `plenary.utils` module usage and bruteforce paths conversions in internal logic. As stated in the `plenary`'s PR the msys2 integration is very fragile so I left all my comments and test notifies for further debugging.
1 parent b4da76b commit 1e6feb8

File tree

6 files changed

+144
-1
lines changed

6 files changed

+144
-1
lines changed

lua/telescope/_.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ local utils = require "telescope.utils"
99

1010
local M = {}
1111

12+
--qqq
13+
local U = require "plenary.utils"
14+
--!qqq
15+
1216
local AsyncJob = {}
1317
AsyncJob.__index = AsyncJob
1418

@@ -31,6 +35,15 @@ function AsyncJob.new(opts)
3135
end
3236
end
3337

38+
--qqq
39+
-- Due to explicit uv.spawn calls w/o plenary's Path/Job
40+
-- we must adjust our path here as well
41+
if U.is_msys2 then
42+
opts.cwd = U.posix_to_windows(opts.cwd)
43+
self.uv_opts.cwd = opts.cwd
44+
end
45+
--!qqq
46+
3447
self.uv_opts.stdio = {
3548
self.stdin.handle,
3649
self.stdout.handle,

lua/telescope/actions/init.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ local from_entry = require "telescope.from_entry"
6969
local transform_mod = require("telescope.actions.mt").transform_mod
7070
local resolver = require "telescope.config.resolve"
7171

72+
--qqq
73+
local U = require "plenary.utils"
74+
--!qqq
75+
7276
local actions = setmetatable({}, {
7377
__index = function(_, k)
7478
error("Key does not exist for 'telescope.actions': " .. tostring(k))
@@ -591,6 +595,24 @@ actions.git_apply_stash = function(prompt_bufnr)
591595
return
592596
end
593597
actions.close(prompt_bufnr)
598+
--qqq
599+
--vim.notify("git_apply_stash: " .. vim.inspect(selection.value), vim.log.levels.ERROR)
600+
if U.is_msys2 then
601+
-- Either because I use zsh.exe or bash.exe (nvim's shell) handles "{}" specially
602+
-- we need to escape it here. And in other places that use "{}" un-escaped.
603+
-- Idk exactly why "{}" disappears from the final command
604+
-- even when I add "{}" to escape chars in "plenary/job.lua:expand()"
605+
-- (which is not called anyway).
606+
-- utils.get_os_command_output { "echo", "$SHELL" } outputs { "$SHELL" }.
607+
-- how can I test it exactly?
608+
--
609+
-- Changing "selection.value" itself due to bad experience with stash previewer.
610+
if not selection.escaped then
611+
selection.value = vim.fn.escape(selection.value, "{}")
612+
selection.escaped = true
613+
end
614+
end
615+
--!qqq
594616
local _, ret, stderr = utils.get_os_command_output { "git", "stash", "apply", "--index", selection.value }
595617
if ret == 0 then
596618
utils.notify("actions.git_apply_stash", {

lua/telescope/builtin/__git.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ local entry_display = require "telescope.pickers.entry_display"
1010
local strings = require "plenary.strings"
1111
local Path = require "plenary.path"
1212

13+
--qqq
14+
local U = require "plenary.utils"
15+
--!qqq
16+
1317
local conf = require("telescope.config").values
1418
local git_command = utils.__git_command
1519

@@ -46,6 +50,10 @@ git.files = function(opts)
4650
git_command({ "-c", "core.quotepath=false", "ls-files", "--exclude-standard", "--cached" }, opts)
4751
)
4852

53+
--qqq
54+
--vim.notify("git.files: " .. vim.inspect(opts), vim.log.levels.ERROR)
55+
--!qqq
56+
4957
pickers
5058
.new(opts, {
5159
prompt_title = "Git Files",
@@ -191,6 +199,13 @@ git.bcommits = function(opts)
191199
opts.git_command =
192200
vim.F.if_nil(opts.git_command, git_command({ "log", "--pretty=oneline", "--abbrev-commit", "--follow" }, opts))
193201

202+
--qqq
203+
--vim.notify("git.bcommits: " .. vim.inspect(git_command) .. ", " .. vim.inspect(opts), vim.log.levels.ERROR)
204+
--if U.is_msys2 then
205+
-- opts.current_file = U.posix_to_windows(opts.current_file)
206+
--end
207+
--!qqq
208+
194209
local title = "Git BCommits"
195210
local finder = finders.new_oneshot_job(
196211
utils.flatten {
@@ -371,6 +386,18 @@ git.status = function(opts)
371386

372387
local args = { "status", "--porcelain=v1", "--", "." }
373388

389+
--qqq
390+
-- Running "nvim ."->":Telescope git_status" ends up with posix-style path.
391+
-- UPD. It gets fixed in later invocations.
392+
--if opts.cwd:find("^/") then
393+
-- if U.is_msys2 then
394+
-- opts.cwd = U.posix_to_windows(opts.cwd)
395+
-- end
396+
-- --vim.notify("git.status: opts.cwd = " .. vim.inspect(opts.cwd), vim.log.levels.ERROR)
397+
-- --error(debug.traceback())
398+
--end
399+
--!qqq
400+
374401
local gen_new_finder = function()
375402
if vim.F.if_nil(opts.expand_dir, true) then
376403
table.insert(args, #args - 1, "-uall")
@@ -404,6 +431,13 @@ git.status = function(opts)
404431
end
405432
end
406433

434+
--qqq
435+
-- If we have no changes and status is empty why git_status does not exit
436+
-- with notification? Is it supposed to be like that, right?
437+
-- On "v0.1.8" version I get notification and Telescope just exits as it should be.
438+
--vim.notify("git.status: self.finder = " .. vim.inspect(self.finder) .. ", count = " .. vim.inspect(count) .. ", prompt = " .. vim.inspect(prompt) , vim.log.levels.WARN)
439+
--!qqq
440+
407441
if count == 0 and prompt == "" then
408442
utils.notify("builtin.git_status", {
409443
msg = "No changes found",
@@ -479,6 +513,15 @@ local set_opts_cwd = function(opts)
479513
opts.cwd = vim.loop.cwd()
480514
end
481515

516+
--qqq
517+
-- Running "nvim ."->":Telescope git_status" ends up with posix-style path
518+
--vim.notify("set_opts_cwd: opts = " .. vim.inspect(opts), vim.log.levels.ERROR)
519+
--error(debug.traceback())
520+
if U.is_msys2 then
521+
opts.cwd = U.posix_to_windows(opts.cwd)
522+
end
523+
--!qqq
524+
482525
local toplevel, ret = utils.get_os_command_output({ "git", "rev-parse", "--show-toplevel" }, opts.cwd)
483526

484527
if ret ~= 0 then

lua/telescope/previewers/buffer_previewer.lua

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ local global_state = require "telescope.state"
88

99
local pscan = require "plenary.scandir"
1010

11+
--qqq
12+
local U = require "plenary.utils"
13+
--!qqq
14+
1115
local buf_delete = utils.buf_delete
1216
local git_command = utils.__git_command
1317

@@ -830,10 +834,37 @@ previewers.git_stash_diff = defaulter(function(opts)
830834
return previewers.new_buffer_previewer {
831835
title = "Git Stash Preview",
832836
get_buffer_by_name = function(_, entry)
837+
--qqq
838+
if not entry.escaped then
839+
entry.value = vim.fn.escape(entry.value, "{}")
840+
entry.escaped = true
841+
end
842+
--!qqq
833843
return entry.value
834844
end,
835845

836846
define_preview = function(self, entry, _)
847+
--qqq
848+
if U.is_msys2 then
849+
-- Either because I use zsh.exe or bash.exe (nvim's shell) handles "{}" specially
850+
-- we need to escape it here. And in other places that use "{}" un-escaped.
851+
-- Idk exactly why "{}" disappears from the final command
852+
-- even when I add "{}" to escape chars in "plenary/job.lua:expand()"
853+
-- (which is not called anyway).
854+
-- utils.get_os_command_output { "echo", "$SHELL" } outputs { "$SHELL" },
855+
-- how can I test it exactly?
856+
--
857+
-- Could be done just in case cause it is in posix-style.
858+
--opts.cwd = U.posix_to_windows(opts.cwd)
859+
-- If we do not change "entry.value" itself for each unique entry it ends up with an error
860+
-- when passed directly escaped like "vim.fn.escape(entry.value)".
861+
if not entry.escaped then
862+
entry.value = vim.fn.escape(entry.value, "{}")
863+
entry.escaped = true
864+
end
865+
--vim.notify("git_stash_diff: opts = " .. vim.inspect(opts) .. ", entry = " .. vim.inspect(entry) .. ", self = " .. vim.inspect(self), vim.log.levels.WARN)
866+
end
867+
--!qqq
837868
local cmd = git_command({ "--no-pager", "stash", "show", "-p", entry.value }, opts)
838869
putils.job_maker(cmd, self.state.bufnr, {
839870
value = entry.value,

lua/telescope/previewers/utils.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ local conf = require("telescope.config").values
55
local Job = require "plenary.job"
66
local Path = require "plenary.path"
77

8+
--qqq
9+
local U = require "plenary.utils"
10+
--!qqq
11+
812
local telescope_utils = require "telescope.utils"
913

1014
local utils = {}
@@ -67,6 +71,15 @@ utils.job_maker = function(cmd, bufnr, opts)
6771
-- if passed, they will be use as the cache key
6872
-- if any of them are missing, cache will be skipped
6973
if opts.bufname ~= opts.value or not opts.bufname or not opts.value then
74+
75+
--qqq
76+
-- Will it be better to change self._raw_cwd in plenary's Job:_execute method?
77+
-- UPD. Making the path transformation in Job:new() solves "nvim ."->":Telescope git_commits"
78+
--if U.is_msys2 then
79+
-- opts.cwd = U.posix_to_windows(opts.cwd)
80+
--end
81+
--!qqq
82+
7083
local command = table.remove(cmd, 1)
7184
local writer = (function()
7285
if opts.writer ~= nil then

lua/telescope/utils.lua

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
local Path = require "plenary.path"
99
local Job = require "plenary.job"
1010

11+
--qqq
12+
local U = require "plenary.utils"
13+
--!qqq
14+
1115
local log = require "telescope.log"
1216

1317
local truncate = require("plenary.strings").truncate
@@ -67,7 +71,15 @@ utils.path_expand = function(path)
6771
end
6872

6973
if path:sub(1, 1) == "~" then
70-
local home = vim.loop.os_homedir() or "~"
74+
--qqq
75+
-- vim.loop.os_homedir() ignores HOME env variable in msys2
76+
if U.is_msys2 then
77+
local home = vim.fn.expand("~") or "~"
78+
else
79+
local home = vim.loop.os_homedir() or "~"
80+
end
81+
--!qqq
82+
--local home = vim.loop.os_homedir() or "~"
7183
if home:sub(-1) == "\\" or home:sub(-1) == "/" then
7284
home = home:sub(1, -2)
7385
end
@@ -561,6 +573,15 @@ function utils.get_os_command_output(cmd, cwd)
561573
})
562574
return {}
563575
end
576+
--qqq
577+
-- Running "nvim ."->":Telescope git_status" ends up with posix-style path
578+
-- UPD. Changing set_opts_cwd solves it.
579+
--if cwd:find("^/") then
580+
-- vim.notify("get_os_command_output: cmd = " .. vim.inspect(cmd) .. ", cwd = " .. vim.inspect(cwd), vim.log.levels.ERROR)
581+
-- error(debug.traceback())
582+
--end
583+
--vim.notify("get_os_command_output: " .. vim.inspect(cmd) .. ", " .. vim.inspect(cwd), vim.log.levels.ERROR)
584+
--!qqq
564585
local command = table.remove(cmd, 1)
565586
local stderr = {}
566587
local stdout, ret = Job:new({

0 commit comments

Comments
 (0)