allow empty dirs to exist when doing checkout
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
diff --git a/src/checkout.c b/src/checkout.c
index defc21d..21f32d8 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -467,6 +467,7 @@ static int checkout_action(
int cmp = -1, act;
int (*strcomp)(const char *, const char *) = data->diff->strcomp;
int (*pfxcomp)(const char *str, const char *pfx) = data->diff->pfxcomp;
+ int error;
/* move workdir iterator to follow along with deltas */
@@ -490,8 +491,11 @@ static int checkout_action(
if (cmp == 0) {
if (wd->mode == GIT_FILEMODE_TREE) {
/* case 2 - entry prefixed by workdir tree */
- if (git_iterator_advance_into(&wd, workdir) < 0)
- goto fail;
+ if ((error = git_iterator_advance_into(&wd, workdir)) < 0) {
+ if (error != GIT_ENOTFOUND ||
+ git_iterator_advance(&wd, workdir) < 0)
+ goto fail;
+ }
*wditem_ptr = wd;
continue;
diff --git a/tests-clar/checkout/tree.c b/tests-clar/checkout/tree.c
index 0748b22..eb129f3 100644
--- a/tests-clar/checkout/tree.c
+++ b/tests-clar/checkout/tree.c
@@ -501,3 +501,28 @@ void test_checkout_tree__issue_1397(void)
git_object_free(tree);
}
+
+void test_checkout_tree__can_write_to_empty_dirs(void)
+{
+ git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
+ git_oid oid;
+ git_object *obj = NULL;
+
+ assert_on_branch(g_repo, "master");
+
+ cl_git_pass(p_mkdir("testrepo/a", 0777));
+
+ /* do first checkout with FORCE because we don't know if testrepo
+ * base data is clean for a checkout or not
+ */
+ opts.checkout_strategy = GIT_CHECKOUT_FORCE;
+
+ cl_git_pass(git_reference_name_to_id(&oid, g_repo, "refs/heads/dir"));
+ cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJ_ANY));
+
+ cl_git_pass(git_checkout_tree(g_repo, obj, &opts));
+
+ cl_assert(git_path_isfile("testrepo/a/b.txt"));
+
+ git_object_free(obj);
+}