With the newly-introduced --worktree option, after a new branch was
created we will add a corresponding new worktree with the same name
automatically.

Signed-off-by: Johannes Schindelin <johannes.schinde...@gmx.de>
---
 Documentation/git-branch.txt |  5 +++--
 builtin/branch.c             | 27 +++++++++++++++++++++++++--
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 4a7037f..963cdcb 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -13,7 +13,7 @@ SYNOPSIS
        [--column[=<options>] | --no-column]
        [(--merged | --no-merged | --contains) [<commit>]] [--sort=<key>]
        [--points-at <object>] [<pattern>...]
-'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> 
[<start-point>]
+'git branch' [--set-upstream | --track | --no-track] [-l] [-f] [-w | 
--worktree] <branchname> [<start-point>]
 'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
 'git branch' --unset-upstream [<branchname>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -46,7 +46,8 @@ which points to the current 'HEAD', or <start-point> if given.
 
 Note that this will create the new branch, but it will not switch the
 working tree to it; use "git checkout <newbranch>" to switch to the
-new branch.
+new branch, or use the `--worktree` option to add a new worktree of
+the same name.
 
 When a local branch is started off a remote-tracking branch, Git sets up the
 branch (specifically the `branch.<name>.remote` and `branch.<name>.merge`
diff --git a/builtin/branch.c b/builtin/branch.c
index 7b45b6b..c681b2e 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -20,6 +20,7 @@
 #include "utf8.h"
 #include "wt-status.h"
 #include "ref-filter.h"
+#include "run-command.h"
 
 static const char * const builtin_branch_usage[] = {
        N_("git branch [<options>] [-r | -a] [--merged | --no-merged]"),
@@ -605,7 +606,7 @@ int cmd_branch(int argc, const char **argv, const char 
*prefix)
 {
        int delete = 0, rename = 0, force = 0, list = 0;
        int reflog = 0, edit_description = 0;
-       int quiet = 0, unset_upstream = 0;
+       int quiet = 0, unset_upstream = 0, new_worktree = 0;
        const char *new_upstream = NULL;
        enum branch_track track;
        struct ref_filter filter;
@@ -622,6 +623,7 @@ int cmd_branch(int argc, const char **argv, const char 
*prefix)
                        BRANCH_TRACK_OVERRIDE),
                OPT_STRING('u', "set-upstream-to", &new_upstream, "upstream", 
"change the upstream info"),
                OPT_BOOL(0, "unset-upstream", &unset_upstream, "Unset the 
upstream info"),
+               OPT_BIT('w', "worktree", &new_worktree, N_("add worktree for 
the new branch"), 1),
                OPT__COLOR(&branch_use_color, N_("use colored output")),
                OPT_SET_INT('r', "remotes",     &filter.kind, N_("act on 
remote-tracking branches"),
                        FILTER_REFS_REMOTES),
@@ -678,6 +680,13 @@ int cmd_branch(int argc, const char **argv, const char 
*prefix)
        if (!delete && !rename && !edit_description && !new_upstream && 
!unset_upstream && argc == 0)
                list = 1;
 
+       if (new_worktree) {
+               if (delete || rename || new_upstream || unset_upstream)
+                       die("--worktree requires creating a new branch");
+               if (new_worktree && (argc < 1 || argc > 2))
+                       die(_("--worktree needs a branch/worktree name"));
+       }
+
        if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || 
filter.points_at.nr)
                list = 1;
 
@@ -797,7 +806,7 @@ int cmd_branch(int argc, const char **argv, const char 
*prefix)
                strbuf_release(&buf);
        } else if (argc > 0 && argc <= 2) {
                struct branch *branch = branch_get(argv[0]);
-               int branch_existed = 0, remote_tracking = 0;
+               int branch_existed = 0, remote_tracking = 0, res = 0;
                struct strbuf buf = STRBUF_INIT;
 
                if (!strcmp(argv[0], "HEAD"))
@@ -820,6 +829,17 @@ int cmd_branch(int argc, const char **argv, const char 
*prefix)
                create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
                              force, reflog, 0, quiet, track);
 
+               if (new_worktree) {
+                       const char *child_argv[] = {
+                               "worktree", "add", NULL, NULL, NULL
+                       };
+                       child_argv[2] = child_argv[3] = argv[0];
+                       if ((res = run_command_v_opt(child_argv, RUN_GIT_CMD)))
+                               error(_("Could not create worktree %s"), 
argv[0]);
+                       else
+                               fprintf(stderr, _("New worktree set up at 
%s\n"), argv[0]);
+               }
+
                /*
                 * We only show the instructions if the user gave us
                 * one branch which doesn't exist locally, but is the
@@ -828,10 +848,13 @@ int cmd_branch(int argc, const char **argv, const char 
*prefix)
                if (argc == 1 && track == BRANCH_TRACK_OVERRIDE &&
                    !branch_existed && remote_tracking) {
                        fprintf(stderr, _("\nIf you wanted to make '%s' track 
'%s', do this:\n\n"), head, branch->name);
+                       if (new_worktree)
+                               fprintf(stderr, _("    # remove worktree 
%s/\n"), branch->name);
                        fprintf(stderr, _("    git branch -d %s\n"), 
branch->name);
                        fprintf(stderr, _("    git branch --set-upstream-to 
%s\n"), branch->name);
                }
 
+               return res;
        } else
                usage_with_options(builtin_branch_usage, options);
 
-- 
2.7.2.windows.1.8.g47d64e6.dirty
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to