Skip to content

Commit 937032e

Browse files
bk2204gitster
authored andcommitted
commit: allow parsing arbitrary buffers with headers
Currently only commits are signed with headers. However, in the future, we'll also sign tags with headers as well. Let's refactor out a function called parse_buffer_signed_by_header which does exactly that. In addition, since we'll want to sign things other than commits this way, let's call the function sign_with_header instead of do_sign_commit. Signed-off-by: brian m. carlson <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 482c119 commit 937032e

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

commit.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ static const char *gpg_sig_headers[] = {
995995
"gpgsig-sha256",
996996
};
997997

998-
static int do_sign_commit(struct strbuf *buf, const char *keyid)
998+
int sign_with_header(struct strbuf *buf, const char *keyid)
999999
{
10001000
struct strbuf sig = STRBUF_INIT;
10011001
int inspos, copypos;
@@ -1035,13 +1035,26 @@ static int do_sign_commit(struct strbuf *buf, const char *keyid)
10351035
return 0;
10361036
}
10371037

1038+
1039+
10381040
int parse_signed_commit(const struct commit *commit,
10391041
struct strbuf *payload, struct strbuf *signature,
10401042
const struct git_hash_algo *algop)
10411043
{
1042-
10431044
unsigned long size;
10441045
const char *buffer = get_commit_buffer(commit, &size);
1046+
int ret = parse_buffer_signed_by_header(buffer, size, payload, signature, algop);
1047+
1048+
unuse_commit_buffer(commit, buffer);
1049+
return ret;
1050+
}
1051+
1052+
int parse_buffer_signed_by_header(const char *buffer,
1053+
unsigned long size,
1054+
struct strbuf *payload,
1055+
struct strbuf *signature,
1056+
const struct git_hash_algo *algop)
1057+
{
10451058
int in_signature = 0, saw_signature = 0, other_signature = 0;
10461059
const char *line, *tail, *p;
10471060
const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(algop)];
@@ -1078,7 +1091,6 @@ int parse_signed_commit(const struct commit *commit,
10781091
}
10791092
line = next;
10801093
}
1081-
unuse_commit_buffer(commit, buffer);
10821094
return saw_signature;
10831095
}
10841096

@@ -1532,7 +1544,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
15321544
if (encoding_is_utf8 && !verify_utf8(&buffer))
15331545
fprintf(stderr, _(commit_utf8_warn));
15341546

1535-
if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1547+
if (sign_commit && sign_with_header(&buffer, sign_commit)) {
15361548
result = -1;
15371549
goto out;
15381550
}

commit.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,13 @@ int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void
360360
LAST_ARG_MUST_BE_NULL
361361
int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);
362362

363+
/* Sign a commit or tag buffer, storing the result in a header. */
364+
int sign_with_header(struct strbuf *buf, const char *keyid);
365+
/* Parse the signature out of a header. */
366+
int parse_buffer_signed_by_header(const char *buffer,
367+
unsigned long size,
368+
struct strbuf *payload,
369+
struct strbuf *signature,
370+
const struct git_hash_algo *algop);
371+
363372
#endif /* COMMIT_H */

0 commit comments

Comments
 (0)