Skip to content

Commit fc56361

Browse files
sunshinecogitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bdf0f37 commit fc56361

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

Documentation/git-worktree.txt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ git-worktree - Manage multiple worktrees
99
SYNOPSIS
1010
--------
1111
[verse]
12+
'git worktree add' <path> <branch>
1213
'git worktree prune' [-n] [-v] [--expire <expire>]
1314

1415
DESCRIPTION
1516
-----------
1617

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.
1919

2020
A git repository can support multiple working trees, allowing you to check
2121
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.
4545

4646
COMMANDS
4747
--------
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+
4854
prune::
4955

5056
Prune working tree information in $GIT_DIR/worktrees.
@@ -118,7 +124,7 @@ refactoring session.
118124

119125
------------
120126
$ git branch emergency-fix master
121-
$ git checkout --to ../temp emergency-fix
127+
$ git worktree add ../temp emergency-fix
122128
$ pushd ../temp
123129
# ... hack hack hack ...
124130
$ git commit -a -m 'emergency fix for boss'
@@ -133,21 +139,15 @@ Multiple checkout support for submodules is incomplete. It is NOT
133139
recommended to make multiple checkouts of a superproject.
134140

135141
git-worktree could provide more automation for tasks currently
136-
performed manually or via other commands, such as:
142+
performed manually, such as:
137143

138-
- `add` to create a new linked worktree
139144
- `remove` to remove a linked worktree and its administrative files (and
140145
warn if the worktree is dirty)
141146
- `mv` to move or rename a worktree and update its administrative files
142147
- `list` to list linked worktrees
143148
- `lock` to prevent automatic pruning of administrative files (for instance,
144149
for a worktree on a portable device)
145150

146-
SEE ALSO
147-
--------
148-
149-
linkgit:git-checkout[1]
150-
151151
GIT
152152
---
153153
Part of the linkgit:git[1] suite

builtin/worktree.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
#include "builtin.h"
33
#include "dir.h"
44
#include "parse-options.h"
5+
#include "argv-array.h"
6+
#include "run-command.h"
57

68
static const char * const worktree_usage[] = {
9+
N_("git worktree add <path> <branch>"),
710
N_("git worktree prune [<options>]"),
811
NULL
912
};
@@ -119,6 +122,32 @@ static int prune(int ac, const char **av, const char *prefix)
119122
return 0;
120123
}
121124

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+
122151
int cmd_worktree(int ac, const char **av, const char *prefix)
123152
{
124153
struct option options[] = {
@@ -127,6 +156,8 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
127156

128157
if (ac < 2)
129158
usage_with_options(worktree_usage, options);
159+
if (!strcmp(av[1], "add"))
160+
return add(ac - 1, av + 1, prefix);
130161
if (!strcmp(av[1], "prune"))
131162
return prune(ac - 1, av + 1, prefix);
132163
usage_with_options(worktree_usage, options);

0 commit comments

Comments
 (0)