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

Re: [PATCHv6 5/7] worktree: add function to check if worktrees are in use

From
Duy Nguyen <pclouds@gmail.com>
Date
Dec 8, 2016, 10:40 UTC
Message-ID
<CACsJy8ANNz6FsJ4_5MOhj2Qqd+wHHu5UpVOAobqEiHU2KM26eg@mail.gmail.com>
In-Reply-To
<20161208014623.7588-6-sbeller@google.com>
On Thu, Dec 8, 2016 at 8:46 AM, Stefan Beller <sbeller@google.com> wrote:
Show 22 quoted lines
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
>  worktree.c | 24 ++++++++++++++++++++++++
>  worktree.h |  7 +++++++
>  2 files changed, 31 insertions(+)
>
> diff --git a/worktree.c b/worktree.c
> index 75db689672..2559f33846 100644
> --- a/worktree.c
> +++ b/worktree.c
> @@ -406,3 +406,27 @@ const struct worktree *find_shared_symref(const char *symref,
>
>         return existing;
>  }
> +
> +static int uses_worktree_internal(struct worktree **worktrees)
> +{
> +       int i;
> +       for (i = 0; worktrees[i]; i++)
> +               ; /* nothing */
> +
> +       free_worktrees(worktrees);

Ayy.. caller allocates, callee frees. This might become a new maintenance nightmare. Elsewhere I believe we (Junio and me) discussed the possibility of returning the number of worktrees from get_worktrees() too. get_worktrees() would take an "int *", if not NULL, we return the number of worktrees in that pointer.

It's probably a better approach, although I'm afraid it'll add a bit more work on you.

Alternatively, we could add a new flag to get_worktrees() to tell it to return all worktrees if there is a least one linked worktree, or return NULL if there's only main worktree. I'm not sure if this is clever or very stupid.

> +       return i > 1;
> +}
> +
> +int uses_worktrees(void)

"has" may be a better verb than "uses". maybe "has_linked_worktrees" since we always have and use the main worktree.

Show 33 quoted lines
> +{
> +       return uses_worktree_internal(get_worktrees(0));
> +}
> +
> +int submodule_uses_worktrees(const char *path)
> +{
> +       struct worktree **worktrees = get_submodule_worktrees(path, 0);
> +       if (!worktrees)
> +               return 0;
> +
> +       return uses_worktree_internal(worktrees);
> +}
> diff --git a/worktree.h b/worktree.h
> index 157fbc4a66..76027b1fd2 100644
> --- a/worktree.h
> +++ b/worktree.h
> @@ -33,6 +33,13 @@ extern struct worktree **get_worktrees(unsigned flags);
>  extern struct worktree **get_submodule_worktrees(const char *path,
>                                                  unsigned flags);
>
> +/*
> + * Returns 1 if more than one worktree exists.
> + * Returns 0 if only the main worktree exists.
> + */
> +extern int uses_worktrees(void);
> +extern int submodule_uses_worktrees(const char *path);
> +
>  /*
>   * Return git dir of the worktree. Note that the path may be relative.
>   * If wt is NULL, git dir of current worktree is returned.
> --
> 2.11.0.rc2.30.gc512cbd.dirty
>
-- 
Duy
Previous: Stefan BellerNext: Duy Nguyen
Message 16 of 20 in “[PATCHv6 0/7] submodule embedgitdirs”
  1. Stefan BellerDec 8, 2016
  2. 1/7 submodule: use absolute path for computing relative path connectingStefan Beller, Dec 8, 2016
  3. Duy NguyenDec 8, 2016
  4. 2/7 submodule helper: support super prefixStefan Beller, Dec 8, 2016
  5. Duy NguyenDec 8, 2016
  6. Stefan BellerDec 8, 2016
  7. 4/7 worktree: get worktrees from submodulesStefan Beller, Dec 8, 2016
  8. Duy NguyenDec 8, 2016
  9. Stefan BellerDec 8, 2016
  10. Duy NguyenDec 9, 2016
  11. Brandon WilliamsDec 9, 2016
  12. Stefan BellerDec 9, 2016
  13. 7/7 submodule: add absorb-git-dir functionStefan Beller, Dec 8, 2016
  14. Duy NguyenDec 8, 2016
  15. 5/7 worktree: add function to check if worktrees are in useStefan Beller, Dec 8, 2016
  16. Duy NguyenDec 8, 2016
  17. Duy NguyenDec 8, 2016
  18. Stefan BellerDec 8, 2016
  19. 3/7 test-lib-functions.sh: teach test_commit -C <dir>Stefan Beller, Dec 8, 2016
  20. 6/7 move connect_work_tree_and_git_dir to dir.hStefan Beller, Dec 8, 2016

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.