Commit d274deea by Carlos Martín Nieto

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.
parent b976f3c2
......@@ -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
......
......@@ -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;
}
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment