Re: [PATCH v3 23/25] checkout: detach if the branch is already checked out elsewhere

2014-02-19 Thread Eric Sunshine
On Tue, Feb 18, 2014 at 8:40 AM, Nguyễn Thái Ngọc Duy  wrote:
> The normal rule is anything outside refs/heads/ is detached. This
> strictens the rule a bit more: if the branch is checked out (either in

s/strictens/increases strictness of/

> $GIT_COMMON_DIR/HEAD or any $GIT_DIR/repos/.../HEAD) then it's
> detached as well.
>
> A hint is given so the user knows where to go and do something there
> if they still want to checkout undetached here.
>
> Signed-off-by: Nguyễn Thái Ngọc Duy 
> ---
> diff --git a/builtin/checkout.c b/builtin/checkout.c
> index f961604..7b86f2b 100644
> --- a/builtin/checkout.c
> +++ b/builtin/checkout.c
> @@ -433,6 +433,11 @@ struct branch_info {
> const char *name; /* The short name used */
> const char *path; /* The full name of a real branch */
> struct commit *commit; /* The named commit */
> +   /*
> +* if not null the branch is detached because it's alrady

s/alrady/already/

> +* checked out in this checkout
> +*/
> +   char *checkout;
>  };
>
> +static void check_linked_checkouts(struct branch_info *new)
> +{
> +   struct strbuf path = STRBUF_INIT;
> +   DIR *dir;
> +   struct dirent *d;
> +
> +   strbuf_addf(&path, "%s/repos", get_git_common_dir());
> +   if ((dir = opendir(path.buf)) == NULL)

strbuf_release(&path);

> +   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;
> +   }
> +
> +   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;
> +   }
> +   strbuf_release(&path);
> +   closedir(dir);
> +}
--
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


Re: [PATCH v3 23/25] checkout: detach if the branch is already checked out elsewhere

2014-02-19 Thread Junio C Hamano
Nguyễn Thái Ngọc Duy   writes:

> The normal rule is anything outside refs/heads/ is detached. This
> strictens the rule a bit more: if the branch is checked out (either in
> $GIT_COMMON_DIR/HEAD or any $GIT_DIR/repos/.../HEAD) then it's
> detached as well.
>
> A hint is given so the user knows where to go and do something there
> if they still want to checkout undetached here.
>
> Signed-off-by: Nguyễn Thái Ngọc Duy 

(Only nitpicks during this round of review).

> diff --git a/t/t2025-checkout-to.sh b/t/t2025-checkout-to.sh
> index 76eae4a..f6a5c47 100755
> --- a/t/t2025-checkout-to.sh
> +++ b/t/t2025-checkout-to.sh
> @@ -13,13 +13,14 @@ 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 &&
>   (
>   cd here &&
>   test_cmp ../init.t init.t &&
> - git symbolic-ref HEAD >actual &&
> - echo refs/heads/master >expect &&
> - test_cmp expect actual &&
> + ! git symbolic-ref HEAD &&

test_must_fail?
--
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


[PATCH v3 23/25] checkout: detach if the branch is already checked out elsewhere

2014-02-18 Thread Nguyễn Thái Ngọc Duy
The normal rule is anything outside refs/heads/ is detached. This
strictens the rule a bit more: if the branch is checked out (either in
$GIT_COMMON_DIR/HEAD or any $GIT_DIR/repos/.../HEAD) then it's
detached as well.

A hint is given so the user knows where to go and do something there
if they still want to checkout undetached here.

Signed-off-by: Nguyễn Thái Ngọc Duy 
---
 builtin/checkout.c | 78 ++
 t/t2025-checkout-to.sh | 15 --
 2 files changed, 90 insertions(+), 3 deletions(-)

diff --git a/builtin/checkout.c b/builtin/checkout.c
index f961604..7b86f2b 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -433,6 +433,11 @@ struct branch_info {
const char *name; /* The short name used */
const char *path; /* The full name of a real branch */
struct commit *commit; /* The named commit */
+   /*
+* if not null the branch is detached because it's alrady
+* checked out in this checkout
+*/
+   char *checkout;
 };
 
 static void setup_branch_path(struct branch_info *branch)
@@ -640,6 +645,11 @@ 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);
@@ -982,6 +992,71 @@ 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)
+{
+   struct strbuf sb = STRBUF_INIT;
+   char *start, *end;
+   if (strbuf_read_file(&sb, path, 0) < 0)
+   return 0;
+   if (!starts_with(sb.buf, "ref:")) {
+   strbuf_release(&sb);
+   return 0;
+   }
+
+   start = sb.buf + 4;
+   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;
+   }
+   strbuf_release(&sb);
+   return 0;
+}
+
+static void check_linked_checkouts(struct branch_info *new)
+{
+   struct strbuf path = STRBUF_INIT;
+   DIR *dir;
+   struct dirent *d;
+
+   strbuf_addf(&path, "%s/repos", get_git_common_dir());
+   if ((dir = opendir(path.buf)) == NULL)
+   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;
+   }
+
+   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;
+   }
+   strbuf_release(&path);
+   closedir(dir);
+}
+
 static int parse_branchname_arg(int argc, const char **argv,
int dwim_new_local_branch_ok,
struct branch_info *new,
@@ -1109,6 +1184,9 @@ static int parse_branchname_arg(int argc, const char 
**argv,
else
new->path = NULL; /* not an existing branch */
 
+   if (new->path)
+   check_linked_checkouts(new);
+
new->commit = lookup_commit_reference_gently(rev, 1);
if (!new->commit) {
/* not a commit */
diff --git a/t/t2025-checkout-to.sh b/t/t2025-checkout-to.sh
index 76eae4a..f6a5c47 100755
--- a/t/t2025-checkout-to.sh
+++ b/t/t2025-checkout-to.sh
@@ -13,13 +13,14 @@ test_expect_success 'checkout --to not updating paths' '
 '
 
 test_expect_success 'checkout --to a new worktree' '
+   git rev-parse HEAD >expect