Skip to content

Commit 5e004be

Browse files
jltoblergitster
authored andcommitted
gpg-interface: introduce sign_buffer_with_key()
The `sign_commit_to_strbuf()` helper in "commit.c" provides fallback logic to get the default configured signing key when a key is not provided and handles generating the commit signature accordingly. This signing operation is not really specific to commits as any arbitrary buffer can be signed. Also, in a subsequent commit, this same logic is reused by git-fast-import(1) when re-signing invalid commit signatures. Move the `sign_commit_to_strbuf()` helper from "commit.c" to "gpg-interface.c" and rename it to `sign_buffer_with_key()`. Also export this function so it can be used by "commit.c" and "builtin/fast-import.c" in the subsequent commit. Signed-off-by: Justin Tobler <jltobler@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 38347b2 commit 5e004be

3 files changed

Lines changed: 22 additions & 14 deletions

File tree

commit.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,18 +1170,6 @@ int add_header_signature(struct strbuf *buf, struct strbuf *sig, const struct gi
11701170
return 0;
11711171
}
11721172

1173-
static int sign_commit_to_strbuf(struct strbuf *sig, struct strbuf *buf, const char *keyid)
1174-
{
1175-
char *keyid_to_free = NULL;
1176-
int ret = 0;
1177-
if (!keyid || !*keyid)
1178-
keyid = keyid_to_free = get_signing_key();
1179-
if (sign_buffer(buf, sig, keyid))
1180-
ret = -1;
1181-
free(keyid_to_free);
1182-
return ret;
1183-
}
1184-
11851173
int parse_signed_commit(const struct commit *commit,
11861174
struct strbuf *payload, struct strbuf *signature,
11871175
const struct git_hash_algo *algop)
@@ -1759,7 +1747,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
17591747
oidcpy(&parent_buf[i++], &p->item->object.oid);
17601748

17611749
write_commit_tree(&buffer, msg, msg_len, tree, parent_buf, nparents, author, committer, extra);
1762-
if (sign_commit && sign_commit_to_strbuf(&sig, &buffer, sign_commit)) {
1750+
if (sign_commit && sign_buffer_with_key(&buffer, &sig, sign_commit)) {
17631751
result = -1;
17641752
goto out;
17651753
}
@@ -1791,7 +1779,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
17911779
free_commit_extra_headers(compat_extra);
17921780
free(mapped_parents);
17931781

1794-
if (sign_commit && sign_commit_to_strbuf(&compat_sig, &compat_buffer, sign_commit)) {
1782+
if (sign_commit && sign_buffer_with_key(&compat_buffer, &compat_sig, sign_commit)) {
17951783
result = -1;
17961784
goto out;
17971785
}

gpg-interface.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,19 @@ int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *sig
981981
return use_format->sign_buffer(buffer, signature, signing_key);
982982
}
983983

984+
int sign_buffer_with_key(struct strbuf *buffer, struct strbuf *signature,
985+
const char *signing_key)
986+
{
987+
char *keyid_to_free = NULL;
988+
int ret = 0;
989+
if (!signing_key || !*signing_key)
990+
signing_key = keyid_to_free = get_signing_key();
991+
if (sign_buffer(buffer, signature, signing_key))
992+
ret = -1;
993+
free(keyid_to_free);
994+
return ret;
995+
}
996+
984997
/*
985998
* Strip CR from the line endings, in case we are on Windows.
986999
* NEEDSWORK: make it trim only CRs before LFs and rename

gpg-interface.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ size_t parse_signed_buffer(const char *buf, size_t size);
8383
int sign_buffer(struct strbuf *buffer, struct strbuf *signature,
8484
const char *signing_key);
8585

86+
/*
87+
* Similar to `sign_buffer()`, but uses the default configured signing key as
88+
* returned by `get_signing_key()` when the provided "signing_key" is NULL or
89+
* empty. Returns 0 on success, non-zero on failure.
90+
*/
91+
int sign_buffer_with_key(struct strbuf *buffer, struct strbuf *signature,
92+
const char *signing_key);
8693

8794
/*
8895
* Returns corresponding string in lowercase for a given member of

0 commit comments

Comments
 (0)