diff --git a/src/commit.c b/src/commit.c index 05f75731a..fc4848733 100644 --- a/src/commit.c +++ b/src/commit.c @@ -191,14 +191,14 @@ int commit_parse_buffer(git_commit *commit, const void *data, size_t len) git_vector_init(&commit->parent_oids, 4, NULL); - if ((error = git__parse_oid(&commit->tree_oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS) + if ((error = git_oid__parse(&commit->tree_oid, &buffer, buffer_end, "tree ")) < GIT_SUCCESS) return git__rethrow(error, "Failed to parse buffer"); /* * TODO: commit grafts! */ - while (git__parse_oid(&parent_oid, &buffer, buffer_end, "parent ") == GIT_SUCCESS) { + while (git_oid__parse(&parent_oid, &buffer, buffer_end, "parent ") == GIT_SUCCESS) { git_oid *new_oid; new_oid = git__malloc(sizeof(git_oid)); diff --git a/src/oid.c b/src/oid.c index 8274cb55c..f12ba30b9 100644 --- a/src/oid.c +++ b/src/oid.c @@ -136,7 +136,7 @@ char *git_oid_to_string(char *out, size_t n, const git_oid *oid) return out; } -int git__parse_oid(git_oid *oid, const char **buffer_out, +int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header) { const size_t sha_len = GIT_OID_HEXSZ; @@ -161,20 +161,6 @@ int git__parse_oid(git_oid *oid, const char **buffer_out, return GIT_SUCCESS; } -int git__write_oid(git_odb_stream *stream, const char *header, const git_oid *oid) -{ - char hex_oid[42]; - - git_oid_fmt(hex_oid + 1, oid); - - hex_oid[0] = ' '; - hex_oid[41] = '\n'; - - stream->write(stream, header, strlen(header)); - stream->write(stream, hex_oid, 42); - return GIT_SUCCESS; -} - void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid) { char hex_oid[GIT_OID_HEXSZ]; diff --git a/src/reflog.c b/src/reflog.c index 63c4c5005..df01199ff 100644 --- a/src/reflog.c +++ b/src/reflog.c @@ -59,8 +59,8 @@ static int reflog_write(git_repository *repo, const char *ref_name, { int error; char log_path[GIT_PATH_MAX]; - char *sig = NULL; - git_filebuf log; + git_buf log = GIT_BUF_INIT; + git_filebuf fbuf; assert(repo && ref_name && oid_old && oid_new && committer); @@ -73,38 +73,33 @@ static int reflog_write(git_repository *repo, const char *ref_name, } else if (git_futils_isfile(log_path)) return git__throw(GIT_ERROR, "Failed to write reflog. `%s` is directory", log_path); - if ((error = git_filebuf_open(&log, log_path, GIT_FILEBUF_APPEND)) < GIT_SUCCESS) - return git__throw(GIT_ERROR, "Failed to write reflog. Cannot open reflog `%s`", log_path); + git_buf_puts(&log, oid_old); + git_buf_puts(&log, oid_new); - if ((error = git_signature__write(&sig, NULL, committer)) < GIT_SUCCESS) - goto cleanup; - - sig[strlen(sig)-1] = '\0'; /* drop LF */ - - if ((error = git_filebuf_printf(&log, "%s %s %s", oid_old, oid_new, sig)) < GIT_SUCCESS) - goto cleanup; + git_signature__writebuf(&log, NULL, committer); + log.size--; /* drop LF */ if (msg) { if (strchr(msg, '\n')) { - error = git__throw(GIT_ERROR, "msg must not contain newline"); - goto cleanup; + git_buf_free(&log); + return git__throw(GIT_ERROR, "Reflog message cannot contain newline"); } - if ((error = git_filebuf_printf(&log, "\t%s", msg)) < GIT_SUCCESS) - goto cleanup; + git_buf_putc(&log, '\t'); + git_buf_puts(&log, msg); } - error = git_filebuf_printf(&log, "\n"); + git_buf_putc(&log, '\n'); -cleanup: - if (error < GIT_SUCCESS) - git_filebuf_cleanup(&log); - else - error = git_filebuf_commit(&log); + if ((error = git_filebuf_open(&fbuf, log_path, GIT_FILEBUF_APPEND)) < GIT_SUCCESS) { + git_buf_free(&log); + return git__throw(GIT_ERROR, "Failed to write reflog. Cannot open reflog `%s`", log_path); + } - if (sig) - free(sig); + git_filebuf_write(&fbuf, log.ptr, log.size); + error = git_filebuf_commit(&fbuf); + git_buf_free(&log); return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to write reflog"); } diff --git a/src/repository.h b/src/repository.h index 1cf426128..2e0b9e352 100644 --- a/src/repository.h +++ b/src/repository.h @@ -43,8 +43,7 @@ struct git_repository { * export */ void git_object__free(void *object); -int git__parse_oid(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header); -int git__write_oid(git_odb_stream *src, const char *header, const git_oid *oid); +int git_oid__parse(git_oid *oid, const char **buffer_out, const char *buffer_end, const char *header); void git_oid__writebuf(git_buf *buf, const char *header, const git_oid *oid); #endif diff --git a/src/signature.c b/src/signature.c index be9ed1ccb..d7c1b2d3e 100644 --- a/src/signature.c +++ b/src/signature.c @@ -314,34 +314,6 @@ clean_exit: return GIT_SUCCESS; } -int git_signature__write(char **signature, const char *header, const git_signature *sig) -{ - int offset, hours, mins; - char sig_buffer[2048]; - int sig_buffer_len; - char sign; - - offset = sig->when.offset; - sign = (sig->when.offset < 0) ? '-' : '+'; - - if (offset < 0) - offset = -offset; - - hours = offset / 60; - mins = offset % 60; - - sig_buffer_len = snprintf(sig_buffer, sizeof(sig_buffer), - "%s%s <%s> %u %c%02d%02d\n", - header ? header : "", sig->name, sig->email, - (unsigned)sig->when.time, sign, hours, mins); - - if (sig_buffer_len < 0 || (size_t)sig_buffer_len > sizeof(sig_buffer)) - return GIT_ENOMEM; - - *signature = git__strdup(sig_buffer); - return sig_buffer_len; -} - void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig) { int offset, hours, mins; diff --git a/src/signature.h b/src/signature.h index 41bc25871..c2e7e7815 100644 --- a/src/signature.h +++ b/src/signature.h @@ -7,7 +7,6 @@ #include int git_signature__parse(git_signature *sig, const char **buffer_out, const char *buffer_end, const char *header); -int git_signature__write(char **signature, const char *header, const git_signature *sig); void git_signature__writebuf(git_buf *buf, const char *header, const git_signature *sig); #endif diff --git a/src/tag.c b/src/tag.c index 12929015e..92b30ba87 100644 --- a/src/tag.c +++ b/src/tag.c @@ -89,7 +89,7 @@ static int parse_tag_buffer(git_tag *tag, const char *buffer, const char *buffer char *search; int error; - if ((error = git__parse_oid(&tag->target, &buffer, buffer_end, "object ")) < 0) + if ((error = git_oid__parse(&tag->target, &buffer, buffer_end, "object ")) < 0) return git__rethrow(error, "Failed to parse tag. Object field invalid"); if (buffer + 5 >= buffer_end) diff --git a/src/util.h b/src/util.h index e64907085..e78b2cd5e 100644 --- a/src/util.h +++ b/src/util.h @@ -95,8 +95,6 @@ extern void git__strtolower(char *str); #define STRLEN(str) (sizeof(str) - 1) -#define GIT_OID_LINE_LENGTH(header) (STRLEN(header) + 1 + GIT_OID_HEXSZ + 1) - extern int git__fnmatch(const char *pattern, const char *name, int flags); /* diff --git a/tests/t04-commit.c b/tests/t04-commit.c index 0f480e552..a0d24dab1 100644 --- a/tests/t04-commit.c +++ b/tests/t04-commit.c @@ -117,14 +117,14 @@ BEGIN_TEST(parse0, "parse the OID line in a commit") const char *ptr = string;\ const char *ptr_original = ptr;\ size_t len = strlen(ptr);\ - must_pass(git__parse_oid(&oid, &ptr, ptr + len, header));\ + must_pass(git_oid__parse(&oid, &ptr, ptr + len, header));\ must_be_true(ptr == ptr_original + len);\ } #define TEST_OID_FAIL(string, header) { \ const char *ptr = string;\ size_t len = strlen(ptr);\ - must_fail(git__parse_oid(&oid, &ptr, ptr + len, header));\ + must_fail(git_oid__parse(&oid, &ptr, ptr + len, header));\ } TEST_OID_PASS("parent 05452d6349abcd67aa396dfb28660d765d8b2a36\n", "parent ");