Debian Codegraph @

lua/gitsigns/git/cmd.lua

raw
1 local async = require('gitsigns.async')
2 local log = require('gitsigns.debug.log')
3 local util = require('gitsigns.util')
4
5 local asystem = async.wrap(3, require('gitsigns.system').system)
6
7 --- @class Gitsigns.Git.JobSpec : vim.SystemOpts
8 --- @field ignore_error? boolean
9
10 --- @async
11 --- @param args string[]
12 --- @param spec? Gitsigns.Git.JobSpec
13 --- @return string[] stdout, string? stderr, integer code
14 local function git_command(args, spec)
15 spec = spec or {}
16 if spec.cwd then
17 -- cwd must be a windows path and not a unix path
18 spec.cwd = util.cygpath(spec.cwd)
19 end
20
21 local cmd = {
22 'git',
23 '--no-pager',
24 '--no-optional-locks',
25 '--literal-pathspecs',
26 '-c',
27 'gc.auto=0', -- Disable auto-packing which emits messages to stderr
28 }
29 vim.list_extend(cmd, args)
30
31 if spec.text == nil then
32 spec.text = true
33 end
34
35 -- Force English messages for git output parsing.
36 -- Git translations can cause our stderr pattern matching to fail.
37 spec.env = vim.tbl_extend('force', spec.env or {}, {
38 LC_ALL = 'C',
39 LANGUAGE = 'C',
40 })
41
42 --- @type vim.SystemCompleted
43 local obj = asystem(cmd, spec)
44 async.schedule()
45
46 if not spec.ignore_error and obj.code > 0 then
47 log.eprintf(
48 "Received exit code %d when running command\n'%s':\n%s",
49 obj.code,
50 table.concat(cmd, ' '),
51 obj.stderr
52 )
53 end
54
55 local stdout_lines = vim.split(obj.stdout or '', '\n')
56
57 if spec.text then
58 -- If stdout ends with a newline, then remove the final empty string after
59 -- the split
60 if stdout_lines[#stdout_lines] == '' then
61 stdout_lines[#stdout_lines] = nil
62 end
63 end
64
65 if log.verbose then
66 log.vprintf('%d lines:', #stdout_lines)
67 for i = 1, math.min(10, #stdout_lines) do
68 log.vprintf('\t%s', stdout_lines[i])
69 end
70 end
71
72 if obj.stderr == '' then
73 obj.stderr = nil
74 end
75
76 return stdout_lines, obj.stderr, obj.code
77 end
78
79 return git_command