git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v5 1/2] worktrees: add find_shared_symref

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Aug 3, 2015, 23:06 UTC
Message-ID
<CAPig+cTFWn6mWtmmG9KrDhK1++r7fgXM_mK4BvCktThoErnBzA@mail.gmail.com>
In-Reply-To
<1438627706-6743-1-git-send-email-dturner@twopensource.com>
On Mon, Aug 3, 2015 at 2:48 PM, David Turner <dturner@twopensource.com> wrote:
Show 7 quoted lines
> Add a new function, find_shared_symref, which contains the heart of
> die_if_checked_out, but works for any symref, not just HEAD.  Refactor
> die_if_checked_out to use the same infrastructure as
> find_shared_symref.
>
> Soon, we will use find_shared_symref to protect notes merges in
> worktrees.

A couple comments below. The first may be worth a re-roll; the second is more a taste thing. Neither is blocking if you don't happen to re-roll.

Show 22 quoted lines
> Signed-off-by: David Turner <dturner@twopensource.com>
> ---
> diff --git a/branch.c b/branch.c
> index c85be07..d2b3586 100644
> --- a/branch.c
> +++ b/branch.c
> @@ -349,31 +350,53 @@ static void check_linked_checkout(const char *branch, const char *id)
>                 strbuf_addstr(&gitdir, get_git_common_dir());
>         skip_prefix(branch, "refs/heads/", &branch);
>         strbuf_strip_suffix(&gitdir, ".git");
> -       die(_("'%s' is already checked out at '%s'"), branch, gitdir.buf);
> +
> +       strbuf_release(&path);
> +       strbuf_release(&sb);
> +       return strbuf_detach(&gitdir, NULL);
>  done:
>         strbuf_release(&path);
>         strbuf_release(&sb);
>         strbuf_release(&gitdir);
> +
> +       return NULL;
>  }

This would be cleaner and less redundant if you assign the existing location to a variable and just fall through to the 'done' label:

    char *existing = NULL;
    ...
        skip_prefix(branch, "refs/heads/", &branch);
        strbuf_strip_suffix(&gitdir, ".git");
        existing = strbuf_detach(&gitdir, NULL);
    done:
        strbuf_release(&path);
        strbuf_release(&sb);
        strbuf_release(&gitdir);
        return existing;

There's no worry that the "existing" path will be clobbered by strbuf_release(&gitdir) since it's been detached already (and it's safe to release the strbuf without affecting what has been detached from it).

Show 27 quoted lines
> -void die_if_checked_out(const char *branch)
> +char *find_shared_symref(const char *symref, const char *target)
>  {
>         struct strbuf path = STRBUF_INIT;
>         DIR *dir;
>         struct dirent *d;
> +       char *existing;
>
> -       check_linked_checkout(branch, NULL);
> +       if ((existing = find_linked_symref(symref, target, NULL)))
> +               return existing;
>
>         strbuf_addf(&path, "%s/worktrees", get_git_common_dir());
>         dir = opendir(path.buf);
>         strbuf_release(&path);
>         if (!dir)
> -               return;
> +               return NULL;
>
>         while ((d = readdir(dir)) != NULL) {
>                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
>                         continue;
> -               check_linked_checkout(branch, d->d_name);
> +               existing = find_linked_symref(symref, target, d->d_name);
> +               if (existing) {
> +                       closedir(dir);
> +                       return existing;

For consistency with code nearby, this could have been handled by adding a 'done' label above the closedir() below and jumping to it from here, and then 'return existing'.

Show 15 quoted lines
> +               }
>         }
>         closedir(dir);
> +
> +       return NULL;
> +}
> +
> +void die_if_checked_out(const char *branch)
> +{
> +       char *existing;
> +
> +       existing = find_shared_symref("HEAD", branch);
> +       if (existing)
> +               die(_("'%s' is already checked out at '%s'"), branch, existing);
>  }
Previous: David TurnerNext: Eric Sunshine
Message 3 of 4 in “worktrees: add find_shared_symref”
  1. 1/2 worktrees: add find_shared_symrefDavid Turner, Aug 3, 2015
  2. 2/2 notes: handle multiple worktreesDavid Turner, Aug 3, 2015
  3. Eric SunshineAug 3, 2015
  4. Eric SunshineAug 9, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.