Merge pull request #1665 from arrbee/checkout-target-directory Add target directory to checkout options
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
diff --git a/include/git2/checkout.h b/include/git2/checkout.h
index f49e875..a086408 100644
--- a/include/git2/checkout.h
+++ b/include/git2/checkout.h
@@ -236,6 +236,8 @@ typedef struct git_checkout_opts {
git_strarray paths;
git_tree *baseline; /** expected content of workdir, defaults to HEAD */
+
+ const char *target_directory; /** alternative checkout path to workdir */
} git_checkout_opts;
#define GIT_CHECKOUT_OPTS_VERSION 1
diff --git a/src/checkout.c b/src/checkout.c
index 065bb50..8f9ec64 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -858,7 +858,7 @@ static int checkout_submodule(
return 0;
if ((error = git_futils_mkdir(
- file->path, git_repository_workdir(data->repo),
+ file->path, data->opts.target_directory,
data->opts.dir_mode, GIT_MKDIR_PATH)) < 0)
return error;
@@ -1030,7 +1030,7 @@ static int checkout_deferred_remove(git_repository *repo, const char *path)
{
#if 0
int error = git_futils_rmdir_r(
- path, git_repository_workdir(repo), GIT_RMDIR_EMPTY_PARENTS);
+ path, data->opts.target_directory, GIT_RMDIR_EMPTY_PARENTS);
if (error == GIT_ENOTFOUND) {
error = 0;
@@ -1163,7 +1163,8 @@ static int checkout_data_init(
return -1;
}
- if ((error = git_repository__ensure_not_bare(repo, "checkout")) < 0)
+ if ((!proposed || !proposed->target_directory) &&
+ (error = git_repository__ensure_not_bare(repo, "checkout")) < 0)
return error;
data->repo = repo;
@@ -1176,6 +1177,13 @@ static int checkout_data_init(
else
memmove(&data->opts, proposed, sizeof(git_checkout_opts));
+ if (!data->opts.target_directory)
+ data->opts.target_directory = git_repository_workdir(repo);
+ else if (!git_path_isdir(data->opts.target_directory) &&
+ (error = git_futils_mkdir(data->opts.target_directory, NULL,
+ GIT_DIR_MODE, GIT_MKDIR_VERIFY_DIR)) < 0)
+ goto cleanup;
+
/* refresh config and index content unless NO_REFRESH is given */
if ((data->opts.checkout_strategy & GIT_CHECKOUT_NO_REFRESH) == 0) {
git_config *cfg;
@@ -1238,7 +1246,8 @@ static int checkout_data_init(
if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 ||
(error = git_pool_init(&data->pool, 1, 0)) < 0 ||
- (error = git_buf_puts(&data->path, git_repository_workdir(repo))) < 0)
+ (error = git_buf_puts(&data->path, data->opts.target_directory)) < 0 ||
+ (error = git_path_to_dir(&data->path)) < 0)
goto cleanup;
data->workdir_len = git_buf_len(&data->path);
@@ -1286,11 +1295,13 @@ int git_checkout_iterator(
GIT_ITERATOR_IGNORE_CASE : GIT_ITERATOR_DONT_IGNORE_CASE;
if ((error = git_iterator_reset(target, data.pfx, data.pfx)) < 0 ||
- (error = git_iterator_for_workdir(
- &workdir, data.repo, iterflags | GIT_ITERATOR_DONT_AUTOEXPAND,
+ (error = git_iterator_for_workdir_ext(
+ &workdir, data.repo, data.opts.target_directory,
+ iterflags | GIT_ITERATOR_DONT_AUTOEXPAND,
data.pfx, data.pfx)) < 0 ||
(error = git_iterator_for_tree(
- &baseline, data.opts.baseline, iterflags, data.pfx, data.pfx)) < 0)
+ &baseline, data.opts.baseline,
+ iterflags, data.pfx, data.pfx)) < 0)
goto cleanup;
/* Should not have case insensitivity mismatch */
@@ -1358,8 +1369,19 @@ int git_checkout_index(
int error;
git_iterator *index_i;
- if ((error = git_repository__ensure_not_bare(repo, "checkout index")) < 0)
- return error;
+ if (!index && !repo) {
+ giterr_set(GITERR_CHECKOUT,
+ "Must provide either repository or index to checkout");
+ return -1;
+ }
+ if (index && repo && git_index_owner(index) != repo) {
+ giterr_set(GITERR_CHECKOUT,
+ "Index to checkout does not match repository");
+ return -1;
+ }
+
+ if (!repo)
+ repo = git_index_owner(index);
if (!index && (error = git_repository_index__weakptr(&index, repo)) < 0)
return error;
@@ -1383,8 +1405,19 @@ int git_checkout_tree(
git_tree *tree = NULL;
git_iterator *tree_i = NULL;
- if ((error = git_repository__ensure_not_bare(repo, "checkout tree")) < 0)
- return error;
+ if (!treeish && !repo) {
+ giterr_set(GITERR_CHECKOUT,
+ "Must provide either repository or tree to checkout");
+ return -1;
+ }
+ if (treeish && repo && git_object_owner(treeish) != repo) {
+ giterr_set(GITERR_CHECKOUT,
+ "Object to checkout does not match repository");
+ return -1;
+ }
+
+ if (!repo)
+ repo = git_object_owner(treeish);
if (git_object_peel((git_object **)&tree, treeish, GIT_OBJ_TREE) < 0) {
giterr_set(
@@ -1409,8 +1442,7 @@ int git_checkout_head(
git_tree *head = NULL;
git_iterator *head_i = NULL;
- if ((error = git_repository__ensure_not_bare(repo, "checkout head")) < 0)
- return error;
+ assert(repo);
if (!(error = checkout_lookup_head_tree(&head, repo)) &&
!(error = git_iterator_for_tree(&head_i, head, 0, NULL, NULL)))
diff --git a/src/iterator.c b/src/iterator.c
index 76b0e41..5917f63 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -1321,9 +1321,10 @@ static void workdir_iterator__free(git_iterator *self)
git_ignore__free(&wi->ignores);
}
-int git_iterator_for_workdir(
+int git_iterator_for_workdir_ext(
git_iterator **out,
git_repository *repo,
+ const char *repo_workdir,
git_iterator_flag_t flags,
const char *start,
const char *end)
@@ -1331,8 +1332,11 @@ int git_iterator_for_workdir(
int error;
workdir_iterator *wi;
- if (git_repository__ensure_not_bare(repo, "scan working directory") < 0)
- return GIT_EBAREREPO;
+ if (!repo_workdir) {
+ if (git_repository__ensure_not_bare(repo, "scan working directory") < 0)
+ return GIT_EBAREREPO;
+ repo_workdir = git_repository_workdir(repo);
+ }
/* initialize as an fs iterator then do overrides */
wi = git__calloc(1, sizeof(workdir_iterator));
@@ -1352,7 +1356,7 @@ int git_iterator_for_workdir(
return error;
}
- return fs_iterator__initialize(out, &wi->fi, git_repository_workdir(repo));
+ return fs_iterator__initialize(out, &wi->fi, repo_workdir);
}
diff --git a/src/iterator.h b/src/iterator.h
index 493ff4b..ea88fa6 100644
--- a/src/iterator.h
+++ b/src/iterator.h
@@ -79,15 +79,26 @@ extern int git_iterator_for_index(
const char *start,
const char *end);
+extern int git_iterator_for_workdir_ext(
+ git_iterator **out,
+ git_repository *repo,
+ const char *repo_workdir,
+ git_iterator_flag_t flags,
+ const char *start,
+ const char *end);
+
/* workdir iterators will match the ignore_case value from the index of the
* repository, unless you override with a non-zero flag value
*/
-extern int git_iterator_for_workdir(
+GIT_INLINE(int) git_iterator_for_workdir(
git_iterator **out,
git_repository *repo,
git_iterator_flag_t flags,
const char *start,
- const char *end);
+ const char *end)
+{
+ return git_iterator_for_workdir_ext(out, repo, NULL, flags, start, end);
+}
/* for filesystem iterators, you have to explicitly pass in the ignore_case
* behavior that you desire
diff --git a/tests-clar/checkout/index.c b/tests-clar/checkout/index.c
index a3a0f8f..c7500db 100644
--- a/tests-clar/checkout/index.c
+++ b/tests-clar/checkout/index.c
@@ -506,3 +506,102 @@ void test_checkout_index__issue_1397(void)
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_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ checkout_counts cts;
+ memset(&cts, 0, sizeof(cts));
+
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+ 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_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ git_index *index;
+ git_object *head = NULL;
+ checkout_counts cts;
+ memset(&cts, 0, sizeof(cts));
+
+ test_checkout_index__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_CREATE;
+
+ 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);
+
+ 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__can_get_repo_from_index(void)
+{
+ git_index *index;
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_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_CREATE;
+
+ 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);
+}
diff --git a/tests-clar/checkout/tree.c b/tests-clar/checkout/tree.c
index 5835ab0..0e65f28 100644
--- a/tests-clar/checkout/tree.c
+++ b/tests-clar/checkout/tree.c
@@ -596,6 +596,8 @@ void test_checkout_tree__fails_when_dir_in_use(void)
cl_git_pass(p_chdir("../.."));
cl_assert(git_path_is_empty_dir("testrepo/a"));
+
+ git_object_free(obj);
#endif
}
@@ -628,5 +630,47 @@ void test_checkout_tree__can_continue_when_dir_in_use(void)
cl_git_pass(p_chdir("../.."));
cl_assert(git_path_is_empty_dir("testrepo/a"));
+
+ git_object_free(obj);
#endif
}
+
+void test_checkout_tree__target_directory_from_bare(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ git_oid oid;
+ checkout_counts cts;
+ memset(&cts, 0, sizeof(cts));
+
+ test_checkout_tree__cleanup(); /* cleanup default checkout */
+
+ g_repo = cl_git_sandbox_init("testrepo.git");
+ cl_assert(git_repository_is_bare(g_repo));
+
+ opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
+
+ opts.notify_flags = GIT_CHECKOUT_NOTIFY_ALL;
+ opts.notify_cb = checkout_count_callback;
+ opts.notify_payload = &cts;
+
+ cl_git_pass(git_reference_name_to_id(&oid, g_repo, "HEAD"));
+ cl_git_pass(git_object_lookup(&g_object, g_repo, &oid, GIT_OBJ_ANY));
+
+ cl_git_fail(git_checkout_tree(g_repo, g_object, &opts));
+
+ opts.target_directory = "alternative";
+ cl_assert(!git_path_isdir("alternative"));
+
+ cl_git_pass(git_checkout_tree(g_repo, g_object, &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);
+
+ 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));
+}