Skip to content

Commit 4e85333

Browse files
tgummerergitster
authored andcommitted
worktree: make add <path> <branch> dwim
Currently 'git worktree add <path> <branch>', errors out when 'branch' is not a local branch. It has no additional dwim'ing features that one might expect. Make it behave more like 'git checkout <branch>' when the branch doesn't exist locally, but a remote tracking branch uniquely matches the desired branch name, i.e. create a new branch from the remote tracking branch and set the upstream to the remote tracking branch. As 'git worktree add' currently just dies in this situation, there are no backwards compatibility worries when introducing this feature. Signed-off-by: Thomas Gummerer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e284e89 commit 4e85333

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

Documentation/git-worktree.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ is linked to the current repository, sharing everything except working
5252
directory specific files such as HEAD, index, etc. `-` may also be
5353
specified as `<commit-ish>`; it is synonymous with `@{-1}`.
5454
+
55+
If <commit-ish> is a branch name (call it `<branch>` and is not found,
56+
and neither `-b` nor `-B` nor `--detach` are used, but there does
57+
exist a tracking branch in exactly one remote (call it `<remote>`)
58+
with a matching name, treat as equivalent to
59+
------------
60+
$ git worktree add --track -b <branch> <path> <remote>/<branch>
61+
------------
62+
+
5563
If `<commit-ish>` is omitted and neither `-b` nor `-B` nor `--detach` used,
5664
then, as a convenience, a new branch based at HEAD is created automatically,
5765
as if `-b $(basename <path>)` was specified.

builtin/worktree.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "cache.h"
2+
#include "checkout.h"
23
#include "config.h"
34
#include "builtin.h"
45
#include "dir.h"
@@ -390,6 +391,21 @@ static int add(int ac, const char **av, const char *prefix)
390391
opts.new_branch = xstrndup(s, n);
391392
}
392393

394+
if (ac == 2 && !opts.new_branch && !opts.detach) {
395+
struct object_id oid;
396+
struct commit *commit;
397+
const char *remote;
398+
399+
commit = lookup_commit_reference_by_name(branch);
400+
if (!commit) {
401+
remote = unique_tracking_name(branch, &oid);
402+
if (remote) {
403+
opts.new_branch = branch;
404+
branch = remote;
405+
}
406+
}
407+
}
408+
393409
if (opts.new_branch) {
394410
struct child_process cp = CHILD_PROCESS_INIT;
395411
cp.git_cmd = 1;

t/t2025-worktree-add.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,4 +365,23 @@ test_expect_success '--no-track avoids setting up tracking' '
365365
)
366366
'
367367

368+
test_expect_success '"add" <path> <non-existent-branch> fails' '
369+
test_must_fail git worktree add foo non-existent
370+
'
371+
372+
test_expect_success '"add" <path> <branch> dwims' '
373+
test_when_finished rm -rf repo_upstream repo_dwim foo &&
374+
setup_remote_repo repo_upstream repo_dwim &&
375+
git init repo_dwim &&
376+
(
377+
cd repo_dwim &&
378+
git worktree add ../foo foo
379+
) &&
380+
(
381+
cd foo &&
382+
test_branch_upstream foo repo_upstream foo &&
383+
test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
384+
)
385+
'
386+
368387
test_done

0 commit comments

Comments
 (0)