summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@github.com>2016-04-28 12:47:14 -0400
committerEdward Thomson <ethomson@github.com>2016-04-28 12:47:14 -0400
commitd383c39b3bc9a2bd5e68882db9a12e64ccd262a4 (patch)
tree33f21286bf1148c10cbdde2b289e5e5bbc421885
parent88284dfb7905c5990babb4238b7cd30bdf823500 (diff)
downloadlibgit2-d383c39b3bc9a2bd5e68882db9a12e64ccd262a4.tar.gz
Introduce `git_signature_from_buffer`ethomson/signature_from_buffer
Allow users to construct a signature from the type of signature lines that actually appear in commits.
-rw-r--r--include/git2/signature.h13
-rw-r--r--src/signature.c27
-rw-r--r--tests/commit/signature.c13
3 files changed, 52 insertions, 1 deletions
diff --git a/include/git2/signature.h b/include/git2/signature.h
index feb1b4073..7a2a0238a 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 d07c93323..dcc379717 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 0070320ae..30bc92967 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);
+}
+