reflog: bring _append and _drop back to the frontend These functions act purely on the reflog data structure.
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
diff --git a/include/git2/sys/refdb_backend.h b/include/git2/sys/refdb_backend.h
index 93c4a52..9cf5073 100644
--- a/include/git2/sys/refdb_backend.h
+++ b/include/git2/sys/refdb_backend.h
@@ -131,23 +131,11 @@ struct git_refdb_backend {
int (*reflog_write)(git_refdb_backend *backend, git_reflog *reflog);
/**
- * Append an entry to the given reflog
- */
- int (*reflog_append)(git_refdb_backend *backend, git_reflog *reflog,
- const git_oid *new_oid, const git_signature *committer,
- const char *msg);
-
- /**
* Rename a reflog
*/
int (*reflog_rename)(git_refdb_backend *_backend, const char *old_name, const char *new_name);
/**
- * Drop an entry from the reflog
- */
- int (*reflog_drop)(git_refdb_backend *_backend, git_reflog *reflog,
- size_t idx, int rewrite_previous_entry);
- /**
* Remove a reflog.
*/
int (*reflog_delete)(git_refdb_backend *backend, const char *name);
diff --git a/src/refdb_fs.c b/src/refdb_fs.c
index 4d69572..f52a66a 100644
--- a/src/refdb_fs.c
+++ b/src/refdb_fs.c
@@ -1308,56 +1308,6 @@ success:
return error;
}
-static int refdb_reflog_fs__append(git_refdb_backend *_backend, git_reflog *reflog,
- const git_oid *new_oid, const git_signature *committer, const char *msg)
-{
- git_reflog_entry *entry;
- const git_reflog_entry *previous;
- const char *newline;
-
- assert(_backend && reflog && new_oid && committer);
-
- entry = git__calloc(1, sizeof(git_reflog_entry));
- GITERR_CHECK_ALLOC(entry);
-
- if ((entry->committer = git_signature_dup(committer)) == NULL)
- goto cleanup;
-
- if (msg != NULL) {
- if ((entry->msg = git__strdup(msg)) == NULL)
- goto cleanup;
-
- newline = strchr(msg, '\n');
-
- if (newline) {
- if (newline[1] != '\0') {
- giterr_set(GITERR_INVALID, "Reflog message cannot contain newline");
- goto cleanup;
- }
-
- entry->msg[newline - msg] = '\0';
- }
- }
-
- previous = git_reflog_entry_byindex(reflog, 0);
-
- if (previous == NULL)
- git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO);
- else
- git_oid_cpy(&entry->oid_old, &previous->oid_cur);
-
- git_oid_cpy(&entry->oid_cur, new_oid);
-
- if (git_vector_insert(&reflog->entries, entry) < 0)
- goto cleanup;
-
- return 0;
-
-cleanup:
- git_reflog_entry__free(entry);
- return -1;
-}
-
static int refdb_reflog_fs__rename(git_refdb_backend *_backend, const char *old_name, const char *new_name)
{
int error = 0, fd;
@@ -1434,57 +1384,6 @@ cleanup:
return error;
}
-static int refdb_reflog_fs__drop(git_refdb_backend *_backend, git_reflog *reflog,
- size_t idx, int rewrite_previous_entry)
-{
- size_t entrycount;
- git_reflog_entry *entry, *previous;
-
- assert(_backend && reflog);
-
- entrycount = git_reflog_entrycount(reflog);
-
- entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
-
- if (entry == NULL) {
- giterr_set(GITERR_REFERENCE, "No reflog entry at index "PRIuZ, idx);
- return GIT_ENOTFOUND;
- }
-
- git_reflog_entry__free(entry);
-
- if (git_vector_remove(
- &reflog->entries, reflog_inverse_index(idx, entrycount)) < 0)
- return -1;
-
- if (!rewrite_previous_entry)
- return 0;
-
- /* No need to rewrite anything when removing the most recent entry */
- if (idx == 0)
- return 0;
-
- /* Have the latest entry just been dropped? */
- if (entrycount == 1)
- return 0;
-
- entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1);
-
- /* If the oldest entry has just been removed... */
- if (idx == entrycount - 1) {
- /* ...clear the oid_old member of the "new" oldest 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;
-}
-
static int refdb_reflog_fs__delete(git_refdb_backend *_backend, const char *name)
{
int error;
@@ -1544,9 +1443,7 @@ int git_refdb_backend_fs(
backend->parent.free = &refdb_fs_backend__free;
backend->parent.reflog_read = &refdb_reflog_fs__read;
backend->parent.reflog_write = &refdb_reflog_fs__write;
- backend->parent.reflog_append = &refdb_reflog_fs__append;
backend->parent.reflog_rename = &refdb_reflog_fs__rename;
- backend->parent.reflog_drop = &refdb_reflog_fs__drop;
backend->parent.reflog_delete = &refdb_reflog_fs__delete;
*backend_out = (git_refdb_backend *)backend;
diff --git a/src/reflog.c b/src/reflog.c
index 923d5a3..cebb87d 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -71,16 +71,53 @@ int git_reflog_write(git_reflog *reflog)
return db->backend->reflog_write(db->backend, reflog);
}
-int git_reflog_append(git_reflog *reflog, const git_oid *new_oid,
- const git_signature *committer, const char *msg)
+int git_reflog_append(git_reflog *reflog, const git_oid *new_oid, const git_signature *committer, const char *msg)
{
- git_refdb *db;
+ git_reflog_entry *entry;
+ const git_reflog_entry *previous;
+ const char *newline;
- assert(reflog && reflog->db && new_oid && committer);
+ assert(reflog && new_oid && committer);
- db = reflog->db;
+ entry = git__calloc(1, sizeof(git_reflog_entry));
+ GITERR_CHECK_ALLOC(entry);
+
+ if ((entry->committer = git_signature_dup(committer)) == NULL)
+ goto cleanup;
- return db->backend->reflog_append(db->backend, reflog, new_oid, committer, msg);
+ if (msg != NULL) {
+ if ((entry->msg = git__strdup(msg)) == NULL)
+ goto cleanup;
+
+ newline = strchr(msg, '\n');
+
+ if (newline) {
+ if (newline[1] != '\0') {
+ giterr_set(GITERR_INVALID, "Reflog message cannot contain newline");
+ goto cleanup;
+ }
+
+ entry->msg[newline - msg] = '\0';
+ }
+ }
+
+ previous = git_reflog_entry_byindex(reflog, 0);
+
+ if (previous == NULL)
+ git_oid_fromstr(&entry->oid_old, GIT_OID_HEX_ZERO);
+ else
+ git_oid_cpy(&entry->oid_old, &previous->oid_cur);
+
+ git_oid_cpy(&entry->oid_cur, new_oid);
+
+ if (git_vector_insert(&reflog->entries, entry) < 0)
+ goto cleanup;
+
+ return 0;
+
+cleanup:
+ git_reflog_entry__free(entry);
+ return -1;
}
int git_reflog_rename(git_repository *repo, const char *old_name, const char *new_name)
@@ -146,17 +183,52 @@ const char * git_reflog_entry_message(const git_reflog_entry *entry)
return entry->msg;
}
-int git_reflog_drop(
- git_reflog *reflog,
- size_t idx,
- int rewrite_previous_entry)
+int git_reflog_drop(git_reflog *reflog, size_t idx, int rewrite_previous_entry)
{
- git_refdb *db;
+ size_t entrycount;
+ git_reflog_entry *entry, *previous;
- assert(reflog && reflog->db);
+ entrycount = git_reflog_entrycount(reflog);
- db = reflog->db;
- return db->backend->reflog_drop(db->backend, reflog, idx, rewrite_previous_entry);
+ entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx);
+
+ if (entry == NULL) {
+ giterr_set(GITERR_REFERENCE, "No reflog entry at index "PRIuZ, idx);
+ return GIT_ENOTFOUND;
+ }
+
+ git_reflog_entry__free(entry);
+
+ if (git_vector_remove(
+ &reflog->entries, reflog_inverse_index(idx, entrycount)) < 0)
+ return -1;
+
+ if (!rewrite_previous_entry)
+ return 0;
+
+ /* No need to rewrite anything when removing the most recent entry */
+ if (idx == 0)
+ return 0;
+
+ /* Have the latest entry just been dropped? */
+ if (entrycount == 1)
+ return 0;
+
+ entry = (git_reflog_entry *)git_reflog_entry_byindex(reflog, idx - 1);
+
+ /* If the oldest entry has just been removed... */
+ if (idx == entrycount - 1) {
+ /* ...clear the oid_old member of the "new" oldest 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;
}
int git_reflog_append_to(git_repository *repo, const char *name, const git_oid *id,