22
23
#define BUILTIN_WORKTREE_ADD_USAGE \
24
N_("git worktree add [-f] [--detach] [--checkout] [--lock [--reason <string>]]\n" \
25
- " [(-b | -B) <new-branch>] <path> [<commit-ish>]")
25
+ " [--orphan] [(-b | -B) <new-branch>] <path> [<commit-ish>]")
26
+
27
#define BUILTIN_WORKTREE_LIST_USAGE \
28
N_("git worktree list [-v | --porcelain [-z]]")
29
#define BUILTIN_WORKTREE_LOCK_USAGE \
96
int detach;
97
int quiet;
98
int checkout;
99
+ int orphan;
100
const char *keep_locked;
101
};
102
370
return run_command(&cp);
371
}
372
373
+static int make_worktree_orphan(const char * ref, const struct add_opts *opts,
374
+ struct strvec *child_env)
375
+{
376
+ struct strbuf symref = STRBUF_INIT;
377
+ struct child_process cp = CHILD_PROCESS_INIT;
378
+
379
+ validate_new_branchname(ref, &symref, 0);
380
+ strvec_pushl(&cp.args, "symbolic-ref", "HEAD", symref.buf, NULL);
381
+ if (opts->quiet)
382
+ strvec_push(&cp.args, "--quiet");
383
+ strvec_pushv(&cp.env, child_env->v);
384
+ strbuf_release(&symref);
385
+ cp.git_cmd = 1;
386
+ return run_command(&cp);
387
+}
388
+
389
static int add_worktree(const char *path, const char *refname,
390
const struct add_opts *opts)
391
{
415
die_if_checked_out(symref.buf, 0);
416
}
417
commit = lookup_commit_reference_by_name(refname);
400
- if (!commit)
418
+ if (!commit && !opts->orphan)
419
die(_("invalid reference: %s"), refname);
420
421
name = worktree_basename(path, &len);
504
strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
505
cp.git_cmd = 1;
506
489
- if (!is_branch)
507
+ if (!is_branch && commit) {
508
strvec_pushl(&cp.args, "update-ref", "HEAD",
509
oid_to_hex(&commit->object.oid), NULL);
492
- else {
510
+ } else {
511
strvec_pushl(&cp.args, "symbolic-ref", "HEAD",
512
symref.buf, NULL);
513
if (opts->quiet)
519
if (ret)
520
goto done;
521
522
+ if (opts->orphan &&
523
+ (ret = make_worktree_orphan(refname, opts, &child_env)))
524
+ goto done;
525
+
526
if (opts->checkout &&
527
(ret = checkout_worktree(opts, &child_env)))
528
goto done;
542
* Hook failure does not warrant worktree deletion, so run hook after
543
* is_junk is cleared, but do return appropriate code when hook fails.
544
*/
523
- if (!ret && opts->checkout) {
545
+ if (!ret && opts->checkout && !opts->orphan) {
546
struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
547
548
strvec_pushl(&opt.env, "GIT_DIR", "GIT_WORK_TREE", NULL);
590
else {
591
struct commit *commit = lookup_commit_reference_by_name(branch);
592
if (!commit)
571
- die(_("invalid reference: %s"), branch);
593
+ BUG(_("unreachable: invalid reference: %s"), branch);
594
fprintf_ln(stderr, _("Preparing worktree (detached HEAD %s)"),
595
repo_find_unique_abbrev(the_repository, &commit->object.oid, DEFAULT_ABBREV));
596
}
642
N_("create a new branch")),
643
OPT_STRING('B', NULL, &new_branch_force, N_("branch"),
644
N_("create or reset a branch")),
645
+ OPT_BOOL(0, "orphan", &opts.orphan, N_("create unborn/orphaned branch")),
646
OPT_BOOL('d', "detach", &opts.detach, N_("detach HEAD at named commit")),
647
OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
648
OPT_BOOL(0, "lock", &keep_locked, N_("keep the new working tree locked")),
663
ac = parse_options(ac, av, prefix, options, git_worktree_add_usage, 0);
664
if (!!opts.detach + !!new_branch + !!new_branch_force > 1)
665
die(_("options '%s', '%s', and '%s' cannot be used together"), "-b", "-B", "--detach");
666
+ if (opts.detach && opts.orphan)
667
+ die(_("options '%s', and '%s' cannot be used together"),
668
+ "--orphan", "--detach");
669
+ if (opts.orphan && opt_track)
670
+ die(_("'%s' and '%s' cannot be used together"), "--orphan", "--track");
671
+ if (opts.orphan && !opts.checkout)
672
+ die(_("'%s' and '%s' cannot be used together"), "--orphan",
673
+ "--no-checkout");
674
+ if (opts.orphan && ac == 2)
675
+ die(_("'%s' and '%s' cannot be used together"), "--orphan",
676
+ _("<commit-ish>"));
677
if (lock_reason && !keep_locked)
678
die(_("the option '%s' requires '%s'"), "--reason", "--lock");
679
if (lock_reason)
702
strbuf_release(&symref);
703
}
704
671
- if (ac < 2 && !new_branch && !opts.detach) {
705
+ if (opts.orphan && !new_branch) {
706
+ int n;
707
+ const char *s = worktree_basename(path, &n);
708
+ new_branch = xstrndup(s, n);
709
+ } else if (new_branch || opts.detach || opts.orphan) {
710
+ // No-op
711
+ } else if (ac < 2) {
712
const char *s = dwim_branch(path, &new_branch);
713
if (s)
714
branch = s;
675
- }
676
-
677
- if (ac == 2 && !new_branch && !opts.detach) {
715
+ } else if (ac == 2) {
716
struct object_id oid;
717
struct commit *commit;
718
const char *remote;
726
}
727
}
728
}
729
+
730
+ if (!opts.orphan && !lookup_commit_reference_by_name(branch)) {
731
+ die(_("invalid reference: %s"), branch);
732
+ }
733
+
734
if (!opts.quiet)
735
print_preparing_worktree_line(opts.detach, branch, new_branch, !!new_branch_force);
736
694
- if (new_branch) {
737
+ if (opts.orphan) {
738
+ branch = new_branch;
739
+ } else if (new_branch) {
740
struct child_process cp = CHILD_PROCESS_INIT;
741
cp.git_cmd = 1;
742
strvec_push(&cp.args, "branch");