Merge pull request #3763 from libgit2/ethomson/signature_from_buffer Introduce `git_signature_from_buffer`
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
diff --git a/include/git2/signature.h b/include/git2/signature.h
index feb1b40..7a2a023 100644
--- a/include/git2/signature.h
+++ b/include/git2/signature.h
@@ -63,6 +63,19 @@ GIT_EXTERN(int) git_signature_now(git_signature **out, const char *name, const c
GIT_EXTERN(int) git_signature_default(git_signature **out, git_repository *repo);
/**
+ * Create a new signature by parsing the given buffer, which is
+ * expected to be in the format "Real Name <email> timestamp tzoffset",
+ * where `timestamp` is the number of seconds since the Unix epoch and
+ * `tzoffset` is the timezone offset in `hhmm` format (note the lack
+ * of a colon separator).
+ *
+ * @param out new signature
+ * @param buf signature string
+ * @return 0 on success, or an error code
+ */
+GIT_EXTERN(int) git_signature_from_buffer(git_signature **out, const char *buf);
+
+/**
* Create a copy of an existing signature. All internal strings are also
* duplicated.
*
diff --git a/src/signature.c b/src/signature.c
index d07c933..dcc3797 100644
--- a/src/signature.c
+++ b/src/signature.c
@@ -200,7 +200,8 @@ int git_signature__parse(git_signature *sig, const char **buffer_out,
memset(sig, 0, sizeof(git_signature));
- if ((buffer_end = memchr(buffer, ender, buffer_end - buffer)) == NULL)
+ if (ender &&
+ (buffer_end = memchr(buffer, ender, buffer_end - buffer)) == NULL)
return signature_error("no newline given");
if (header) {
@@ -262,6 +263,30 @@ int git_signature__parse(git_signature *sig, const char **buffer_out,
return 0;
}
+int git_signature_from_buffer(git_signature **out, const char *buf)
+{
+ git_signature *sig;
+ const char *buf_end;
+ int error;
+
+ assert(out && buf);
+
+ *out = NULL;
+
+ sig = git__calloc(1, sizeof(git_signature));
+ GITERR_CHECK_ALLOC(sig);
+
+ buf_end = buf + strlen(buf);
+ error = git_signature__parse(sig, &buf, buf_end, NULL, '\0');
+
+ if (error)
+ git__free(sig);
+ else
+ *out = sig;
+
+ return error;
+}
+
void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig)
{
int offset, hours, mins;
diff --git a/tests/commit/signature.c b/tests/commit/signature.c
index 0070320..30bc929 100644
--- a/tests/commit/signature.c
+++ b/tests/commit/signature.c
@@ -86,3 +86,16 @@ void test_commit_signature__create_zero_char(void)
cl_git_fail(git_signature_new(&sign, "", "x@y.z", 1234567890, 60));
cl_assert(sign == NULL);
}
+
+void test_commit_signature__from_buf(void)
+{
+ git_signature *sign;
+
+ cl_git_pass(git_signature_from_buffer(&sign, "Test User <test@test.tt> 1461698487 +0200"));
+ cl_assert_equal_s("Test User", sign->name);
+ cl_assert_equal_s("test@test.tt", sign->email);
+ cl_assert_equal_i(1461698487, sign->when.time);
+ cl_assert_equal_i(120, sign->when.offset);
+ git_signature_free(sign);
+}
+