Skip to content

Commit e92445a

Browse files
tgummerergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 71d6682 commit e92445a

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

Documentation/config.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,3 +3425,13 @@ web.browser::
34253425
Specify a web browser that may be used by some commands.
34263426
Currently only linkgit:git-instaweb[1] and linkgit:git-help[1]
34273427
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,9 @@ OPTIONS
121121
branch in exactly one remote matching the basename of `<path>,
122122
base the new branch on the remote-tracking branch, and mark
123123
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.
124127

125128
--[no-]track::
126129
When creating a new branch, if `<commit-ish>` is a branch,

builtin/worktree.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,19 @@ struct add_opts {
3333

3434
static int show_only;
3535
static int verbose;
36+
static int guess_remote;
3637
static timestamp_t expire;
3738

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+
3849
static int prune_worktree(const char *id, struct strbuf *reason)
3950
{
4051
struct stat st;
@@ -343,7 +354,6 @@ static int add(int ac, const char **av, const char *prefix)
343354
char *path;
344355
const char *branch;
345356
const char *opt_track = NULL;
346-
int guess_remote = 0;
347357
struct option options[] = {
348358
OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
349359
OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
@@ -591,7 +601,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
591601
OPT_END()
592602
};
593603

594-
git_config(git_default_config, NULL);
604+
git_config(git_worktree_config, NULL);
595605

596606
if (ac < 2)
597607
usage_with_options(worktree_usage, options);

t/t2025-worktree-add.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,35 @@ test_expect_success 'git worktree add --guess-remote sets up tracking' '
413413
)
414414
'
415415

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+
416447
test_done

0 commit comments

Comments
 (0)