worktree: introduce "add" command

The plan is to relocate "git checkout --to" functionality to "git worktree add". As a first step, introduce a bare-bones git-worktree "add" command along with documentation. At this stage, "git worktree add" merely invokes "git checkout --to" behind the scenes, but an upcoming patch will move the actual functionality (checkout.c:prepare_linked_checkout() and its helpers) to worktree.c. Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Jul 6, 2015 at 13:30 UTC fc56361f58a20f1259e1ba2364424ec27825c37f
2 files changed +41 -10
Documentation/git-worktree.txt
+10 -10
@@ -9,13 +9,13 @@ git-worktree - Manage multiple worktrees
9 SYNOPSIS
10 --------
11 [verse]
12 +'git worktree add' <path> <branch>
13 'git worktree prune' [-n] [-v] [--expire <expire>]
14
15 DESCRIPTION
16 -----------
17
17 -Manage multiple worktrees attached to the same repository. These are
18 -created by the command `git checkout --to`.
18 +Manage multiple worktrees attached to the same repository.
19
20 A git repository can support multiple working trees, allowing you to check
21 out more than one branch at a time. With `git checkout --to` a new working
@@ -45,6 +45,12 @@ pruning should be suppressed. See section "DETAILS" for more information.
45
46 COMMANDS
47 --------
48 +add <path> <branch>::
49 +
50 +Create `<path>` and checkout `<branch>` into it. The new working directory
51 +is linked to the current repository, sharing everything except working
52 +directory specific files such as HEAD, index, etc.
53 +
54 prune::
55
56 Prune working tree information in $GIT_DIR/worktrees.
@@ -118,7 +124,7 @@ refactoring session.
124
125 ------------
126 $ git branch emergency-fix master
121 -$ git checkout --to ../temp emergency-fix
127 +$ git worktree add ../temp emergency-fix
128 $ pushd ../temp
129 # ... hack hack hack ...
130 $ git commit -a -m 'emergency fix for boss'
@@ -133,9 +139,8 @@ Multiple checkout support for submodules is incomplete. It is NOT
139 recommended to make multiple checkouts of a superproject.
140
141 git-worktree could provide more automation for tasks currently
136 -performed manually or via other commands, such as:
142 +performed manually, such as:
143
138 -- `add` to create a new linked worktree
144 - `remove` to remove a linked worktree and its administrative files (and
145 warn if the worktree is dirty)
146 - `mv` to move or rename a worktree and update its administrative files
@@ -143,11 +148,6 @@ performed manually or via other commands, such as:
148 - `lock` to prevent automatic pruning of administrative files (for instance,
149 for a worktree on a portable device)
150
146 -SEE ALSO
147 ---------
148 -
149 -linkgit:git-checkout[1]
150 -
151 GIT
152 ---
153 Part of the linkgit:git[1] suite
builtin/worktree.c
+31
@@ -2,8 +2,11 @@
2 #include "builtin.h"
3 #include "dir.h"
4 #include "parse-options.h"
5 +#include "argv-array.h"
6 +#include "run-command.h"
7
8 static const char * const worktree_usage[] = {
9 + N_("git worktree add <path> <branch>"),
10 N_("git worktree prune [<options>]"),
11 NULL
12 };
@@ -119,6 +122,32 @@ static int prune(int ac, const char **av, const char *prefix)
122 return 0;
123 }
124
125 +static int add(int ac, const char **av, const char *prefix)
126 +{
127 + struct child_process c;
128 + const char *path, *branch;
129 + struct argv_array cmd = ARGV_ARRAY_INIT;
130 + struct option options[] = {
131 + OPT_END()
132 + };
133 +
134 + ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
135 + if (ac != 2)
136 + usage_with_options(worktree_usage, options);
137 +
138 + path = prefix ? prefix_filename(prefix, strlen(prefix), av[0]) : av[0];
139 + branch = av[1];
140 +
141 + argv_array_push(&cmd, "checkout");
142 + argv_array_pushl(&cmd, "--to", path, NULL);
143 + argv_array_push(&cmd, branch);
144 +
145 + memset(&c, 0, sizeof(c));
146 + c.git_cmd = 1;
147 + c.argv = cmd.argv;
148 + return run_command(&c);
149 +}
150 +
151 int cmd_worktree(int ac, const char **av, const char *prefix)
152 {
153 struct option options[] = {
@@ -127,6 +156,8 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
156
157 if (ac < 2)
158 usage_with_options(worktree_usage, options);
159 + if (!strcmp(av[1], "add"))
160 + return add(ac - 1, av + 1, prefix);
161 if (!strcmp(av[1], "prune"))
162 return prune(ac - 1, av + 1, prefix);
163 usage_with_options(worktree_usage, options);