prevent 'got commit' from comitting empty sub-directories; found by tracey
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
diff --git a/lib/worktree.c b/lib/worktree.c
index 34717c9..f430566 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -3843,13 +3843,13 @@ done:
return err;
}
-static const struct got_error *write_tree(struct got_object_id **,
+static const struct got_error *write_tree(struct got_object_id **, int *,
struct got_tree_object *, const char *, struct got_pathlist_head *,
got_worktree_status_cb status_cb, void *status_arg,
struct got_repository *);
static const struct got_error *
-write_subtree(struct got_object_id **new_subtree_id,
+write_subtree(struct got_object_id **new_subtree_id, int *nentries,
struct got_tree_entry *te, const char *parent_path,
struct got_pathlist_head *commitable_paths,
got_worktree_status_cb status_cb, void *status_arg,
@@ -3867,8 +3867,8 @@ write_subtree(struct got_object_id **new_subtree_id,
if (err)
return err;
- err = write_tree(new_subtree_id, subtree, subpath, commitable_paths,
- status_cb, status_arg, repo);
+ err = write_tree(new_subtree_id, nentries, subtree, subpath,
+ commitable_paths, status_cb, status_arg, repo);
got_object_tree_close(subtree);
free(subpath);
return err;
@@ -4088,6 +4088,7 @@ make_subtree_for_added_blob(struct got_tree_entry **new_tep,
struct got_tree_entry *new_te;
char *subtree_path;
struct got_object_id *id = NULL;
+ int nentries;
*new_tep = NULL;
@@ -4106,7 +4107,7 @@ make_subtree_for_added_blob(struct got_tree_entry **new_tep,
err = got_error(GOT_ERR_NO_SPACE);
goto done;
}
- err = write_tree(&id, NULL, subtree_path,
+ err = write_tree(&id, &nentries, NULL, subtree_path,
commitable_paths, status_cb, status_arg, repo);
if (err) {
free(new_te);
@@ -4122,7 +4123,7 @@ done:
}
static const struct got_error *
-write_tree(struct got_object_id **new_tree_id,
+write_tree(struct got_object_id **new_tree_id, int *nentries,
struct got_tree_object *base_tree, const char *path_base_tree,
struct got_pathlist_head *commitable_paths,
got_worktree_status_cb status_cb, void *status_arg,
@@ -4132,9 +4133,9 @@ write_tree(struct got_object_id **new_tree_id,
struct got_pathlist_head paths;
struct got_tree_entry *te, *new_te = NULL;
struct got_pathlist_entry *pe;
- int nentries = 0;
TAILQ_INIT(&paths);
+ *nentries = 0;
/* Insert, and recurse into, newly added entries first. */
TAILQ_FOREACH(pe, commitable_paths, entry) {
@@ -4167,7 +4168,7 @@ write_tree(struct got_object_id **new_tree_id,
err = insert_tree_entry(new_te, &paths);
if (err)
goto done;
- nentries++;
+ (*nentries)++;
} else {
*slash = '\0'; /* trim trailing path components */
if (base_tree == NULL ||
@@ -4182,7 +4183,7 @@ write_tree(struct got_object_id **new_tree_id,
err = insert_tree_entry(new_te, &paths);
if (err)
goto done;
- nentries++;
+ (*nentries)++;
}
}
}
@@ -4203,7 +4204,7 @@ write_tree(struct got_object_id **new_tree_id,
err = insert_tree_entry(new_te, &paths);
if (err)
goto done;
- nentries++;
+ (*nentries)++;
continue;
}
@@ -4219,11 +4220,18 @@ write_tree(struct got_object_id **new_tree_id,
/* Avoid recursion into unmodified subtrees. */
if (modified) {
struct got_object_id *new_id;
- err = write_subtree(&new_id, te,
+ int nsubentries;
+ err = write_subtree(&new_id,
+ &nsubentries, te,
path_base_tree, commitable_paths,
status_cb, status_arg, repo);
if (err)
goto done;
+ if (nsubentries == 0) {
+ /* All entries were deleted. */
+ free(new_id);
+ continue;
+ }
memcpy(&new_te->id, new_id,
sizeof(new_te->id));
free(new_id);
@@ -4231,7 +4239,7 @@ write_tree(struct got_object_id **new_tree_id,
err = insert_tree_entry(new_te, &paths);
if (err)
goto done;
- nentries++;
+ (*nentries)++;
continue;
}
@@ -4251,7 +4259,7 @@ write_tree(struct got_object_id **new_tree_id,
err = insert_tree_entry(new_te, &paths);
if (err)
goto done;
- nentries++;
+ (*nentries)++;
}
err = report_ct_status(ct, status_cb,
status_arg);
@@ -4265,13 +4273,13 @@ write_tree(struct got_object_id **new_tree_id,
err = insert_tree_entry(new_te, &paths);
if (err)
goto done;
- nentries++;
+ (*nentries)++;
}
}
}
/* Write new list of entries; deleted entries have been dropped. */
- err = got_object_tree_create(new_tree_id, &paths, nentries, repo);
+ err = got_object_tree_create(new_tree_id, &paths, *nentries, repo);
done:
got_pathlist_free(&paths);
return err;
@@ -4384,6 +4392,7 @@ commit_worktree(struct got_object_id **new_commit_id,
struct got_object_id *head_commit_id2 = NULL;
struct got_tree_object *head_tree = NULL;
struct got_object_id *new_tree_id = NULL;
+ int nentries;
struct got_object_id_queue parent_ids;
struct got_object_qid *pid = NULL;
char *logmsg = NULL;
@@ -4438,8 +4447,8 @@ commit_worktree(struct got_object_id **new_commit_id,
}
/* Recursively write new tree objects. */
- err = write_tree(&new_tree_id, head_tree, "/", commitable_paths,
- status_cb, status_arg, repo);
+ err = write_tree(&new_tree_id, &nentries, head_tree, "/",
+ commitable_paths, status_cb, status_arg, repo);
if (err)
goto done;
diff --git a/regress/cmdline/commit.sh b/regress/cmdline/commit.sh
index a131681..270ee1f 100755
--- a/regress/cmdline/commit.sh
+++ b/regress/cmdline/commit.sh
@@ -238,6 +238,47 @@ function test_commit_added_subdirs {
test_done "$testroot" "$ret"
}
+function test_commit_deleted_subdirs {
+ local testroot=`test_init commit_deleted_subdirs`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got rm -R $testroot/wt/{epsilon,gamma} >/dev/null)
+
+ (cd $testroot/wt && got commit -m 'test commit_deleted_subdirs' \
+ > $testroot/stdout 2> $testroot/stderr)
+
+ local head_rev=`git_show_head $testroot/repo`
+ echo "D epsilon/zeta" > $testroot/stdout.expected
+ echo "D gamma/delta" >> $testroot/stdout.expected
+ echo "Created commit $head_rev" >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got tree -r $testroot/repo > $testroot/stdout
+
+ echo "alpha" > $testroot/stdout.expected
+ echo "beta" >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
function test_commit_rejects_conflicted_file {
local testroot=`test_init commit_rejects_conflicted_file`
@@ -749,6 +790,7 @@ run_test test_commit_subdir
run_test test_commit_single_file
run_test test_commit_out_of_date
run_test test_commit_added_subdirs
+run_test test_commit_deleted_subdirs
run_test test_commit_rejects_conflicted_file
run_test test_commit_single_file_multiple
run_test test_commit_added_and_modified_in_same_dir