Initial commit - full huishou project

This commit is contained in:
jiapengyu
2026-07-27 14:07:26 +08:00
commit 60790ad1b3
39127 changed files with 5989265 additions and 0 deletions
+212
View File
@@ -0,0 +1,212 @@
#include "clar_libgit2.h"
#include "posix.h"
#include "reset_helpers.h"
#include "path.h"
static git_repository *_repo;
static git_object *_target;
static git_strarray _pathspecs;
static git_index *_index;
static void initialize(const char *repo_name)
{
_repo = cl_git_sandbox_init(repo_name);
cl_git_pass(git_repository_index(&_index, _repo));
_target = NULL;
_pathspecs.strings = NULL;
_pathspecs.count = 0;
}
void test_reset_default__initialize(void)
{
}
void test_reset_default__cleanup(void)
{
git_object_free(_target);
_target = NULL;
git_index_free(_index);
_index = NULL;
cl_git_sandbox_cleanup();
}
static void assert_content_in_index(
git_strarray *pathspecs,
bool should_exist,
git_strarray *expected_shas)
{
size_t i, pos;
int error;
for (i = 0; i < pathspecs->count; i++) {
error = git_index_find(&pos, _index, pathspecs->strings[i]);
if (should_exist) {
const git_index_entry *entry;
cl_assert(error != GIT_ENOTFOUND);
entry = git_index_get_byindex(_index, pos);
cl_assert(entry != NULL);
if (!expected_shas)
continue;
cl_git_pass(git_oid_streq(&entry->id, expected_shas->strings[i]));
} else
cl_assert_equal_i(should_exist, error != GIT_ENOTFOUND);
}
}
void test_reset_default__resetting_filepaths_against_a_null_target_removes_them_from_the_index(void)
{
char *paths[] = { "staged_changes", "staged_new_file" };
initialize("status");
_pathspecs.strings = paths;
_pathspecs.count = 2;
assert_content_in_index(&_pathspecs, true, NULL);
cl_git_pass(git_reset_default(_repo, NULL, &_pathspecs));
assert_content_in_index(&_pathspecs, false, NULL);
}
/*
* $ git ls-files --cached -s --abbrev=7 -- "staged*"
* 100644 55d316c 0 staged_changes
* 100644 a6be623 0 staged_changes_file_deleted
* ...
*
* $ git reset 0017bd4 -- staged_changes staged_changes_file_deleted
* Unstaged changes after reset:
* ...
*
* $ git ls-files --cached -s --abbrev=7 -- "staged*"
* 100644 32504b7 0 staged_changes
* 100644 061d42a 0 staged_changes_file_deleted
* ...
*/
void test_reset_default__resetting_filepaths_replaces_their_corresponding_index_entries(void)
{
git_strarray before, after;
char *paths[] = { "staged_changes", "staged_changes_file_deleted" };
char *before_shas[] = { "55d316c9ba708999f1918e9677d01dfcae69c6b9",
"a6be623522ce87a1d862128ac42672604f7b468b" };
char *after_shas[] = { "32504b727382542f9f089e24fddac5e78533e96c",
"061d42a44cacde5726057b67558821d95db96f19" };
initialize("status");
_pathspecs.strings = paths;
_pathspecs.count = 2;
before.strings = before_shas;
before.count = 2;
after.strings = after_shas;
after.count = 2;
cl_git_pass(git_revparse_single(&_target, _repo, "0017bd4"));
assert_content_in_index(&_pathspecs, true, &before);
cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
assert_content_in_index(&_pathspecs, true, &after);
}
/*
* $ git ls-files --cached -s --abbrev=7 -- conflicts-one.txt
* 100644 1f85ca5 1 conflicts-one.txt
* 100644 6aea5f2 2 conflicts-one.txt
* 100644 516bd85 3 conflicts-one.txt
*
* $ git reset 9a05ccb -- conflicts-one.txt
* Unstaged changes after reset:
* ...
*
* $ git ls-files --cached -s --abbrev=7 -- conflicts-one.txt
* 100644 1f85ca5 0 conflicts-one.txt
*
*/
void test_reset_default__resetting_filepaths_clears_previous_conflicts(void)
{
const git_index_entry *conflict_entry[3];
git_strarray after;
char *paths[] = { "conflicts-one.txt" };
char *after_shas[] = { "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81" };
initialize("mergedrepo");
_pathspecs.strings = paths;
_pathspecs.count = 1;
after.strings = after_shas;
after.count = 1;
cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
&conflict_entry[2], _index, "conflicts-one.txt"));
cl_git_pass(git_revparse_single(&_target, _repo, "9a05ccb"));
cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
assert_content_in_index(&_pathspecs, true, &after);
cl_assert_equal_i(GIT_ENOTFOUND, git_index_conflict_get(&conflict_entry[0],
&conflict_entry[1], &conflict_entry[2], _index, "conflicts-one.txt"));
}
/*
$ git reset HEAD -- "I_am_not_there.txt" "me_neither.txt"
Unstaged changes after reset:
...
*/
void test_reset_default__resetting_unknown_filepaths_does_not_fail(void)
{
char *paths[] = { "I_am_not_there.txt", "me_neither.txt" };
initialize("status");
_pathspecs.strings = paths;
_pathspecs.count = 2;
assert_content_in_index(&_pathspecs, false, NULL);
cl_git_pass(git_revparse_single(&_target, _repo, "HEAD"));
cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
assert_content_in_index(&_pathspecs, false, NULL);
}
void test_reset_default__staged_rename_reset_delete(void)
{
git_index_entry entry;
const git_index_entry *existing;
char *paths[] = { "new.txt" };
initialize("testrepo2");
existing = git_index_get_bypath(_index, "new.txt", 0);
cl_assert(existing);
memcpy(&entry, existing, sizeof(entry));
cl_git_pass(git_index_remove_bypath(_index, "new.txt"));
entry.path = "renamed.txt";
cl_git_pass(git_index_add(_index, &entry));
_pathspecs.strings = paths;
_pathspecs.count = 1;
assert_content_in_index(&_pathspecs, false, NULL);
cl_git_pass(git_revparse_single(&_target, _repo, "HEAD"));
cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
assert_content_in_index(&_pathspecs, true, NULL);
}
+293
View File
@@ -0,0 +1,293 @@
#include "clar_libgit2.h"
#include "posix.h"
#include "reset_helpers.h"
#include "path.h"
#include "futils.h"
static git_repository *repo;
static git_object *target;
void test_reset_hard__initialize(void)
{
repo = cl_git_sandbox_init("status");
target = NULL;
}
void test_reset_hard__cleanup(void)
{
if (target != NULL) {
git_object_free(target);
target = NULL;
}
cl_git_sandbox_cleanup();
}
static int strequal_ignore_eol(const char *exp, const char *str)
{
while (*exp && *str) {
if (*exp != *str) {
while (*exp == '\r' || *exp == '\n') ++exp;
while (*str == '\r' || *str == '\n') ++str;
if (*exp != *str)
return false;
} else {
exp++; str++;
}
}
return (!*exp && !*str);
}
void test_reset_hard__resetting_reverts_modified_files(void)
{
git_buf path = GIT_BUF_INIT, content = GIT_BUF_INIT;
int i;
static const char *files[4] = {
"current_file",
"modified_file",
"staged_new_file",
"staged_changes_modified_file" };
static const char *before[4] = {
"current_file\n",
"modified_file\nmodified_file\n",
"staged_new_file\n",
"staged_changes_modified_file\nstaged_changes_modified_file\nstaged_changes_modified_file\n"
};
static const char *after[4] = {
"current_file\n",
"modified_file\n",
NULL,
"staged_changes_modified_file\n"
};
const char *wd = git_repository_workdir(repo);
cl_assert(wd);
for (i = 0; i < 4; ++i) {
cl_git_pass(git_buf_joinpath(&path, wd, files[i]));
cl_git_pass(git_futils_readbuffer(&content, path.ptr));
cl_assert_equal_s(before[i], content.ptr);
}
cl_git_pass(git_revparse_single(&target, repo, "26a125e"));
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
for (i = 0; i < 4; ++i) {
cl_git_pass(git_buf_joinpath(&path, wd, files[i]));
if (after[i]) {
cl_git_pass(git_futils_readbuffer(&content, path.ptr));
cl_assert(strequal_ignore_eol(after[i], content.ptr));
} else {
cl_assert(!git_path_exists(path.ptr));
}
}
git_buf_dispose(&content);
git_buf_dispose(&path);
}
void test_reset_hard__cannot_reset_in_a_bare_repository(void)
{
git_repository *bare;
cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git")));
cl_assert(git_repository_is_bare(bare) == true);
cl_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO));
cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_HARD, NULL));
git_repository_free(bare);
}
static void index_entry_init(git_index *index, int side, git_oid *oid)
{
git_index_entry entry;
memset(&entry, 0x0, sizeof(git_index_entry));
entry.path = "conflicting_file";
GIT_INDEX_ENTRY_STAGE_SET(&entry, side);
entry.mode = 0100644;
git_oid_cpy(&entry.id, oid);
cl_git_pass(git_index_add(index, &entry));
}
static void unmerged_index_init(git_index *index, int entries)
{
int write_ancestor = 1;
int write_ours = 2;
int write_theirs = 4;
git_oid ancestor, ours, theirs;
git_oid_fromstr(&ancestor, "452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
git_oid_fromstr(&ours, "32504b727382542f9f089e24fddac5e78533e96c");
git_oid_fromstr(&theirs, "061d42a44cacde5726057b67558821d95db96f19");
cl_git_rewritefile("status/conflicting_file", "conflicting file\n");
if (entries & write_ancestor)
index_entry_init(index, 1, &ancestor);
if (entries & write_ours)
index_entry_init(index, 2, &ours);
if (entries & write_theirs)
index_entry_init(index, 3, &theirs);
}
void test_reset_hard__resetting_reverts_unmerged(void)
{
git_index *index;
int entries;
/* Ensure every permutation of non-zero stage entries results in the
* path being cleaned up. */
for (entries = 1; entries < 8; entries++) {
cl_git_pass(git_repository_index(&index, repo));
unmerged_index_init(index, entries);
cl_git_pass(git_index_write(index));
cl_git_pass(git_revparse_single(&target, repo, "26a125e"));
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
cl_assert(git_path_exists("status/conflicting_file") == 0);
git_object_free(target);
target = NULL;
git_index_free(index);
}
}
void test_reset_hard__cleans_up_merge(void)
{
git_buf merge_head_path = GIT_BUF_INIT,
merge_msg_path = GIT_BUF_INIT,
merge_mode_path = GIT_BUF_INIT,
orig_head_path = GIT_BUF_INIT;
cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD"));
cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n");
cl_git_pass(git_buf_joinpath(&merge_msg_path, git_repository_path(repo), "MERGE_MSG"));
cl_git_mkfile(git_buf_cstr(&merge_msg_path), "Merge commit 0017bd4ab1ec30440b17bae1680cff124ab5f1f6\n");
cl_git_pass(git_buf_joinpath(&merge_mode_path, git_repository_path(repo), "MERGE_MODE"));
cl_git_mkfile(git_buf_cstr(&merge_mode_path), "");
cl_git_pass(git_buf_joinpath(&orig_head_path, git_repository_path(repo), "ORIG_HEAD"));
cl_git_mkfile(git_buf_cstr(&orig_head_path), "0017bd4ab1ec30440b17bae1680cff124ab5f1f6");
cl_git_pass(git_revparse_single(&target, repo, "0017bd4"));
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
cl_assert(!git_path_exists(git_buf_cstr(&merge_head_path)));
cl_assert(!git_path_exists(git_buf_cstr(&merge_msg_path)));
cl_assert(!git_path_exists(git_buf_cstr(&merge_mode_path)));
cl_assert(git_path_exists(git_buf_cstr(&orig_head_path)));
cl_git_pass(p_unlink(git_buf_cstr(&orig_head_path)));
git_buf_dispose(&merge_head_path);
git_buf_dispose(&merge_msg_path);
git_buf_dispose(&merge_mode_path);
git_buf_dispose(&orig_head_path);
}
void test_reset_hard__reflog_is_correct(void)
{
git_buf buf = GIT_BUF_INIT;
git_annotated_commit *annotated;
const char *exp_msg = "commit: Add a file which name should appear before the "
"\"subdir/\" folder while being dealt with by the treewalker";
reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);
/* Branch not moving, no reflog entry */
cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
reflog_check(repo, "HEAD", 3, "emeric.fermas@gmail.com", exp_msg);
reflog_check(repo, "refs/heads/master", 3, "emeric.fermas@gmail.com", exp_msg);
git_object_free(target);
/* Moved branch, expect id in message */
cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
cl_git_pass(git_buf_printf(&buf, "reset: moving to %s", git_oid_tostr_s(git_object_id(target))));
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
reflog_check(repo, "HEAD", 4, NULL, git_buf_cstr(&buf));
reflog_check(repo, "refs/heads/master", 4, NULL, git_buf_cstr(&buf));
git_buf_dispose(&buf);
/* Moved branch, expect revspec in message */
exp_msg = "reset: moving to HEAD~^{commit}";
cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "HEAD~^{commit}"));
cl_git_pass(git_reset_from_annotated(repo, annotated, GIT_RESET_HARD, NULL));
reflog_check(repo, "HEAD", 5, NULL, exp_msg);
reflog_check(repo, "refs/heads/master", 5, NULL, exp_msg);
git_annotated_commit_free(annotated);
}
void test_reset_hard__switch_file_to_dir(void)
{
git_index_entry entry = {{ 0 }};
git_index *idx;
git_odb *odb;
git_object *commit;
git_tree *tree;
git_signature *sig;
git_oid src_tree_id, tgt_tree_id;
git_oid src_id, tgt_id;
cl_git_pass(git_repository_odb(&odb, repo));
cl_git_pass(git_odb_write(&entry.id, odb, "", 0, GIT_OBJECT_BLOB));
git_odb_free(odb);
entry.mode = GIT_FILEMODE_BLOB;
cl_git_pass(git_index_new(&idx));
cl_git_pass(git_signature_now(&sig, "foo", "bar"));
/* Create the old tree */
entry.path = "README";
cl_git_pass(git_index_add(idx, &entry));
entry.path = "dir";
cl_git_pass(git_index_add(idx, &entry));
cl_git_pass(git_index_write_tree_to(&src_tree_id, idx, repo));
cl_git_pass(git_index_clear(idx));
cl_git_pass(git_tree_lookup(&tree, repo, &src_tree_id));
cl_git_pass(git_commit_create(&src_id, repo, NULL, sig, sig, NULL, "foo", tree, 0, NULL));
git_tree_free(tree);
/* Create the new tree */
entry.path = "README";
cl_git_pass(git_index_add(idx, &entry));
entry.path = "dir/FILE";
cl_git_pass(git_index_add(idx, &entry));
cl_git_pass(git_index_write_tree_to(&tgt_tree_id, idx, repo));
cl_git_pass(git_tree_lookup(&tree, repo, &tgt_tree_id));
cl_git_pass(git_commit_create(&tgt_id, repo, NULL, sig, sig, NULL, "foo", tree, 0, NULL));
git_tree_free(tree);
git_index_free(idx);
git_signature_free(sig);
/* Let's go to a known state of the src commit with the file named 'dir' */
cl_git_pass(git_object_lookup(&commit, repo, &src_id, GIT_OBJECT_COMMIT));
cl_git_pass(git_reset(repo, commit, GIT_RESET_HARD, NULL));
git_object_free(commit);
/* And now we move over to the commit with the directory named 'dir' */
cl_git_pass(git_object_lookup(&commit, repo, &tgt_id, GIT_OBJECT_COMMIT));
cl_git_pass(git_reset(repo, commit, GIT_RESET_HARD, NULL));
git_object_free(commit);
}
+85
View File
@@ -0,0 +1,85 @@
#include "clar_libgit2.h"
#include "posix.h"
#include "reset_helpers.h"
#include "path.h"
static git_repository *repo;
static git_object *target;
void test_reset_mixed__initialize(void)
{
repo = cl_git_sandbox_init("attr");
target = NULL;
}
void test_reset_mixed__cleanup(void)
{
git_object_free(target);
target = NULL;
cl_git_sandbox_cleanup();
}
void test_reset_mixed__cannot_reset_in_a_bare_repository(void)
{
git_repository *bare;
cl_git_pass(git_repository_open(&bare, cl_fixture("testrepo.git")));
cl_assert(git_repository_is_bare(bare) == true);
cl_git_pass(git_revparse_single(&target, bare, KNOWN_COMMIT_IN_BARE_REPO));
cl_assert_equal_i(GIT_EBAREREPO, git_reset(bare, target, GIT_RESET_MIXED, NULL));
git_repository_free(bare);
}
void test_reset_mixed__resetting_refreshes_the_index_to_the_commit_tree(void)
{
unsigned int status;
cl_git_pass(git_status_file(&status, repo, "macro_bad"));
cl_assert(status == GIT_STATUS_CURRENT);
cl_git_pass(git_revparse_single(&target, repo, "605812a"));
cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL));
cl_git_pass(git_status_file(&status, repo, "macro_bad"));
cl_assert(status == GIT_STATUS_WT_NEW);
}
void test_reset_mixed__reflog_is_correct(void)
{
git_buf buf = GIT_BUF_INIT;
git_annotated_commit *annotated;
const char *exp_msg = "commit: Updating test data so we can test inter-hunk-context";
reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
/* Branch not moving, no reflog entry */
cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL));
reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
reflog_check(repo, "refs/heads/master", 9, "yoram.harmelin@gmail.com", exp_msg);
git_object_free(target);
target = NULL;
/* Moved branch, expect id in message */
cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
git_buf_clear(&buf);
cl_git_pass(git_buf_printf(&buf, "reset: moving to %s", git_oid_tostr_s(git_object_id(target))));
cl_git_pass(git_reset(repo, target, GIT_RESET_MIXED, NULL));
reflog_check(repo, "HEAD", 10, NULL, git_buf_cstr(&buf));
reflog_check(repo, "refs/heads/master", 10, NULL, git_buf_cstr(&buf));
git_buf_dispose(&buf);
/* Moved branch, expect revspec in message */
exp_msg = "reset: moving to HEAD~^{commit}";
cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "HEAD~^{commit}"));
cl_git_pass(git_reset_from_annotated(repo, annotated, GIT_RESET_MIXED, NULL));
reflog_check(repo, "HEAD", 11, NULL, exp_msg);
reflog_check(repo, "refs/heads/master", 11, NULL, exp_msg);
git_annotated_commit_free(annotated);
}
+20
View File
@@ -0,0 +1,20 @@
#include "clar_libgit2.h"
#include "reset_helpers.h"
void reflog_check(git_repository *repo, const char *refname,
size_t exp_count, const char *exp_email, const char *exp_msg)
{
git_reflog *log;
const git_reflog_entry *entry;
GIT_UNUSED(exp_email);
cl_git_pass(git_reflog_read(&log, repo, refname));
cl_assert_equal_i(exp_count, git_reflog_entrycount(log));
entry = git_reflog_entry_byindex(log, 0);
if (exp_msg)
cl_assert_equal_s(exp_msg, git_reflog_entry_message(entry));
git_reflog_free(log);
}
+7
View File
@@ -0,0 +1,7 @@
#include "common.h"
#define KNOWN_COMMIT_IN_BARE_REPO "e90810b8df3e80c413d903f631643c716887138d"
#define KNOWN_COMMIT_IN_ATTR_REPO "217878ab49e1314388ea2e32dc6fdb58a1b969e0"
void reflog_check(git_repository *repo, const char *refname,
size_t exp_count, const char *exp_email, const char *exp_msg);
+189
View File
@@ -0,0 +1,189 @@
#include "clar_libgit2.h"
#include "posix.h"
#include "reset_helpers.h"
#include "path.h"
#include "repo/repo_helpers.h"
static git_repository *repo;
static git_object *target;
void test_reset_soft__initialize(void)
{
repo = cl_git_sandbox_init("testrepo.git");
}
void test_reset_soft__cleanup(void)
{
git_object_free(target);
target = NULL;
cl_git_sandbox_cleanup();
}
static void assert_reset_soft(bool should_be_detached)
{
git_oid oid;
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
cl_git_fail(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
cl_assert(git_repository_head_detached(repo) == should_be_detached);
cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
cl_assert(git_repository_head_detached(repo) == should_be_detached);
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
}
void test_reset_soft__can_reset_the_non_detached_Head_to_the_specified_commit(void)
{
assert_reset_soft(false);
}
void test_reset_soft__can_reset_the_detached_Head_to_the_specified_commit(void)
{
git_repository_detach_head(repo);
assert_reset_soft(true);
}
void test_reset_soft__resetting_to_the_commit_pointed_at_by_the_Head_does_not_change_the_target_of_the_Head(void)
{
git_oid oid;
char raw_head_oid[GIT_OID_HEXSZ + 1];
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
git_oid_fmt(raw_head_oid, &oid);
raw_head_oid[GIT_OID_HEXSZ] = '\0';
cl_git_pass(git_revparse_single(&target, repo, raw_head_oid));
cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
cl_git_pass(git_oid_streq(&oid, raw_head_oid));
}
void test_reset_soft__resetting_to_a_tag_sets_the_Head_to_the_peeled_commit(void)
{
git_oid oid;
/* b25fa35 is a tag, pointing to another tag which points to commit e90810b */
cl_git_pass(git_revparse_single(&target, repo, "b25fa35"));
cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
cl_assert(git_repository_head_detached(repo) == false);
cl_git_pass(git_reference_name_to_id(&oid, repo, "HEAD"));
cl_git_pass(git_oid_streq(&oid, KNOWN_COMMIT_IN_BARE_REPO));
}
void test_reset_soft__cannot_reset_to_a_tag_not_pointing_at_a_commit(void)
{
/* 53fc32d is the tree of commit e90810b */
cl_git_pass(git_revparse_single(&target, repo, "53fc32d"));
cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL));
git_object_free(target);
/* 521d87c is an annotated tag pointing to a blob */
cl_git_pass(git_revparse_single(&target, repo, "521d87c"));
cl_git_fail(git_reset(repo, target, GIT_RESET_SOFT, NULL));
}
void test_reset_soft__resetting_against_an_unborn_head_repo_makes_the_head_no_longer_unborn(void)
{
git_reference *head;
cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
make_head_unborn(repo, NON_EXISTING_HEAD);
cl_assert_equal_i(true, git_repository_head_unborn(repo));
cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
cl_assert_equal_i(false, git_repository_head_unborn(repo));
cl_git_pass(git_reference_lookup(&head, repo, NON_EXISTING_HEAD));
cl_assert_equal_i(0, git_oid_streq(git_reference_target(head), KNOWN_COMMIT_IN_BARE_REPO));
git_reference_free(head);
}
void test_reset_soft__fails_when_merging(void)
{
git_buf merge_head_path = GIT_BUF_INIT;
cl_git_pass(git_repository_detach_head(repo));
cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD"));
cl_git_mkfile(git_buf_cstr(&merge_head_path), "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef\n");
cl_git_pass(git_revparse_single(&target, repo, KNOWN_COMMIT_IN_BARE_REPO));
cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL));
cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path)));
git_buf_dispose(&merge_head_path);
}
void test_reset_soft__fails_when_index_contains_conflicts_independently_of_MERGE_HEAD_file_existence(void)
{
git_index *index;
git_reference *head;
git_buf merge_head_path = GIT_BUF_INIT;
cl_git_sandbox_cleanup();
repo = cl_git_sandbox_init("mergedrepo");
cl_git_pass(git_buf_joinpath(&merge_head_path, git_repository_path(repo), "MERGE_HEAD"));
cl_git_pass(p_unlink(git_buf_cstr(&merge_head_path)));
git_buf_dispose(&merge_head_path);
cl_git_pass(git_repository_index(&index, repo));
cl_assert_equal_i(true, git_index_has_conflicts(index));
git_index_free(index);
cl_git_pass(git_repository_head(&head, repo));
cl_git_pass(git_reference_peel(&target, head, GIT_OBJECT_COMMIT));
git_reference_free(head);
cl_assert_equal_i(GIT_EUNMERGED, git_reset(repo, target, GIT_RESET_SOFT, NULL));
}
void test_reset_soft__reflog_is_correct(void)
{
git_annotated_commit *annotated;
const char *exp_msg = "checkout: moving from br2 to master";
const char *master_msg = "commit: checking in";
reflog_check(repo, "HEAD", 7, "yoram.harmelin@gmail.com", exp_msg);
reflog_check(repo, "refs/heads/master", 2, "yoram.harmelin@gmail.com", master_msg);
/* Branch not moving, no reflog entry */
cl_git_pass(git_revparse_single(&target, repo, "HEAD^{commit}"));
cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
reflog_check(repo, "HEAD", 7, "yoram.harmelin@gmail.com", exp_msg);
reflog_check(repo, "refs/heads/master", 2, "yoram.harmelin@gmail.com", master_msg);
git_object_free(target);
/* Moved branch, expect id in message */
exp_msg = "reset: moving to be3563ae3f795b2b4353bcce3a527ad0a4f7f644";
cl_git_pass(git_revparse_single(&target, repo, "HEAD~^{commit}"));
cl_git_pass(git_reset(repo, target, GIT_RESET_SOFT, NULL));
reflog_check(repo, "HEAD", 8, "yoram.harmelin@gmail.com", exp_msg);
reflog_check(repo, "refs/heads/master", 3, NULL, exp_msg);
/* Moved branch, expect message with annotated string */
exp_msg = "reset: moving to HEAD~^{commit}";
cl_git_pass(git_annotated_commit_from_revspec(&annotated, repo, "HEAD~^{commit}"));
cl_git_pass(git_reset_from_annotated(repo, annotated, GIT_RESET_SOFT, NULL));
reflog_check(repo, "HEAD", 9, "yoram.harmelin@gmail.com", exp_msg);
reflog_check(repo, "refs/heads/master", 4, NULL, exp_msg);
git_annotated_commit_free(annotated);
}