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

fix: get the repo version of the username #1079

Merged
merged 2 commits into from
Jul 3, 2024
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
2 changes: 1 addition & 1 deletion lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ M.blame_line = async.create(1, function(opts)
local blame_linespec = create_blame_fmt(is_committed, opts.full)

if is_committed and opts.full then
local body = bcache.git_obj:command(
local body = bcache.git_obj.repo:command(
{ 'show', '-s', '--format=%B', result.sha },
{ text = true }
)
Expand Down
2 changes: 1 addition & 1 deletion lua/gitsigns/blame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ end
local show_commit = async.create(3, function(win, open, bcache)
local cursor = api.nvim_win_get_cursor(win)[1]
local sha = bcache.blame[cursor].commit.sha
local res = bcache.git_obj:command({ 'show', sha })
local res = bcache.git_obj.repo:command({ 'show', sha })
async.scheduler()
local commit_buf = api.nvim_create_buf(true, true)
api.nvim_buf_set_name(commit_buf, bcache:get_rev_bufname(sha, true))
Expand Down
99 changes: 37 additions & 62 deletions lua/gitsigns/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ M.Obj = Obj
--- @field abbrev_head string

--- @class Gitsigns.Repo : Gitsigns.RepoInfo
---
--- Username configured for the repo.
--- Needed for to determine "You" in current line blame.
--- @field username string
local Repo = {}
M.Repo = Repo

--- @class Gitsigns.Git.JobSpec : vim.SystemOpts
--- @field command? string
--- @field ignore_error? boolean

--- @async
Expand All @@ -64,7 +66,7 @@ local function git_command(args, spec)
spec = spec or {}

local cmd = {
spec.command or 'git',
'git',
'--no-pager',
'--no-optional-locks',
'--literal-pathspecs',
Expand All @@ -82,15 +84,17 @@ local function git_command(args, spec)

--- @type vim.SystemCompleted
local obj = asystem(cmd, spec)
local stdout = obj.stdout
local stderr = obj.stderr

if not spec.ignore_error and obj.code > 0 then
local cmd_str = table.concat(cmd, ' ')
log.eprintf("Received exit code %d when running command\n'%s':\n%s", obj.code, cmd_str, stderr)
log.eprintf(
"Received exit code %d when running command\n'%s':\n%s",
obj.code,
table.concat(cmd, ' '),
obj.stderr
)
end

local stdout_lines = vim.split(stdout or '', '\n')
local stdout_lines = vim.split(obj.stdout or '', '\n')

if spec.text then
-- If stdout ends with a newline, then remove the final empty string after
Expand All @@ -107,11 +111,11 @@ local function git_command(args, spec)
end
end

if stderr == '' then
stderr = nil
if obj.stderr == '' then
obj.stderr = nil
end

return stdout_lines, stderr
return stdout_lines, obj.stderr
end

--- @async
Expand Down Expand Up @@ -139,18 +143,16 @@ function M.diff(file_cmp, file_buf, indent_heuristic, diff_algo)
end

--- @async
--- @param gitdir string
--- @param gitdir? string
--- @param head_str string
--- @param cwd string
--- @param cmd? string
--- @return string
local function process_abbrev_head(gitdir, head_str, cwd, cmd)
local function process_abbrev_head(gitdir, head_str, cwd)
if not gitdir then
return head_str
end
if head_str == 'HEAD' then
local short_sha = git_command({ 'rev-parse', '--short', 'HEAD' }, {
command = cmd,
ignore_error = true,
cwd = cwd,
})[1] or ''
Expand All @@ -170,38 +172,26 @@ end

local has_cygpath = jit and jit.os == 'Windows' and vim.fn.executable('cygpath') == 1

local cygpath_convert ---@type fun(path: string): string

if has_cygpath then
cygpath_convert = function(path)
--- @type vim.SystemCompleted
local obj = asystem({ 'cygpath', '-aw', path })
return obj.stdout
end
end

--- @param path string
--- @return string
--- @param path? string
--- @return string?
local function normalize_path(path)
if path and has_cygpath and not uv.fs_stat(path) then
-- If on windows and path isn't recognizable as a file, try passing it
-- through cygpath
path = cygpath_convert(path)
path = asystem({ 'cygpath', '-aw', path }).stdout
end
return path
end

--- @async
--- @param cwd string
--- @param cmd? string
--- @param gitdir? string
--- @param toplevel? string
--- @return Gitsigns.RepoInfo
function M.get_repo_info(cwd, cmd, gitdir, toplevel)
function M.get_repo_info(cwd, gitdir, toplevel)
-- Does git rev-parse have --absolute-git-dir, added in 2.13:
-- https://public-inbox.org/git/20170203024829.8071-16-szeder.dev@gmail.com/
local has_abs_gd = check_version({ 2, 13 })
local git_dir_opt = has_abs_gd and '--absolute-git-dir' or '--git-dir'

-- Wait for internal scheduler to settle before running command (#215)
scheduler()
Expand All @@ -219,13 +209,12 @@ function M.get_repo_info(cwd, cmd, gitdir, toplevel)
vim.list_extend(args, {
'rev-parse',
'--show-toplevel',
git_dir_opt,
has_abs_gd and '--absolute-git-dir' or '--git-dir',
'--abbrev-ref',
'HEAD',
})

local results = git_command(args, {
command = cmd,
ignore_error = true,
cwd = toplevel or cwd,
})
Expand All @@ -240,7 +229,7 @@ function M.get_repo_info(cwd, cmd, gitdir, toplevel)
return {
toplevel = toplevel_r,
gitdir = gitdir_r,
abbrev_head = process_abbrev_head(gitdir_r, results[3], cwd, cmd),
abbrev_head = process_abbrev_head(gitdir_r, results[3], cwd),
detached = toplevel_r and gitdir_r ~= toplevel_r .. '/.git',
}
end
Expand All @@ -258,10 +247,7 @@ function Repo:command(args, spec)
spec = spec or {}
spec.cwd = self.toplevel

local args1 = {
'--git-dir',
self.gitdir,
}
local args1 = { '--git-dir', self.gitdir }

if self.detached then
vim.list_extend(args1, { '--work-tree', self.toplevel })
Expand Down Expand Up @@ -307,7 +293,6 @@ function Repo:get_show_text(object, encoding)

if encoding and encoding ~= 'utf-8' and iconv_supported(encoding) then
for i, l in ipairs(stdout) do
--- @diagnostic disable-next-line:param-type-mismatch
stdout[i] = vim.iconv(l, encoding, 'utf-8')
end
end
Expand All @@ -328,34 +313,26 @@ end
function Repo.new(dir, gitdir, toplevel)
local self = setmetatable({}, { __index = Repo })

self.username = git_command({ 'config', 'user.name' }, { ignore_error = true })[1]
local info = M.get_repo_info(dir, nil, gitdir, toplevel)
local info = M.get_repo_info(dir, gitdir, toplevel)
for k, v in
pairs(info --[[@as table<string,any>]])
do
---@diagnostic disable-next-line:no-unknown
self[k] = v
end

self.username = self:command({ 'config', 'user.name' }, { ignore_error = true })[1]

return self
end

--------------------------------------------------------------------------------
-- Git object methods
--------------------------------------------------------------------------------

--- Run git command the with the objects gitdir and toplevel
--- @param args string[]
--- @param spec? Gitsigns.Git.JobSpec
--- @return string[] stdout, string? stderr
function Obj:command(args, spec)
return self.repo:command(args, spec)
end

--- @param revision? string
function Obj:update_revision(revision)
revision = util.norm_base(revision)
self.revision = revision
self.revision = util.norm_base(revision)
self:update()
end

Expand Down Expand Up @@ -427,7 +404,7 @@ function Obj:file_info_index(file, silent)

cmd[#cmd + 1] = file or self.file

local results, stderr = self:command(cmd, { ignore_error = true })
local results, stderr = self.repo:command(cmd, { ignore_error = true })

if stderr and not silent then
-- ignore_error for the cases when we run:
Expand Down Expand Up @@ -474,7 +451,7 @@ end
--- @param silent? boolean
--- @return Gitsigns.FileInfo
function Obj:file_info_tree(file, silent)
local results, stderr = self:command({
local results, stderr = self.repo:command({
'-c',
'core.quotepath=off',
'ls-tree',
Expand Down Expand Up @@ -544,7 +521,7 @@ local function autocmd_changed(file)
end

function Obj:unstage_file()
self:command({ 'reset', self.file })
self.repo:command({ 'reset', self.file })
autocmd_changed(self.file)
end

Expand Down Expand Up @@ -594,12 +571,12 @@ local function ensure_file_in_index(obj)

if not obj.object_name then
-- If there is no object_name then it is not yet in the index so add it
obj:command({ 'add', '--intent-to-add', obj.file })
obj.repo:command({ 'add', '--intent-to-add', obj.file })
else
-- Update the index with the common ancestor (stage 1) which is what bcache
-- stores
local info = string.format('%s,%s,%s', obj.mode_bits, obj.object_name, obj.relpath)
obj:command({ 'update-index', '--add', '--cacheinfo', info })
obj.repo:command({ 'update-index', '--add', '--cacheinfo', info })
end

obj:update()
Expand All @@ -608,7 +585,7 @@ end
--- Stage 'lines' as the entire contents of the file
--- @param lines string[]
function Obj:stage_lines(lines)
local stdout = self:command({
local stdout = self.repo:command({
'hash-object',
'-w',
'--path',
Expand All @@ -618,7 +595,7 @@ function Obj:stage_lines(lines)

local new_object = stdout[1]

self:command({
self.repo:command({
'update-index',
'--cacheinfo',
string.format('%s,%s,%s', self.mode_bits, new_object, self.relpath),
Expand All @@ -632,9 +609,7 @@ end
function Obj:stage_hunks(hunks, invert)
ensure_file_in_index(self)

local gs_hunks = require('gitsigns.hunks')

local patch = gs_hunks.create_patch(self.relpath, hunks, self.mode_bits, invert)
local patch = require('gitsigns.hunks').create_patch(self.relpath, hunks, self.mode_bits, invert)

if not self.i_crlf and self.w_crlf then
-- Remove cr
Expand All @@ -643,7 +618,7 @@ function Obj:stage_hunks(hunks, invert)
end
end

self:command({
self.repo:command({
'apply',
'--whitespace=nowarn',
'--cached',
Expand All @@ -658,7 +633,7 @@ end

--- @return string?
function Obj:has_moved()
local out = self:command({ 'diff', '--name-status', '-C', '--cached' })
local out = self.repo:command({ 'diff', '--name-status', '-C', '--cached' })
local orig_relpath = self.orig_relpath or self.relpath
for _, l in ipairs(out) do
local parts = vim.split(l, '%s+')
Expand Down
2 changes: 1 addition & 1 deletion lua/gitsigns/git/blame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function M.run_blame(obj, lines, lnum, revision, opts)
process_incremental(data, commits, ret)
end

local _, stderr = obj:command(args, { stdin = lines, stdout = on_stdout, ignore_error = true })
local _, stderr = obj.repo:command(args, { stdin = lines, stdout = on_stdout, ignore_error = true })

if stderr then
error_once('Error running git-blame: ' .. stderr)
Expand Down
8 changes: 3 additions & 5 deletions test/actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ local function expect_hunks(exp_hunks)
end

describe('actions', function()
local config
local config --- @type Gitsigns.Config

before_each(function()
clear()
Expand Down Expand Up @@ -308,10 +308,8 @@ describe('actions', function()
local newfile = helpers.newfile
exec_lua([[vim.g.editorconfig = false]])
system("printf 'This is a file with no nl at eof' > " .. newfile)
helpers.gitm({
{ 'add', newfile },
{ 'commit', '-m', 'commit on main' },
})
helpers.git({ 'add', newfile })
helpers.git({ 'commit', '-m', 'commit on main' })

edit(newfile)
check({ status = { head = 'master', added = 0, changed = 0, removed = 0 } })
Expand Down
8 changes: 4 additions & 4 deletions test/gitdir_watcher_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ describe('gitdir_watcher', function()

match_debug_messages({
'attach(1): Attaching (trigger=BufReadPost)',
np('run_job: git .* config user.name'),
np(
'run_job: git .* rev%-parse %-%-show%-toplevel %-%-absolute%-git%-dir %-%-abbrev%-ref HEAD'
),
np('run_job: git .* config user.name'),
np('run_job: git .* ls%-files .* ' .. vim.pesc(test_file)),
n('watch_gitdir(1): Watching git dir'),
np('run_job: git .* show .*'),
Expand Down Expand Up @@ -147,8 +147,8 @@ describe('gitdir_watcher', function()
helpers.write_to_file(f1, { '1', '2', '3' })
helpers.write_to_file(f2, { '1', '2', '3' })

helpers.gitf({ 'add', f1, f2 })
helpers.gitf({ 'commit', '-m', 'init commit' })
git({ 'add', f1, f2 })
git({ 'commit', '-m', 'init commit' })

setup_gitsigns(test_config)

Expand All @@ -165,7 +165,7 @@ describe('gitdir_watcher', function()
helpers.check({ signs = { changed = 1 } }, b1)
helpers.check({ signs = { changed = 1 } }, b2)

helpers.gitf({ 'add', f1, f2 })
git({ 'add', f1, f2 })

helpers.check({ signs = {} }, b1)
helpers.check({ signs = {} }, b2)
Expand Down
Loading
Loading