Skip to content

Commit 71d6682

Browse files
tgummerergitster
authored andcommitted
worktree: add --guess-remote flag to add subcommand
Currently 'git worktree add <path>' creates a new branch named after the basename of the <path>, that matches the HEAD of whichever worktree we were on when calling "git worktree add <path>". It's sometimes useful to have 'git worktree add <path> behave more like the dwim machinery in 'git checkout <new-branch>', i.e. check if the new branch name, derived from the basename of the <path>, uniquely matches the branch name of a remote-tracking branch, and if so check out that branch and set the upstream to the remote-tracking branch. Add a new --guess-remote option that enables exactly that behaviour. Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4e85333 commit 71d6682

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

Documentation/git-worktree.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ OPTIONS
115115
such as configuring sparse-checkout. See "Sparse checkout"
116116
in linkgit:git-read-tree[1].
117117

118+
--[no-]guess-remote::
119+
With `worktree add <path>`, without `<commit-ish>`, instead
120+
of creating a new branch from HEAD, if there exists a tracking
121+
branch in exactly one remote matching the basename of `<path>,
122+
base the new branch on the remote-tracking branch, and mark
123+
the remote-tracking branch as "upstream" from the new branch.
124+
118125
--[no-]track::
119126
When creating a new branch, if `<commit-ish>` is a branch,
120127
mark it as "upstream" from the new branch. This is the

builtin/worktree.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ static int add(int ac, const char **av, const char *prefix)
343343
char *path;
344344
const char *branch;
345345
const char *opt_track = NULL;
346+
int guess_remote = 0;
346347
struct option options[] = {
347348
OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
348349
OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
@@ -355,6 +356,8 @@ static int add(int ac, const char **av, const char *prefix)
355356
OPT_PASSTHRU(0, "track", &opt_track, NULL,
356357
N_("set up tracking mode (see git-branch(1))"),
357358
PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
359+
OPT_BOOL(0, "guess-remote", &guess_remote,
360+
N_("try to match the new branch name with a remote-tracking branch")),
358361
OPT_END()
359362
};
360363

@@ -389,6 +392,13 @@ static int add(int ac, const char **av, const char *prefix)
389392
int n;
390393
const char *s = worktree_basename(path, &n);
391394
opts.new_branch = xstrndup(s, n);
395+
if (guess_remote) {
396+
struct object_id oid;
397+
const char *remote =
398+
unique_tracking_name(opts.new_branch, &oid);
399+
if (remote)
400+
branch = remote;
401+
}
392402
}
393403

394404
if (ac == 2 && !opts.new_branch && !opts.detach) {

t/t2025-worktree-add.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,33 @@ test_expect_success '"add" <path> <branch> dwims' '
384384
)
385385
'
386386

387+
test_expect_success 'git worktree add does not match remote' '
388+
test_when_finished rm -rf repo_a repo_b foo &&
389+
setup_remote_repo repo_a repo_b &&
390+
(
391+
cd repo_b &&
392+
git worktree add ../foo
393+
) &&
394+
(
395+
cd foo &&
396+
test_must_fail git config "branch.foo.remote" &&
397+
test_must_fail git config "branch.foo.merge" &&
398+
! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
399+
)
400+
'
401+
402+
test_expect_success 'git worktree add --guess-remote sets up tracking' '
403+
test_when_finished rm -rf repo_a repo_b foo &&
404+
setup_remote_repo repo_a repo_b &&
405+
(
406+
cd repo_b &&
407+
git worktree add --guess-remote ../foo
408+
) &&
409+
(
410+
cd foo &&
411+
test_branch_upstream foo repo_a foo &&
412+
test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
413+
)
414+
'
415+
387416
test_done

0 commit comments

Comments
 (0)