From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org
Cc: "Junio C Hamano" <gitster@pobox.com>,
"Max Kirillov" <max@max630.net>,
"Eric Sunshine" <sunshine@sunshineco.com>,
git@drmicha.warpmail.net,
"Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH v2 4/8] checkout: no auto-detach if the ref is already checked out
Date: Tue, 29 Jul 2014 20:50:27 +0700 [thread overview]
Message-ID: <1406641831-2390-5-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1406641831-2390-1-git-send-email-pclouds@gmail.com>
Give the user a choice in this case: detach, make a new branch,
release current branch in the other checkout or simply 'cd' there and
continue to work.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
Documentation/config.txt | 4 +++
advice.c | 2 ++
advice.h | 1 +
builtin/checkout.c | 76 ++++++++++++++++++++++++++----------------------
t/t2025-checkout-to.sh | 16 +++++-----
5 files changed, 55 insertions(+), 44 deletions(-)
diff --git a/Documentation/config.txt b/Documentation/config.txt
index 57999fa..b2c3388 100644
--- a/Documentation/config.txt
+++ b/Documentation/config.txt
@@ -201,6 +201,10 @@ advice.*::
rmHints::
In case of failure in the output of linkgit:git-rm[1],
show directions on how to proceed from the current state.
+ checkoutLocked::
+ In multiple checkout setup, attempting to checkout a
+ branch already checked out elsewhere will fail. Show
+ some useful options to proceed.
--
core.fileMode::
diff --git a/advice.c b/advice.c
index 9b42033..cf3b0f7 100644
--- a/advice.c
+++ b/advice.c
@@ -15,6 +15,7 @@ int advice_detached_head = 1;
int advice_set_upstream_failure = 1;
int advice_object_name_warning = 1;
int advice_rm_hints = 1;
+int advice_checkout_locked = 1;
static struct {
const char *name;
@@ -35,6 +36,7 @@ static struct {
{ "setupstreamfailure", &advice_set_upstream_failure },
{ "objectnamewarning", &advice_object_name_warning },
{ "rmhints", &advice_rm_hints },
+ { "checkoutlocked", &advice_checkout_locked },
/* make this an alias for backward compatibility */
{ "pushnonfastforward", &advice_push_update_rejected }
diff --git a/advice.h b/advice.h
index 5ecc6c1..935631d 100644
--- a/advice.h
+++ b/advice.h
@@ -18,6 +18,7 @@ extern int advice_detached_head;
extern int advice_set_upstream_failure;
extern int advice_object_name_warning;
extern int advice_rm_hints;
+extern int advice_checkout_locked;
int git_default_advice_config(const char *var, const char *value);
__attribute__((format (printf, 1, 2)))
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 6ac89eb..0714856 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -645,11 +645,6 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
if (old->path && advice_detached_head)
detach_advice(new->name);
describe_detached_head(_("HEAD is now at"), new->commit);
- if (new->checkout && !*new->checkout)
- fprintf(stderr, _("hint: the main checkout is holding this branch\n"));
- else if (new->checkout)
- fprintf(stderr, _("hint: the linked checkout %s is holding this branch\n"),
- new->checkout);
}
} else if (new->path) { /* Switch branches. */
create_symref("HEAD", new->path, msg.buf);
@@ -1006,31 +1001,50 @@ static const char *unique_tracking_name(const char *name, unsigned char *sha1)
return NULL;
}
-static int check_linked_checkout(struct branch_info *new,
- const char *name, const char *path)
+static void check_linked_checkout(struct branch_info *new, const char *id)
{
struct strbuf sb = STRBUF_INIT;
+ struct strbuf path = STRBUF_INIT;
+ struct strbuf gitdir = STRBUF_INIT;
const char *start, *end;
- if (strbuf_read_file(&sb, path, 0) < 0 ||
- !skip_prefix(sb.buf, "ref:", &start)) {
- strbuf_release(&sb);
- return 0;
- }
+ if (id)
+ strbuf_addf(&path, "%s/repos/%s/HEAD", get_git_common_dir(), id);
+ else
+ strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
+
+ if (strbuf_read_file(&sb, path.buf, 0) < 0 ||
+ !skip_prefix(sb.buf, "ref:", &start))
+ goto done;
while (isspace(*start))
start++;
end = start;
while (*end && !isspace(*end))
end++;
- if (!strncmp(start, new->path, end - start) &&
- new->path[end - start] == '\0') {
- strbuf_release(&sb);
- new->path = NULL; /* detach */
- new->checkout = xstrdup(name); /* reason */
- return 1;
- }
+ if (strncmp(start, new->path, end - start) || new->path[end - start] != '\0')
+ goto done;
+ if (id) {
+ strbuf_reset(&path);
+ strbuf_addf(&path, "%s/repos/%s/gitdir", get_git_common_dir(), id);
+ if (strbuf_read_file(&gitdir, path.buf, 0) <= 0)
+ goto done;
+ strbuf_rtrim(&gitdir);
+ } else
+ strbuf_addstr(&gitdir, get_git_common_dir());
+ if (advice_checkout_locked)
+ die(_("'%s' is already checked out at %s\n"
+ "Either go there and continue working, or detach HEAD using\n"
+ " git checkout --detach [more options] %s\n"
+ "or create a new branch based off '%s' using\n"
+ " git checkout -b <branch name> [more options] %s\n"
+ "or switch to another branch at the other checkout and retry."),
+ new->name, gitdir.buf, new->name, new->name, new->name);
+ else
+ die(_("'%s' is already checked out at %s"), new->name, gitdir.buf);
+done:
+ strbuf_release(&path);
strbuf_release(&sb);
- return 0;
+ strbuf_release(&gitdir);
}
static void check_linked_checkouts(struct branch_info *new)
@@ -1045,27 +1059,17 @@ static void check_linked_checkouts(struct branch_info *new)
return;
}
- strbuf_reset(&path);
- strbuf_addf(&path, "%s/HEAD", get_git_common_dir());
/*
* $GIT_COMMON_DIR/HEAD is practically outside
* $GIT_DIR so resolve_ref_unsafe() won't work (it
* uses git_path). Parse the ref ourselves.
*/
- if (check_linked_checkout(new, "", path.buf)) {
- strbuf_release(&path);
- closedir(dir);
- return;
- }
+ check_linked_checkout(new, NULL);
while ((d = readdir(dir)) != NULL) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
- strbuf_reset(&path);
- strbuf_addf(&path, "%s/repos/%s/HEAD",
- get_git_common_dir(), d->d_name);
- if (check_linked_checkout(new, d->d_name, path.buf))
- break;
+ check_linked_checkout(new, d->d_name);
}
strbuf_release(&path);
closedir(dir);
@@ -1076,7 +1080,8 @@ static int parse_branchname_arg(int argc, const char **argv,
struct branch_info *new,
struct tree **source_tree,
unsigned char rev[20],
- const char **new_branch)
+ const char **new_branch,
+ int force_detach)
{
int argcount = 0;
unsigned char branch_rev[20];
@@ -1198,7 +1203,7 @@ static int parse_branchname_arg(int argc, const char **argv,
else
new->path = NULL; /* not an existing branch */
- if (new->path) {
+ if (new->path && !force_detach && !*new_branch) {
unsigned char sha1[20];
int flag;
char *head_ref = resolve_refdup("HEAD", sha1, 0, &flag);
@@ -1417,7 +1422,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
!opts.new_branch;
int n = parse_branchname_arg(argc, argv, dwim_ok,
&new, &opts.source_tree,
- rev, &opts.new_branch);
+ rev, &opts.new_branch,
+ opts.force_detach);
argv += n;
argc -= n;
}
diff --git a/t/t2025-checkout-to.sh b/t/t2025-checkout-to.sh
index b0d97a0..c6601a4 100755
--- a/t/t2025-checkout-to.sh
+++ b/t/t2025-checkout-to.sh
@@ -14,7 +14,7 @@ test_expect_success 'checkout --to not updating paths' '
test_expect_success 'checkout --to a new worktree' '
git rev-parse HEAD >expect &&
- git checkout --to here master &&
+ git checkout --detach --to here master &&
(
cd here &&
test_cmp ../init.t init.t &&
@@ -28,7 +28,7 @@ test_expect_success 'checkout --to a new worktree' '
test_expect_success 'checkout --to from a linked checkout' '
(
cd here &&
- git checkout --to nested-here master
+ git checkout --detach --to nested-here master
cd nested-here &&
git fsck
)
@@ -46,19 +46,17 @@ test_expect_success 'checkout --to a new worktree creating new branch' '
)
'
-test_expect_success 'detach if the same branch is already checked out' '
+test_expect_success 'die the same branch is already checked out' '
(
cd here &&
- git checkout newmaster &&
- test_must_fail git symbolic-ref HEAD
+ test_must_fail git checkout newmaster
)
'
-test_expect_success 'not detach on re-checking out current branch' '
+test_expect_success 'not die on re-checking out current branch' '
(
cd there &&
- git checkout newmaster &&
- git symbolic-ref HEAD
+ git checkout newmaster
)
'
@@ -66,7 +64,7 @@ test_expect_success 'checkout --to from a bare repo' '
(
git clone --bare . bare &&
cd bare &&
- git checkout --to ../there2 master
+ git checkout --to ../there2 -b bare-master master
)
'
--
2.1.0.rc0.78.gc0d8480
next prev parent reply other threads:[~2014-07-29 13:53 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-07-23 11:43 [PATCH 0/5] nd/multiple-work-trees follow-ups Nguyễn Thái Ngọc Duy
2014-07-23 11:43 ` [PATCH 1/5] gitrepository-layout.txt: s/ignored/ignored if/ Nguyễn Thái Ngọc Duy
2014-07-23 11:43 ` [PATCH 2/5] prune --repos: fix uninitialized access Nguyễn Thái Ngọc Duy
2014-07-23 19:59 ` Junio C Hamano
2014-07-24 10:14 ` Duy Nguyen
2014-07-23 11:43 ` [PATCH 3/5] checkout --to: no auto-detach if the ref is already checked out Nguyễn Thái Ngọc Duy
2014-07-23 13:48 ` Michael J Gruber
2014-07-23 17:46 ` Junio C Hamano
2014-07-24 9:58 ` Duy Nguyen
2014-07-24 21:30 ` Junio C Hamano
2014-07-25 6:51 ` Michael J Gruber
2014-07-30 18:03 ` Junio C Hamano
2014-07-30 18:52 ` Junio C Hamano
2014-08-27 11:58 ` Duy Nguyen
2014-08-27 16:08 ` Junio C Hamano
2014-07-23 21:16 ` Junio C Hamano
2014-07-24 10:09 ` Duy Nguyen
2014-07-24 16:39 ` Junio C Hamano
2014-07-24 18:13 ` Junio C Hamano
2014-07-23 11:43 ` [PATCH 4/5] checkout --to: fix dangling pointers in remove_junk() Nguyễn Thái Ngọc Duy
2014-07-23 11:43 ` [PATCH 5/5] environment.c: fix incorrect git_graft_file initialization Nguyễn Thái Ngọc Duy
2014-07-23 21:22 ` Junio C Hamano
2014-07-29 13:50 ` [PATCH v2 0/8] nd/multiple-work-trees follow-ups Nguyễn Thái Ngọc Duy
2014-07-29 13:50 ` [PATCH v2 1/8] gitrepository-layout.txt: s/ignored/ignored if/ Nguyễn Thái Ngọc Duy
2014-07-29 13:50 ` [PATCH v2 2/8] checkout: no need to call check_linked_checkouts if head_ref is NULL Nguyễn Thái Ngọc Duy
2014-07-29 13:50 ` [PATCH v2 3/8] prune --repos: fix uninitialized access Nguyễn Thái Ngọc Duy
2014-07-29 13:50 ` Nguyễn Thái Ngọc Duy [this message]
2014-07-29 13:50 ` [PATCH v2 5/8] checkout --to: fix dangling pointers in remove_junk() Nguyễn Thái Ngọc Duy
2014-07-29 13:50 ` [PATCH v2 6/8] environment.c: fix incorrect git_graft_file initialization Nguyễn Thái Ngọc Duy
2014-07-29 13:50 ` [PATCH v2 7/8] checkout: prefix --to argument properly when cwd is moved Nguyễn Thái Ngọc Duy
2014-07-29 20:51 ` Junio C Hamano
2014-07-30 10:32 ` Duy Nguyen
2014-07-29 13:50 ` [PATCH v2 8/8] checkout --to: do not touch existing target directory Nguyễn Thái Ngọc Duy
2014-07-30 17:51 ` [PATCH 0/5] nd/multiple-work-trees follow-ups Junio C Hamano
2014-07-31 10:13 ` Duy Nguyen
2014-07-31 17:00 ` Junio C Hamano
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1406641831-2390-5-git-send-email-pclouds@gmail.com \
--to=pclouds@gmail.com \
--cc=git@drmicha.warpmail.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=max@max630.net \
--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.
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 NNTP newsgroup(s).