Skip to content

Commit 5e8ba35

Browse files
committed
Merge pull request libgit2#3763 from libgit2/ethomson/signature_from_buffer
Introduce `git_signature_from_buffer`
2 parents 1e7fa83 + d383c39 commit 5e8ba35

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

include/git2/signature.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ GIT_EXTERN(int) git_signature_now(git_signature **out, const char *name, const c
6262
*/
6363
GIT_EXTERN(int) git_signature_default(git_signature **out, git_repository *repo);
6464

65+
/**
66+
* Create a new signature by parsing the given buffer, which is
67+
* expected to be in the format "Real Name <email> timestamp tzoffset",
68+
* where `timestamp` is the number of seconds since the Unix epoch and
69+
* `tzoffset` is the timezone offset in `hhmm` format (note the lack
70+
* of a colon separator).
71+
*
72+
* @param out new signature
73+
* @param buf signature string
74+
* @return 0 on success, or an error code
75+
*/
76+
GIT_EXTERN(int) git_signature_from_buffer(git_signature **out, const char *buf);
77+
6578
/**
6679
* Create a copy of an existing signature. All internal strings are also
6780
* duplicated.

src/signature.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ int git_signature__parse(git_signature *sig, const char **buffer_out,
200200

201201
memset(sig, 0, sizeof(git_signature));
202202

203-
if ((buffer_end = memchr(buffer, ender, buffer_end - buffer)) == NULL)
203+
if (ender &&
204+
(buffer_end = memchr(buffer, ender, buffer_end - buffer)) == NULL)
204205
return signature_error("no newline given");
205206

206207
if (header) {
@@ -262,6 +263,30 @@ int git_signature__parse(git_signature *sig, const char **buffer_out,
262263
return 0;
263264
}
264265

266+
int git_signature_from_buffer(git_signature **out, const char *buf)
267+
{
268+
git_signature *sig;
269+
const char *buf_end;
270+
int error;
271+
272+
assert(out && buf);
273+
274+
*out = NULL;
275+
276+
sig = git__calloc(1, sizeof(git_signature));
277+
GITERR_CHECK_ALLOC(sig);
278+
279+
buf_end = buf + strlen(buf);
280+
error = git_signature__parse(sig, &buf, buf_end, NULL, '\0');
281+
282+
if (error)
283+
git__free(sig);
284+
else
285+
*out = sig;
286+
287+
return error;
288+
}
289+
265290
void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig)
266291
{
267292
int offset, hours, mins;

tests/commit/signature.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,16 @@ void test_commit_signature__create_zero_char(void)
8686
cl_git_fail(git_signature_new(&sign, "", "x@y.z", 1234567890, 60));
8787
cl_assert(sign == NULL);
8888
}
89+
90+
void test_commit_signature__from_buf(void)
91+
{
92+
git_signature *sign;
93+
94+
cl_git_pass(git_signature_from_buffer(&sign, "Test User <test@test.tt> 1461698487 +0200"));
95+
cl_assert_equal_s("Test User", sign->name);
96+
cl_assert_equal_s("test@test.tt", sign->email);
97+
cl_assert_equal_i(1461698487, sign->when.time);
98+
cl_assert_equal_i(120, sign->when.offset);
99+
git_signature_free(sign);
100+
}
101+

0 commit comments

Comments
 (0)