summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2013-08-20 11:12:34 +0200
committerCarlos Martín Nieto <cmn@dwim.me>2013-10-02 06:53:24 +0200
commitd274deea23a2c2a70ee1695e8f917dbca50c76b7 (patch)
treef3ddf7a3476ad6ae465d2657a2bbfc3ae6268e7d
parentb976f3c2c228413d124be8fea3280a44bd5e3136 (diff)
downloadlibgit2-d274deea23a2c2a70ee1695e8f917dbca50c76b7.tar.gz
reflog: add a convenience append function
Provide a function that reads a reflog, performs an append and writes back to the backend in one call.
-rw-r--r--include/git2/reflog.h17
-rw-r--r--src/reflog.c19
-rw-r--r--tests-clar/refs/reflog/reflog.c82
3 files changed, 93 insertions, 25 deletions
diff --git a/include/git2/reflog.h b/include/git2/reflog.h
index b31472c37..2d1b6eeaa 100644
--- a/include/git2/reflog.h
+++ b/include/git2/reflog.h
@@ -60,6 +60,23 @@ GIT_EXTERN(int) git_reflog_write(git_reflog *reflog);
GIT_EXTERN(int) git_reflog_append(git_reflog *reflog, const git_oid *id, const git_signature *committer, const char *msg);
/**
+ * Add a new entry to the named reflog.
+ *
+ * This utility function loads the named reflog, appends to it and
+ * writes it back out to the backend.
+ *
+ * `msg` is optional and can be NULL.
+ *
+ * @param repo the repository to act on
+ * @param name the reflog's name
+ * @param id the OID the reference is now pointing to
+ * @param committer the signature of the committer
+ * @param msg the reflog message
+ * @return 0 or an error code
+ */
+GIT_EXTERN(int) git_reflog_append_to(git_repository *repo, const char *name, const git_oid *id, const git_signature *committer, const char *msg);
+
+/**
* Rename a reflog
*
* The reflog to be renamed is expected to already exist
diff --git a/src/reflog.c b/src/reflog.c
index c10ae9fd4..923d5a32d 100644
--- a/src/reflog.c
+++ b/src/reflog.c
@@ -158,3 +158,22 @@ int git_reflog_drop(
db = reflog->db;
return db->backend->reflog_drop(db->backend, reflog, idx, rewrite_previous_entry);
}
+
+int git_reflog_append_to(git_repository *repo, const char *name, const git_oid *id,
+ const git_signature *committer, const char *msg)
+{
+ int error;
+ git_reflog *reflog;
+
+ if ((error = git_reflog_read(&reflog, repo, name)) < 0)
+ return error;
+
+ if ((error = git_reflog_append(reflog, id, committer, msg)) < 0)
+ goto cleanup;
+
+ error = git_reflog_write(reflog);
+
+cleanup:
+ git_reflog_free(reflog);
+ return error;
+}
diff --git a/tests-clar/refs/reflog/reflog.c b/tests-clar/refs/reflog/reflog.c
index 327c85799..bcd224270 100644
--- a/tests-clar/refs/reflog/reflog.c
+++ b/tests-clar/refs/reflog/reflog.c
@@ -13,7 +13,7 @@ static git_repository *g_repo;
// helpers
-static void assert_signature(git_signature *expected, git_signature *actual)
+static void assert_signature(const git_signature *expected, const git_signature *actual)
{
cl_assert(actual);
cl_assert_equal_s(expected->name, actual->name);
@@ -34,30 +34,13 @@ void test_refs_reflog_reflog__cleanup(void)
cl_git_sandbox_cleanup();
}
-void test_refs_reflog_reflog__append_then_read(void)
+static void assert_appends(const git_signature *committer, const git_oid *oid)
{
- // write a reflog for a given reference and ensure it can be read back
git_repository *repo2;
- git_reference *ref, *lookedup_ref;
- git_oid oid;
- git_signature *committer;
+ git_reference *lookedup_ref;
git_reflog *reflog;
const git_reflog_entry *entry;
- /* Create a new branch pointing at the HEAD */
- git_oid_fromstr(&oid, current_master_tip);
- cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0));
-
- cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
-
- cl_git_pass(git_reflog_read(&reflog, g_repo, new_ref));
-
- cl_git_fail(git_reflog_append(reflog, &oid, committer, "no inner\nnewline"));
- cl_git_pass(git_reflog_append(reflog, &oid, committer, NULL));
- cl_git_pass(git_reflog_append(reflog, &oid, committer, commit_msg "\n"));
- cl_git_pass(git_reflog_write(reflog));
- git_reflog_free(reflog);
-
/* Reopen a new instance of the repository */
cl_git_pass(git_repository_open(&repo2, "testrepo.git"));
@@ -71,23 +54,72 @@ void test_refs_reflog_reflog__append_then_read(void)
entry = git_reflog_entry_byindex(reflog, 1);
assert_signature(committer, entry->committer);
cl_assert(git_oid_streq(&entry->oid_old, GIT_OID_HEX_ZERO) == 0);
- cl_assert(git_oid_cmp(&oid, &entry->oid_cur) == 0);
+ cl_assert(git_oid_cmp(oid, &entry->oid_cur) == 0);
cl_assert(entry->msg == NULL);
entry = git_reflog_entry_byindex(reflog, 0);
assert_signature(committer, entry->committer);
- cl_assert(git_oid_cmp(&oid, &entry->oid_old) == 0);
- cl_assert(git_oid_cmp(&oid, &entry->oid_cur) == 0);
+ cl_assert(git_oid_cmp(oid, &entry->oid_old) == 0);
+ cl_assert(git_oid_cmp(oid, &entry->oid_cur) == 0);
cl_assert_equal_s(commit_msg, entry->msg);
- git_signature_free(committer);
git_reflog_free(reflog);
git_repository_free(repo2);
- git_reference_free(ref);
git_reference_free(lookedup_ref);
}
+void test_refs_reflog_reflog__append_then_read(void)
+{
+ /* write a reflog for a given reference and ensure it can be read back */
+ git_reference *ref;
+ git_oid oid;
+ git_signature *committer;
+ git_reflog *reflog;
+
+ /* Create a new branch pointing at the HEAD */
+ git_oid_fromstr(&oid, current_master_tip);
+ cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0));
+ git_reference_free(ref);
+
+ cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
+
+ cl_git_pass(git_reflog_read(&reflog, g_repo, new_ref));
+
+ cl_git_fail(git_reflog_append(reflog, &oid, committer, "no inner\nnewline"));
+ cl_git_pass(git_reflog_append(reflog, &oid, committer, NULL));
+ cl_git_pass(git_reflog_append(reflog, &oid, committer, commit_msg "\n"));
+ cl_git_pass(git_reflog_write(reflog));
+ git_reflog_free(reflog);
+
+ assert_appends(committer, &oid);
+
+ git_signature_free(committer);
+}
+
+void test_refs_reflog_reflog__append_to_then_read(void)
+{
+ /* write a reflog for a given reference and ensure it can be read back */
+ git_reference *ref;
+ git_oid oid;
+ git_signature *committer;
+
+ /* Create a new branch pointing at the HEAD */
+ git_oid_fromstr(&oid, current_master_tip);
+ cl_git_pass(git_reference_create(&ref, g_repo, new_ref, &oid, 0));
+ git_reference_free(ref);
+
+ cl_git_pass(git_signature_now(&committer, "foo", "foo@bar"));
+
+ cl_git_fail(git_reflog_append_to(g_repo, new_ref, &oid, committer, "no inner\nnewline"));
+ cl_git_pass(git_reflog_append_to(g_repo, new_ref, &oid, committer, NULL));
+ cl_git_pass(git_reflog_append_to(g_repo, new_ref, &oid, committer, commit_msg "\n"));
+
+ assert_appends(committer, &oid);
+
+ git_signature_free(committer);
+}
+
void test_refs_reflog_reflog__renaming_the_reference_moves_the_reflog(void)
{
git_reference *master, *new_master;