Skip to content

Commit

Permalink
fix: fix ssh signing with custom program
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperBo committed May 18, 2024
1 parent 2a5f96c commit 4a373dd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 42 deletions.
63 changes: 33 additions & 30 deletions lua/fugit2/core/git_gpg.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ local utils = require "fugit2.utils"

local M = {}

---@class Fugit2GitGPGConfig
---@field use_ssh boolean
---@field keyid string?
---@field program string?

---@param keyid string
---@return boolean whether is literal key
---@return string key
Expand Down Expand Up @@ -51,10 +56,11 @@ end

---@param buf string
---@param keyid string signing_key
---@param program string?
---@return string? signed
---@return integer err
---@return string err_msg
local function sign_buffer_ssh(buf, keyid)
local function sign_buffer_ssh(buf, keyid, program)
local key_file, err_msg, err_name

local is_literal, key = is_literal_ssh_key(keyid)
Expand Down Expand Up @@ -82,7 +88,7 @@ local function sign_buffer_ssh(buf, keyid)
end

local job = PlenaryJob:new {
command = "ssh-keygen",
command = program or "ssh-keygen",
args = ssh_keygen_args,
enable_recording = true,
writer = buf,
Expand Down Expand Up @@ -150,11 +156,12 @@ end
---@param commit_content string
---@param msg string
---@param keyid string
---@param program string?
---@return GitObjectId?
---@return integer err
---@return string err_msg
local function create_commit_with_ssh(repo, commit_content, msg, keyid)
local ssh_sign, err, err_msg = sign_buffer_ssh(commit_content, keyid)
local function create_commit_with_ssh(repo, commit_content, msg, keyid, program)
local ssh_sign, err, err_msg = sign_buffer_ssh(commit_content, keyid, program)
if not ssh_sign then
return nil, err, "Failed to sign commit with ssh, " .. err_msg
end
Expand All @@ -167,20 +174,19 @@ end
---@param index GitIndex
---@param signature GitSignature
---@param msg string commit message
---@param use_ssh boolean,
---@param keyid string? signing key id
---@param config Fugit2GitGPGConfig
---@return GitObjectId?
---@return integer err_code
---@return string err_msg
function M.create_commit_gpg(repo, index, signature, msg, use_ssh, keyid)
function M.create_commit_gpg(repo, index, signature, msg, config)
local context, commit_content, err, err_msg

if use_ssh and not keyid then
if config.use_ssh and not config.keyid then
return nil, -1, "user.signingkey is need when gpg.format is ssh"
end

if not use_ssh then
context, err, err_msg = create_gpgme_context(keyid)
if not config.use_ssh then
context, err, err_msg = create_gpgme_context(config.keyid)
if not context then
return nil, err, err_msg
end
Expand All @@ -191,10 +197,10 @@ function M.create_commit_gpg(repo, index, signature, msg, use_ssh, keyid)
return nil, err, "Failed to create git commit content"
end

if not use_ssh and context then
if not config.use_ssh and context then
return create_commit_with_context(repo, context, commit_content, msg)
elseif keyid then
return create_commit_with_ssh(repo, commit_content, msg, keyid)
elseif config.keyid then
return create_commit_with_ssh(repo, commit_content, msg, config.keyid, config.program)
end

return nil, -1, "unknown error"
Expand All @@ -205,20 +211,19 @@ end
---@param index GitIndex?
---@param signature GitSignature?
---@param msg string? commit message
---@param use_ssh boolean use ssh signing instead of gpg
---@param keyid string? signing key id
---@param config Fugit2GitGPGConfig
---@return GitObjectId?
---@return integer err_code
---@return string err_msg
function M.amend_commit_gpg(repo, index, signature, msg, use_ssh, keyid)
function M.amend_commit_gpg(repo, index, signature, msg, config)
local context, commit_content, err, err_msg

if use_ssh and not keyid then
if config.use_ssh and not config.keyid then
return nil, -1, "user.signingkey is need when gpg.format is ssh"
end

if not use_ssh then
context, err, err_msg = create_gpgme_context(keyid)
if not config.use_ssh then
context, err, err_msg = create_gpgme_context(config.keyid)
if not context then
return nil, err, err_msg
end
Expand All @@ -229,10 +234,10 @@ function M.amend_commit_gpg(repo, index, signature, msg, use_ssh, keyid)
return nil, err, "Failed to create git commit content"
end

if not use_ssh and context then
if not config.use_ssh and context then
return create_commit_with_context(repo, context, commit_content, msg)
elseif keyid then
return create_commit_with_ssh(repo, commit_content, msg, keyid)
elseif config.keyid then
return create_commit_with_ssh(repo, commit_content, msg, config.keyid, config.program)
end

return nil, -1, "unknown error"
Expand All @@ -242,25 +247,23 @@ end
---@param repo GitRepository
---@param signature GitSignature?
---@param msg string? commit message
---@param use_ssh boolean use ssh signing instead of gpg
---@param keyid string? signing key id
---@param config Fugit2GitGPGConfig
---@return GitObjectId?
---@return integer err_code
---@return string err_msg
function M.reword_commit_gpg(repo, signature, msg, use_ssh, keyid)
return M.amend_commit_gpg(repo, nil, signature, msg, use_ssh, keyid)
function M.reword_commit_gpg(repo, signature, msg, config)
return M.amend_commit_gpg(repo, nil, signature, msg, config)
end

-- Extend commit with gpg signing
---@param repo GitRepository
---@param index GitIndex
---@param use_ssh boolean use ssh signing instead of gpg
---@param keyid string? signing key id
---@param config Fugit2GitGPGConfig
---@return GitObjectId?
---@return integer err_code
---@return string err_msg
function M.extend_commit_gpg(repo, index, use_ssh, keyid)
return M.amend_commit_gpg(repo, index, nil, nil, use_ssh, keyid)
function M.extend_commit_gpg(repo, index, config)
return M.amend_commit_gpg(repo, index, nil, nil, config)
end

return M
27 changes: 15 additions & 12 deletions lua/fugit2/view/git_status.lua
Original file line number Diff line number Diff line change
Expand Up @@ -825,12 +825,11 @@ function GitStatus:read_config()
end

-- Read gpg config use_ssh and keyid
---@return boolean use_ssh
---@return string? keyid
---@return Fugit2GitGPGConfig
function GitStatus:read_gpg_config()
local config = self:read_config()
if not config then
return false, nil
return { use_ssh = false }
end

local keyid = config:get_string "user.signingkey" or nil
Expand All @@ -841,7 +840,11 @@ function GitStatus:read_gpg_config()
keyid = tostring(self._git.signature)
end

return use_ssh, keyid
return {
use_ssh = use_ssh,
keyid = keyid,
program = config:get_string "gpg.ssh.program" or nil,
}
end

-- Updates git status.
Expand Down Expand Up @@ -1265,10 +1268,10 @@ function GitStatus:_git_create_commit(message, args)
result.err = err
else
-- create commit with gpg sign
local use_ssh, keyid = self:read_gpg_config()
local conf = self:read_gpg_config()
local err_msg
commit_id, err, err_msg =
git_gpg.create_commit_gpg(self.repo, self.index, self._git.signature, prettified, use_ssh, keyid)
git_gpg.create_commit_gpg(self.repo, self.index, self._git.signature, prettified, conf)
result.commit_id = commit_id
result.err = err
result.message = err_msg
Expand Down Expand Up @@ -1302,8 +1305,8 @@ function GitStatus:_git_extend_commit(args)
commit_id, err = self.repo:amend_extend(self.index)
else
-- extend commit with gpg sign
local use_ssh, keyid = self:read_gpg_config()
commit_id, err, err_msg = git_gpg.extend_commit_gpg(self.repo, self.index, use_ssh, keyid)
local conf = self:read_gpg_config()
commit_id, err, err_msg = git_gpg.extend_commit_gpg(self.repo, self.index, conf)
end

if commit_id then
Expand Down Expand Up @@ -1340,8 +1343,8 @@ function GitStatus:_git_reword_commit(message, args)
commit_id, err = self.repo:amend_reword(signature, prettified)
else
-- reword commit with gpg sign
local use_ssh, keyid = self:read_gpg_config()
commit_id, err, err_msg = git_gpg.reword_commit_gpg(self.repo, signature, prettified, use_ssh, keyid)
local conf = self:read_gpg_config()
commit_id, err, err_msg = git_gpg.reword_commit_gpg(self.repo, signature, prettified, conf)
end

if commit_id then
Expand Down Expand Up @@ -1380,8 +1383,8 @@ function GitStatus:_git_amend_commit(message, args)
if not gpg_sign then
commit_id, err = self.repo:amend(self.index, self._git.signature, prettified)
else
local use_ssh, keyid = self:read_gpg_config()
commit_id, err, err_msg = git_gpg.amend_commit_gpg(self.repo, self.index, signature, prettified, use_ssh, keyid)
local conf = self:read_gpg_config()
commit_id, err, err_msg = git_gpg.amend_commit_gpg(self.repo, self.index, signature, prettified, conf)
end

if commit_id then
Expand Down

0 comments on commit 4a373dd

Please sign in to comment.