reflog: introduce git_reflog_entry_drop()
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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
diff --git a/include/git2/reflog.h b/include/git2/reflog.h
index 8acba34..7467e81 100644
--- a/include/git2/reflog.h
+++ b/include/git2/reflog.h
@@ -87,6 +87,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
*
* @param entry a reflog entry
diff --git a/src/reflog.c b/src/reflog.c
index 0e07583..8e9d973 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 0000000..be40494
--- /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));
+}