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 <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Thomas Gummerer committed
Nov 26, 2017 at 19:43 UTC
4e8533319760c1e9255c56c2059c721286dc8dab
3 files changed
+43
Documentation/git-worktree.txt
+8
@@ -52,6 +52,14 @@ is linked to the current repository, sharing everything except working
52
directory specific files such as HEAD, index, etc. `-` may also be
53
specified as `<commit-ish>`; it is synonymous with `@{-1}`.
54
+
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
++
63
If `<commit-ish>` is omitted and neither `-b` nor `-B` nor `--detach` used,
64
then, as a convenience, a new branch based at HEAD is created automatically,
65
as if `-b $(basename <path>)` was specified.
builtin/worktree.c
+16
@@ -1,4 +1,5 @@
1
#include "cache.h"
2
+#include "checkout.h"
3
#include "config.h"
4
#include "builtin.h"
5
#include "dir.h"
@@ -390,6 +391,21 @@ static int add(int ac, const char **av, const char *prefix)
391
opts.new_branch = xstrndup(s, n);
392
}
393
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
+
409
if (opts.new_branch) {
410
struct child_process cp = CHILD_PROCESS_INIT;
411
cp.git_cmd = 1;
t/t2025-worktree-add.sh
+19
@@ -365,4 +365,23 @@ test_expect_success '--no-track avoids setting up tracking' '
365
)
366
'
367
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
+
387
test_done