diff --git a/include/git2/reflog.h b/include/git2/reflog.h index 7f2781fc3..53b344733 100644 --- a/include/git2/reflog.h +++ b/include/git2/reflog.h @@ -91,7 +91,7 @@ GIT_EXTERN(const git_reflog_entry *) git_reflog_entry_byindex(git_reflog *reflog * @param entry a reflog entry * @return the old oid */ -GIT_EXTERN(char *) git_reflog_entry_oidold(const git_reflog_entry *entry); +GIT_EXTERN(const git_oid *) git_reflog_entry_oidold(const git_reflog_entry *entry); /** * Get the new oid @@ -99,7 +99,7 @@ GIT_EXTERN(char *) git_reflog_entry_oidold(const git_reflog_entry *entry); * @param entry a reflog entry * @return the new oid at this time */ -GIT_EXTERN(char *) git_reflog_entry_oidnew(const git_reflog_entry *entry); +GIT_EXTERN(const git_oid *) git_reflog_entry_oidnew(const git_reflog_entry *entry); /** * Get the committer of this entry diff --git a/src/reflog.c b/src/reflog.c index 7609d9a4d..a2dea8830 100644 --- a/src/reflog.c +++ b/src/reflog.c @@ -113,10 +113,12 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size) if (entry == NULL) return GIT_ENOMEM; - entry->oid_old = git__strndup(buf, GIT_OID_HEXSZ); + if (git_oid_fromstrn(&entry->oid_old, buf, GIT_OID_HEXSZ) < GIT_SUCCESS) + return GIT_ERROR; seek_forward(GIT_OID_HEXSZ + 1); - entry->oid_cur = git__strndup(buf, GIT_OID_HEXSZ); + if (git_oid_fromstrn(&entry->oid_cur, buf, GIT_OID_HEXSZ) < GIT_SUCCESS) + return GIT_ERROR; seek_forward(GIT_OID_HEXSZ + 1); ptr = buf; @@ -165,9 +167,6 @@ void git_reflog_free(git_reflog *reflog) for (i=0; i < reflog->entries.length; i++) { entry = git_vector_get(&reflog->entries, i); - free(entry->oid_old); - free(entry->oid_cur); - git_signature_free(entry->committer); free(entry->msg); @@ -193,8 +192,10 @@ int git_reflog_read(git_reflog **reflog, git_reference *ref) git_path_join_n(log_path, 3, ref->owner->path_repository, GIT_REFLOG_DIR, ref->name); - if ((error = git_futils_readbuffer(&log_file, log_path)) < GIT_SUCCESS) + if ((error = git_futils_readbuffer(&log_file, log_path)) < GIT_SUCCESS) { + git_reflog_free(log); return git__rethrow(error, "Failed to read reflog. Cannot read file `%s`", log_path); + } error = reflog_parse(log, log_file.data, log_file.len); @@ -202,6 +203,8 @@ int git_reflog_read(git_reflog **reflog, git_reference *ref) if (error == GIT_SUCCESS) *reflog = log; + else + git_reflog_free(log); return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to read reflog"); } @@ -255,16 +258,16 @@ const git_reflog_entry * git_reflog_entry_byindex(git_reflog *reflog, unsigned i return git_vector_get(&reflog->entries, idx); } -char * git_reflog_entry_oidold(const git_reflog_entry *entry) +const git_oid * git_reflog_entry_oidold(const git_reflog_entry *entry) { assert(entry); - return entry->oid_old; + return &entry->oid_old; } -char * git_reflog_entry_oidnew(const git_reflog_entry *entry) +const git_oid * git_reflog_entry_oidnew(const git_reflog_entry *entry) { assert(entry); - return entry->oid_cur; + return &entry->oid_cur; } git_signature * git_reflog_entry_committer(const git_reflog_entry *entry) diff --git a/src/reflog.h b/src/reflog.h index da352ca8f..b6daf2a76 100644 --- a/src/reflog.h +++ b/src/reflog.h @@ -10,8 +10,8 @@ #define GIT_REFLOG_SIZE_MIN (2*GIT_OID_HEXSZ+2+17) struct git_reflog_entry { - char *oid_old; - char *oid_cur; + git_oid oid_old; + git_oid oid_cur; git_signature *committer; diff --git a/tests/t10-refs.c b/tests/t10-refs.c index f80c3f510..65fbdd69f 100644 --- a/tests/t10-refs.c +++ b/tests/t10-refs.c @@ -1026,6 +1026,7 @@ BEGIN_TEST(reflog0, "write a reflog for a given reference and ensure it can be r git_signature *committer; git_reflog *reflog; git_reflog_entry *entry; + char oid_str[GIT_OID_HEXSZ+1]; must_pass(open_temp_repo(&repo, REPOSITORY_FOLDER)); @@ -1037,6 +1038,7 @@ BEGIN_TEST(reflog0, "write a reflog for a given reference and ensure it can be r must_pass(git_signature_now(&committer, "foo", "foo@bar")); must_pass(git_reflog_write(ref, NULL, committer, NULL)); + must_fail(git_reflog_write(ref, NULL, committer, "no ancestor NULL for an existing reflog")); must_fail(git_reflog_write(ref, NULL, committer, "no\nnewline")); must_pass(git_reflog_write(ref, &oid, committer, commit_msg)); @@ -1054,14 +1056,18 @@ BEGIN_TEST(reflog0, "write a reflog for a given reference and ensure it can be r entry = (git_reflog_entry *)git_vector_get(&reflog->entries, 0); must_pass(assert_signature(committer, entry->committer)); - must_be_true(strcmp("0000000000000000000000000000000000000000", entry->oid_old) == 0); - must_be_true(strcmp(current_master_tip, entry->oid_cur) == 0); + git_oid_to_string(oid_str, GIT_OID_HEXSZ+1, &entry->oid_old); + must_be_true(strcmp("0000000000000000000000000000000000000000", oid_str) == 0); + git_oid_to_string(oid_str, GIT_OID_HEXSZ+1, &entry->oid_cur); + must_be_true(strcmp(current_master_tip, oid_str) == 0); must_be_true(entry->msg == NULL); entry = (git_reflog_entry *)git_vector_get(&reflog->entries, 1); must_pass(assert_signature(committer, entry->committer)); - must_be_true(strcmp(current_master_tip, entry->oid_old) == 0); - must_be_true(strcmp(current_master_tip, entry->oid_cur) == 0); + git_oid_to_string(oid_str, GIT_OID_HEXSZ+1, &entry->oid_old); + must_be_true(strcmp(current_master_tip, oid_str) == 0); + git_oid_to_string(oid_str, GIT_OID_HEXSZ+1, &entry->oid_cur); + must_be_true(strcmp(current_master_tip, oid_str) == 0); must_be_true(strcmp(commit_msg, entry->msg) == 0); git_signature_free(committer);