add worktree.guessRemote config option
Some users might want to have the --guess-remote option introduced in the previous commit on by default, so they don't have to type it out every time they create a new worktree. Add a config option worktree.guessRemote that allows users to configure the default behaviour for themselves. Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Thomas Gummerer committed
Nov 29, 2017 at 20:04 UTC
e92445a73173cf21a11b8e79cb7be16485deec69
4 files changed
+56
-2
Documentation/config.txt
+10
@@ -3425,3 +3425,13 @@ web.browser::
3425
Specify a web browser that may be used by some commands.
3426
Currently only linkgit:git-instaweb[1] and linkgit:git-help[1]
3427
may use it.
3428
+
3429
+worktree.guessRemote::
3430
+ With `add`, if no branch argument, and neither of `-b` nor
3431
+ `-B` nor `--detach` are given, the command defaults to
3432
+ creating a new branch from HEAD. If `worktree.guessRemote` is
3433
+ set to true, `worktree add` tries to find a remote-tracking
3434
+ branch whose name uniquely matches the new branch name. If
3435
+ such a branch exists, it is checked out and set as "upstream"
3436
+ for the new branch. If no such match can be found, it falls
3437
+ back to creating a new branch from the current HEAD.
Documentation/git-worktree.txt
+3
@@ -121,6 +121,9 @@ OPTIONS
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
++
125
+This can also be set up as the default behaviour by using the
126
+`worktree.guessRemote` config option.
127
128
--[no-]track::
129
When creating a new branch, if `<commit-ish>` is a branch,
builtin/worktree.c
+12
-2
@@ -33,8 +33,19 @@ struct add_opts {
33
34
static int show_only;
35
static int verbose;
36
+static int guess_remote;
37
static timestamp_t expire;
38
39
+static int git_worktree_config(const char *var, const char *value, void *cb)
40
+{
41
+ if (!strcmp(var, "worktree.guessremote")) {
42
+ guess_remote = git_config_bool(var, value);
43
+ return 0;
44
+ }
45
+
46
+ return git_default_config(var, value, cb);
47
+}
48
+
49
static int prune_worktree(const char *id, struct strbuf *reason)
50
{
51
struct stat st;
@@ -343,7 +354,6 @@ static int add(int ac, const char **av, const char *prefix)
354
char *path;
355
const char *branch;
356
const char *opt_track = NULL;
346
- int guess_remote = 0;
357
struct option options[] = {
358
OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
359
OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
@@ -591,7 +601,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
601
OPT_END()
602
};
603
594
- git_config(git_default_config, NULL);
604
+ git_config(git_worktree_config, NULL);
605
606
if (ac < 2)
607
usage_with_options(worktree_usage, options);
t/t2025-worktree-add.sh
+31
@@ -413,4 +413,35 @@ test_expect_success 'git worktree add --guess-remote sets up tracking' '
413
)
414
'
415
416
+test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' '
417
+ test_when_finished rm -rf repo_a repo_b foo &&
418
+ setup_remote_repo repo_a repo_b &&
419
+ (
420
+ cd repo_b &&
421
+ git config worktree.guessRemote true &&
422
+ git worktree add ../foo
423
+ ) &&
424
+ (
425
+ cd foo &&
426
+ test_branch_upstream foo repo_a foo &&
427
+ test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
428
+ )
429
+'
430
+
431
+test_expect_success 'git worktree --no-guess-remote option overrides config' '
432
+ test_when_finished rm -rf repo_a repo_b foo &&
433
+ setup_remote_repo repo_a repo_b &&
434
+ (
435
+ cd repo_b &&
436
+ git config worktree.guessRemote true &&
437
+ git worktree add --no-guess-remote ../foo
438
+ ) &&
439
+ (
440
+ cd foo &&
441
+ test_must_fail git config "branch.foo.remote" &&
442
+ test_must_fail git config "branch.foo.merge" &&
443
+ ! test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
444
+ )
445
+'
446
+
447
test_done