checkout: factor out worktree checkout code

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Apr 25, 2019 at 16:45 UTC 4058199c0ec5b68ffa2225dcdf5235a328f2443e
1 file changed +59 -49
builtin/checkout.c
+59 -49
@@ -330,17 +330,73 @@ static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce,
330 }
331 }
332
333 +static int checkout_worktree(const struct checkout_opts *opts)
334 +{
335 + struct checkout state = CHECKOUT_INIT;
336 + int nr_checkouts = 0, nr_unmerged = 0;
337 + int errs = 0;
338 + int pos;
339 +
340 + state.force = 1;
341 + state.refresh_cache = 1;
342 + state.istate = &the_index;
343 +
344 + enable_delayed_checkout(&state);
345 + for (pos = 0; pos < active_nr; pos++) {
346 + struct cache_entry *ce = active_cache[pos];
347 + if (ce->ce_flags & CE_MATCHED) {
348 + if (!ce_stage(ce)) {
349 + errs |= checkout_entry(ce, &state,
350 + NULL, &nr_checkouts);
351 + continue;
352 + }
353 + if (opts->writeout_stage)
354 + errs |= checkout_stage(opts->writeout_stage,
355 + ce, pos,
356 + &state,
357 + &nr_checkouts, opts->overlay_mode);
358 + else if (opts->merge)
359 + errs |= checkout_merged(pos, &state,
360 + &nr_unmerged);
361 + pos = skip_same_name(ce, pos) - 1;
362 + }
363 + }
364 + remove_marked_cache_entries(&the_index, 1);
365 + remove_scheduled_dirs();
366 + errs |= finish_delayed_checkout(&state, &nr_checkouts);
367 +
368 + if (opts->count_checkout_paths) {
369 + if (nr_unmerged)
370 + fprintf_ln(stderr, Q_("Recreated %d merge conflict",
371 + "Recreated %d merge conflicts",
372 + nr_unmerged),
373 + nr_unmerged);
374 + if (opts->source_tree)
375 + fprintf_ln(stderr, Q_("Updated %d path from %s",
376 + "Updated %d paths from %s",
377 + nr_checkouts),
378 + nr_checkouts,
379 + find_unique_abbrev(&opts->source_tree->object.oid,
380 + DEFAULT_ABBREV));
381 + else if (!nr_unmerged || nr_checkouts)
382 + fprintf_ln(stderr, Q_("Updated %d path from the index",
383 + "Updated %d paths from the index",
384 + nr_checkouts),
385 + nr_checkouts);
386 + }
387 +
388 + return errs;
389 +}
390 +
391 static int checkout_paths(const struct checkout_opts *opts,
392 const char *revision)
393 {
394 int pos;
337 - struct checkout state = CHECKOUT_INIT;
395 static char *ps_matched;
396 struct object_id rev;
397 struct commit *head;
398 int errs = 0;
399 struct lock_file lock_file = LOCK_INIT;
343 - int nr_checkouts = 0, nr_unmerged = 0;
400
401 trace2_cmd_mode(opts->patch_mode ? "patch" : "path");
402
@@ -426,53 +482,7 @@ static int checkout_paths(const struct checkout_opts *opts,
482 return 1;
483
484 /* Now we are committed to check them out */
429 - state.force = 1;
430 - state.refresh_cache = 1;
431 - state.istate = &the_index;
432 -
433 - enable_delayed_checkout(&state);
434 - for (pos = 0; pos < active_nr; pos++) {
435 - struct cache_entry *ce = active_cache[pos];
436 - if (ce->ce_flags & CE_MATCHED) {
437 - if (!ce_stage(ce)) {
438 - errs |= checkout_entry(ce, &state,
439 - NULL, &nr_checkouts);
440 - continue;
441 - }
442 - if (opts->writeout_stage)
443 - errs |= checkout_stage(opts->writeout_stage,
444 - ce, pos,
445 - &state,
446 - &nr_checkouts, opts->overlay_mode);
447 - else if (opts->merge)
448 - errs |= checkout_merged(pos, &state,
449 - &nr_unmerged);
450 - pos = skip_same_name(ce, pos) - 1;
451 - }
452 - }
453 - remove_marked_cache_entries(&the_index, 1);
454 - remove_scheduled_dirs();
455 - errs |= finish_delayed_checkout(&state, &nr_checkouts);
456 -
457 - if (opts->count_checkout_paths) {
458 - if (nr_unmerged)
459 - fprintf_ln(stderr, Q_("Recreated %d merge conflict",
460 - "Recreated %d merge conflicts",
461 - nr_unmerged),
462 - nr_unmerged);
463 - if (opts->source_tree)
464 - fprintf_ln(stderr, Q_("Updated %d path from %s",
465 - "Updated %d paths from %s",
466 - nr_checkouts),
467 - nr_checkouts,
468 - find_unique_abbrev(&opts->source_tree->object.oid,
469 - DEFAULT_ABBREV));
470 - else if (!nr_unmerged || nr_checkouts)
471 - fprintf_ln(stderr, Q_("Updated %d path from the index",
472 - "Updated %d paths from the index",
473 - nr_checkouts),
474 - nr_checkouts);
475 - }
485 + errs |= checkout_worktree(opts);
486
487 if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
488 die(_("unable to write new index file"));