make 'got commit' work with changed stanges
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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
diff --git a/lib/worktree.c b/lib/worktree.c
index 0f55d72..2e5c680 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2852,6 +2852,7 @@ struct collect_commitables_arg {
struct got_pathlist_head *commitable_paths;
struct got_repository *repo;
struct got_worktree *worktree;
+ int have_staged_files;
};
static const struct got_error *
@@ -2867,12 +2868,20 @@ collect_commitables(void *arg, unsigned char status,
char *parent_path = NULL, *path = NULL;
struct stat sb;
- if (status == GOT_STATUS_CONFLICT)
- return got_error(GOT_ERR_COMMIT_CONFLICT);
+ if (a->have_staged_files) {
+ if (staged_status != GOT_STATUS_MODIFY &&
+ staged_status != GOT_STATUS_ADD &&
+ staged_status != GOT_STATUS_DELETE)
+ return NULL;
+ } else {
+ if (status == GOT_STATUS_CONFLICT)
+ return got_error(GOT_ERR_COMMIT_CONFLICT);
- if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
- status != GOT_STATUS_DELETE)
- return NULL;
+ if (status != GOT_STATUS_MODIFY &&
+ status != GOT_STATUS_ADD &&
+ status != GOT_STATUS_DELETE)
+ return NULL;
+ }
if (asprintf(&path, "/%s", relpath) == -1) {
err = got_error_from_errno("asprintf");
@@ -2899,7 +2908,7 @@ collect_commitables(void *arg, unsigned char status,
err = got_error_from_errno("asprintf");
goto done;
}
- if (status == GOT_STATUS_DELETE) {
+ if (status == GOT_STATUS_DELETE || staged_status == GOT_STATUS_DELETE) {
sb.st_mode = GOT_DEFAULT_FILE_MODE;
} else {
if (lstat(ct->ondisk_path, &sb) != 0) {
@@ -2917,8 +2926,10 @@ collect_commitables(void *arg, unsigned char status,
}
ct->status = status;
+ ct->staged_status = staged_status;
ct->blob_id = NULL; /* will be filled in when blob gets created */
- if (ct->status != GOT_STATUS_ADD) {
+ if (ct->status != GOT_STATUS_ADD &&
+ ct->staged_status != GOT_STATUS_ADD) {
ct->base_blob_id = got_object_id_dup(blob_id);
if (ct->base_blob_id == NULL) {
err = got_error_from_errno("got_object_id_dup");
@@ -2930,7 +2941,6 @@ collect_commitables(void *arg, unsigned char status,
goto done;
}
}
- ct->staged_status = staged_status;
if (ct->staged_status == GOT_STATUS_ADD ||
ct->staged_status == GOT_STATUS_MODIFY) {
ct->staged_blob_id = got_object_id_dup(staged_blob_id);
@@ -3026,7 +3036,10 @@ alloc_modified_blob_tree_entry(struct got_tree_entry **new_te,
(*new_te)->mode = get_ct_file_mode(ct);
free((*new_te)->id);
- (*new_te)->id = got_object_id_dup(ct->blob_id);
+ if (ct->staged_status == GOT_STATUS_MODIFY)
+ (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
+ else
+ (*new_te)->id = got_object_id_dup(ct->blob_id);
if ((*new_te)->id == NULL) {
err = got_error_from_errno("got_object_id_dup");
goto done;
@@ -3065,7 +3078,10 @@ alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
(*new_te)->mode = get_ct_file_mode(ct);
- (*new_te)->id = got_object_id_dup(ct->blob_id);
+ if (ct->staged_status == GOT_STATUS_ADD)
+ (*new_te)->id = got_object_id_dup(ct->staged_blob_id);
+ else
+ (*new_te)->id = got_object_id_dup(ct->blob_id);
if ((*new_te)->id == NULL) {
err = got_error_from_errno("got_object_id_dup");
goto done;
@@ -3098,9 +3114,17 @@ report_ct_status(struct got_commitable *ct,
got_worktree_status_cb status_cb, void *status_arg)
{
const char *ct_path = ct->path;
+ unsigned char status;
+
while (ct_path[0] == '/')
ct_path++;
- return (*status_cb)(status_arg, ct->status, GOT_STATUS_NO_CHANGE,
+
+ if (ct->staged_status != GOT_STATUS_NO_CHANGE)
+ status = ct->staged_status;
+ else
+ status = ct->status;
+
+ return (*status_cb)(status_arg, status, GOT_STATUS_NO_CHANGE,
ct_path, ct->blob_id, NULL, NULL);
}
@@ -3146,9 +3170,15 @@ match_deleted_or_modified_ct(struct got_commitable **ctp,
char *ct_name = NULL;
int path_matches;
- if (ct->status != GOT_STATUS_MODIFY &&
- ct->status != GOT_STATUS_DELETE)
- continue;
+ if (ct->staged_status == GOT_STATUS_NO_CHANGE) {
+ if (ct->status != GOT_STATUS_MODIFY &&
+ ct->status != GOT_STATUS_DELETE)
+ continue;
+ } else {
+ if (ct->staged_status != GOT_STATUS_MODIFY &&
+ ct->staged_status != GOT_STATUS_DELETE)
+ continue;
+ }
if (got_object_id_cmp(ct->base_blob_id, te->id) != 0)
continue;
@@ -3235,7 +3265,8 @@ write_tree(struct got_object_id **new_tree_id,
struct got_commitable *ct = pe->data;
char *child_path = NULL, *slash;
- if (ct->status != GOT_STATUS_ADD ||
+ if ((ct->status != GOT_STATUS_ADD &&
+ ct->staged_status != GOT_STATUS_ADD) ||
(ct->flags & GOT_COMMITABLE_ADDED))
continue;
@@ -3312,7 +3343,8 @@ write_tree(struct got_object_id **new_tree_id,
path_base_tree, commitable_paths);
if (ct) {
/* NB: Deleted entries get dropped here. */
- if (ct->status == GOT_STATUS_MODIFY) {
+ if (ct->status == GOT_STATUS_MODIFY ||
+ ct->staged_status == GOT_STATUS_MODIFY) {
err = alloc_modified_blob_tree_entry(
&new_te, te, ct);
if (err)
@@ -3363,9 +3395,17 @@ update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
if (ie) {
- if (ct->status == GOT_STATUS_DELETE) {
+ if (ct->status == GOT_STATUS_DELETE ||
+ ct->staged_status == GOT_STATUS_DELETE) {
got_fileindex_entry_remove(fileindex, ie);
got_fileindex_entry_free(ie);
+ } else if (ct->staged_status == GOT_STATUS_ADD ||
+ ct->staged_status == GOT_STATUS_MODIFY) {
+ got_fileindex_entry_stage_set(ie,
+ GOT_FILEIDX_STAGE_NONE);
+ err = got_fileindex_entry_update(ie,
+ ct->ondisk_path, ct->staged_blob_id->sha1,
+ new_base_commit_id->sha1, 1);
} else
err = got_fileindex_entry_update(ie,
ct->ondisk_path, ct->blob_id->sha1,
@@ -3387,14 +3427,15 @@ update_fileindex_after_commit(struct got_pathlist_head *commitable_paths,
static const struct got_error *
check_out_of_date(const char *in_repo_path, unsigned char status,
- struct got_object_id *base_blob_id, struct got_object_id *base_commit_id,
+ unsigned char staged_status, struct got_object_id *base_blob_id,
+ struct got_object_id *base_commit_id,
struct got_object_id *head_commit_id, struct got_repository *repo,
int ood_errcode)
{
const struct got_error *err = NULL;
struct got_object_id *id = NULL;
- if (status != GOT_STATUS_ADD) {
+ if (status != GOT_STATUS_ADD && staged_status != GOT_STATUS_ADD) {
/* Trivial case: base commit == head commit */
if (got_object_id_cmp(base_commit_id, head_commit_id) == 0)
return NULL;
@@ -3473,6 +3514,11 @@ commit_worktree(struct got_object_id **new_commit_id,
struct got_commitable *ct = pe->data;
char *ondisk_path;
+ /* Blobs for staged files already exist. */
+ if (ct->staged_status == GOT_STATUS_ADD ||
+ ct->staged_status == GOT_STATUS_MODIFY)
+ continue;
+
if (ct->status != GOT_STATUS_ADD &&
ct->status != GOT_STATUS_MODIFY)
continue;
@@ -3591,6 +3637,27 @@ check_staged_file(void *arg, struct got_fileindex_entry *ie)
return NULL;
}
+static const struct got_error *
+check_non_staged_files(struct got_fileindex *fileindex,
+ struct got_pathlist_head *paths)
+{
+ struct got_pathlist_entry *pe;
+ struct got_fileindex_entry *ie;
+
+ TAILQ_FOREACH(pe, paths, entry) {
+ if (pe->path[0] == '\0')
+ continue;
+ ie = got_fileindex_entry_get(fileindex, pe->path, pe->path_len);
+ if (ie == NULL)
+ return got_error_path(pe->path, GOT_ERR_BAD_PATH);
+ if (got_fileindex_entry_stage_get(ie) == GOT_FILEIDX_STAGE_NONE)
+ return got_error_path(pe->path,
+ GOT_ERR_FILE_NOT_STAGED);
+ }
+
+ return NULL;
+}
+
const struct got_error *
got_worktree_commit(struct got_object_id **new_commit_id,
struct got_worktree *worktree, struct got_pathlist_head *paths,
@@ -3629,9 +3696,20 @@ got_worktree_commit(struct got_object_id **new_commit_id,
if (err)
goto done;
+ err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
+ &have_staged_files);
+ if (err && err->code != GOT_ERR_CANCELLED)
+ goto done;
+ if (have_staged_files) {
+ err = check_non_staged_files(fileindex, paths);
+ if (err)
+ goto done;
+ }
+
cc_arg.commitable_paths = &commitable_paths;
cc_arg.worktree = worktree;
cc_arg.repo = repo;
+ cc_arg.have_staged_files = have_staged_files;
TAILQ_FOREACH(pe, paths, entry) {
err = worktree_status(worktree, pe->path, fileindex, repo,
collect_commitables, &cc_arg, NULL, NULL);
@@ -3650,11 +3728,6 @@ got_worktree_commit(struct got_object_id **new_commit_id,
goto done;
}
- err = got_fileindex_for_each_entry_safe(fileindex, check_staged_file,
- &have_staged_files);
- if (err && err->code != GOT_ERR_CANCELLED)
- goto done;
-
TAILQ_FOREACH(pe, &commitable_paths, entry) {
struct got_commitable *ct = pe->data;
const char *ct_path = ct->in_repo_path;
@@ -3662,15 +3735,10 @@ got_worktree_commit(struct got_object_id **new_commit_id,
while (ct_path[0] == '/')
ct_path++;
err = check_out_of_date(ct_path, ct->status,
- ct->base_blob_id, ct->base_commit_id, head_commit_id,
- repo, GOT_ERR_COMMIT_OUT_OF_DATE);
+ ct->staged_status, ct->base_blob_id, ct->base_commit_id,
+ head_commit_id, repo, GOT_ERR_COMMIT_OUT_OF_DATE);
if (err)
goto done;
- if (have_staged_files &&
- ct->staged_status == GOT_STATUS_NO_CHANGE) {
- err = got_error_path(ct_path, GOT_ERR_FILE_NOT_STAGED);
- goto done;
- }
}
@@ -4177,6 +4245,7 @@ rebase_commit(struct got_object_id **new_commit_id,
cc_arg.commitable_paths = &commitable_paths;
cc_arg.worktree = worktree;
cc_arg.repo = repo;
+ cc_arg.have_staged_files = 0;
/*
* If possible get the status of individual files directly to
* avoid crawling the entire work tree once per rebased commit.
@@ -5017,8 +5086,9 @@ stage_check_out_of_date(const char *relpath, const char *ondisk_path,
p = in_repo_path;
while (p[0] == '/')
p++;
- err = check_out_of_date(p, status, blob_idp, base_commit_idp,
- head_commit_id, repo, GOT_ERR_STAGE_OUT_OF_DATE);
+ err = check_out_of_date(p, status, GOT_STATUS_NO_CHANGE,
+ blob_idp, base_commit_idp, head_commit_id, repo,
+ GOT_ERR_STAGE_OUT_OF_DATE);
done:
free(in_repo_path);
return err;
diff --git a/regress/cmdline/stage.sh b/regress/cmdline/stage.sh
index 61e10ca..775dfc6 100755
--- a/regress/cmdline/stage.sh
+++ b/regress/cmdline/stage.sh
@@ -939,6 +939,113 @@ function test_stage_commit_non_staged {
test_done "$testroot" "$ret"
}
+function test_stage_commit {
+ local testroot=`test_init stage_commit`
+ local first_commit=`git_show_head $testroot/repo`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified file" > $testroot/wt/alpha
+ (cd $testroot/wt && got rm beta > /dev/null)
+ echo "new file" > $testroot/wt/foo
+ (cd $testroot/wt && got add foo > /dev/null)
+ echo "modified file" > $testroot/wt/alpha
+ (cd $testroot/wt && got stage alpha beta foo > /dev/null)
+
+ echo "modified file again" > $testroot/wt/alpha
+ echo "new file changed" > $testroot/wt/foo
+ echo "non-staged change" > $testroot/wt/gamma/delta
+ echo "non-staged new file" > $testroot/wt/epsilon/new
+ (cd $testroot/wt && got add epsilon/new > /dev/null)
+ (cd $testroot/wt && got rm epsilon/zeta > /dev/null)
+
+ (cd $testroot/wt && got stage -l alpha) | cut -d' ' -f 1 \
+ > $testroot/blob_id_alpha
+ (cd $testroot/wt && got stage -l foo) | cut -d' ' -f 1 \
+ > $testroot/blob_id_foo
+
+ (cd $testroot/wt && got commit -m "staged changes" \
+ > $testroot/stdout)
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got commit command failed unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ local head_commit=`git_show_head $testroot/repo`
+ echo "A foo" > $testroot/stdout.expected
+ echo "M alpha" >> $testroot/stdout.expected
+ echo "D beta" >> $testroot/stdout.expected
+ echo "Created commit $head_commit" >> $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 diff -r $testroot/repo $first_commit $head_commit \
+ > $testroot/stdout
+
+ echo "diff $first_commit $head_commit" \
+ > $testroot/stdout.expected
+ echo -n 'blob - ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -i -c $first_commit | \
+ grep 'alpha$' | cut -d' ' -f 1 \
+ >> $testroot/stdout.expected
+ echo -n 'blob + ' >> $testroot/stdout.expected
+ cat $testroot/blob_id_alpha >> $testroot/stdout.expected
+ echo '--- alpha' >> $testroot/stdout.expected
+ echo '+++ alpha' >> $testroot/stdout.expected
+ echo '@@ -1 +1 @@' >> $testroot/stdout.expected
+ echo '-alpha' >> $testroot/stdout.expected
+ echo '+modified file' >> $testroot/stdout.expected
+ echo -n 'blob - ' >> $testroot/stdout.expected
+ got tree -r $testroot/repo -i -c $first_commit \
+ | grep 'beta$' | cut -d' ' -f 1 \
+ >> $testroot/stdout.expected
+ echo 'blob + /dev/null' >> $testroot/stdout.expected
+ echo '--- beta' >> $testroot/stdout.expected
+ echo '+++ /dev/null' >> $testroot/stdout.expected
+ echo '@@ -1 +0,0 @@' >> $testroot/stdout.expected
+ echo '-beta' >> $testroot/stdout.expected
+ echo 'blob - /dev/null' >> $testroot/stdout.expected
+ echo -n 'blob + ' >> $testroot/stdout.expected
+ cat $testroot/blob_id_foo >> $testroot/stdout.expected
+ echo '--- /dev/null' >> $testroot/stdout.expected
+ echo '+++ foo' >> $testroot/stdout.expected
+ echo '@@ -0,0 +1 @@' >> $testroot/stdout.expected
+ echo '+new file' >> $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
+
+ echo 'A epsilon/new' > $testroot/stdout.expected
+ echo 'D epsilon/zeta' >> $testroot/stdout.expected
+ echo 'M gamma/delta' >> $testroot/stdout.expected
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
run_test test_stage_basic
run_test test_stage_conflict
run_test test_stage_out_of_date
@@ -952,3 +1059,4 @@ run_test test_stage_histedit
run_test test_stage_rebase
run_test test_stage_update
run_test test_stage_commit_non_staged
+run_test test_stage_commit