static int update_reflog( git_oid *w_commit_oid, git_repository *repo, const git_signature *stasher, const char *message) { git_reference *stash = NULL; git_reflog *reflog = NULL; int error; if ((error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, w_commit_oid, 1)) < 0) goto cleanup; if ((error = git_reflog_read(&reflog, stash)) < 0) goto cleanup; if ((error = git_reflog_append(reflog, w_commit_oid, stasher, message)) < 0) goto cleanup; if ((error = git_reflog_write(reflog)) < 0) goto cleanup; cleanup: git_reference_free(stash); git_reflog_free(reflog); return error; }
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, NULL)); 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__cannot_write_a_moved_reflog(void) { git_reference *master, *new_master; git_buf master_log_path = GIT_BUF_INIT, moved_log_path = GIT_BUF_INIT; git_reflog *reflog; cl_git_pass(git_reference_lookup(&master, g_repo, "refs/heads/master")); cl_git_pass(git_reflog_read(&reflog, g_repo, "refs/heads/master")); cl_git_pass(git_reflog_write(reflog)); cl_git_pass(git_reference_rename(&new_master, master, "refs/moved", 0, NULL)); git_reference_free(master); cl_git_fail(git_reflog_write(reflog)); git_reflog_free(reflog); git_reference_free(new_master); git_buf_dispose(&moved_log_path); git_buf_dispose(&master_log_path); }
void test_refs_reflog_drop__can_persist_deletion_on_disk(void) { cl_assert(entrycount > 2); cl_git_pass(git_reflog_drop(g_reflog, 0, 1)); cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); cl_git_pass(git_reflog_write(g_reflog)); git_reflog_free(g_reflog); git_reflog_read(&g_reflog, g_repo, "HEAD"); cl_assert_equal_sz(entrycount - 1, git_reflog_entrycount(g_reflog)); }
int git_stash_drop( git_repository *repo, size_t index) { git_reference *stash; git_reflog *reflog = NULL; size_t max; int error; if ((error = git_reference_lookup(&stash, repo, GIT_REFS_STASH_FILE)) < 0) return error; if ((error = git_reflog_read(&reflog, stash)) < 0) goto cleanup; max = git_reflog_entrycount(reflog); if (index > max - 1) { error = GIT_ENOTFOUND; giterr_set(GITERR_STASH, "No stashed state at position %" PRIuZ, index); goto cleanup; } if ((error = git_reflog_drop(reflog, index, true)) < 0) goto cleanup; if ((error = git_reflog_write(reflog)) < 0) goto cleanup; if (max == 1) { error = git_reference_delete(stash); git_reference_free(stash); stash = NULL; } else if (index == 0) { const git_reflog_entry *entry; entry = git_reflog_entry_byindex(reflog, 0); git_reference_free(stash); error = git_reference_create(&stash, repo, GIT_REFS_STASH_FILE, &entry->oid_cur, 1); } cleanup: git_reference_free(stash); git_reflog_free(reflog); return error; }
void test_refs_reflog_drop__can_persist_deletion_on_disk(void) { git_reference *ref; cl_assert(entrycount > 2); cl_git_pass(git_reference_lookup(&ref, g_repo, g_reflog->ref_name)); cl_git_pass(git_reflog_drop(g_reflog, entrycount - 1, 1)); cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog)); cl_git_pass(git_reflog_write(g_reflog)); git_reflog_free(g_reflog); git_reflog_read(&g_reflog, ref); git_reference_free(ref); cl_assert_equal_i(entrycount - 1, git_reflog_entrycount(g_reflog)); }
/** * ggit_reflog_write: * @reflog: a #GgitReflog. * @error: a #GError. * * Write the reflog to disk. * * Returns: %TRUE if the reflog was successfully written, or %FALSE on error. * **/ gboolean ggit_reflog_write (GgitReflog *reflog, GError **error) { gint ret; g_return_val_if_fail (reflog != NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); ret = git_reflog_write (reflog->reflog); if (ret != GIT_OK) { _ggit_error_set (error, ret); return FALSE; } return TRUE; }