Skip to content

Commit b979d95

Browse files
sunshinecogitster
authored andcommitted
checkout: retire --to option
Now that "git worktree add" has achieved user-facing feature-parity with "git checkout --to", retire the latter. Move the actual linked worktree creation functionality, prepare_linked_checkout() and its helpers, verbatim from checkout.c to worktree.c. This effectively reverts changes to checkout.c by 529fef2 (checkout: support checking out into a new working directory, 2014-11-30) with the exception of merge_working_tree() and switch_branches() which still require specialized knowledge that a the checkout is occurring in a newly-created linked worktree (signaled to them by the private GIT_CHECKOUT_NEW_WORKTREE environment variable). Signed-off-by: Eric Sunshine <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f194b1e commit b979d95

File tree

3 files changed

+139
-173
lines changed

3 files changed

+139
-173
lines changed

Documentation/git-checkout.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,6 @@ This means that you can use `git checkout -p` to selectively discard
225225
edits from your current working tree. See the ``Interactive Mode''
226226
section of linkgit:git-add[1] to learn how to operate the `--patch` mode.
227227

228-
--to=<path>::
229-
Check out a branch in a separate working directory at
230-
`<path>`. A new working directory is linked to the current
231-
repository, sharing everything except working directory
232-
specific files such as HEAD, index, etc. See
233-
linkgit:git-worktree[1] for a description of linked worktrees.
234-
235228
--ignore-other-worktrees::
236229
`git checkout` refuses when the wanted ref is already checked
237230
out by another worktree. This option makes it check the ref

builtin/checkout.c

Lines changed: 1 addition & 160 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include "ll-merge.h"
2020
#include "resolve-undo.h"
2121
#include "submodule.h"
22-
#include "argv-array.h"
23-
#include "sigchain.h"
2422

2523
static const char * const checkout_usage[] = {
2624
N_("git checkout [options] <branch>"),
@@ -51,8 +49,6 @@ struct checkout_opts {
5149
struct pathspec pathspec;
5250
struct tree *source_tree;
5351

54-
const char *new_worktree;
55-
const char **saved_argv;
5652
int new_worktree_mode;
5753
};
5854

@@ -255,9 +251,6 @@ static int checkout_paths(const struct checkout_opts *opts,
255251
die(_("Cannot update paths and switch to branch '%s' at the same time."),
256252
opts->new_branch);
257253

258-
if (opts->new_worktree)
259-
die(_("'%s' cannot be used with updating paths"), "--to");
260-
261254
if (opts->patch_mode)
262255
return run_add_interactive(revision, "--patch=checkout",
263256
&opts->pathspec);
@@ -825,142 +818,6 @@ static int switch_branches(const struct checkout_opts *opts,
825818
return ret || writeout_error;
826819
}
827820

828-
static char *junk_work_tree;
829-
static char *junk_git_dir;
830-
static int is_junk;
831-
static pid_t junk_pid;
832-
833-
static void remove_junk(void)
834-
{
835-
struct strbuf sb = STRBUF_INIT;
836-
if (!is_junk || getpid() != junk_pid)
837-
return;
838-
if (junk_git_dir) {
839-
strbuf_addstr(&sb, junk_git_dir);
840-
remove_dir_recursively(&sb, 0);
841-
strbuf_reset(&sb);
842-
}
843-
if (junk_work_tree) {
844-
strbuf_addstr(&sb, junk_work_tree);
845-
remove_dir_recursively(&sb, 0);
846-
}
847-
strbuf_release(&sb);
848-
}
849-
850-
static void remove_junk_on_signal(int signo)
851-
{
852-
remove_junk();
853-
sigchain_pop(signo);
854-
raise(signo);
855-
}
856-
857-
static int prepare_linked_checkout(const char *path, const char **child_argv)
858-
{
859-
struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT;
860-
struct strbuf sb = STRBUF_INIT;
861-
const char *name;
862-
struct stat st;
863-
struct child_process cp;
864-
int counter = 0, len, ret;
865-
unsigned char rev[20];
866-
867-
if (file_exists(path) && !is_empty_dir(path))
868-
die(_("'%s' already exists"), path);
869-
870-
len = strlen(path);
871-
while (len && is_dir_sep(path[len - 1]))
872-
len--;
873-
874-
for (name = path + len - 1; name > path; name--)
875-
if (is_dir_sep(*name)) {
876-
name++;
877-
break;
878-
}
879-
strbuf_addstr(&sb_repo,
880-
git_path("worktrees/%.*s", (int)(path + len - name), name));
881-
len = sb_repo.len;
882-
if (safe_create_leading_directories_const(sb_repo.buf))
883-
die_errno(_("could not create leading directories of '%s'"),
884-
sb_repo.buf);
885-
while (!stat(sb_repo.buf, &st)) {
886-
counter++;
887-
strbuf_setlen(&sb_repo, len);
888-
strbuf_addf(&sb_repo, "%d", counter);
889-
}
890-
name = strrchr(sb_repo.buf, '/') + 1;
891-
892-
junk_pid = getpid();
893-
atexit(remove_junk);
894-
sigchain_push_common(remove_junk_on_signal);
895-
896-
if (mkdir(sb_repo.buf, 0777))
897-
die_errno(_("could not create directory of '%s'"), sb_repo.buf);
898-
junk_git_dir = xstrdup(sb_repo.buf);
899-
is_junk = 1;
900-
901-
/*
902-
* lock the incomplete repo so prune won't delete it, unlock
903-
* after the preparation is over.
904-
*/
905-
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
906-
write_file(sb.buf, 1, "initializing\n");
907-
908-
strbuf_addf(&sb_git, "%s/.git", path);
909-
if (safe_create_leading_directories_const(sb_git.buf))
910-
die_errno(_("could not create leading directories of '%s'"),
911-
sb_git.buf);
912-
junk_work_tree = xstrdup(path);
913-
914-
strbuf_reset(&sb);
915-
strbuf_addf(&sb, "%s/gitdir", sb_repo.buf);
916-
write_file(sb.buf, 1, "%s\n", real_path(sb_git.buf));
917-
write_file(sb_git.buf, 1, "gitdir: %s/worktrees/%s\n",
918-
real_path(get_git_common_dir()), name);
919-
/*
920-
* This is to keep resolve_ref() happy. We need a valid HEAD
921-
* or is_git_directory() will reject the directory. Moreover, HEAD
922-
* in the new worktree must resolve to the same value as HEAD in
923-
* the current tree since the command invoked to populate the new
924-
* worktree will be handed the branch/ref specified by the user.
925-
* For instance, if the user asks for the new worktree to be based
926-
* at HEAD~5, then the resolved HEAD~5 in the new worktree must
927-
* match the resolved HEAD~5 in the current tree in order to match
928-
* the user's expectation.
929-
*/
930-
if (!resolve_ref_unsafe("HEAD", 0, rev, NULL))
931-
die(_("unable to resolve HEAD"));
932-
strbuf_reset(&sb);
933-
strbuf_addf(&sb, "%s/HEAD", sb_repo.buf);
934-
write_file(sb.buf, 1, "%s\n", sha1_to_hex(rev));
935-
strbuf_reset(&sb);
936-
strbuf_addf(&sb, "%s/commondir", sb_repo.buf);
937-
write_file(sb.buf, 1, "../..\n");
938-
939-
fprintf_ln(stderr, _("Enter %s (identifier %s)"), path, name);
940-
941-
setenv("GIT_CHECKOUT_NEW_WORKTREE", "1", 1);
942-
setenv(GIT_DIR_ENVIRONMENT, sb_git.buf, 1);
943-
setenv(GIT_WORK_TREE_ENVIRONMENT, path, 1);
944-
memset(&cp, 0, sizeof(cp));
945-
cp.git_cmd = 1;
946-
cp.argv = child_argv;
947-
ret = run_command(&cp);
948-
if (!ret) {
949-
is_junk = 0;
950-
free(junk_work_tree);
951-
free(junk_git_dir);
952-
junk_work_tree = NULL;
953-
junk_git_dir = NULL;
954-
}
955-
strbuf_reset(&sb);
956-
strbuf_addf(&sb, "%s/locked", sb_repo.buf);
957-
unlink_or_warn(sb.buf);
958-
strbuf_release(&sb);
959-
strbuf_release(&sb_repo);
960-
strbuf_release(&sb_git);
961-
return ret;
962-
}
963-
964821
static int git_checkout_config(const char *var, const char *value, void *cb)
965822
{
966823
if (!strcmp(var, "diff.ignoresubmodules")) {
@@ -1299,13 +1156,6 @@ static int checkout_branch(struct checkout_opts *opts,
12991156
free(head_ref);
13001157
}
13011158

1302-
if (opts->new_worktree) {
1303-
if (!new->commit)
1304-
die(_("no branch specified"));
1305-
return prepare_linked_checkout(opts->new_worktree,
1306-
opts->saved_argv);
1307-
}
1308-
13091159
if (!new->commit && opts->new_branch) {
13101160
unsigned char rev[20];
13111161
int flag;
@@ -1348,8 +1198,6 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
13481198
N_("do not limit pathspecs to sparse entries only")),
13491199
OPT_HIDDEN_BOOL(0, "guess", &dwim_new_local_branch,
13501200
N_("second guess 'git checkout no-such-branch'")),
1351-
OPT_FILENAME(0, "to", &opts.new_worktree,
1352-
N_("check a branch out in a separate working directory")),
13531201
OPT_BOOL(0, "ignore-other-worktrees", &opts.ignore_other_worktrees,
13541202
N_("do not check if another worktree is holding the given ref")),
13551203
OPT_END(),
@@ -1360,9 +1208,6 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
13601208
opts.overwrite_ignore = 1;
13611209
opts.prefix = prefix;
13621210

1363-
opts.saved_argv = xmalloc(sizeof(const char *) * (argc + 2));
1364-
memcpy(opts.saved_argv, argv, sizeof(const char *) * (argc + 1));
1365-
13661211
gitmodules_config();
13671212
git_config(git_checkout_config, &opts);
13681213

@@ -1371,13 +1216,9 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
13711216
argc = parse_options(argc, argv, prefix, options, checkout_usage,
13721217
PARSE_OPT_KEEP_DASHDASH);
13731218

1374-
/* recursive execution from checkout_new_worktree() */
13751219
opts.new_worktree_mode = getenv("GIT_CHECKOUT_NEW_WORKTREE") != NULL;
1376-
if (opts.new_worktree_mode)
1377-
opts.new_worktree = NULL;
13781220

1379-
if (!opts.new_worktree)
1380-
setup_work_tree();
1221+
setup_work_tree();
13811222

13821223
if (conflict_style) {
13831224
opts.merge = 1; /* implied */

0 commit comments

Comments
 (0)