reflog: fix bogus removal of reflog entries
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
diff --git a/src/reflog.c b/src/reflog.c
index 17cd6d9..f0a6e2a 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -469,18 +469,18 @@ int git_reflog_drop(
if (!rewrite_previous_entry)
return 0;
- /* No need to rewrite anything when removing the first entry */
- if (idx == 0)
+ /* No need to rewrite anything when removing the most recent entry */
+ if (idx == entrycount - 1)
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);
+ entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
- /* If the last entry has just been removed... */
- if (idx == entrycount - 1) {
+ /* If the oldest entry has just been removed... */
+ if (idx == 0) {
/* ...clear the oid_old member of the "new" last entry */
if (git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO) < 0)
return -1;
@@ -488,7 +488,7 @@ int git_reflog_drop(
return 0;
}
- previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
+ previous = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1);
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
index 86781c0..4be857b 100644
--- a/tests-clar/refs/reflog/drop.c
+++ b/tests-clar/refs/reflog/drop.c
@@ -43,57 +43,48 @@ void test_refs_reflog_drop__can_drop_an_entry(void)
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;
+ const git_reflog_entry *before_current;
+ const git_reflog_entry *after_current;
+ git_oid before_current_old_oid, before_current_cur_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);
+ before_current = git_reflog_entry_byindex(g_reflog, 2);
+ git_oid_cpy(&before_current_old_oid, &before_current->oid_old);
+ git_oid_cpy(&before_current_cur_oid, &before_current->oid_cur);
cl_git_pass(git_reflog_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);
+ after_current = git_reflog_entry_byindex(g_reflog, 2);
- 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));
+ cl_assert_equal_i(0, git_oid_cmp(&before_current_old_oid, &after_current->oid_old));
+ cl_assert(0 != git_oid_cmp(&before_current_cur_oid, &after_current->oid_cur));
}
-void test_refs_reflog_drop__can_drop_the_first_entry(void)
-{
- cl_assert(entrycount > 2);
-
- cl_git_pass(git_reflog_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)
+void test_refs_reflog_drop__can_drop_the_oldest_entry(void)
{
const git_reflog_entry *entry;
cl_assert(entrycount > 2);
- cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 0));
+ cl_git_pass(git_reflog_drop(g_reflog, 0, 0));
cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
- entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
+ entry = git_reflog_entry_byindex(g_reflog, 0);
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)
+void test_refs_reflog_drop__can_drop_the_oldest_entry_and_rewrite_the_log_history(void)
{
const git_reflog_entry *entry;
cl_assert(entrycount > 2);
- cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1));
+ cl_git_pass(git_reflog_drop(g_reflog, 0, 1));
cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog));
- entry = git_reflog_entry_byindex(g_reflog, entrycount - 2);
+ entry = git_reflog_entry_byindex(g_reflog, 0);
cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0);
}