diff --git a/include/git2/reflog.h b/include/git2/reflog.h index 8acba349b..7467e81ed 100644 --- a/include/git2/reflog.h +++ b/include/git2/reflog.h @@ -86,6 +86,26 @@ GIT_EXTERN(unsigned int) git_reflog_entrycount(git_reflog *reflog); */ GIT_EXTERN(const git_reflog_entry *) git_reflog_entry_byindex(git_reflog *reflog, unsigned int idx); +/** + * Remove an entry from the reflog by its index + * + * To ensure there's no gap in the log history, set the `rewrite_previosu_entry` to 1. + * When deleting entry `n`, member old_oid of entry `n-1` (if any) will be updated with + * the value of memeber new_oid of entry `n+1`. + * + * @param reflog a previously loaded reflog. + * + * @param idx the position of the entry to remove. + * + * @param rewrite_previous_entry 1 to rewrite the history; 0 otherwise. + * + * @return 0 on success or an error code. + */ +GIT_EXTERN(int) git_reflog_entry_drop( + git_reflog *reflog, + unsigned int idx, + int rewrite_previous_entry); + /** * Get the old oid * diff --git a/src/reflog.c b/src/reflog.c index 0e0758381..8e9d973d3 100644 --- a/src/reflog.c +++ b/src/reflog.c @@ -160,6 +160,14 @@ fail: return -1; } +static void reflog_entry_free(git_reflog_entry *entry) +{ + git_signature_free(entry->committer); + + git__free(entry->msg); + git__free(entry); +} + void git_reflog_free(git_reflog *reflog) { unsigned int i; @@ -168,10 +176,7 @@ void git_reflog_free(git_reflog *reflog) for (i=0; i < reflog->entries.length; i++) { entry = git_vector_get(&reflog->entries, i); - git_signature_free(entry->committer); - - git__free(entry->msg); - git__free(entry); + reflog_entry_free(entry); } git_vector_free(&reflog->entries); @@ -370,3 +375,52 @@ char * git_reflog_entry_msg(const git_reflog_entry *entry) assert(entry); return entry->msg; } + +int git_reflog_entry_drop( + git_reflog *reflog, + unsigned int idx, + int rewrite_previous_entry) +{ + unsigned int entrycount; + git_reflog_entry *entry, *previous; + + assert(reflog); + + entrycount = git_reflog_entrycount(reflog); + + if (idx >= entrycount) + return GIT_ENOTFOUND; + + entry = git_vector_get(&reflog->entries, idx); + reflog_entry_free(entry); + + if (git_vector_remove(&reflog->entries, idx) < 0) + return -1; + + if (!rewrite_previous_entry) + return 0; + + /* No need to rewrite anything when removing the first entry */ + if (idx == 0) + return 0; + + /* There are no more entries in the log */ + if (entrycount == 1) + return 0; + + entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1); + + /* If the last entry has just been removed... */ + if (idx == entrycount - 1) { + /* ...clear the oid_old member of the "new" last entry */ + if (git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO) < 0) + return -1; + + return 0; + } + + previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx); + git_oid_cpy(&entry->oid_old, &previous->oid_cur); + + return 0; +} diff --git a/tests-clar/refs/reflog/drop.c b/tests-clar/refs/reflog/drop.c new file mode 100644 index 000000000..be404947e --- /dev/null +++ b/tests-clar/refs/reflog/drop.c @@ -0,0 +1,111 @@ +#include "clar_libgit2.h" + +#include "reflog.h" + +static git_repository *g_repo; +static git_reflog *g_reflog; +static unsigned int entrycount; + +void test_refs_reflog_drop__initialize(void) +{ + git_reference *ref; + + g_repo = cl_git_sandbox_init("testrepo.git"); + cl_git_pass(git_reference_lookup(&ref, g_repo, "HEAD")); + + git_reflog_read(&g_reflog, ref); + entrycount = git_reflog_entrycount(g_reflog); + + git_reference_free(ref); +} + +void test_refs_reflog_drop__cleanup(void) +{ + git_reflog_free(g_reflog); + + cl_git_sandbox_cleanup(); +} + +void test_refs_reflog_drop__dropping_a_non_exisiting_entry_from_the_log_returns_ENOTFOUND(void) +{ + cl_assert_equal_i(GIT_ENOTFOUND, git_reflog_entry_drop(g_reflog, entrycount, 0)); + + cl_assert_equal_i(entrycount, git_reflog_entrycount(g_reflog)); +} + +void test_refs_reflog_drop__can_drop_an_entry(void) +{ + cl_assert(entrycount > 4); + + cl_git_pass(git_reflog_entry_drop(g_reflog, 2, 0)); + cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog)); +} + +void test_refs_reflog_drop__can_drop_an_entry_and_rewrite_the_log_history(void) +{ + const git_reflog_entry *before_previous, *before_next; + const git_reflog_entry *after_next; + git_oid before_next_old_oid; + + cl_assert(entrycount > 4); + + before_previous = git_reflog_entry_byindex(g_reflog, 3); + before_next = git_reflog_entry_byindex(g_reflog, 1); + git_oid_cpy(&before_next_old_oid, &before_next->oid_old); + + cl_git_pass(git_reflog_entry_drop(g_reflog, 2, 1)); + cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog)); + + after_next = git_reflog_entry_byindex(g_reflog, 1); + + cl_assert_equal_i(0, git_oid_cmp(&before_next->oid_cur, &after_next->oid_cur)); + cl_assert(git_oid_cmp(&before_next_old_oid, &after_next->oid_old) != 0); + cl_assert_equal_i(0, git_oid_cmp(&before_previous->oid_cur, &after_next->oid_old)); +} + +void test_refs_reflog_drop__can_drop_the_first_entry(void) +{ + cl_assert(entrycount > 2); + + cl_git_pass(git_reflog_entry_drop(g_reflog, 0, 0)); + cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog)); +} + +void test_refs_reflog_drop__can_drop_the_last_entry(void) +{ + const git_reflog_entry *entry; + + cl_assert(entrycount > 2); + + cl_git_pass(git_reflog_entry_drop(g_reflog, entrycount - 1, 0)); + cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog)); + + entry = git_reflog_entry_byindex(g_reflog, entrycount - 2); + cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) != 0); +} + +void test_refs_reflog_drop__can_drop_the_last_entry_and_rewrite_the_log_history(void) +{ + const git_reflog_entry *entry; + + cl_assert(entrycount > 2); + + cl_git_pass(git_reflog_entry_drop(g_reflog, entrycount - 1, 1)); + cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog)); + + entry = git_reflog_entry_byindex(g_reflog, entrycount - 2); + cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0); +} + +void test_refs_reflog_drop__can_drop_all_the_entries(void) +{ + cl_assert(--entrycount > 0); + + do { + cl_git_pass(git_reflog_entry_drop(g_reflog, --entrycount, 1)); + } while (entrycount > 0); + + cl_git_pass(git_reflog_entry_drop(g_reflog, 0, 1)); + + cl_assert_equal_i(0, git_reflog_entrycount(g_reflog)); +}