git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Thomas Gummerer <t.gummerer@gmail.com>
To: git@vger.kernel.org
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>,
	"Junio C Hamano" <gitster@pobox.com>,
	"Eric Sunshine" <sunshine@sunshineco.com>,
	"Thomas Gummerer" <t.gummerer@gmail.com>
Subject: [PATCH v4 0/4] make git worktree add dwim more
Date: Wed, 22 Nov 2017 22:30:16 +0000	[thread overview]
Message-ID: <20171122223020.2780-1-t.gummerer@gmail.com> (raw)
In-Reply-To: <mailto:20171118224706.13810-1-t.gummerer@gmail.com>

The previous rounds were at
https://public-inbox.org/git/20171112134305.3949-1-t.gummerer@gmail.com/,
https://public-inbox.org/git/20171118181103.28354-1-t.gummerer@gmail.com/ and
https://public-inbox.org/git/20171118224706.13810-1-t.gummerer@gmail.com/.

Thanks Eric for the review of the previous round.

The main change is that the --[no-]track flag now works generally
instead of just disabling the new dwim mode.

Interdiff below:
diff --git a/Documentation/git-worktree.txt b/Documentation/git-worktree.txt
index eedead2c4c..abc8f1f50d 100644
--- a/Documentation/git-worktree.txt
+++ b/Documentation/git-worktree.txt
@@ -56,22 +56,23 @@ If <branch> is not found, and neither `-b` nor `-B` nor `--detach` are
 used, but there does exist a tracking branch in exactly one remote
 (call it <remote>) with a matching name, treat as equivalent to
 ------------
-$ git worktree add -b <branch> <path> <remote>/<branch>
+$ git worktree add --track -b <branch> <path> <remote>/<branch>
 ------------
 +
 If `<branch>` is omitted and neither `-b` nor `-B` nor `--detach` used,
 then, as a convenience, if there exists a tracking branch in exactly
-one remote (call it <remote>) matching the basename of the path
-(call it <branch>), treat it as equivalent to
+one remote (call it `<remote>`) matching the basename of the path
+(call it `<branch>`), treat it as equivalent to
 ------------
-$ git worktree add -b <branch> <path> <remote>/<branch>
+$ git worktree add --track -b <branch> <path> <remote>/<branch>
 ------------
-If no tracking branch exists in exactly one remote, <branch> is
+If no tracking branch exists in exactly one remote, `<branch>` is
 created based on HEAD, as if `-b $(basename <path>)` was specified.
 +
 To disable the behaviour of trying to match the basename of <path> to
 a remote, and always create a new branch from HEAD, the `--no-track`
 flag can be passed to `git worktree add`.
+
 list::
 
 List details of each worktree.  The main worktree is listed first, followed by
@@ -123,6 +124,12 @@ OPTIONS
 	such as configuring sparse-checkout. See "Sparse checkout"
 	in linkgit:git-read-tree[1].
 
+--[no-]track::
+	With `--track` `<branch>` is set as "tracking" branch for
+	`<new-branch>`.  This is the default if `<branch>` is a remote
+	tracking branch, and can be suppressed with `--no-track`.  See
+	also linkgit:git-branch[1].
+
 --lock::
 	Keep the working tree locked after creation. This is the
 	equivalent of `git worktree lock` after `git worktree add`,
diff --git a/builtin/worktree.c b/builtin/worktree.c
index b2a6dd020c..cbcceb0385 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -342,7 +342,7 @@ static int add(int ac, const char **av, const char *prefix)
 	const char *new_branch_force = NULL;
 	char *path;
 	const char *branch;
-	int track_dwim = 1;
+	const char *opt_track = NULL;
 	struct option options[] = {
 		OPT__FORCE(&opts.force, N_("checkout <branch> even if already checked out in other worktree")),
 		OPT_STRING('b', NULL, &opts.new_branch, N_("branch"),
@@ -352,7 +352,9 @@ static int add(int ac, const char **av, const char *prefix)
 		OPT_BOOL(0, "detach", &opts.detach, N_("detach HEAD at named commit")),
 		OPT_BOOL(0, "checkout", &opts.checkout, N_("populate the new working tree")),
 		OPT_BOOL(0, "lock", &opts.keep_locked, N_("keep the new working tree locked")),
-		OPT_BOOL(0, "track", &track_dwim, N_("checkout upstream branch if there's a unique match")),
+		OPT_PASSTHRU(0, "track", &opt_track, NULL,
+			     N_("set up tracking mode (see git-branch(1))"),
+			     PARSE_OPT_NOARG | PARSE_OPT_OPTARG),
 		OPT_END()
 	};
 
@@ -387,7 +389,7 @@ static int add(int ac, const char **av, const char *prefix)
 		int n;
 		const char *s = worktree_basename(path, &n);
 		opts.new_branch = xstrndup(s, n);
-		if (track_dwim) {
+		if (!opt_track || strcmp(opt_track, "--no-track")) {
 			struct object_id oid;
 			const char *remote =
 				unique_tracking_name(opts.new_branch, &oid);
@@ -402,11 +404,12 @@ static int add(int ac, const char **av, const char *prefix)
 		const char *remote;
 
 		commit = lookup_commit_reference_by_name(branch);
-		if (!commit)
+		if (!commit) {
 			remote = unique_tracking_name(branch, &oid);
-		if (!commit && remote) {
-			opts.new_branch = branch;
-			branch = remote;
+			if (remote) {
+				opts.new_branch = branch;
+				branch = remote;
+			}
 		}
 	}
 
@@ -418,9 +421,13 @@ static int add(int ac, const char **av, const char *prefix)
 			argv_array_push(&cp.args, "--force");
 		argv_array_push(&cp.args, opts.new_branch);
 		argv_array_push(&cp.args, branch);
+		if (opt_track)
+			argv_array_push(&cp.args, opt_track);
 		if (run_command(&cp))
 			return -1;
 		branch = opts.new_branch;
+	} else if (opt_track) {
+		die(_("--[no-]track can only be used if a new branch is created"));
 	}
 
 	UNLEAK(path);
diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh
index 87e233f812..6fd3da4036 100755
--- a/t/t2025-worktree-add.sh
+++ b/t/t2025-worktree-add.sh
@@ -6,16 +6,6 @@ test_description='test git worktree add'
 
 . "$TEST_DIRECTORY"/lib-rebase.sh
 
-# Is branch "refs/heads/$1" set to pull from "$2/$3"?
-test_branch_upstream () {
-	printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
-	{
-		git config "branch.$1.remote" &&
-		git config "branch.$1.merge"
-	} >actual.upstream &&
-	test_cmp expect.upstream actual.upstream
-}
-
 test_expect_success 'setup' '
 	test_commit init
 '
@@ -323,32 +313,72 @@ test_expect_success 'checkout a branch under bisect' '
 test_expect_success 'rename a branch under bisect not allowed' '
 	test_must_fail git branch -M under-bisect bisect-with-new-name
 '
+# Is branch "refs/heads/$1" set to pull from "$2/$3"?
+test_branch_upstream () {
+	printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
+	{
+		git config "branch.$1.remote" &&
+		git config "branch.$1.merge"
+	} >actual.upstream &&
+	test_cmp expect.upstream actual.upstream
+}
 
-test_expect_success '"add" <path> <non-existent-branch> fails' '
-	test_must_fail git worktree add foo non-existent
+test_expect_success '--track sets up tracking' '
+	test_when_finished rm -rf track &&
+	git worktree add --track -b track track master &&
+	git config "branch.track.merge" &&
+	(
+		test_branch_upstream track . master
+	)
 '
 
-test_expect_success '"add" <path> <branch> dwims' '
-	test_when_finished rm -rf repo_upstream &&
-	test_when_finished rm -rf repo_dwim &&
-	test_when_finished rm -rf foo &&
-	git init repo_upstream &&
+# setup remote repository $1 and repository $2 with $1 set up as
+# remote.  The remote has two branches, master and foo.
+setup_remote_repo () {
+	git init $1 &&
 	(
-		cd repo_upstream &&
-		test_commit upstream_master &&
+		cd $1 &&
+		test_commit $1_master &&
 		git checkout -b foo &&
-		test_commit a_foo
+		test_commit upstream_foo
 	) &&
+	git init $2 &&
+	(
+		cd $2 &&
+		test_commit $2_master &&
+		git remote add $1 ../$1 &&
+		git config remote.$1.fetch \
+			"refs/heads/*:refs/remotes/$1/*" &&
+		git fetch --all
+	)
+}
+
+test_expect_success '--no-track avoids setting up tracking' '
+	test_when_finished rm -rf repo_upstream repo_local foo &&
+	setup_remote_repo repo_upstream repo_local &&
+	(
+		cd repo_local &&
+		git worktree add --no-track -b foo ../foo repo_upstream/foo
+	) &&
+	(
+		cd foo &&
+		! test_branch_upstream foo repo_upstream foo &&
+		git rev-parse repo_upstream/foo >expect &&
+		git rev-parse foo >actual &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success '"add" <path> <non-existent-branch> fails' '
+	test_must_fail git worktree add foo non-existent
+'
+
+test_expect_success '"add" <path> <branch> dwims' '
+	test_when_finished rm -rf repo_upstream repo_dwim foo &&
+	setup_remote_repo repo_upstream repo_dwim &&
 	git init repo_dwim &&
 	(
 		cd repo_dwim &&
-		test_commit dwim_master &&
-		git remote add repo_upstream ../repo_upstream &&
-		git config remote.repo_upstream.fetch \
-			  "refs/heads/*:refs/remotes/repo_upstream/*" &&
-		git fetch --all &&
-		test_must_fail git worktree add -b foo ../foo foo &&
-		test_must_fail git worktree add --detach ../foo foo &&
 		git worktree add ../foo foo
 	) &&
 	(
@@ -361,60 +391,32 @@ test_expect_success '"add" <path> <branch> dwims' '
 '
 
 test_expect_success 'git worktree add --no-track does not set up tracking' '
-	test_when_finished rm -rf repo_a &&
-	test_when_finished rm -rf repo_b &&
-	test_when_finished rm -rf foo &&
-	git init repo_a &&
-	(
-		cd repo_a &&
-		test_commit a_master &&
-		git checkout -b foo &&
-		test_commit a_foo
-	) &&
-	git init repo_b &&
+	test_when_finished rm -rf repo_a repo_b foo &&
+	setup_remote_repo repo_a repo_b &&
 	(
 		cd repo_b &&
-		test_commit b_master &&
-		git remote add repo_a ../repo_a &&
-		git config remote.repo_a.fetch \
-			"+refs/heads/*:refs/remotes/other_a/*" &&
-		git fetch --all &&
 		git worktree add --no-track ../foo
 	) &&
 	(
 		cd foo &&
 		! test_branch_upstream foo repo_a foo &&
-		git rev-parse other_a/foo >expect &&
+		git rev-parse repo_a/foo >expect &&
 		git rev-parse foo >actual &&
 		! test_cmp expect actual
 	)
 '
 
 test_expect_success 'git worktree add sets up tracking' '
-	test_when_finished rm -rf repo_a &&
-	test_when_finished rm -rf repo_b &&
-	test_when_finished rm -rf foo &&
-	git init repo_a &&
-	(
-		cd repo_a &&
-		test_commit a_master &&
-		git checkout -b foo &&
-		test_commit a_foo
-	) &&
-	git init repo_b &&
+	test_when_finished rm -rf repo_a repo_b &&
+	setup_remote_repo repo_a repo_b &&
 	(
 		cd repo_b &&
-		test_commit b_master &&
-		git remote add repo_a ../repo_a &&
-		git config remote.repo_a.fetch \
-			"+refs/heads/*:refs/remotes/other_a/*" &&
-		git fetch --all &&
 		git worktree add ../foo
 	) &&
 	(
 		cd foo &&
 		test_branch_upstream foo repo_a foo &&
-		git rev-parse other_a/foo >expect &&
+		git rev-parse repo_a/foo >expect &&
 		git rev-parse foo >actual &&
 		test_cmp expect actual
 	)


Thomas Gummerer (4):
  checkout: factor out functions to new lib file
  worktree: add --[no-]track option to the add subcommand
  worktree: make add <path> <branch> dwim
  worktree: make add <path> dwim

 Documentation/git-worktree.txt |  27 ++++++++++-
 Makefile                       |   1 +
 builtin/checkout.c             |  41 +---------------
 builtin/worktree.c             |  31 ++++++++++++
 checkout.c                     |  42 ++++++++++++++++
 checkout.h                     |  13 +++++
 t/t2025-worktree-add.sh        | 108 +++++++++++++++++++++++++++++++++++++++++
 7 files changed, 221 insertions(+), 42 deletions(-)
 create mode 100644 checkout.c
 create mode 100644 checkout.h

-- 
2.15.0.345.gf926f18f3


       reply	other threads:[~2017-11-22 22:30 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailto:20171118224706.13810-1-t.gummerer@gmail.com>
2017-11-22 22:30 ` Thomas Gummerer [this message]
2017-11-22 22:30   ` [PATCH v4 1/4] checkout: factor out functions to new lib file Thomas Gummerer
2017-11-24  6:47     ` Junio C Hamano
2017-11-22 22:30   ` [PATCH v4 2/4] worktree: add --[no-]track option to the add subcommand Thomas Gummerer
2017-11-24  6:57     ` Junio C Hamano
2017-11-25 16:58       ` Thomas Gummerer
2017-11-22 22:30   ` [PATCH v4 3/4] worktree: make add <path> <branch> dwim Thomas Gummerer
2017-11-24  6:59     ` Junio C Hamano
2017-11-22 22:30   ` [PATCH v4 4/4] worktree: make add <path> dwim Thomas Gummerer
2017-11-24  7:11     ` Junio C Hamano
2017-11-25 17:50       ` Thomas Gummerer
2017-11-25 18:26         ` Paul Smith
2017-11-25 20:06           ` Thomas Gummerer
2017-11-25 20:39             ` Randall S. Becker
2017-11-25 21:48               ` Thomas Gummerer
2017-11-25 23:11             ` Paul Smith
2017-11-26  3:35         ` Junio C Hamano
2017-11-26 11:37           ` Thomas Gummerer
2017-11-26 19:43   ` [PATCH v5 0/6] make git worktree add dwim more Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 1/6] checkout: factor out functions to new lib file Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 2/6] worktree: add can be created from any commit-ish Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 3/6] worktree: add --[no-]track option to the add subcommand Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 4/6] worktree: make add <path> <branch> dwim Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 5/6] worktree: add --guess-remote flag to add subcommand Thomas Gummerer
2017-11-27  6:36       ` Junio C Hamano
2017-11-27 20:56         ` Thomas Gummerer
2017-11-26 19:43     ` [PATCH v5 6/6] add worktree.guessRemote config option Thomas Gummerer
2017-11-27  6:45       ` Junio C Hamano
2017-11-27 20:59         ` Thomas Gummerer
2017-11-29 20:04     ` [PATCH v6 0/6] make git worktree add dwim more Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 1/6] checkout: factor out functions to new lib file Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 2/6] worktree: add can be created from any commit-ish Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 3/6] worktree: add --[no-]track option to the add subcommand Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 4/6] worktree: make add <path> <branch> dwim Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 5/6] worktree: add --guess-remote flag to add subcommand Thomas Gummerer
2017-11-29 20:04       ` [PATCH v6 6/6] add worktree.guessRemote config option Thomas Gummerer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171122223020.2780-1-t.gummerer@gmail.com \
    --to=t.gummerer@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=pclouds@gmail.com \
    --cc=sunshine@sunshineco.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).