Blame tests/reset/default.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "posix.h"
Packit Service 20376f
#include "reset_helpers.h"
Packit Service 20376f
#include "path.h"
Packit Service 20376f
Packit Service 20376f
static git_repository *_repo;
Packit Service 20376f
static git_object *_target;
Packit Service 20376f
static git_strarray _pathspecs;
Packit Service 20376f
static git_index *_index;
Packit Service 20376f
Packit Service 20376f
static void initialize(const char *repo_name)
Packit Service 20376f
{
Packit Service 20376f
	_repo = cl_git_sandbox_init(repo_name);
Packit Service 20376f
	cl_git_pass(git_repository_index(&_index, _repo));
Packit Service 20376f
Packit Service 20376f
	_target = NULL;
Packit Service 20376f
Packit Service 20376f
	_pathspecs.strings = NULL;
Packit Service 20376f
	_pathspecs.count = 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_reset_default__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_reset_default__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	git_object_free(_target);
Packit Service 20376f
	_target = NULL;
Packit Service 20376f
Packit Service 20376f
	git_index_free(_index);
Packit Service 20376f
	_index = NULL;
Packit Service 20376f
Packit Service 20376f
	cl_git_sandbox_cleanup();
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void assert_content_in_index(
Packit Service 20376f
	git_strarray *pathspecs,
Packit Service 20376f
	bool should_exist,
Packit Service 20376f
	git_strarray *expected_shas)
Packit Service 20376f
{
Packit Service 20376f
	size_t i, pos;
Packit Service 20376f
	int error;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < pathspecs->count; i++) {
Packit Service 20376f
		error = git_index_find(&pos, _index, pathspecs->strings[i]);
Packit Service 20376f
Packit Service 20376f
		if (should_exist) {
Packit Service 20376f
			const git_index_entry *entry;
Packit Service 20376f
Packit Service 20376f
			cl_assert(error != GIT_ENOTFOUND);
Packit Service 20376f
Packit Service 20376f
			entry = git_index_get_byindex(_index, pos);
Packit Service 20376f
			cl_assert(entry != NULL);
Packit Service 20376f
Packit Service 20376f
			if (!expected_shas)
Packit Service 20376f
				continue;
Packit Service 20376f
Packit Service 20376f
			cl_git_pass(git_oid_streq(&entry->id, expected_shas->strings[i]));
Packit Service 20376f
		} else
Packit Service 20376f
			cl_assert_equal_i(should_exist, error != GIT_ENOTFOUND);
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_reset_default__resetting_filepaths_against_a_null_target_removes_them_from_the_index(void)
Packit Service 20376f
{
Packit Service 20376f
	char *paths[] = { "staged_changes", "staged_new_file" };
Packit Service 20376f
Packit Service 20376f
	initialize("status");
Packit Service 20376f
Packit Service 20376f
	_pathspecs.strings = paths;
Packit Service 20376f
	_pathspecs.count = 2;
Packit Service 20376f
Packit Service 20376f
	assert_content_in_index(&_pathspecs, true, NULL);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reset_default(_repo, NULL, &_pathspecs));
Packit Service 20376f
Packit Service 20376f
	assert_content_in_index(&_pathspecs, false, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * $ git ls-files --cached -s --abbrev=7 -- "staged*"
Packit Service 20376f
 * 100644 55d316c 0        staged_changes
Packit Service 20376f
 * 100644 a6be623 0        staged_changes_file_deleted
Packit Service 20376f
 * ...
Packit Service 20376f
 *
Packit Service 20376f
 * $ git reset 0017bd4 -- staged_changes staged_changes_file_deleted
Packit Service 20376f
 * Unstaged changes after reset:
Packit Service 20376f
 * ...
Packit Service 20376f
 *
Packit Service 20376f
 * $ git ls-files --cached -s --abbrev=7 -- "staged*"
Packit Service 20376f
 * 100644 32504b7 0        staged_changes
Packit Service 20376f
 * 100644 061d42a 0        staged_changes_file_deleted
Packit Service 20376f
 * ...
Packit Service 20376f
 */
Packit Service 20376f
void test_reset_default__resetting_filepaths_replaces_their_corresponding_index_entries(void)
Packit Service 20376f
{
Packit Service 20376f
	git_strarray before, after;
Packit Service 20376f
Packit Service 20376f
	char *paths[] = { "staged_changes", "staged_changes_file_deleted" };
Packit Service 20376f
	char *before_shas[] = { "55d316c9ba708999f1918e9677d01dfcae69c6b9",
Packit Service 20376f
		"a6be623522ce87a1d862128ac42672604f7b468b" };
Packit Service 20376f
	char *after_shas[] = { "32504b727382542f9f089e24fddac5e78533e96c",
Packit Service 20376f
		"061d42a44cacde5726057b67558821d95db96f19" };
Packit Service 20376f
Packit Service 20376f
	initialize("status");
Packit Service 20376f
Packit Service 20376f
	_pathspecs.strings = paths;
Packit Service 20376f
	_pathspecs.count = 2;
Packit Service 20376f
	before.strings = before_shas;
Packit Service 20376f
	before.count = 2;
Packit Service 20376f
	after.strings = after_shas;
Packit Service 20376f
	after.count = 2;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_revparse_single(&_target, _repo, "0017bd4"));
Packit Service 20376f
	assert_content_in_index(&_pathspecs, true, &before);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
Packit Service 20376f
Packit Service 20376f
	assert_content_in_index(&_pathspecs, true, &after);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
 * $ git ls-files --cached -s --abbrev=7 -- conflicts-one.txt
Packit Service 20376f
 * 100644 1f85ca5 1        conflicts-one.txt
Packit Service 20376f
 * 100644 6aea5f2 2        conflicts-one.txt
Packit Service 20376f
 * 100644 516bd85 3        conflicts-one.txt
Packit Service 20376f
 *
Packit Service 20376f
 * $  git reset 9a05ccb -- conflicts-one.txt
Packit Service 20376f
 * Unstaged changes after reset:
Packit Service 20376f
 * ...
Packit Service 20376f
 *
Packit Service 20376f
 * $ git ls-files --cached -s --abbrev=7 -- conflicts-one.txt
Packit Service 20376f
 * 100644 1f85ca5 0        conflicts-one.txt
Packit Service 20376f
 *
Packit Service 20376f
 */
Packit Service 20376f
void test_reset_default__resetting_filepaths_clears_previous_conflicts(void)
Packit Service 20376f
{
Packit Service 20376f
	const git_index_entry *conflict_entry[3];
Packit Service 20376f
	git_strarray after;
Packit Service 20376f
Packit Service 20376f
	char *paths[] = { "conflicts-one.txt" };
Packit Service 20376f
	char *after_shas[] = { "1f85ca51b8e0aac893a621b61a9c2661d6aa6d81" };
Packit Service 20376f
Packit Service 20376f
	initialize("mergedrepo");
Packit Service 20376f
Packit Service 20376f
	_pathspecs.strings = paths;
Packit Service 20376f
	_pathspecs.count = 1;
Packit Service 20376f
	after.strings = after_shas;
Packit Service 20376f
	after.count = 1;
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_index_conflict_get(&conflict_entry[0], &conflict_entry[1],
Packit Service 20376f
		&conflict_entry[2], _index, "conflicts-one.txt"));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_revparse_single(&_target, _repo, "9a05ccb"));
Packit Service 20376f
	cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
Packit Service 20376f
Packit Service 20376f
	assert_content_in_index(&_pathspecs, true, &after);
Packit Service 20376f
Packit Service 20376f
	cl_assert_equal_i(GIT_ENOTFOUND, git_index_conflict_get(&conflict_entry[0],
Packit Service 20376f
		&conflict_entry[1], &conflict_entry[2], _index, "conflicts-one.txt"));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/*
Packit Service 20376f
$  git reset HEAD -- "I_am_not_there.txt" "me_neither.txt"
Packit Service 20376f
Unstaged changes after reset:
Packit Service 20376f
...
Packit Service 20376f
*/
Packit Service 20376f
void test_reset_default__resetting_unknown_filepaths_does_not_fail(void)
Packit Service 20376f
{
Packit Service 20376f
	char *paths[] = { "I_am_not_there.txt", "me_neither.txt" };
Packit Service 20376f
Packit Service 20376f
	initialize("status");
Packit Service 20376f
Packit Service 20376f
	_pathspecs.strings = paths;
Packit Service 20376f
	_pathspecs.count = 2;
Packit Service 20376f
Packit Service 20376f
	assert_content_in_index(&_pathspecs, false, NULL);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_revparse_single(&_target, _repo, "HEAD"));
Packit Service 20376f
	cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
Packit Service 20376f
Packit Service 20376f
	assert_content_in_index(&_pathspecs, false, NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_reset_default__staged_rename_reset_delete(void)
Packit Service 20376f
{
Packit Service 20376f
	git_index_entry entry;
Packit Service 20376f
	const git_index_entry *existing;
Packit Service 20376f
	char *paths[] = { "new.txt" };
Packit Service 20376f
Packit Service 20376f
	initialize("testrepo2");
Packit Service 20376f
Packit Service 20376f
	existing = git_index_get_bypath(_index, "new.txt", 0);
Packit Service 20376f
	cl_assert(existing);
Packit Service 20376f
	memcpy(&entry, existing, sizeof(entry));
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_index_remove_bypath(_index, "new.txt"));
Packit Service 20376f
Packit Service 20376f
	entry.path = "renamed.txt";
Packit Service 20376f
	cl_git_pass(git_index_add(_index, &entry));
Packit Service 20376f
Packit Service 20376f
	_pathspecs.strings = paths;
Packit Service 20376f
	_pathspecs.count = 1;
Packit Service 20376f
Packit Service 20376f
	assert_content_in_index(&_pathspecs, false, NULL);
Packit Service 20376f
Packit Service 20376f
	cl_git_pass(git_revparse_single(&_target, _repo, "HEAD"));
Packit Service 20376f
	cl_git_pass(git_reset_default(_repo, _target, &_pathspecs));
Packit Service 20376f
Packit Service 20376f
	assert_content_in_index(&_pathspecs, true, NULL);
Packit Service 20376f
}