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
+58
View File
@@ -0,0 +1,58 @@
#include "clar_libgit2.h"
#include "refs.h"
#include "repo/repo_helpers.h"
#include "path.h"
#include "futils.h"
static git_repository *g_repo;
void test_checkout_binaryunicode__initialize(void)
{
g_repo = cl_git_sandbox_init("binaryunicode");
}
void test_checkout_binaryunicode__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static void execute_test(void)
{
git_oid oid, check;
git_commit *commit;
git_tree *tree;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/branch1"));
cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
cl_git_pass(git_commit_tree(&tree, commit));
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_checkout_tree(g_repo, (git_object *)tree, &opts));
git_tree_free(tree);
git_commit_free(commit);
/* Verify that the lenna.jpg file was checked out correctly */
cl_git_pass(git_oid_fromstr(&check, "8ab005d890fe53f65eda14b23672f60d9f4ec5a1"));
cl_git_pass(git_odb_hashfile(&oid, "binaryunicode/lenna.jpg", GIT_OBJECT_BLOB));
cl_assert_equal_oid(&oid, &check);
/* Verify that the text file was checked out correctly */
cl_git_pass(git_oid_fromstr(&check, "965b223880dd4249e2c66a0cc0b4cffe1dc40f5a"));
cl_git_pass(git_odb_hashfile(&oid, "binaryunicode/utf16_withbom_noeol_crlf.txt", GIT_OBJECT_BLOB));
cl_assert_equal_oid(&oid, &check);
}
void test_checkout_binaryunicode__noautocrlf(void)
{
cl_repo_set_bool(g_repo, "core.autocrlf", false);
execute_test();
}
void test_checkout_binaryunicode__autocrlf(void)
{
cl_repo_set_bool(g_repo, "core.autocrlf", true);
execute_test();
}
+151
View File
@@ -0,0 +1,151 @@
#include "clar_libgit2.h"
#include "checkout_helpers.h"
#include "refs.h"
#include "futils.h"
#include "index.h"
void assert_on_branch(git_repository *repo, const char *branch)
{
git_reference *head;
git_buf bname = GIT_BUF_INIT;
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
cl_assert_(git_reference_type(head) == GIT_REFERENCE_SYMBOLIC, branch);
cl_git_pass(git_buf_joinpath(&bname, "refs/heads", branch));
cl_assert_equal_s(bname.ptr, git_reference_symbolic_target(head));
git_reference_free(head);
git_buf_dispose(&bname);
}
void reset_index_to_treeish(git_object *treeish)
{
git_object *tree;
git_index *index;
git_repository *repo = git_object_owner(treeish);
cl_git_pass(git_object_peel(&tree, treeish, GIT_OBJECT_TREE));
cl_git_pass(git_repository_index(&index, repo));
cl_git_pass(git_index_read_tree(index, (git_tree *)tree));
cl_git_pass(git_index_write(index));
git_object_free(tree);
git_index_free(index);
}
int checkout_count_callback(
git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload)
{
checkout_counts *ct = payload;
GIT_UNUSED(baseline); GIT_UNUSED(target); GIT_UNUSED(workdir);
if (why & GIT_CHECKOUT_NOTIFY_CONFLICT) {
ct->n_conflicts++;
if (ct->debug) {
if (workdir) {
if (baseline) {
if (target)
fprintf(stderr, "M %s (conflicts with M %s)\n",
workdir->path, target->path);
else
fprintf(stderr, "M %s (conflicts with D %s)\n",
workdir->path, baseline->path);
} else {
if (target)
fprintf(stderr, "Existing %s (conflicts with A %s)\n",
workdir->path, target->path);
else
fprintf(stderr, "How can an untracked file be a conflict (%s)\n", workdir->path);
}
} else {
if (baseline) {
if (target)
fprintf(stderr, "D %s (conflicts with M %s)\n",
target->path, baseline->path);
else
fprintf(stderr, "D %s (conflicts with D %s)\n",
baseline->path, baseline->path);
} else {
if (target)
fprintf(stderr, "How can an added file with no workdir be a conflict (%s)\n", target->path);
else
fprintf(stderr, "How can a nonexistent file be a conflict (%s)\n", path);
}
}
}
}
if (why & GIT_CHECKOUT_NOTIFY_DIRTY) {
ct->n_dirty++;
if (ct->debug) {
if (workdir)
fprintf(stderr, "M %s\n", workdir->path);
else
fprintf(stderr, "D %s\n", baseline->path);
}
}
if (why & GIT_CHECKOUT_NOTIFY_UPDATED) {
ct->n_updates++;
if (ct->debug) {
if (baseline) {
if (target)
fprintf(stderr, "update: M %s\n", path);
else
fprintf(stderr, "update: D %s\n", path);
} else {
if (target)
fprintf(stderr, "update: A %s\n", path);
else
fprintf(stderr, "update: this makes no sense %s\n", path);
}
}
}
if (why & GIT_CHECKOUT_NOTIFY_UNTRACKED) {
ct->n_untracked++;
if (ct->debug)
fprintf(stderr, "? %s\n", path);
}
if (why & GIT_CHECKOUT_NOTIFY_IGNORED) {
ct->n_ignored++;
if (ct->debug)
fprintf(stderr, "I %s\n", path);
}
return 0;
}
void tick_index(git_index *index)
{
struct timespec ts;
struct p_timeval times[2];
cl_assert(index->on_disk);
cl_assert(git_index_path(index));
cl_git_pass(git_index_read(index, true));
ts = index->stamp.mtime;
times[0].tv_sec = ts.tv_sec;
times[0].tv_usec = ts.tv_nsec / 1000;
times[1].tv_sec = ts.tv_sec + 5;
times[1].tv_usec = ts.tv_nsec / 1000;
cl_git_pass(p_utimes(git_index_path(index), times));
cl_git_pass(git_index_read(index, true));
}
+31
View File
@@ -0,0 +1,31 @@
#include "buffer.h"
#include "git2/object.h"
#include "git2/repository.h"
extern void assert_on_branch(git_repository *repo, const char *branch);
extern void reset_index_to_treeish(git_object *treeish);
#define check_file_contents(PATH,EXP) \
cl_assert_equal_file(EXP,0,PATH)
#define check_file_contents_nocr(PATH,EXP) \
cl_assert_equal_file_ignore_cr(EXP,0,PATH)
typedef struct {
int n_conflicts;
int n_dirty;
int n_updates;
int n_untracked;
int n_ignored;
int debug;
} checkout_counts;
extern int checkout_count_callback(
git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload);
extern void tick_index(git_index *index);
File diff suppressed because it is too large Load Diff
+496
View File
@@ -0,0 +1,496 @@
#include "clar_libgit2.h"
#include "checkout_helpers.h"
#include "../filter/crlf.h"
#include "futils.h"
#include "git2/checkout.h"
#include "repository.h"
#include "index.h"
#include "posix.h"
static git_repository *g_repo;
static const char *systype;
static git_buf expected_fixture = GIT_BUF_INIT;
static int unlink_file(void *payload, git_buf *path)
{
char *fn;
cl_assert(fn = git_path_basename(path->ptr));
GIT_UNUSED(payload);
if (strcmp(fn, ".git"))
cl_must_pass(p_unlink(path->ptr));
git__free(fn);
return 0;
}
void test_checkout_crlf__initialize(void)
{
git_buf reponame = GIT_BUF_INIT;
g_repo = cl_git_sandbox_init("crlf");
/*
* remove the contents of the working directory so that we can
* check out over it.
*/
cl_git_pass(git_buf_puts(&reponame, "crlf"));
cl_git_pass(git_path_direach(&reponame, 0, unlink_file, NULL));
if (GIT_EOL_NATIVE == GIT_EOL_CRLF)
systype = "windows";
else
systype = "posix";
git_buf_dispose(&reponame);
}
void test_checkout_crlf__cleanup(void)
{
cl_git_sandbox_cleanup();
if (expected_fixture.size) {
cl_fixture_cleanup(expected_fixture.ptr);
git_buf_dispose(&expected_fixture);
}
}
struct compare_data
{
const char *dirname;
const char *autocrlf;
const char *attrs;
};
static int compare_file(void *payload, git_buf *actual_path)
{
git_buf expected_path = GIT_BUF_INIT;
git_buf actual_contents = GIT_BUF_INIT;
git_buf expected_contents = GIT_BUF_INIT;
struct compare_data *cd = payload;
bool failed = true;
int cmp_git, cmp_gitattributes;
char *basename;
basename = git_path_basename(actual_path->ptr);
cmp_git = strcmp(basename, ".git");
cmp_gitattributes = strcmp(basename, ".gitattributes");
if (cmp_git == 0 || cmp_gitattributes == 0) {
failed = false;
goto done;
}
cl_git_pass(git_buf_joinpath(&expected_path, cd->dirname, basename));
if (!git_path_isfile(expected_path.ptr) ||
!git_path_isfile(actual_path->ptr))
goto done;
if (git_futils_readbuffer(&actual_contents, actual_path->ptr) < 0 ||
git_futils_readbuffer(&expected_contents, expected_path.ptr) < 0)
goto done;
if (actual_contents.size != expected_contents.size)
goto done;
if (memcmp(actual_contents.ptr, expected_contents.ptr, expected_contents.size) != 0)
goto done;
failed = false;
done:
if (failed) {
git_buf details = GIT_BUF_INIT;
git_buf_printf(&details, "filename=%s, system=%s, autocrlf=%s, attrs={%s}",
git_path_basename(actual_path->ptr), systype, cd->autocrlf, cd->attrs);
clar__fail(__FILE__, __LINE__,
"checked out contents did not match expected", details.ptr, 0);
git_buf_dispose(&details);
}
git__free(basename);
git_buf_dispose(&expected_contents);
git_buf_dispose(&actual_contents);
git_buf_dispose(&expected_path);
return 0;
}
static void test_checkout(const char *autocrlf, const char *attrs)
{
git_buf attrbuf = GIT_BUF_INIT;
git_buf expected_dirname = GIT_BUF_INIT;
git_buf systype_and_direction = GIT_BUF_INIT;
git_buf sandboxname = GIT_BUF_INIT;
git_buf reponame = GIT_BUF_INIT;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct compare_data compare_data = { NULL, autocrlf, attrs };
const char *c;
cl_git_pass(git_buf_puts(&reponame, "crlf"));
cl_git_pass(git_buf_puts(&systype_and_direction, systype));
cl_git_pass(git_buf_puts(&systype_and_direction, "_to_workdir"));
cl_git_pass(git_buf_puts(&sandboxname, "autocrlf_"));
cl_git_pass(git_buf_puts(&sandboxname, autocrlf));
if (*attrs) {
cl_git_pass(git_buf_puts(&sandboxname, ","));
for (c = attrs; *c; c++) {
if (*c == ' ')
cl_git_pass(git_buf_putc(&sandboxname, ','));
else if (*c == '=')
cl_git_pass(git_buf_putc(&sandboxname, '_'));
else
cl_git_pass(git_buf_putc(&sandboxname, *c));
}
cl_git_pass(git_buf_printf(&attrbuf, "* %s\n", attrs));
cl_git_mkfile("crlf/.gitattributes", attrbuf.ptr);
}
cl_repo_set_string(g_repo, "core.autocrlf", autocrlf);
cl_git_pass(git_buf_joinpath(&expected_dirname, systype_and_direction.ptr, sandboxname.ptr));
cl_git_pass(git_buf_joinpath(&expected_fixture, "crlf_data", expected_dirname.ptr));
cl_fixture_sandbox(expected_fixture.ptr);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
compare_data.dirname = sandboxname.ptr;
cl_git_pass(git_path_direach(&reponame, 0, compare_file, &compare_data));
cl_fixture_cleanup(expected_fixture.ptr);
git_buf_dispose(&expected_fixture);
git_buf_dispose(&attrbuf);
git_buf_dispose(&expected_fixture);
git_buf_dispose(&expected_dirname);
git_buf_dispose(&sandboxname);
git_buf_dispose(&systype_and_direction);
git_buf_dispose(&reponame);
}
static void empty_workdir(const char *name)
{
git_vector contents = GIT_VECTOR_INIT;
char *basename;
int cmp;
size_t i;
const char *fn;
cl_git_pass(git_path_dirload(&contents, name, 0, 0));
git_vector_foreach(&contents, i, fn) {
cl_assert(basename = git_path_basename(fn));
cmp = strncasecmp(basename, ".git", 4);
git__free(basename);
if (cmp)
cl_git_pass(p_unlink(fn));
}
git_vector_free_deep(&contents);
}
void test_checkout_crlf__matches_core_git(void)
{
const char *autocrlf[] = { "true", "false", "input", NULL };
const char *attrs[] = { "", "-crlf", "-text", "eol=crlf", "eol=lf",
"text", "text eol=crlf", "text eol=lf",
"text=auto", "text=auto eol=crlf", "text=auto eol=lf",
NULL };
const char **a, **b;
for (a = autocrlf; *a; a++) {
for (b = attrs; *b; b++) {
empty_workdir("crlf");
test_checkout(*a, *b);
}
}
}
void test_checkout_crlf__detect_crlf_autocrlf_false(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", false);
git_checkout_head(g_repo, &opts);
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
void test_checkout_crlf__autocrlf_false_index_size_is_unfiltered_size(void)
{
git_index *index;
const git_index_entry *entry;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", false);
git_repository_index(&index, g_repo);
tick_index(index);
git_checkout_head(g_repo, &opts);
cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
cl_assert(entry->file_size == strlen(ALL_LF_TEXT_RAW));
cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
cl_assert(entry->file_size == strlen(ALL_CRLF_TEXT_RAW));
git_index_free(index);
}
void test_checkout_crlf__detect_crlf_autocrlf_true(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
git_checkout_head(g_repo, &opts);
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
void test_checkout_crlf__detect_crlf_autocrlf_true_utf8(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
git_repository_set_head(g_repo, "refs/heads/master");
git_checkout_head(g_repo, &opts);
check_file_contents("./crlf/few-utf8-chars-lf", FEW_UTF8_CRLF_RAW);
check_file_contents("./crlf/many-utf8-chars-lf", MANY_UTF8_CRLF_RAW);
check_file_contents("./crlf/few-utf8-chars-crlf", FEW_UTF8_CRLF_RAW);
check_file_contents("./crlf/many-utf8-chars-crlf", MANY_UTF8_CRLF_RAW);
}
void test_checkout_crlf__autocrlf_true_index_size_is_filtered_size(void)
{
git_index *index;
const git_index_entry *entry;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
git_repository_index(&index, g_repo);
tick_index(index);
git_checkout_head(g_repo, &opts);
cl_assert((entry = git_index_get_bypath(index, "all-lf", 0)) != NULL);
cl_assert_equal_sz(strlen(ALL_LF_TEXT_AS_CRLF), entry->file_size);
cl_assert((entry = git_index_get_bypath(index, "all-crlf", 0)) != NULL);
cl_assert_equal_sz(strlen(ALL_CRLF_TEXT_RAW), entry->file_size);
git_index_free(index);
}
void test_checkout_crlf__with_ident(void)
{
git_index *index;
const git_index_entry *entry;
git_blob *blob;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_mkfile("crlf/.gitattributes",
"*.txt text\n*.bin binary\n"
"*.crlf text eol=crlf\n"
"*.lf text eol=lf\n"
"*.ident text ident\n"
"*.identcrlf ident text eol=crlf\n"
"*.identlf ident text eol=lf\n");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
/* add files with $Id$ */
cl_git_mkfile("crlf/lf.ident", ALL_LF_TEXT_RAW "\n$Id: initial content$\n");
cl_git_mkfile("crlf/crlf.ident", ALL_CRLF_TEXT_RAW "\r\n$Id$\r\n\r\n");
cl_git_mkfile("crlf/more1.identlf", "$Id$\n" MORE_LF_TEXT_RAW);
cl_git_mkfile("crlf/more2.identcrlf", "\r\n$Id: $\r\n" MORE_CRLF_TEXT_RAW);
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "lf.ident"));
cl_git_pass(git_index_add_bypath(index, "crlf.ident"));
cl_git_pass(git_index_add_bypath(index, "more1.identlf"));
cl_git_pass(git_index_add_bypath(index, "more2.identcrlf"));
cl_repo_commit_from_index(NULL, g_repo, NULL, 0, "Some ident files\n");
git_checkout_head(g_repo, &opts);
/* check that blobs have $Id$ */
cl_assert((entry = git_index_get_bypath(index, "lf.ident", 0)));
cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
cl_assert_equal_s(
ALL_LF_TEXT_RAW "\n$Id$\n", git_blob_rawcontent(blob));
git_blob_free(blob);
cl_assert((entry = git_index_get_bypath(index, "more2.identcrlf", 0)));
cl_git_pass(git_blob_lookup(&blob, g_repo, &entry->id));
cl_assert_equal_s(
"\n$Id$\n" MORE_CRLF_TEXT_AS_LF, git_blob_rawcontent(blob));
git_blob_free(blob);
/* check that filesystem is initially untouched - matching core Git */
cl_assert_equal_file(
ALL_LF_TEXT_RAW "\n$Id: initial content$\n", 0, "crlf/lf.ident");
/* check that forced checkout rewrites correctly */
p_unlink("crlf/lf.ident");
p_unlink("crlf/crlf.ident");
p_unlink("crlf/more1.identlf");
p_unlink("crlf/more2.identcrlf");
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert_equal_file(
ALL_LF_TEXT_AS_CRLF
"\r\n$Id: fcf6d4d9c212dc66563b1171b1cd99953c756467 $\r\n",
0, "crlf/lf.ident");
cl_assert_equal_file(
ALL_CRLF_TEXT_RAW
"\r\n$Id: f2c66ad9b2b5a734d9bf00d5000cc10a62b8a857 $\r\n\r\n",
0, "crlf/crlf.ident");
cl_assert_equal_file(
"$Id: f7830382dac1f1583422be5530fdfbd26289431b $\n"
MORE_LF_TEXT_AS_LF, 0, "crlf/more1.identlf");
cl_assert_equal_file(
"\r\n$Id: 74677a68413012ce8d7e7cfc3f12603df3a3eac4 $\r\n"
MORE_CRLF_TEXT_AS_CRLF, 0, "crlf/more2.identcrlf");
git_index_free(index);
}
void test_checkout_crlf__autocrlf_false_no_attrs(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", false);
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
void test_checkout_crlf__autocrlf_true_no_attrs(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
}
void test_checkout_crlf__autocrlf_input_no_attrs(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_string(g_repo, "core.autocrlf", "input");
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
void test_checkout_crlf__autocrlf_false_text_auto_attr(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
cl_repo_set_bool(g_repo, "core.autocrlf", false);
cl_git_pass(git_checkout_head(g_repo, &opts));
if (GIT_EOL_NATIVE == GIT_EOL_CRLF) {
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
} else {
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
}
void test_checkout_crlf__autocrlf_true_text_auto_attr(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_AS_CRLF);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_AS_CRLF);
}
void test_checkout_crlf__autocrlf_input_text_auto_attr(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_mkfile("./crlf/.gitattributes", "* text=auto\n");
cl_repo_set_string(g_repo, "core.autocrlf", "input");
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/all-lf", ALL_LF_TEXT_RAW);
check_file_contents("./crlf/all-crlf", ALL_CRLF_TEXT_RAW);
}
void test_checkout_crlf__can_write_empty_file(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_repository_set_head(g_repo, "refs/heads/empty-files"));
cl_git_pass(git_checkout_head(g_repo, &opts));
check_file_contents("./crlf/test1.txt", "");
check_file_contents("./crlf/test2.txt", "test2.txt's content\r\n");
check_file_contents("./crlf/test3.txt", "");
}
+266
View File
@@ -0,0 +1,266 @@
#include "clar_libgit2.h"
#include "refs.h"
#include "repo/repo_helpers.h"
#include "path.h"
#include "futils.h"
static git_repository *g_repo;
void test_checkout_head__initialize(void)
{
g_repo = cl_git_sandbox_init("testrepo");
}
void test_checkout_head__cleanup(void)
{
cl_git_sandbox_cleanup();
}
void test_checkout_head__unborn_head_returns_GIT_EUNBORNBRANCH(void)
{
make_head_unborn(g_repo, NON_EXISTING_HEAD);
cl_assert_equal_i(GIT_EUNBORNBRANCH, git_checkout_head(g_repo, NULL));
}
void test_checkout_head__with_index_only_tree(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
/* let's start by getting things into a known state */
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
/* now let's stage some new stuff including a new directory */
cl_git_pass(git_repository_index(&index, g_repo));
p_mkdir("testrepo/newdir", 0777);
cl_git_mkfile("testrepo/newdir/newfile.txt", "new file\n");
cl_git_pass(git_index_add_bypath(index, "newdir/newfile.txt"));
cl_git_pass(git_index_write(index));
cl_assert(git_path_isfile("testrepo/newdir/newfile.txt"));
cl_assert(git_index_get_bypath(index, "newdir/newfile.txt", 0) != NULL);
git_index_free(index);
/* okay, so now we have staged this new file; let's see if we can remove */
opts.checkout_strategy = GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_git_pass(git_repository_index(&index, g_repo));
cl_assert(!git_path_isfile("testrepo/newdir/newfile.txt"));
cl_assert(git_index_get_bypath(index, "newdir/newfile.txt", 0) == NULL);
git_index_free(index);
}
void test_checkout_head__do_not_remove_untracked_file(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
cl_git_pass(p_mkdir("testrepo/tracked", 0755));
cl_git_mkfile("testrepo/tracked/tracked", "tracked\n");
cl_git_mkfile("testrepo/tracked/untracked", "untracked\n");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "tracked/tracked"));
cl_git_pass(git_index_write(index));
git_index_free(index);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(!git_path_isfile("testrepo/tracked/tracked"));
cl_assert(git_path_isfile("testrepo/tracked/untracked"));
}
void test_checkout_head__do_not_remove_untracked_file_in_subdir(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
cl_git_pass(p_mkdir("testrepo/tracked", 0755));
cl_git_pass(p_mkdir("testrepo/tracked/subdir", 0755));
cl_git_mkfile("testrepo/tracked/tracked", "tracked\n");
cl_git_mkfile("testrepo/tracked/subdir/tracked", "tracked\n");
cl_git_mkfile("testrepo/tracked/subdir/untracked", "untracked\n");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "tracked/tracked"));
cl_git_pass(git_index_add_bypath(index, "tracked/subdir/tracked"));
cl_git_pass(git_index_write(index));
git_index_free(index);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(!git_path_isfile("testrepo/tracked/tracked"));
cl_assert(!git_path_isfile("testrepo/tracked/subdir/tracked"));
cl_assert(git_path_isfile("testrepo/tracked/subdir/untracked"));
}
void test_checkout_head__do_remove_tracked_subdir(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
cl_git_pass(p_mkdir("testrepo/subdir", 0755));
cl_git_pass(p_mkdir("testrepo/subdir/tracked", 0755));
cl_git_mkfile("testrepo/subdir/tracked-file", "tracked\n");
cl_git_mkfile("testrepo/subdir/untracked-file", "untracked\n");
cl_git_mkfile("testrepo/subdir/tracked/tracked1", "tracked\n");
cl_git_mkfile("testrepo/subdir/tracked/tracked2", "tracked\n");
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "subdir/tracked-file"));
cl_git_pass(git_index_add_bypath(index, "subdir/tracked/tracked1"));
cl_git_pass(git_index_add_bypath(index, "subdir/tracked/tracked2"));
cl_git_pass(git_index_write(index));
git_index_free(index);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_assert(!git_path_isdir("testrepo/subdir/tracked"));
cl_assert(!git_path_isfile("testrepo/subdir/tracked-file"));
cl_assert(git_path_isfile("testrepo/subdir/untracked-file"));
}
void test_checkout_head__typechange_workdir(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *target;
struct stat st;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_revparse_single(&target, g_repo, "HEAD"));
cl_git_pass(git_reset(g_repo, target, GIT_RESET_HARD, NULL));
cl_must_pass(p_chmod("testrepo/new.txt", 0755));
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_git_pass(p_stat("testrepo/new.txt", &st));
cl_assert(!GIT_PERMS_IS_EXEC(st.st_mode));
git_object_free(target);
}
void test_checkout_head__typechange_index_and_workdir(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *target;
git_index *index;
struct stat st;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_revparse_single(&target, g_repo, "HEAD"));
cl_git_pass(git_reset(g_repo, target, GIT_RESET_HARD, NULL));
cl_must_pass(p_chmod("testrepo/new.txt", 0755));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "new.txt"));
cl_git_pass(git_index_write(index));
cl_git_pass(git_checkout_head(g_repo, &opts));
cl_git_pass(p_stat("testrepo/new.txt", &st));
cl_assert(!GIT_PERMS_IS_EXEC(st.st_mode));
git_object_free(target);
git_index_free(index);
}
void test_checkout_head__workdir_filemode_is_simplified(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *target, *branch;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_revparse_single(&target, g_repo, "a38d028f71eaa590febb7d716b1ca32350cf70da"));
cl_git_pass(git_reset(g_repo, target, GIT_RESET_HARD, NULL));
cl_must_pass(p_chmod("testrepo/branch_file.txt", 0666));
/*
* Checkout should not fail with a conflict; though the file mode
* on disk is literally different to the base (0666 vs 0644), Git
* ignores the actual mode and simply treats both as non-executable.
*/
cl_git_pass(git_revparse_single(&branch, g_repo, "099fabac3a9ea935598528c27f866e34089c2eff"));
opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE;
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
cl_git_pass(git_checkout_tree(g_repo, branch, NULL));
git_object_free(branch);
git_object_free(target);
}
void test_checkout_head__obeys_filemode_true(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *target, *branch;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
/* In this commit, `README` is executable */
cl_git_pass(git_revparse_single(&target, g_repo, "f9ed4af42472941da45a3c"));
cl_git_pass(git_reset(g_repo, target, GIT_RESET_HARD, NULL));
cl_repo_set_bool(g_repo, "core.filemode", true);
cl_must_pass(p_chmod("testrepo/README", 0644));
/*
* Checkout will fail with a conflict; the file mode is updated in
* the checkout target, but the contents have changed in our branch.
*/
cl_git_pass(git_revparse_single(&branch, g_repo, "099fabac3a9ea935598528c27f866e34089c2eff"));
opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE;
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
cl_git_fail_with(GIT_ECONFLICT, git_checkout_tree(g_repo, branch, NULL));
git_object_free(branch);
git_object_free(target);
}
void test_checkout_head__obeys_filemode_false(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_object *target, *branch;
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
/* In this commit, `README` is executable */
cl_git_pass(git_revparse_single(&target, g_repo, "f9ed4af42472941da45a3c"));
cl_git_pass(git_reset(g_repo, target, GIT_RESET_HARD, NULL));
cl_repo_set_bool(g_repo, "core.filemode", false);
cl_must_pass(p_chmod("testrepo/README", 0644));
/*
* Checkout will fail with a conflict; the file contents are updated
* in the checkout target, but the filemode has changed in our branch.
*/
cl_git_pass(git_revparse_single(&branch, g_repo, "099fabac3a9ea935598528c27f866e34089c2eff"));
opts.checkout_strategy &= ~GIT_CHECKOUT_FORCE;
opts.checkout_strategy |= GIT_CHECKOUT_SAFE;
cl_git_pass(git_checkout_tree(g_repo, branch, NULL));
git_object_free(branch);
git_object_free(target);
}
+292
View File
@@ -0,0 +1,292 @@
#include "clar_libgit2.h"
#include "git2/checkout.h"
#include "refs.h"
#include "path.h"
#include "repository.h"
#ifdef GIT_WIN32
# include <windows.h>
#else
# include <dirent.h>
#endif
static git_repository *repo;
static git_object *obj;
static git_checkout_options checkout_opts;
void test_checkout_icase__initialize(void)
{
git_oid id;
git_config *cfg;
int icase = 0;
repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_repository_config_snapshot(&cfg, repo));
git_config_get_bool(&icase, cfg, "core.ignorecase");
git_config_free(cfg);
if (!icase)
cl_skip();
cl_git_pass(git_reference_name_to_id(&id, repo, "refs/heads/dir"));
cl_git_pass(git_object_lookup(&obj, repo, &id, GIT_OBJECT_ANY));
git_checkout_options_init(&checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION);
checkout_opts.checkout_strategy = GIT_CHECKOUT_NONE;
}
void test_checkout_icase__cleanup(void)
{
git_object_free(obj);
cl_git_sandbox_cleanup();
}
static char *get_filename(const char *in)
{
char *search_dirname, *search_filename, *filename = NULL;
git_buf out = GIT_BUF_INIT;
DIR *dir;
struct dirent *de;
cl_assert(search_dirname = git_path_dirname(in));
cl_assert(search_filename = git_path_basename(in));
cl_assert(dir = opendir(search_dirname));
while ((de = readdir(dir))) {
if (strcasecmp(de->d_name, search_filename) == 0) {
git_buf_join(&out, '/', search_dirname, de->d_name);
filename = git_buf_detach(&out);
break;
}
}
closedir(dir);
git__free(search_dirname);
git__free(search_filename);
git_buf_dispose(&out);
return filename;
}
static void assert_name_is(const char *expected)
{
char *actual;
size_t actual_len, expected_len, start;
cl_assert(actual = get_filename(expected));
expected_len = strlen(expected);
actual_len = strlen(actual);
cl_assert(actual_len >= expected_len);
start = actual_len - expected_len;
cl_assert_equal_s(expected, actual + start);
if (start)
cl_assert_equal_strn("/", actual + (start - 1), 1);
free(actual);
}
static int symlink_or_fake(git_repository *repo, const char *a, const char *b)
{
int symlinks;
cl_git_pass(git_repository__configmap_lookup(&symlinks, repo, GIT_CONFIGMAP_SYMLINKS));
if (symlinks)
return p_symlink(a, b);
else
return git_futils_fake_symlink(a, b);
}
void test_checkout_icase__refuses_to_overwrite_files_for_files(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_git_write2file("testrepo/BRANCH_FILE.txt", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/BRANCH_FILE.txt");
}
void test_checkout_icase__overwrites_files_for_files_when_forced(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_write2file("testrepo/NEW.txt", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/new.txt");
}
void test_checkout_icase__refuses_to_overwrite_links_for_files(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_must_pass(symlink_or_fake(repo, "../tmp", "testrepo/BRANCH_FILE.txt"));
cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
cl_assert(!git_path_exists("tmp"));
assert_name_is("testrepo/BRANCH_FILE.txt");
}
void test_checkout_icase__overwrites_links_for_files_when_forced(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_must_pass(symlink_or_fake(repo, "../tmp", "testrepo/NEW.txt"));
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
cl_assert(!git_path_exists("tmp"));
assert_name_is("testrepo/new.txt");
}
void test_checkout_icase__overwrites_empty_folders_for_files(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_must_pass(p_mkdir("testrepo/NEW.txt", 0777));
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/new.txt");
cl_assert(!git_path_isdir("testrepo/new.txt"));
}
void test_checkout_icase__refuses_to_overwrite_populated_folders_for_files(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_must_pass(p_mkdir("testrepo/BRANCH_FILE.txt", 0777));
cl_git_write2file("testrepo/BRANCH_FILE.txt/foobar", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/BRANCH_FILE.txt");
cl_assert(git_path_isdir("testrepo/BRANCH_FILE.txt"));
}
void test_checkout_icase__overwrites_folders_for_files_when_forced(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_must_pass(p_mkdir("testrepo/NEW.txt", 0777));
cl_git_write2file("testrepo/NEW.txt/foobar", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/new.txt");
cl_assert(!git_path_isdir("testrepo/new.txt"));
}
void test_checkout_icase__refuses_to_overwrite_files_for_folders(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_git_write2file("testrepo/A", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/A");
cl_assert(!git_path_isdir("testrepo/A"));
}
void test_checkout_icase__overwrites_files_for_folders_when_forced(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_write2file("testrepo/A", "neue file\n", 10, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
assert_name_is("testrepo/a");
cl_assert(git_path_isdir("testrepo/a"));
}
void test_checkout_icase__refuses_to_overwrite_links_for_folders(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE|GIT_CHECKOUT_RECREATE_MISSING;
cl_must_pass(symlink_or_fake(repo, "..", "testrepo/A"));
cl_git_fail(git_checkout_tree(repo, obj, &checkout_opts));
cl_assert(!git_path_exists("b.txt"));
assert_name_is("testrepo/A");
}
void test_checkout_icase__overwrites_links_for_folders_when_forced(void)
{
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_must_pass(symlink_or_fake(repo, "..", "testrepo/A"));
cl_git_pass(git_checkout_tree(repo, obj, &checkout_opts));
cl_assert(!git_path_exists("b.txt"));
assert_name_is("testrepo/a");
}
void test_checkout_icase__ignores_unstaged_casechange(void)
{
git_reference *orig_ref, *br2_ref;
git_commit *orig, *br2;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_reference_lookup_resolved(&orig_ref, repo, "HEAD", 100));
cl_git_pass(git_commit_lookup(&orig, repo, git_reference_target(orig_ref)));
cl_git_pass(git_reset(repo, (git_object *)orig, GIT_RESET_HARD, NULL));
cl_rename("testrepo/branch_file.txt", "testrepo/Branch_File.txt");
cl_git_pass(git_reference_lookup_resolved(&br2_ref, repo, "refs/heads/br2", 100));
cl_git_pass(git_commit_lookup(&br2, repo, git_reference_target(br2_ref)));
cl_git_pass(git_checkout_tree(repo, (const git_object *)br2, &checkout_opts));
git_commit_free(orig);
git_commit_free(br2);
git_reference_free(orig_ref);
git_reference_free(br2_ref);
}
void test_checkout_icase__conflicts_with_casechanged_subtrees(void)
{
git_reference *orig_ref;
git_object *orig, *subtrees;
git_oid oid;
git_checkout_options checkout_opts = GIT_CHECKOUT_OPTIONS_INIT;
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
cl_git_pass(git_reference_lookup_resolved(&orig_ref, repo, "HEAD", 100));
cl_git_pass(git_object_lookup(&orig, repo, git_reference_target(orig_ref), GIT_OBJECT_COMMIT));
cl_git_pass(git_reset(repo, (git_object *)orig, GIT_RESET_HARD, NULL));
cl_must_pass(p_mkdir("testrepo/AB", 0777));
cl_must_pass(p_mkdir("testrepo/AB/C", 0777));
cl_git_write2file("testrepo/AB/C/3.txt", "Foobar!\n", 8, O_RDWR|O_CREAT, 0666);
cl_git_pass(git_reference_name_to_id(&oid, repo, "refs/heads/subtrees"));
cl_git_pass(git_object_lookup(&subtrees, repo, &oid, GIT_OBJECT_ANY));
cl_git_fail(git_checkout_tree(repo, subtrees, &checkout_opts));
git_object_free(orig);
git_object_free(subtrees);
git_reference_free(orig_ref);
}
+826
View File
@@ -0,0 +1,826 @@
#include "clar_libgit2.h"
#include "checkout_helpers.h"
#include "git2/checkout.h"
#include "futils.h"
#include "repository.h"
#include "remote.h"
#include "repo/repo_helpers.h"
static git_repository *g_repo;
static git_buf g_global_path = GIT_BUF_INIT;
void test_checkout_index__initialize(void)
{
git_tree *tree;
g_repo = cl_git_sandbox_init("testrepo");
cl_git_pass(git_repository_head_tree(&tree, g_repo));
reset_index_to_treeish((git_object *)tree);
git_tree_free(tree);
cl_git_rewritefile(
"./testrepo/.gitattributes",
"* text eol=lf\n");
git_libgit2_opts(GIT_OPT_GET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
&g_global_path);
}
void test_checkout_index__cleanup(void)
{
git_libgit2_opts(GIT_OPT_SET_SEARCH_PATH, GIT_CONFIG_LEVEL_GLOBAL,
g_global_path.ptr);
git_buf_dispose(&g_global_path);
cl_git_sandbox_cleanup();
/* try to remove directories created by tests */
cl_fixture_cleanup("alternative");
cl_fixture_cleanup("symlink");
cl_fixture_cleanup("symlink.git");
cl_fixture_cleanup("tmp_global_path");
}
void test_checkout_index__cannot_checkout_a_bare_repository(void)
{
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("testrepo.git");
cl_git_fail(git_checkout_index(g_repo, NULL, NULL));
}
void test_checkout_index__can_create_missing_files(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/README", "hey there\n");
check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
check_file_contents("./testrepo/new.txt", "my new file\n");
}
void test_checkout_index__can_remove_untracked_files(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_futils_mkdir("./testrepo/dir/subdir/subsubdir", 0755, GIT_MKDIR_PATH);
cl_git_mkfile("./testrepo/dir/one", "one\n");
cl_git_mkfile("./testrepo/dir/subdir/two", "two\n");
cl_assert_equal_i(true, git_path_isdir("./testrepo/dir/subdir/subsubdir"));
opts.checkout_strategy =
GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING |
GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert_equal_i(false, git_path_isdir("./testrepo/dir"));
}
void test_checkout_index__honor_the_specified_pathspecs(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
char *entries[] = { "*.txt" };
opts.paths.strings = entries;
opts.paths.count = 1;
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
check_file_contents("./testrepo/new.txt", "my new file\n");
}
void test_checkout_index__honor_the_gitattributes_directives(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
const char *attributes =
"branch_file.txt text eol=crlf\n"
"new.txt text eol=lf\n";
cl_git_mkfile("./testrepo/.gitattributes", attributes);
cl_repo_set_bool(g_repo, "core.autocrlf", false);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/README", "hey there\n");
check_file_contents("./testrepo/new.txt", "my new file\n");
check_file_contents("./testrepo/branch_file.txt", "hi\r\nbye!\r\n");
}
void test_checkout_index__honor_coreautocrlf_setting_set_to_true(void)
{
#ifdef GIT_WIN32
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
const char *expected_readme_text = "hey there\r\n";
cl_git_pass(p_unlink("./testrepo/.gitattributes"));
cl_repo_set_bool(g_repo, "core.autocrlf", true);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/README", expected_readme_text);
#endif
}
static void populate_symlink_workdir(void)
{
git_buf path = GIT_BUF_INIT;
git_repository *repo;
git_remote *origin;
git_object *target;
const char *url = git_repository_path(g_repo);
cl_git_pass(git_buf_joinpath(&path, clar_sandbox_path(), "symlink.git"));
cl_git_pass(git_repository_init(&repo, path.ptr, true));
cl_git_pass(git_repository_set_workdir(repo, "symlink", 1));
/* Delete the `origin` repo (if it exists) so we can recreate it. */
git_remote_delete(repo, GIT_REMOTE_ORIGIN);
cl_git_pass(git_remote_create(&origin, repo, GIT_REMOTE_ORIGIN, url));
cl_git_pass(git_remote_fetch(origin, NULL, NULL, NULL));
git_remote_free(origin);
cl_git_pass(git_revparse_single(&target, repo, "remotes/origin/master"));
cl_git_pass(git_reset(repo, target, GIT_RESET_HARD, NULL));
git_object_free(target);
git_repository_free(repo);
git_buf_dispose(&path);
}
void test_checkout_index__honor_coresymlinks_default_true(void)
{
char link_data[GIT_PATH_MAX];
int link_size = GIT_PATH_MAX;
cl_must_pass(p_mkdir("symlink", 0777));
if (!git_path_supports_symlinks("symlink/test"))
cl_skip();
#ifdef GIT_WIN32
/*
* Windows explicitly requires the global configuration to have
* core.symlinks=true in addition to actual filesystem support.
*/
create_tmp_global_config("tmp_global_path", "core.symlinks", "true");
#endif
populate_symlink_workdir();
link_size = p_readlink("./symlink/link_to_new.txt", link_data, link_size);
cl_assert(link_size >= 0);
link_data[link_size] = '\0';
cl_assert_equal_i(link_size, strlen("new.txt"));
cl_assert_equal_s(link_data, "new.txt");
check_file_contents("./symlink/link_to_new.txt", "my new file\n");
}
void test_checkout_index__honor_coresymlinks_default_false(void)
{
cl_must_pass(p_mkdir("symlink", 0777));
#ifndef GIT_WIN32
/*
* This test is largely for Windows platforms to ensure that
* we respect an unset core.symlinks even when the platform
* supports symlinks. Bail entirely on POSIX platforms that
* do support symlinks.
*/
if (git_path_supports_symlinks("symlink/test"))
cl_skip();
#endif
populate_symlink_workdir();
check_file_contents("./symlink/link_to_new.txt", "new.txt");
}
void test_checkout_index__coresymlinks_set_to_true_fails_when_unsupported(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
if (git_path_supports_symlinks("testrepo/test")) {
cl_skip();
}
cl_repo_set_bool(g_repo, "core.symlinks", true);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
}
void test_checkout_index__honor_coresymlinks_setting_set_to_true(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
char link_data[GIT_PATH_MAX];
size_t link_size = GIT_PATH_MAX;
if (!git_path_supports_symlinks("testrepo/test")) {
cl_skip();
}
cl_repo_set_bool(g_repo, "core.symlinks", true);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
link_size = p_readlink("./testrepo/link_to_new.txt", link_data, link_size);
link_data[link_size] = '\0';
cl_assert_equal_i(link_size, strlen("new.txt"));
cl_assert_equal_s(link_data, "new.txt");
check_file_contents("./testrepo/link_to_new.txt", "my new file\n");
}
void test_checkout_index__honor_coresymlinks_setting_set_to_false(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_repo_set_bool(g_repo, "core.symlinks", false);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/link_to_new.txt", "new.txt");
}
void test_checkout_index__donot_overwrite_modified_file_by_default(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
/* set this up to not return an error code on conflicts, but it
* still will not have permission to overwrite anything...
*/
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_ALLOW_CONFLICTS;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/new.txt", "This isn't what's stored!");
}
void test_checkout_index__can_overwrite_modified_file(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/new.txt", "my new file\n");
}
void test_checkout_index__options_disable_filters(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_mkfile("./testrepo/.gitattributes", "*.txt text eol=crlf\n");
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
opts.disable_filters = false;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/new.txt", "my new file\r\n");
p_unlink("./testrepo/new.txt");
opts.disable_filters = true;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/new.txt", "my new file\n");
}
void test_checkout_index__options_dir_modes(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct stat st;
git_oid oid;
git_commit *commit;
mode_t um;
if (!cl_is_chmod_supported())
return;
cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
reset_index_to_treeish((git_object *)commit);
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
opts.dir_mode = 0701;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
/* umask will influence actual directory creation mode */
(void)p_umask(um = p_umask(022));
cl_git_pass(p_stat("./testrepo/a", &st));
/* Haiku & Hurd use other mode bits, so we must mask them out */
cl_assert_equal_i_fmt(st.st_mode & (S_IFMT | 07777), (GIT_FILEMODE_TREE | 0701) & ~um, "%07o");
/* File-mode test, since we're on the 'dir' branch */
cl_git_pass(p_stat("./testrepo/a/b.txt", &st));
cl_assert_equal_i_fmt(st.st_mode & (S_IFMT | 07777), GIT_FILEMODE_BLOB_EXECUTABLE & ~um, "%07o");
git_commit_free(commit);
}
void test_checkout_index__options_override_file_modes(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct stat st;
if (!cl_is_chmod_supported())
return;
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
opts.file_mode = 0700;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_git_pass(p_stat("./testrepo/new.txt", &st));
cl_assert_equal_i_fmt(st.st_mode & GIT_MODE_PERMS_MASK, 0700, "%07o");
}
void test_checkout_index__options_open_flags(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_mkfile("./testrepo/new.txt", "hi\n");
opts.checkout_strategy =
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_DONT_REMOVE_EXISTING;
opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
}
struct notify_data {
const char *file;
const char *sha;
};
static int test_checkout_notify_cb(
git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload)
{
struct notify_data *expectations = (struct notify_data *)payload;
GIT_UNUSED(workdir);
cl_assert_equal_i(GIT_CHECKOUT_NOTIFY_CONFLICT, why);
cl_assert_equal_s(expectations->file, path);
cl_assert_equal_i(0, git_oid_streq(&baseline->id, expectations->sha));
cl_assert_equal_i(0, git_oid_streq(&target->id, expectations->sha));
return 0;
}
void test_checkout_index__can_notify_of_skipped_files(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
struct notify_data data;
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
/*
* $ git ls-tree HEAD
* 100644 blob a8233120f6ad708f843d861ce2b7228ec4e3dec6 README
* 100644 blob 3697d64be941a53d4ae8f6a271e4e3fa56b022cc branch_file.txt
* 100644 blob a71586c1dfe8a71c6cbf6c129f404c5642ff31bd new.txt
*/
data.file = "new.txt";
data.sha = "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd";
opts.checkout_strategy = GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING |
GIT_CHECKOUT_ALLOW_CONFLICTS;
opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
opts.notify_cb = test_checkout_notify_cb;
opts.notify_payload = &data;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
}
static int dont_notify_cb(
git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload)
{
GIT_UNUSED(why);
GIT_UNUSED(path);
GIT_UNUSED(baseline);
GIT_UNUSED(target);
GIT_UNUSED(workdir);
GIT_UNUSED(payload);
cl_assert(false);
return 0;
}
void test_checkout_index__wont_notify_of_expected_line_ending_changes(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_pass(p_unlink("./testrepo/.gitattributes"));
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_mkfile("./testrepo/new.txt", "my new file\r\n");
opts.checkout_strategy =
GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING |
GIT_CHECKOUT_ALLOW_CONFLICTS;
opts.notify_flags = GIT_CHECKOUT_NOTIFY_CONFLICT;
opts.notify_cb = dont_notify_cb;
opts.notify_payload = NULL;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
}
static void checkout_progress_counter(
const char *path, size_t cur, size_t tot, void *payload)
{
GIT_UNUSED(path); GIT_UNUSED(cur); GIT_UNUSED(tot);
(*(int *)payload)++;
}
void test_checkout_index__calls_progress_callback(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
int calls = 0;
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
opts.progress_cb = checkout_progress_counter;
opts.progress_payload = &calls;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert(calls > 0);
}
void test_checkout_index__can_overcome_name_clashes(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
cl_git_pass(git_repository_index(&index, g_repo));
git_index_clear(index);
cl_git_mkfile("./testrepo/path0", "content\r\n");
cl_git_pass(p_mkdir("./testrepo/path1", 0777));
cl_git_mkfile("./testrepo/path1/file1", "content\r\n");
cl_git_pass(git_index_add_bypath(index, "path0"));
cl_git_pass(git_index_add_bypath(index, "path1/file1"));
cl_git_pass(p_unlink("./testrepo/path0"));
cl_git_pass(git_futils_rmdir_r(
"./testrepo/path1", NULL, GIT_RMDIR_REMOVE_FILES));
cl_git_mkfile("./testrepo/path1", "content\r\n");
cl_git_pass(p_mkdir("./testrepo/path0", 0777));
cl_git_mkfile("./testrepo/path0/file0", "content\r\n");
cl_assert(git_path_isfile("./testrepo/path1"));
cl_assert(git_path_isfile("./testrepo/path0/file0"));
opts.checkout_strategy =
GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING |
GIT_CHECKOUT_ALLOW_CONFLICTS;
cl_git_pass(git_checkout_index(g_repo, index, &opts));
cl_assert(git_path_isfile("./testrepo/path1"));
cl_assert(git_path_isfile("./testrepo/path0/file0"));
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_index(g_repo, index, &opts));
cl_assert(git_path_isfile("./testrepo/path0"));
cl_assert(git_path_isfile("./testrepo/path1/file1"));
git_index_free(index);
}
void test_checkout_index__validates_struct_version(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
const git_error *err;
opts.version = 1024;
cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
err = git_error_last();
cl_assert_equal_i(err->klass, GIT_ERROR_INVALID);
opts.version = 0;
git_error_clear();
cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
err = git_error_last();
cl_assert_equal_i(err->klass, GIT_ERROR_INVALID);
}
void test_checkout_index__can_update_prefixed_files(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
cl_git_mkfile("./testrepo/READ", "content\n");
cl_git_mkfile("./testrepo/README.after", "content\n");
cl_git_pass(p_mkdir("./testrepo/branch_file", 0777));
cl_git_pass(p_mkdir("./testrepo/branch_file/contained_dir", 0777));
cl_git_mkfile("./testrepo/branch_file/contained_file", "content\n");
cl_git_pass(p_mkdir("./testrepo/branch_file.txt.after", 0777));
opts.checkout_strategy =
GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING |
GIT_CHECKOUT_REMOVE_UNTRACKED;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
/* remove untracked will remove the .gitattributes file before the blobs
* were created, so they will have had crlf filtering applied on Windows
*/
check_file_contents_nocr("./testrepo/README", "hey there\n");
check_file_contents_nocr("./testrepo/branch_file.txt", "hi\nbye!\n");
check_file_contents_nocr("./testrepo/new.txt", "my new file\n");
cl_assert(!git_path_exists("testrepo/READ"));
cl_assert(!git_path_exists("testrepo/README.after"));
cl_assert(!git_path_exists("testrepo/branch_file"));
cl_assert(!git_path_exists("testrepo/branch_file.txt.after"));
}
void test_checkout_index__can_checkout_a_newly_initialized_repository(void)
{
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("empty_standard_repo");
cl_git_remove_placeholders(git_repository_path(g_repo), "dummy-marker.txt");
cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
}
void test_checkout_index__issue_1397(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("issue_1397");
cl_repo_set_bool(g_repo, "core.autocrlf", true);
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
check_file_contents("./issue_1397/crlf_file.txt", "first line\r\nsecond line\r\nboth with crlf");
}
void test_checkout_index__target_directory(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
checkout_counts cts;
memset(&cts, 0, sizeof(cts));
opts.checkout_strategy = GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING;
opts.target_directory = "alternative";
cl_assert(!git_path_isdir("alternative"));
opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
opts.notify_cb = checkout_count_callback;
opts.notify_payload = &cts;
/* create some files that *would* conflict if we were using the wd */
cl_git_mkfile("testrepo/README", "I'm in the way!\n");
cl_git_mkfile("testrepo/new.txt", "my new file\n");
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert_equal_i(0, cts.n_untracked);
cl_assert_equal_i(0, cts.n_ignored);
cl_assert_equal_i(4, cts.n_updates);
check_file_contents("./alternative/README", "hey there\n");
check_file_contents("./alternative/branch_file.txt", "hi\nbye!\n");
check_file_contents("./alternative/new.txt", "my new file\n");
cl_git_pass(git_futils_rmdir_r(
"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
}
void test_checkout_index__target_directory_from_bare(void)
{
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_index *index;
git_object *head = NULL;
checkout_counts cts;
memset(&cts, 0, sizeof(cts));
cl_git_sandbox_cleanup();
g_repo = cl_git_sandbox_init("testrepo.git");
cl_assert(git_repository_is_bare(g_repo));
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_revparse_single(&head, g_repo, "HEAD^{tree}"));
cl_git_pass(git_index_read_tree(index, (const git_tree *)head));
cl_git_pass(git_index_write(index));
git_index_free(index);
opts.checkout_strategy = GIT_CHECKOUT_SAFE |
GIT_CHECKOUT_RECREATE_MISSING;
opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
opts.notify_cb = checkout_count_callback;
opts.notify_payload = &cts;
/* fail to checkout a bare repo */
cl_git_fail(git_checkout_index(g_repo, NULL, &opts));
opts.target_directory = "alternative";
cl_assert(!git_path_isdir("alternative"));
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_assert_equal_i(0, cts.n_untracked);
cl_assert_equal_i(0, cts.n_ignored);
cl_assert_equal_i(3, cts.n_updates);
/* files will have been filtered if needed, so strip CR */
check_file_contents_nocr("./alternative/README", "hey there\n");
check_file_contents_nocr("./alternative/branch_file.txt", "hi\nbye!\n");
check_file_contents_nocr("./alternative/new.txt", "my new file\n");
cl_git_pass(git_futils_rmdir_r(
"alternative", NULL, GIT_RMDIR_REMOVE_FILES));
git_object_free(head);
}
void test_checkout_index__can_get_repo_from_index(void)
{
git_index *index;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/branch_file.txt"));
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
opts.checkout_strategy = GIT_CHECKOUT_SAFE | GIT_CHECKOUT_RECREATE_MISSING;
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_checkout_index(NULL, index, &opts));
check_file_contents("./testrepo/README", "hey there\n");
check_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
check_file_contents("./testrepo/new.txt", "my new file\n");
git_index_free(index);
}
static void add_conflict(git_index *index, const char *path)
{
git_index_entry entry;
memset(&entry, 0, sizeof(git_index_entry));
entry.mode = 0100644;
entry.path = path;
git_oid_fromstr(&entry.id, "d427e0b2e138501a3d15cc376077a3631e15bd46");
GIT_INDEX_ENTRY_STAGE_SET(&entry, 1);
cl_git_pass(git_index_add(index, &entry));
git_oid_fromstr(&entry.id, "4e886e602529caa9ab11d71f86634bd1b6e0de10");
GIT_INDEX_ENTRY_STAGE_SET(&entry, 2);
cl_git_pass(git_index_add(index, &entry));
git_oid_fromstr(&entry.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863");
GIT_INDEX_ENTRY_STAGE_SET(&entry, 3);
cl_git_pass(git_index_add(index, &entry));
}
void test_checkout_index__writes_conflict_file(void)
{
git_index *index;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_buf conflicting_buf = GIT_BUF_INIT;
cl_git_pass(git_repository_index(&index, g_repo));
add_conflict(index, "conflicting.txt");
cl_git_pass(git_index_write(index));
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_git_pass(git_futils_readbuffer(&conflicting_buf, "testrepo/conflicting.txt"));
cl_assert(strcmp(conflicting_buf.ptr,
"<<<<<<< ours\n"
"this file is changed in master and branch\n"
"=======\n"
"this file is changed in branch and master\n"
">>>>>>> theirs\n") == 0);
git_buf_dispose(&conflicting_buf);
git_index_free(index);
}
void test_checkout_index__adding_conflict_removes_stage_0(void)
{
git_index *new_index, *index;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_git_pass(git_index_new(&new_index));
add_conflict(new_index, "new.txt");
cl_git_pass(git_checkout_index(g_repo, new_index, &opts));
cl_git_pass(git_repository_index(&index, g_repo));
cl_assert(git_index_get_bypath(index, "new.txt", 0) == NULL);
cl_assert(git_index_get_bypath(index, "new.txt", 1) != NULL);
cl_assert(git_index_get_bypath(index, "new.txt", 2) != NULL);
cl_assert(git_index_get_bypath(index, "new.txt", 3) != NULL);
git_index_free(index);
git_index_free(new_index);
}
void test_checkout_index__conflicts_honor_coreautocrlf(void)
{
#ifdef GIT_WIN32
git_index *index;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_buf conflicting_buf = GIT_BUF_INIT;
cl_git_pass(p_unlink("./testrepo/.gitattributes"));
cl_repo_set_bool(g_repo, "core.autocrlf", true);
cl_git_pass(git_repository_index(&index, g_repo));
add_conflict(index, "conflicting.txt");
cl_git_pass(git_index_write(index));
cl_git_pass(git_checkout_index(g_repo, NULL, &opts));
cl_git_pass(git_futils_readbuffer(&conflicting_buf, "testrepo/conflicting.txt"));
cl_assert(strcmp(conflicting_buf.ptr,
"<<<<<<< ours\r\n"
"this file is changed in master and branch\r\n"
"=======\r\n"
"this file is changed in branch and master\r\n"
">>>>>>> theirs\r\n") == 0);
git_buf_dispose(&conflicting_buf);
git_index_free(index);
#endif
}
+387
View File
@@ -0,0 +1,387 @@
#include "clar_libgit2.h"
#include "checkout_helpers.h"
#include "git2/checkout.h"
#include "repository.h"
#include "buffer.h"
#include "futils.h"
static const char *repo_name = "nasty";
static git_repository *repo;
static git_checkout_options checkout_opts;
void test_checkout_nasty__initialize(void)
{
repo = cl_git_sandbox_init(repo_name);
GIT_INIT_STRUCTURE(&checkout_opts, GIT_CHECKOUT_OPTIONS_VERSION);
checkout_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
}
void test_checkout_nasty__cleanup(void)
{
cl_git_sandbox_cleanup();
}
static void test_checkout_passes(const char *refname, const char *filename)
{
git_oid commit_id;
git_commit *commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_buf path = GIT_BUF_INIT;
cl_git_pass(git_buf_joinpath(&path, repo_name, filename));
cl_git_pass(git_reference_name_to_id(&commit_id, repo, refname));
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
opts.checkout_strategy = GIT_CHECKOUT_FORCE |
GIT_CHECKOUT_DONT_UPDATE_INDEX;
cl_git_pass(git_checkout_tree(repo, (const git_object *)commit, &opts));
cl_assert(!git_path_exists(path.ptr));
git_commit_free(commit);
git_buf_dispose(&path);
}
static void test_checkout_fails(const char *refname, const char *filename)
{
git_oid commit_id;
git_commit *commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
git_buf path = GIT_BUF_INIT;
cl_git_pass(git_buf_joinpath(&path, repo_name, filename));
cl_git_pass(git_reference_name_to_id(&commit_id, repo, refname));
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_fail(git_checkout_tree(repo, (const git_object *)commit, &opts));
cl_assert(!git_path_exists(path.ptr));
git_commit_free(commit);
git_buf_dispose(&path);
}
/* A tree that contains ".git" as a tree, with a blob inside
* (".git/foobar").
*/
void test_checkout_nasty__dotgit_tree(void)
{
test_checkout_fails("refs/heads/dotgit_tree", ".git/foobar");
}
/* A tree that contains ".GIT" as a tree, with a blob inside
* (".GIT/foobar").
*/
void test_checkout_nasty__dotcapitalgit_tree(void)
{
test_checkout_fails("refs/heads/dotcapitalgit_tree", ".GIT/foobar");
}
/* A tree that contains a tree ".", with a blob inside ("./foobar").
*/
void test_checkout_nasty__dot_tree(void)
{
test_checkout_fails("refs/heads/dot_tree", "foobar");
}
/* A tree that contains a tree ".", with a tree ".git", with a blob
* inside ("./.git/foobar").
*/
void test_checkout_nasty__dot_dotgit_tree(void)
{
test_checkout_fails("refs/heads/dot_dotgit_tree", ".git/foobar");
}
/* A tree that contains a tree, with a tree "..", with a tree ".git", with a
* blob inside ("foo/../.git/foobar").
*/
void test_checkout_nasty__dotdot_dotgit_tree(void)
{
test_checkout_fails("refs/heads/dotdot_dotgit_tree", ".git/foobar");
}
/* A tree that contains a tree, with a tree "..", with a blob inside
* ("foo/../foobar").
*/
void test_checkout_nasty__dotdot_tree(void)
{
test_checkout_fails("refs/heads/dotdot_tree", "foobar");
}
/* A tree that contains a blob with the rogue name ".git/foobar" */
void test_checkout_nasty__dotgit_path(void)
{
test_checkout_fails("refs/heads/dotgit_path", ".git/foobar");
}
/* A tree that contains a blob with the rogue name ".GIT/foobar" */
void test_checkout_nasty__dotcapitalgit_path(void)
{
test_checkout_fails("refs/heads/dotcapitalgit_path", ".GIT/foobar");
}
/* A tree that contains a blob with the rogue name "./.git/foobar" */
void test_checkout_nasty__dot_dotgit_path(void)
{
test_checkout_fails("refs/heads/dot_dotgit_path", ".git/foobar");
}
/* A tree that contains a blob with the rogue name "./.GIT/foobar" */
void test_checkout_nasty__dot_dotcapitalgit_path(void)
{
test_checkout_fails("refs/heads/dot_dotcapitalgit_path", ".GIT/foobar");
}
/* A tree that contains a blob with the rogue name "foo/../.git/foobar" */
void test_checkout_nasty__dotdot_dotgit_path(void)
{
test_checkout_fails("refs/heads/dotdot_dotgit_path", ".git/foobar");
}
/* A tree that contains a blob with the rogue name "foo/../.GIT/foobar" */
void test_checkout_nasty__dotdot_dotcapitalgit_path(void)
{
test_checkout_fails("refs/heads/dotdot_dotcapitalgit_path", ".GIT/foobar");
}
/* A tree that contains a blob with the rogue name "foo/." */
void test_checkout_nasty__dot_path(void)
{
test_checkout_fails("refs/heads/dot_path", "./foobar");
}
/* A tree that contains a blob with the rogue name "foo/." */
void test_checkout_nasty__dot_path_two(void)
{
test_checkout_fails("refs/heads/dot_path_two", "foo/.");
}
/* A tree that contains a blob with the rogue name "foo/../foobar" */
void test_checkout_nasty__dotdot_path(void)
{
test_checkout_fails("refs/heads/dotdot_path", "foobar");
}
/* A tree that contains an entry with a backslash ".git\foobar" */
void test_checkout_nasty__dotgit_backslash_path(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dotgit_backslash_path", ".git/foobar");
#endif
}
/* A tree that contains an entry with a backslash ".GIT\foobar" */
void test_checkout_nasty__dotcapitalgit_backslash_path(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dotcapitalgit_backslash_path", ".GIT/foobar");
#endif
}
/* A tree that contains an entry with a backslash ".\.GIT\foobar" */
void test_checkout_nasty__dot_backslash_dotcapitalgit_path(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dot_backslash_dotcapitalgit_path", ".GIT/foobar");
#endif
}
/* A tree that contains an entry ".git.", because Win32 APIs will drop the
* trailing slash.
*/
void test_checkout_nasty__dot_git_dot(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dot_git_dot", ".git/foobar");
#endif
}
/* A tree that contains an entry "git~1", because that is typically the
* short name for ".git".
*/
void test_checkout_nasty__git_tilde1(void)
{
test_checkout_fails("refs/heads/git_tilde1", ".git/foobar");
test_checkout_fails("refs/heads/git_tilde1", "git~1/foobar");
}
/* A tree that contains an entry "git~2", when we have forced the short
* name for ".git" into "GIT~2".
*/
void test_checkout_nasty__git_custom_shortname(void)
{
#ifdef GIT_WIN32
if (!cl_sandbox_supports_8dot3())
clar__skip();
cl_must_pass(p_rename("nasty/.git", "nasty/_temp"));
cl_git_write2file("nasty/git~1", "", 0, O_RDWR|O_CREAT, 0666);
cl_must_pass(p_rename("nasty/_temp", "nasty/.git"));
test_checkout_fails("refs/heads/git_tilde2", ".git/foobar");
#endif
}
/* A tree that contains an entry "git~3", which should be allowed, since
* it is not the typical short name ("GIT~1") or the actual short name
* ("GIT~2") for ".git".
*/
void test_checkout_nasty__only_looks_like_a_git_shortname(void)
{
#ifdef GIT_WIN32
git_oid commit_id;
git_commit *commit;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
cl_must_pass(p_rename("nasty/.git", "nasty/_temp"));
cl_git_write2file("nasty/git~1", "", 0, O_RDWR|O_CREAT, 0666);
cl_must_pass(p_rename("nasty/_temp", "nasty/.git"));
cl_git_pass(git_reference_name_to_id(&commit_id, repo, "refs/heads/git_tilde3"));
cl_git_pass(git_commit_lookup(&commit, repo, &commit_id));
opts.checkout_strategy = GIT_CHECKOUT_FORCE;
cl_git_pass(git_checkout_tree(repo, (const git_object *)commit, &opts));
cl_assert(git_path_exists("nasty/git~3/foobar"));
git_commit_free(commit);
#endif
}
/* A tree that contains an entry "git:", because Win32 APIs will reject
* that as looking too similar to a drive letter.
*/
void test_checkout_nasty__dot_git_colon(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dot_git_colon", ".git/foobar");
#endif
}
/* A tree that contains an entry "git:foo", because Win32 APIs will turn
* that into ".git".
*/
void test_checkout_nasty__dot_git_colon_stuff(void)
{
#ifdef GIT_WIN32
test_checkout_fails("refs/heads/dot_git_colon_stuff", ".git/foobar");
#endif
}
/* A tree that contains an entry ".git::$INDEX_ALLOCATION" because NTFS
* will interpret that as a synonym to ".git", even when mounted via SMB
* on macOS.
*/
void test_checkout_nasty__dotgit_alternate_data_stream(void)
{
test_checkout_fails("refs/heads/dotgit_alternate_data_stream", ".git/dummy-file");
test_checkout_fails("refs/heads/dotgit_alternate_data_stream", ".git::$INDEX_ALLOCATION/dummy-file");
}
/* Trees that contains entries with a tree ".git" that contain
* byte sequences:
* { 0xe2, 0x80, 0x8c }
* { 0xe2, 0x80, 0x8d }
* { 0xe2, 0x80, 0x8e }
* { 0xe2, 0x80, 0x8f }
* { 0xe2, 0x80, 0xaa }
* { 0xe2, 0x80, 0xab }
* { 0xe2, 0x80, 0xac }
* { 0xe2, 0x80, 0xad }
* { 0xe2, 0x81, 0xae }
* { 0xe2, 0x81, 0xaa }
* { 0xe2, 0x81, 0xab }
* { 0xe2, 0x81, 0xac }
* { 0xe2, 0x81, 0xad }
* { 0xe2, 0x81, 0xae }
* { 0xe2, 0x81, 0xaf }
* { 0xef, 0xbb, 0xbf }
* Because these map to characters that HFS filesystems "ignore". Thus
* ".git<U+200C>" will map to ".git".
*/
void test_checkout_nasty__dot_git_hfs_ignorable(void)
{
#ifdef __APPLE__
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_1", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_2", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_3", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_4", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_5", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_6", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_7", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_8", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_9", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_10", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_11", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_12", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_13", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_14", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_15", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_16", ".git/foobar");
#endif
}
void test_checkout_nasty__honors_core_protecthfs(void)
{
cl_repo_set_bool(repo, "core.protectHFS", true);
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_1", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_2", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_3", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_4", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_5", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_6", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_7", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_8", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_9", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_10", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_11", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_12", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_13", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_14", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_15", ".git/foobar");
test_checkout_fails("refs/heads/dotgit_hfs_ignorable_16", ".git/foobar");
}
void test_checkout_nasty__honors_core_protectntfs(void)
{
cl_repo_set_bool(repo, "core.protectNTFS", true);
test_checkout_fails("refs/heads/dotgit_backslash_path", ".git/foobar");
test_checkout_fails("refs/heads/dotcapitalgit_backslash_path", ".GIT/foobar");
test_checkout_fails("refs/heads/dot_git_dot", ".git/foobar");
test_checkout_fails("refs/heads/git_tilde1", ".git/foobar");
}
void test_checkout_nasty__symlink1(void)
{
test_checkout_passes("refs/heads/symlink1", ".git/foobar");
}
void test_checkout_nasty__symlink2(void)
{
test_checkout_passes("refs/heads/symlink2", ".git/foobar");
}
void test_checkout_nasty__symlink3(void)
{
test_checkout_passes("refs/heads/symlink3", ".git/foobar");
}
void test_checkout_nasty__gitmodules_symlink(void)
{
cl_repo_set_bool(repo, "core.protectHFS", true);
test_checkout_fails("refs/heads/gitmodules-symlink", ".gitmodules");
cl_repo_set_bool(repo, "core.protectHFS", false);
cl_repo_set_bool(repo, "core.protectNTFS", true);
test_checkout_fails("refs/heads/gitmodules-symlink", ".gitmodules");
cl_repo_set_bool(repo, "core.protectNTFS", false);
test_checkout_fails("refs/heads/gitmodules-symlink", ".gitmodules");
}
File diff suppressed because it is too large Load Diff
+335
View File
@@ -0,0 +1,335 @@
#include "clar_libgit2.h"
#include "diff_generate.h"
#include "git2/checkout.h"
#include "path.h"
#include "posix.h"
#include "futils.h"
static git_repository *g_repo = NULL;
/*
From the test repo used for this test:
--------------------------------------
This is a test repo for libgit2 where tree entries have type changes
The key types that could be found in tree entries are:
1 - GIT_FILEMODE_NEW = 0000000
2 - GIT_FILEMODE_TREE = 0040000
3 - GIT_FILEMODE_BLOB = 0100644
4 - GIT_FILEMODE_BLOB_EXECUTABLE = 0100755
5 - GIT_FILEMODE_LINK = 0120000
6 - GIT_FILEMODE_COMMIT = 0160000
I will try to have every type of transition somewhere in the history
of this repo.
Commits
-------
Initial commit - a(1) b(1) c(1) d(1) e(1)
Create content - a(1->2) b(1->3) c(1->4) d(1->5) e(1->6)
Changes #1 - a(2->3) b(3->4) c(4->5) d(5->6) e(6->2)
Changes #2 - a(3->5) b(4->6) c(5->2) d(6->3) e(2->4)
Changes #3 - a(5->3) b(6->4) c(2->5) d(3->6) e(4->2)
Changes #4 - a(3->2) b(4->3) c(5->4) d(6->5) e(2->6)
Changes #5 - a(2->1) b(3->1) c(4->1) d(5->1) e(6->1)
*/
static const char *g_typechange_oids[] = {
"79b9f23e85f55ea36a472a902e875bc1121a94cb",
"9bdb75b73836a99e3dbeea640a81de81031fdc29",
"0e7ed140b514b8cae23254cb8656fe1674403aff",
"9d0235c7a7edc0889a18f97a42ee6db9fe688447",
"9b19edf33a03a0c59cdfc113bfa5c06179bf9b1a",
"1b63caae4a5ca96f78e8dfefc376c6a39a142475",
"6eae26c90e8ccc4d16208972119c40635489c6f0",
NULL
};
static bool g_typechange_empty[] = {
true, false, false, false, false, false, true, true
};
static const int g_typechange_expected_conflicts[] = {
1, 2, 3, 3, 2, 3, 2
};
static const int g_typechange_expected_untracked[] = {
6, 4, 3, 2, 3, 2, 5
};
void test_checkout_typechange__initialize(void)
{
g_repo = cl_git_sandbox_init("typechanges");
cl_fixture_sandbox("submod2_target");
p_rename("submod2_target/.gitted", "submod2_target/.git");
}
void test_checkout_typechange__cleanup(void)
{
cl_git_sandbox_cleanup();
cl_fixture_cleanup("submod2_target");
}
static void assert_file_exists(const char *path)
{
cl_assert_(git_path_isfile(path), path);
}
static void assert_dir_exists(const char *path)
{
cl_assert_(git_path_isdir(path), path);
}
static void assert_workdir_matches_tree(
git_repository *repo, const git_oid *id, const char *root, bool recurse)
{
git_object *obj;
git_tree *tree;
size_t i, max_i;
git_buf path = GIT_BUF_INIT;
if (!root)
root = git_repository_workdir(repo);
cl_assert(root);
cl_git_pass(git_object_lookup(&obj, repo, id, GIT_OBJECT_ANY));
cl_git_pass(git_object_peel((git_object **)&tree, obj, GIT_OBJECT_TREE));
git_object_free(obj);
max_i = git_tree_entrycount(tree);
for (i = 0; i < max_i; ++i) {
const git_tree_entry *te = git_tree_entry_byindex(tree, i);
cl_assert(te);
cl_git_pass(git_buf_joinpath(&path, root, git_tree_entry_name(te)));
switch (git_tree_entry_type(te)) {
case GIT_OBJECT_COMMIT:
assert_dir_exists(path.ptr);
break;
case GIT_OBJECT_TREE:
assert_dir_exists(path.ptr);
if (recurse)
assert_workdir_matches_tree(
repo, git_tree_entry_id(te), path.ptr, true);
break;
case GIT_OBJECT_BLOB:
switch (git_tree_entry_filemode(te)) {
case GIT_FILEMODE_BLOB:
case GIT_FILEMODE_BLOB_EXECUTABLE:
assert_file_exists(path.ptr);
/* because of cross-platform, don't confirm exec bit yet */
break;
case GIT_FILEMODE_LINK:
cl_assert_(git_path_exists(path.ptr), path.ptr);
/* because of cross-platform, don't confirm link yet */
break;
default:
cl_assert(false); /* really?! */
}
break;
default:
cl_assert(false); /* really?!! */
}
}
git_tree_free(tree);
git_buf_dispose(&path);
}
void test_checkout_typechange__checkout_typechanges_safe(void)
{
int i;
git_object *obj;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
for (i = 0; g_typechange_oids[i] != NULL; ++i) {
cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i]));
opts.checkout_strategy = !i ? GIT_CHECKOUT_FORCE : GIT_CHECKOUT_SAFE;
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_git_pass(
git_repository_set_head_detached(g_repo, git_object_id(obj)));
assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true);
git_object_free(obj);
if (!g_typechange_empty[i]) {
cl_assert(git_path_isdir("typechanges"));
cl_assert(git_path_exists("typechanges/a"));
cl_assert(git_path_exists("typechanges/b"));
cl_assert(git_path_exists("typechanges/c"));
cl_assert(git_path_exists("typechanges/d"));
cl_assert(git_path_exists("typechanges/e"));
} else {
cl_assert(git_path_isdir("typechanges"));
cl_assert(!git_path_exists("typechanges/a"));
cl_assert(!git_path_exists("typechanges/b"));
cl_assert(!git_path_exists("typechanges/c"));
cl_assert(!git_path_exists("typechanges/d"));
cl_assert(!git_path_exists("typechanges/e"));
}
}
}
typedef struct {
int conflicts;
int dirty;
int updates;
int untracked;
int ignored;
} notify_counts;
static int notify_counter(
git_checkout_notify_t why,
const char *path,
const git_diff_file *baseline,
const git_diff_file *target,
const git_diff_file *workdir,
void *payload)
{
notify_counts *cts = payload;
GIT_UNUSED(path);
GIT_UNUSED(baseline);
GIT_UNUSED(target);
GIT_UNUSED(workdir);
switch (why) {
case GIT_CHECKOUT_NOTIFY_CONFLICT: cts->conflicts++; break;
case GIT_CHECKOUT_NOTIFY_DIRTY: cts->dirty++; break;
case GIT_CHECKOUT_NOTIFY_UPDATED: cts->updates++; break;
case GIT_CHECKOUT_NOTIFY_UNTRACKED: cts->untracked++; break;
case GIT_CHECKOUT_NOTIFY_IGNORED: cts->ignored++; break;
default: break;
}
return 0;
}
static void force_create_file(const char *file)
{
int error = git_futils_rmdir_r(file, NULL,
GIT_RMDIR_REMOVE_FILES | GIT_RMDIR_REMOVE_BLOCKERS);
cl_assert(!error || error == GIT_ENOTFOUND);
cl_git_pass(git_futils_mkpath2file(file, 0777));
cl_git_rewritefile(file, "yowza!!");
}
static int make_submodule_dirty(git_submodule *sm, const char *name, void *payload)
{
git_buf submodulepath = GIT_BUF_INIT;
git_buf dirtypath = GIT_BUF_INIT;
git_repository *submodule_repo;
GIT_UNUSED(name);
GIT_UNUSED(payload);
/* remove submodule directory in preparation for init and repo_init */
cl_git_pass(git_buf_joinpath(
&submodulepath,
git_repository_workdir(g_repo),
git_submodule_path(sm)
));
git_futils_rmdir_r(git_buf_cstr(&submodulepath), NULL, GIT_RMDIR_REMOVE_FILES);
/* initialize submodule's repository */
cl_git_pass(git_submodule_repo_init(&submodule_repo, sm, 0));
/* create a file in the submodule workdir to make it dirty */
cl_git_pass(
git_buf_joinpath(&dirtypath, git_repository_workdir(submodule_repo), "dirty"));
force_create_file(git_buf_cstr(&dirtypath));
git_buf_dispose(&dirtypath);
git_buf_dispose(&submodulepath);
git_repository_free(submodule_repo);
return 0;
}
void test_checkout_typechange__checkout_with_conflicts(void)
{
int i;
git_object *obj;
git_checkout_options opts = GIT_CHECKOUT_OPTIONS_INIT;
notify_counts cts = {0};
opts.notify_flags =
GIT_CHECKOUT_NOTIFY_CONFLICT | GIT_CHECKOUT_NOTIFY_UNTRACKED;
opts.notify_cb = notify_counter;
opts.notify_payload = &cts;
for (i = 0; g_typechange_oids[i] != NULL; ++i) {
cl_git_pass(git_revparse_single(&obj, g_repo, g_typechange_oids[i]));
force_create_file("typechanges/a/blocker");
force_create_file("typechanges/b");
force_create_file("typechanges/c/sub/sub/file");
git_futils_rmdir_r("typechanges/d", NULL, GIT_RMDIR_REMOVE_FILES);
p_mkdir("typechanges/d", 0777); /* intentionally empty dir */
force_create_file("typechanges/untracked");
cl_git_pass(git_submodule_foreach(g_repo, make_submodule_dirty, NULL));
opts.checkout_strategy = GIT_CHECKOUT_SAFE;
memset(&cts, 0, sizeof(cts));
cl_git_fail(git_checkout_tree(g_repo, obj, &opts));
cl_assert_equal_i(cts.conflicts, g_typechange_expected_conflicts[i]);
cl_assert_equal_i(cts.untracked, g_typechange_expected_untracked[i]);
cl_assert_equal_i(cts.dirty, 0);
cl_assert_equal_i(cts.updates, 0);
cl_assert_equal_i(cts.ignored, 0);
opts.checkout_strategy =
GIT_CHECKOUT_FORCE | GIT_CHECKOUT_REMOVE_UNTRACKED;
memset(&cts, 0, sizeof(cts));
cl_assert(git_path_exists("typechanges/untracked"));
cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
cl_assert_equal_i(0, cts.conflicts);
cl_assert(!git_path_exists("typechanges/untracked"));
cl_git_pass(
git_repository_set_head_detached(g_repo, git_object_id(obj)));
assert_workdir_matches_tree(g_repo, git_object_id(obj), NULL, true);
git_object_free(obj);
}
}
void test_checkout_typechange__status_char(void)
{
size_t i;
git_oid oid;
git_commit *commit;
git_diff *diff;
const git_diff_delta *delta;
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
char expected[8] = {'M', 'M', 'R', 'T', 'D', 'R', 'A', 'R'};
git_oid_fromstr(&oid, "9b19edf33a03a0c59cdfc113bfa5c06179bf9b1a");
cl_git_pass(git_commit_lookup(&commit, g_repo, &oid));
diffopts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE;
cl_git_pass(git_diff__commit(&diff, g_repo, commit, &diffopts));
cl_git_pass(git_diff_find_similar(diff, NULL));
for (i = 0; i < git_diff_num_deltas(diff); i++) {
delta = git_diff_get_delta(diff, i);
cl_assert_equal_i(expected[i], git_diff_status_char(delta->status));
}
git_diff_free(diff);
git_commit_free(commit);
}