mirror of
https://git.proxmox.com/git/libgit2
synced 2025-09-19 05:45:44 +00:00
checkout: segregate checkout strategies
This commit is contained in:
parent
020cda99c2
commit
c214fa1caf
@ -21,13 +21,16 @@
|
|||||||
*/
|
*/
|
||||||
GIT_BEGIN_DECL
|
GIT_BEGIN_DECL
|
||||||
|
|
||||||
|
enum {
|
||||||
#define GIT_CHECKOUT_OVERWRITE_EXISTING 0 /* default */
|
GIT_CHECKOUT_DEFAULT = (1 << 0),
|
||||||
#define GIT_CHECKOUT_SKIP_EXISTING 1
|
GIT_CHECKOUT_OVERWRITE_MODIFIED = (1 << 1),
|
||||||
|
GIT_CHECKOUT_CREATE_MISSING = (1 << 2),
|
||||||
|
GIT_CHECKOUT_REMOVE_UNTRACKED = (1 << 3),
|
||||||
|
};
|
||||||
|
|
||||||
/* Use zeros to indicate default settings */
|
/* Use zeros to indicate default settings */
|
||||||
typedef struct git_checkout_opts {
|
typedef struct git_checkout_opts {
|
||||||
int existing_file_action; /* default: GIT_CHECKOUT_OVERWRITE_EXISTING */
|
unsigned int checkout_strategy; /* default: GIT_CHECKOUT_DEFAULT */
|
||||||
int disable_filters;
|
int disable_filters;
|
||||||
int dir_mode; /* default is 0755 */
|
int dir_mode; /* default is 0755 */
|
||||||
int file_mode; /* default is 0644 */
|
int file_mode; /* default is 0644 */
|
||||||
|
@ -143,6 +143,9 @@ static int checkout_diff_fn(
|
|||||||
|
|
||||||
switch (delta->status) {
|
switch (delta->status) {
|
||||||
case GIT_DELTA_UNTRACKED:
|
case GIT_DELTA_UNTRACKED:
|
||||||
|
if (!(data->checkout_opts->checkout_strategy & GIT_CHECKOUT_REMOVE_UNTRACKED))
|
||||||
|
return 0;
|
||||||
|
|
||||||
if (!git__suffixcmp(delta->new_file.path, "/"))
|
if (!git__suffixcmp(delta->new_file.path, "/"))
|
||||||
error = git_futils_rmdir_r(git_buf_cstr(data->path), GIT_DIRREMOVAL_FILES_AND_DIRS);
|
error = git_futils_rmdir_r(git_buf_cstr(data->path), GIT_DIRREMOVAL_FILES_AND_DIRS);
|
||||||
else
|
else
|
||||||
@ -150,11 +153,24 @@ static int checkout_diff_fn(
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case GIT_DELTA_MODIFIED:
|
case GIT_DELTA_MODIFIED:
|
||||||
/* Deal with pre-existing files */
|
if (!(data->checkout_opts->checkout_strategy & GIT_CHECKOUT_OVERWRITE_MODIFIED))
|
||||||
if (data->checkout_opts->existing_file_action == GIT_CHECKOUT_SKIP_EXISTING)
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
if (checkout_blob(
|
||||||
|
data->owner,
|
||||||
|
&delta->old_file.oid,
|
||||||
|
git_buf_cstr(data->path),
|
||||||
|
delta->old_file.mode,
|
||||||
|
data->can_symlink,
|
||||||
|
data->checkout_opts) < 0)
|
||||||
|
goto cleanup;
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
case GIT_DELTA_DELETED:
|
case GIT_DELTA_DELETED:
|
||||||
|
if (!(data->checkout_opts->checkout_strategy & GIT_CHECKOUT_CREATE_MISSING))
|
||||||
|
return 0;
|
||||||
|
|
||||||
if (checkout_blob(
|
if (checkout_blob(
|
||||||
data->owner,
|
data->owner,
|
||||||
&delta->old_file.oid,
|
&delta->old_file.oid,
|
||||||
@ -209,8 +225,8 @@ static void normalize_options(git_checkout_opts *normalized, git_checkout_opts *
|
|||||||
memmove(normalized, proposed, sizeof(git_checkout_opts));
|
memmove(normalized, proposed, sizeof(git_checkout_opts));
|
||||||
|
|
||||||
/* Default options */
|
/* Default options */
|
||||||
if (!normalized->existing_file_action)
|
if (!normalized->checkout_strategy)
|
||||||
normalized->existing_file_action = GIT_CHECKOUT_OVERWRITE_EXISTING;
|
normalized->checkout_strategy = GIT_CHECKOUT_DEFAULT;
|
||||||
|
|
||||||
/* opts->disable_filters is false by default */
|
/* opts->disable_filters is false by default */
|
||||||
if (!normalized->dir_mode)
|
if (!normalized->dir_mode)
|
||||||
|
@ -28,6 +28,7 @@ int git_reset(
|
|||||||
git_index *index = NULL;
|
git_index *index = NULL;
|
||||||
git_tree *tree = NULL;
|
git_tree *tree = NULL;
|
||||||
int error = -1;
|
int error = -1;
|
||||||
|
git_checkout_opts opts;
|
||||||
|
|
||||||
assert(repo && target);
|
assert(repo && target);
|
||||||
assert(reset_type == GIT_RESET_SOFT
|
assert(reset_type == GIT_RESET_SOFT
|
||||||
@ -81,7 +82,13 @@ int git_reset(
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (git_checkout_index(repo, NULL, NULL, NULL) < 0) {
|
memset(&opts, 0, sizeof(opts));
|
||||||
|
opts.checkout_strategy =
|
||||||
|
GIT_CHECKOUT_CREATE_MISSING
|
||||||
|
| GIT_CHECKOUT_OVERWRITE_MODIFIED
|
||||||
|
| GIT_CHECKOUT_REMOVE_UNTRACKED;
|
||||||
|
|
||||||
|
if (git_checkout_index(repo, &opts, NULL) < 0) {
|
||||||
giterr_set(GITERR_INDEX, "%s - Failed to checkout the index.", ERROR_MSG);
|
giterr_set(GITERR_INDEX, "%s - Failed to checkout the index.", ERROR_MSG);
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ void test_checkout_index__initialize(void)
|
|||||||
git_tree *tree;
|
git_tree *tree;
|
||||||
|
|
||||||
memset(&g_opts, 0, sizeof(g_opts));
|
memset(&g_opts, 0, sizeof(g_opts));
|
||||||
|
g_opts.checkout_strategy = GIT_CHECKOUT_CREATE_MISSING;
|
||||||
|
|
||||||
g_repo = cl_git_sandbox_init("testrepo");
|
g_repo = cl_git_sandbox_init("testrepo");
|
||||||
|
|
||||||
@ -71,19 +72,34 @@ void test_checkout_index__cannot_checkout_a_bare_repository(void)
|
|||||||
cl_git_fail(git_checkout_index(g_repo, NULL, NULL));
|
cl_git_fail(git_checkout_index(g_repo, NULL, NULL));
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_checkout_index__update_the_content_of_workdir_with_missing_files(void)
|
void test_checkout_index__can_create_missing_files(void)
|
||||||
{
|
{
|
||||||
cl_assert_equal_i(false, git_path_isfile("./testrepo/README"));
|
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/branch_file.txt"));
|
||||||
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
|
cl_assert_equal_i(false, git_path_isfile("./testrepo/new.txt"));
|
||||||
|
|
||||||
cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
|
g_opts.checkout_strategy = GIT_CHECKOUT_CREATE_MISSING;
|
||||||
|
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
||||||
|
|
||||||
test_file_contents("./testrepo/README", "hey there\n");
|
test_file_contents("./testrepo/README", "hey there\n");
|
||||||
test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
|
test_file_contents("./testrepo/branch_file.txt", "hi\nbye!\n");
|
||||||
test_file_contents("./testrepo/new.txt", "my new file\n");
|
test_file_contents("./testrepo/new.txt", "my new file\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test_checkout_index__can_remove_untracked_files(void)
|
||||||
|
{
|
||||||
|
git_futils_mkdir("./testrepo/dir/subdir/subsubdir", NULL, 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"));
|
||||||
|
|
||||||
|
g_opts.checkout_strategy = GIT_CHECKOUT_REMOVE_UNTRACKED;
|
||||||
|
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
||||||
|
|
||||||
|
cl_assert_equal_i(false, git_path_isdir("./testrepo/dir"));
|
||||||
|
}
|
||||||
|
|
||||||
void test_checkout_index__honor_the_specified_pathspecs(void)
|
void test_checkout_index__honor_the_specified_pathspecs(void)
|
||||||
{
|
{
|
||||||
git_strarray paths;
|
git_strarray paths;
|
||||||
@ -128,7 +144,7 @@ void test_checkout_index__honor_the_gitattributes_directives(void)
|
|||||||
cl_git_mkfile("./testrepo/.gitattributes", attributes);
|
cl_git_mkfile("./testrepo/.gitattributes", attributes);
|
||||||
set_core_autocrlf_to(false);
|
set_core_autocrlf_to(false);
|
||||||
|
|
||||||
cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
|
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
||||||
|
|
||||||
test_file_contents("./testrepo/README", "hey there\n");
|
test_file_contents("./testrepo/README", "hey there\n");
|
||||||
test_file_contents("./testrepo/new.txt", "my new file\n");
|
test_file_contents("./testrepo/new.txt", "my new file\n");
|
||||||
@ -143,7 +159,7 @@ void test_checkout_index__honor_coreautocrlf_setting_set_to_true(void)
|
|||||||
cl_git_pass(p_unlink("./testrepo/.gitattributes"));
|
cl_git_pass(p_unlink("./testrepo/.gitattributes"));
|
||||||
set_core_autocrlf_to(true);
|
set_core_autocrlf_to(true);
|
||||||
|
|
||||||
cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
|
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
||||||
|
|
||||||
test_file_contents("./testrepo/README", expected_readme_text);
|
test_file_contents("./testrepo/README", expected_readme_text);
|
||||||
#endif
|
#endif
|
||||||
@ -158,7 +174,7 @@ void test_checkout_index__honor_coresymlinks_setting_set_to_true(void)
|
|||||||
{
|
{
|
||||||
set_repo_symlink_handling_cap_to(true);
|
set_repo_symlink_handling_cap_to(true);
|
||||||
|
|
||||||
cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
|
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
||||||
|
|
||||||
#ifdef GIT_WIN32
|
#ifdef GIT_WIN32
|
||||||
test_file_contents("./testrepo/link_to_new.txt", "new.txt");
|
test_file_contents("./testrepo/link_to_new.txt", "new.txt");
|
||||||
@ -180,26 +196,26 @@ void test_checkout_index__honor_coresymlinks_setting_set_to_false(void)
|
|||||||
{
|
{
|
||||||
set_repo_symlink_handling_cap_to(false);
|
set_repo_symlink_handling_cap_to(false);
|
||||||
|
|
||||||
cl_git_pass(git_checkout_index(g_repo, NULL, NULL));
|
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
||||||
|
|
||||||
test_file_contents("./testrepo/link_to_new.txt", "new.txt");
|
test_file_contents("./testrepo/link_to_new.txt", "new.txt");
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_checkout_index__options_skip_existing_file(void)
|
void test_checkout_index__donot_overwrite_modified_file_by_default(void)
|
||||||
{
|
{
|
||||||
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
|
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
|
||||||
g_opts.existing_file_action = GIT_CHECKOUT_SKIP_EXISTING;
|
|
||||||
|
|
||||||
|
g_opts.checkout_strategy = 0;
|
||||||
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
||||||
|
|
||||||
test_file_contents("./testrepo/new.txt", "This isn't what's stored!");
|
test_file_contents("./testrepo/new.txt", "This isn't what's stored!");
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_checkout_index__options_overwrite_existing_file(void)
|
void test_checkout_index__can_overwrite_modified_file(void)
|
||||||
{
|
{
|
||||||
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
|
cl_git_mkfile("./testrepo/new.txt", "This isn't what's stored!");
|
||||||
g_opts.existing_file_action = GIT_CHECKOUT_OVERWRITE_EXISTING;
|
|
||||||
|
|
||||||
|
g_opts.checkout_strategy = GIT_CHECKOUT_OVERWRITE_MODIFIED;
|
||||||
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
||||||
|
|
||||||
test_file_contents("./testrepo/new.txt", "my new file\n");
|
test_file_contents("./testrepo/new.txt", "my new file\n");
|
||||||
@ -267,6 +283,8 @@ void test_checkout_index__options_open_flags(void)
|
|||||||
cl_git_mkfile("./testrepo/new.txt", "hi\n");
|
cl_git_mkfile("./testrepo/new.txt", "hi\n");
|
||||||
|
|
||||||
g_opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
|
g_opts.file_open_flags = O_CREAT | O_RDWR | O_APPEND;
|
||||||
|
|
||||||
|
g_opts.checkout_strategy |= GIT_CHECKOUT_OVERWRITE_MODIFIED;
|
||||||
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
cl_git_pass(git_checkout_index(g_repo, &g_opts, NULL));
|
||||||
|
|
||||||
test_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
|
test_file_contents("./testrepo/new.txt", "hi\nmy new file\n");
|
||||||
|
Loading…
Reference in New Issue
Block a user