{"thread":{"id":"44655","subject":"[PATCHv7 0/6] submodule absorbgitdirs","startedAt":"2016-12-08T21:04:13Z","lastAt":"2016-12-10T11:16:45Z","messageCount":10,"participants":["Stefan Beller","Duy Nguyen"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"307305","messageId":"20161208210329.12919-1-sbeller@google.com","threadId":"44655","inReplyTo":null,"subject":"[PATCHv7 0/6] submodule absorbgitdirs","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-12-08T21:03:23Z","receivedAt":"2016-12-08T21:04:13Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"v7:\n* do not expose submodule_get_worktrees. The values may be wrong internally,\n  but for our purpose we do not care about the actual values, only the count.\n  Document the wrong values!\n* more strings are _(marked up)\n* renamed variables in connect_work_tree_and_git_dir\n* unsigned instead of int for options in the submodule helper.\n* \n\nv6:\n * renamed embedgitdirs to absorbgitdirs embedding may be interpreted as\n   embedding the git dir into the working directory, whereas absorbing sounds\n   more like the submodule is absorbed by the superproject, making the\n   submodule less independent\n * Worktrees API offer uses_worktrees(void) and submodule_uses_worktree(path).\n * moved the printing to stderr and one layer up (out of the pure\n   relocate_git_dir function).\n * connect_... is in dir.h now.\n\nv5:\n* Add another layer of abstraction, i.e. the relocate_git_dir is only about\n  moving a git dir of one repository. The submodule specific stuff (e.g.\n  recursion into nested submodules) is in submodule.{c,h}\n\n  This was motivated by reviews on the series of checkout aware of submodules\n  building on top of this series, as we want to directly call the embed-git-dirs\n  function without the overhead of spawning a child process.\n\nv4:\n* rebuilt on top of nd/worktree-list-fixup\n* fix and test behavior for un-init submodules (don't crash, rather do nothing)\n* incorporated a \"static\" as pointed out by Ramsay\n* use internal functions instead of duplicating code in worktree.c\n  (use get_common_dir_noenv for the submodule to actually get the common dir)\n* fixed a memory leak in relocate_gitdir\n\nv3:\n* have a slightly more generic function \"relocate_gitdir\".\n  The recursion is strictly related to submodules, though.\n* bail out if a submodule is using worktrees.\n  This also lays the groundwork for later doing the proper thing,\n  as worktree.h offers a function `get_submodule_worktrees(path)`\n* nit by duy: use git_path instead of git_common_dir\n\nv2:\n* fixed commit message for patch:\n \"submodule: use absolute path for computing relative path connecting\"\n* a new patch \"submodule helper: support super prefix\"\n* redid the final patch with more tests and fixing bugs along the way\n* \"test-lib-functions.sh: teach test_commit -C <dir>\" unchanged\n\nv1:\nThe discussion of the submodule checkout series revealed to me that a command\nis needed to move the git directory from the submodules working tree to be\nembedded into the superprojects git directory.\n\nSo I wrote the code to intern the submodules git dir into the superproject,\nbut whilst writing the code I realized this could be valueable for our use\nin testing too. So I exposed it via the submodule--helper. But as the\nsubmodule helper ought to be just an internal API, we could also\noffer it via the proper submodule command.\n\nThe command as it is has little value to the end user for now, but\nbreaking it out of the submodule checkout series hopefully makes review easier.\n\nThanks,\nStefan\nStefan Beller (6):\n  submodule: use absolute path for computing relative path connecting\n  submodule helper: support super prefix\n  test-lib-functions.sh: teach test_commit -C <dir>\n  worktree: have a function to check if worktrees are in use\n  move connect_work_tree_and_git_dir to dir.h\n  submodule: add absorb-git-dir function\n\n Documentation/git-submodule.txt    |  15 +++++\n builtin/submodule--helper.c        |  69 ++++++++++++++++----\n dir.c                              |  37 +++++++++++\n dir.h                              |   4 ++\n git-submodule.sh                   |   7 +-\n git.c                              |   2 +-\n submodule.c                        | 127 ++++++++++++++++++++++++++++++-------\n submodule.h                        |   5 +-\n t/t7412-submodule-absorbgitdirs.sh | 101 +++++++++++++++++++++++++++++\n t/test-lib-functions.sh            |  20 ++++--\n worktree.c                         |  68 +++++++++++++++++---\n worktree.h                         |   7 ++\n 12 files changed, 409 insertions(+), 53 deletions(-)\n create mode 100755 t/t7412-submodule-absorbgitdirs.sh\n\n-- \n2.11.0.rc2.29.g7c00390.dirty\n\n"},{"id":"307306","messageId":"20161208210329.12919-2-sbeller@google.com","threadId":"44655","inReplyTo":"20161208210329.12919-1-sbeller@google.com","subject":"[PATCHv7 1/6] submodule: use absolute path for computing relative path connecting","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-12-08T21:03:24Z","receivedAt":"2016-12-08T21:04:16Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"The current caller of connect_work_tree_and_git_dir passes\nan absolute path for the `git_dir` parameter. In the future patch\nwe will also pass in relative path for `git_dir`. Extend the functionality\nof connect_work_tree_and_git_dir to take relative paths for parameters.\n\nWe could work around this in the future patch by computing the absolute\npath for the git_dir in the calling site, however accepting relative\npaths for either parameter makes the API for this function much harder\nto misuse.\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n submodule.c | 13 +++++++------\n 1 file changed, 7 insertions(+), 6 deletions(-)\n\ndiff --git a/submodule.c b/submodule.c\nindex 6f7d883de9..d4f7afe2f1 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1223,27 +1223,28 @@ int merge_submodule(unsigned char result[20], const char *path,\n }\n \n /* Update gitfile and core.worktree setting to connect work tree and git dir */\n-void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir)\n+void connect_work_tree_and_git_dir(const char *work_tree_, const char *git_dir_)\n {\n \tstruct strbuf file_name = STRBUF_INIT;\n \tstruct strbuf rel_path = STRBUF_INIT;\n-\tconst char *real_work_tree = xstrdup(real_path(work_tree));\n+\tchar *git_dir = xstrdup(real_path(git_dir_));\n+\tchar *work_tree = xstrdup(real_path(work_tree_));\n \n \t/* Update gitfile */\n \tstrbuf_addf(&file_name, \"%s/.git\", work_tree);\n \twrite_file(file_name.buf, \"gitdir: %s\",\n-\t\t   relative_path(git_dir, real_work_tree, &rel_path));\n+\t\t   relative_path(git_dir, work_tree, &rel_path));\n \n \t/* Update core.worktree setting */\n \tstrbuf_reset(&file_name);\n \tstrbuf_addf(&file_name, \"%s/config\", git_dir);\n \tgit_config_set_in_file(file_name.buf, \"core.worktree\",\n-\t\t\t       relative_path(real_work_tree, git_dir,\n-\t\t\t\t\t     &rel_path));\n+\t\t\t       relative_path(work_tree, git_dir, &rel_path));\n \n \tstrbuf_release(&file_name);\n \tstrbuf_release(&rel_path);\n-\tfree((void *)real_work_tree);\n+\tfree(work_tree);\n+\tfree(git_dir);\n }\n \n int parallel_submodules(void)\n-- \n2.11.0.rc2.29.g7c00390.dirty\n\n"},{"id":"307307","messageId":"20161208210329.12919-3-sbeller@google.com","threadId":"44655","inReplyTo":"20161208210329.12919-1-sbeller@google.com","subject":"[PATCHv7 2/6] submodule helper: support super prefix","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-12-08T21:03:25Z","receivedAt":"2016-12-08T21:04:18Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"Just like main commands in Git, the submodule helper needs\naccess to the superproject prefix. Enable this in the git.c\nbut have its own fuse in the helper code by having a flag to\nturn on the super prefix.\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n builtin/submodule--helper.c | 31 ++++++++++++++++++++-----------\n git.c                       |  2 +-\n 2 files changed, 21 insertions(+), 12 deletions(-)\n\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex 4beeda5f9f..5643848667 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -1076,21 +1076,24 @@ static int resolve_remote_submodule_branch(int argc, const char **argv,\n \treturn 0;\n }\n \n+#define SUPPORT_SUPER_PREFIX (1<<0)\n+\n struct cmd_struct {\n \tconst char *cmd;\n \tint (*fn)(int, const char **, const char *);\n+\tunsigned option;\n };\n \n static struct cmd_struct commands[] = {\n-\t{\"list\", module_list},\n-\t{\"name\", module_name},\n-\t{\"clone\", module_clone},\n-\t{\"update-clone\", update_clone},\n-\t{\"relative-path\", resolve_relative_path},\n-\t{\"resolve-relative-url\", resolve_relative_url},\n-\t{\"resolve-relative-url-test\", resolve_relative_url_test},\n-\t{\"init\", module_init},\n-\t{\"remote-branch\", resolve_remote_submodule_branch}\n+\t{\"list\", module_list, 0},\n+\t{\"name\", module_name, 0},\n+\t{\"clone\", module_clone, 0},\n+\t{\"update-clone\", update_clone, 0},\n+\t{\"relative-path\", resolve_relative_path, 0},\n+\t{\"resolve-relative-url\", resolve_relative_url, 0},\n+\t{\"resolve-relative-url-test\", resolve_relative_url_test, 0},\n+\t{\"init\", module_init, 0},\n+\t{\"remote-branch\", resolve_remote_submodule_branch, 0},\n };\n \n int cmd_submodule__helper(int argc, const char **argv, const char *prefix)\n@@ -1100,9 +1103,15 @@ int cmd_submodule__helper(int argc, const char **argv, const char *prefix)\n \t\tdie(_(\"submodule--helper subcommand must be \"\n \t\t      \"called with a subcommand\"));\n \n-\tfor (i = 0; i < ARRAY_SIZE(commands); i++)\n-\t\tif (!strcmp(argv[1], commands[i].cmd))\n+\tfor (i = 0; i < ARRAY_SIZE(commands); i++) {\n+\t\tif (!strcmp(argv[1], commands[i].cmd)) {\n+\t\t\tif (get_super_prefix() &&\n+\t\t\t    !(commands[i].option & SUPPORT_SUPER_PREFIX))\n+\t\t\t\tdie(_(\"%s doesn't support --super-prefix\"),\n+\t\t\t\t    commands[i].cmd);\n \t\t\treturn commands[i].fn(argc - 1, argv + 1, prefix);\n+\t\t}\n+\t}\n \n \tdie(_(\"'%s' is not a valid submodule--helper \"\n \t      \"subcommand\"), argv[1]);\ndiff --git a/git.c b/git.c\nindex efa1059fe0..98dcf6c518 100644\n--- a/git.c\n+++ b/git.c\n@@ -493,7 +493,7 @@ static struct cmd_struct commands[] = {\n \t{ \"stage\", cmd_add, RUN_SETUP | NEED_WORK_TREE },\n \t{ \"status\", cmd_status, RUN_SETUP | NEED_WORK_TREE },\n \t{ \"stripspace\", cmd_stripspace },\n-\t{ \"submodule--helper\", cmd_submodule__helper, RUN_SETUP },\n+\t{ \"submodule--helper\", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX},\n \t{ \"symbolic-ref\", cmd_symbolic_ref, RUN_SETUP },\n \t{ \"tag\", cmd_tag, RUN_SETUP },\n \t{ \"unpack-file\", cmd_unpack_file, RUN_SETUP },\n-- \n2.11.0.rc2.29.g7c00390.dirty\n\n"},{"id":"307308","messageId":"20161208210329.12919-6-sbeller@google.com","threadId":"44655","inReplyTo":"20161208210329.12919-1-sbeller@google.com","subject":"[PATCHv7 5/6] move connect_work_tree_and_git_dir to dir.h","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-12-08T21:03:28Z","receivedAt":"2016-12-08T21:04:23Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"That function was primarily used by submodule code, but the function\nitself is not inherently about submodules. In the next patch we'll\nintroduce relocate_git_dir, which can be used by worktrees as well,\nso find a neutral middle ground in dir.h.\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n dir.c       | 25 +++++++++++++++++++++++++\n dir.h       |  1 +\n submodule.c | 25 -------------------------\n submodule.h |  1 -\n 4 files changed, 26 insertions(+), 26 deletions(-)\n\ndiff --git a/dir.c b/dir.c\nindex bfa8c8a9a5..e0efd3c2c3 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -2748,3 +2748,28 @@ void untracked_cache_add_to_index(struct index_state *istate,\n {\n \tuntracked_cache_invalidate_path(istate, path);\n }\n+\n+/* Update gitfile and core.worktree setting to connect work tree and git dir */\n+void connect_work_tree_and_git_dir(const char *work_tree_, const char *git_dir_)\n+{\n+\tstruct strbuf file_name = STRBUF_INIT;\n+\tstruct strbuf rel_path = STRBUF_INIT;\n+\tchar *git_dir = xstrdup(real_path(git_dir_));\n+\tchar *work_tree = xstrdup(real_path(work_tree_));\n+\n+\t/* Update gitfile */\n+\tstrbuf_addf(&file_name, \"%s/.git\", work_tree);\n+\twrite_file(file_name.buf, \"gitdir: %s\",\n+\t\t   relative_path(git_dir, work_tree, &rel_path));\n+\n+\t/* Update core.worktree setting */\n+\tstrbuf_reset(&file_name);\n+\tstrbuf_addf(&file_name, \"%s/config\", git_dir);\n+\tgit_config_set_in_file(file_name.buf, \"core.worktree\",\n+\t\t\t       relative_path(work_tree, git_dir, &rel_path));\n+\n+\tstrbuf_release(&file_name);\n+\tstrbuf_release(&rel_path);\n+\tfree(work_tree);\n+\tfree(git_dir);\n+}\ndiff --git a/dir.h b/dir.h\nindex 97c83bb383..051674a431 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -335,4 +335,5 @@ struct untracked_cache *read_untracked_extension(const void *data, unsigned long\n void write_untracked_extension(struct strbuf *out, struct untracked_cache *untracked);\n void add_untracked_cache(struct index_state *istate);\n void remove_untracked_cache(struct index_state *istate);\n+extern void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);\n #endif\ndiff --git a/submodule.c b/submodule.c\nindex d4f7afe2f1..0bb50b4b62 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -1222,31 +1222,6 @@ int merge_submodule(unsigned char result[20], const char *path,\n \treturn 0;\n }\n \n-/* Update gitfile and core.worktree setting to connect work tree and git dir */\n-void connect_work_tree_and_git_dir(const char *work_tree_, const char *git_dir_)\n-{\n-\tstruct strbuf file_name = STRBUF_INIT;\n-\tstruct strbuf rel_path = STRBUF_INIT;\n-\tchar *git_dir = xstrdup(real_path(git_dir_));\n-\tchar *work_tree = xstrdup(real_path(work_tree_));\n-\n-\t/* Update gitfile */\n-\tstrbuf_addf(&file_name, \"%s/.git\", work_tree);\n-\twrite_file(file_name.buf, \"gitdir: %s\",\n-\t\t   relative_path(git_dir, work_tree, &rel_path));\n-\n-\t/* Update core.worktree setting */\n-\tstrbuf_reset(&file_name);\n-\tstrbuf_addf(&file_name, \"%s/config\", git_dir);\n-\tgit_config_set_in_file(file_name.buf, \"core.worktree\",\n-\t\t\t       relative_path(work_tree, git_dir, &rel_path));\n-\n-\tstrbuf_release(&file_name);\n-\tstrbuf_release(&rel_path);\n-\tfree(work_tree);\n-\tfree(git_dir);\n-}\n-\n int parallel_submodules(void)\n {\n \treturn parallel_jobs;\ndiff --git a/submodule.h b/submodule.h\nindex d9e197a948..4e3bf469b4 100644\n--- a/submodule.h\n+++ b/submodule.h\n@@ -65,7 +65,6 @@ int merge_submodule(unsigned char result[20], const char *path, const unsigned c\n int find_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name,\n \t\tstruct string_list *needs_pushing);\n int push_unpushed_submodules(unsigned char new_sha1[20], const char *remotes_name);\n-void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);\n int parallel_submodules(void);\n \n /*\n-- \n2.11.0.rc2.29.g7c00390.dirty\n\n"},{"id":"307309","messageId":"20161208210329.12919-4-sbeller@google.com","threadId":"44655","inReplyTo":"20161208210329.12919-1-sbeller@google.com","subject":"[PATCHv7 3/6] test-lib-functions.sh: teach test_commit -C <dir>","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-12-08T21:03:26Z","receivedAt":"2016-12-08T21:04:27Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"Specifically when setting up submodule tests, it comes in handy if\nwe can create commits in repositories that are not at the root of\nthe tested trash dir. Add \"-C <dir>\" similar to gits -C parameter\nthat will perform the operation in the given directory.\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n t/test-lib-functions.sh | 20 +++++++++++++++-----\n 1 file changed, 15 insertions(+), 5 deletions(-)\n\ndiff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh\nindex fdaeb3a96b..579e812506 100644\n--- a/t/test-lib-functions.sh\n+++ b/t/test-lib-functions.sh\n@@ -157,16 +157,21 @@ debug () {\n \t GIT_TEST_GDB=1 \"$@\"\n }\n \n-# Call test_commit with the arguments \"<message> [<file> [<contents> [<tag>]]]\"\n+# Call test_commit with the arguments\n+# [-C <directory>] <message> [<file> [<contents> [<tag>]]]\"\n #\n # This will commit a file with the given contents and the given commit\n # message, and tag the resulting commit with the given tag name.\n #\n # <file>, <contents>, and <tag> all default to <message>.\n+#\n+# If the first argument is \"-C\", the second argument is used as a path for\n+# the git invocations.\n \n test_commit () {\n \tnotick= &&\n \tsignoff= &&\n+\tindir= &&\n \twhile test $# != 0\n \tdo\n \t\tcase \"$1\" in\n@@ -176,21 +181,26 @@ test_commit () {\n \t\t--signoff)\n \t\t\tsignoff=\"$1\"\n \t\t\t;;\n+\t\t-C)\n+\t\t\tindir=\"$2\"\n+\t\t\tshift\n+\t\t\t;;\n \t\t*)\n \t\t\tbreak\n \t\t\t;;\n \t\tesac\n \t\tshift\n \tdone &&\n+\tindir=${indir:+\"$indir\"/} &&\n \tfile=${2:-\"$1.t\"} &&\n-\techo \"${3-$1}\" > \"$file\" &&\n-\tgit add \"$file\" &&\n+\techo \"${3-$1}\" > \"$indir$file\" &&\n+\tgit ${indir:+ -C \"$indir\"} add \"$file\" &&\n \tif test -z \"$notick\"\n \tthen\n \t\ttest_tick\n \tfi &&\n-\tgit commit $signoff -m \"$1\" &&\n-\tgit tag \"${4:-$1}\"\n+\tgit ${indir:+ -C \"$indir\"} commit $signoff -m \"$1\" &&\n+\tgit ${indir:+ -C \"$indir\"} tag \"${4:-$1}\"\n }\n \n # Call test_merge with the arguments \"<message> <commit>\", where <commit>\n-- \n2.11.0.rc2.29.g7c00390.dirty\n\n"},{"id":"307310","messageId":"20161208210329.12919-5-sbeller@google.com","threadId":"44655","inReplyTo":"20161208210329.12919-1-sbeller@google.com","subject":"[PATCHv7 4/6] worktree: have a function to check if worktrees are in use","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-12-08T21:03:27Z","receivedAt":"2016-12-08T21:04:28Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"In a later patch we want to move around the the git directory of\na submodule. Both submodules as well as worktrees are involved in\nplacing git directories at unusual places, so their functionality\nmay collide. To react appropriately to situations where worktrees\nin submodules are in use, offer a new function to query the\na submodule if it uses the worktree feature.\n\nThis can be done cheaply (both in new code to write as well as run time)\nby obtaining the list of worktrees based off that submodules git\ndirectory. However as we have loaded the variables for the current\nrepository, the values in the submodule worktree\ncan be wrong, e.g.\n* core.ignorecase may differ between these two repositories\n* the ref resolution is broken (refs/heads/branch in the submodule\n  resolves to the sha1 value of the `branch` in the current repository\n  that may not exist or have another sha1)\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n worktree.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++---------\n worktree.h |  7 +++++++\n 2 files changed, 65 insertions(+), 10 deletions(-)\n\ndiff --git a/worktree.c b/worktree.c\nindex eb6121263b..1c46fcf25f 100644\n--- a/worktree.c\n+++ b/worktree.c\n@@ -72,7 +72,7 @@ static void add_head_info(struct strbuf *head_ref, struct worktree *worktree)\n /**\n  * get the main worktree\n  */\n-static struct worktree *get_main_worktree(void)\n+static struct worktree *get_main_worktree(const char *git_common_dir)\n {\n \tstruct worktree *worktree = NULL;\n \tstruct strbuf path = STRBUF_INIT;\n@@ -81,12 +81,12 @@ static struct worktree *get_main_worktree(void)\n \tint is_bare = 0;\n \tint is_detached = 0;\n \n-\tstrbuf_add_absolute_path(&worktree_path, get_git_common_dir());\n+\tstrbuf_add_absolute_path(&worktree_path, git_common_dir);\n \tis_bare = !strbuf_strip_suffix(&worktree_path, \"/.git\");\n \tif (is_bare)\n \t\tstrbuf_strip_suffix(&worktree_path, \"/.\");\n \n-\tstrbuf_addf(&path, \"%s/HEAD\", get_git_common_dir());\n+\tstrbuf_addf(&path, \"%s/HEAD\", git_common_dir);\n \n \tworktree = xcalloc(1, sizeof(*worktree));\n \tworktree->path = strbuf_detach(&worktree_path, NULL);\n@@ -101,7 +101,8 @@ static struct worktree *get_main_worktree(void)\n \treturn worktree;\n }\n \n-static struct worktree *get_linked_worktree(const char *id)\n+static struct worktree *get_linked_worktree(const char *git_common_dir,\n+\t\t\t\t\t    const char *id)\n {\n \tstruct worktree *worktree = NULL;\n \tstruct strbuf path = STRBUF_INIT;\n@@ -112,7 +113,7 @@ static struct worktree *get_linked_worktree(const char *id)\n \tif (!id)\n \t\tdie(\"Missing linked worktree name\");\n \n-\tstrbuf_git_common_path(&path, \"worktrees/%s/gitdir\", id);\n+\tstrbuf_addf(&path, \"%s/worktrees/%s/gitdir\", git_common_dir, id);\n \tif (strbuf_read_file(&worktree_path, path.buf, 0) <= 0)\n \t\t/* invalid gitdir file */\n \t\tgoto done;\n@@ -125,7 +126,7 @@ static struct worktree *get_linked_worktree(const char *id)\n \t}\n \n \tstrbuf_reset(&path);\n-\tstrbuf_addf(&path, \"%s/worktrees/%s/HEAD\", get_git_common_dir(), id);\n+\tstrbuf_addf(&path, \"%s/worktrees/%s/HEAD\", git_common_dir, id);\n \n \tif (parse_ref(path.buf, &head_ref, &is_detached) < 0)\n \t\tgoto done;\n@@ -167,7 +168,8 @@ static int compare_worktree(const void *a_, const void *b_)\n \treturn fspathcmp((*a)->path, (*b)->path);\n }\n \n-struct worktree **get_worktrees(unsigned flags)\n+static struct worktree **get_worktrees_internal(const char *git_common_dir,\n+\t\t\t\t\t\tunsigned flags)\n {\n \tstruct worktree **list = NULL;\n \tstruct strbuf path = STRBUF_INIT;\n@@ -177,9 +179,10 @@ struct worktree **get_worktrees(unsigned flags)\n \n \tlist = xmalloc(alloc * sizeof(struct worktree *));\n \n-\tlist[counter++] = get_main_worktree();\n+\tif (!(flags & GWT_LINKED_ONLY))\n+\t\tlist[counter++] = get_main_worktree(git_common_dir);\n \n-\tstrbuf_addf(&path, \"%s/worktrees\", get_git_common_dir());\n+\tstrbuf_addf(&path, \"%s/worktrees\", git_common_dir);\n \tdir = opendir(path.buf);\n \tstrbuf_release(&path);\n \tif (dir) {\n@@ -188,7 +191,7 @@ struct worktree **get_worktrees(unsigned flags)\n \t\t\tif (is_dot_or_dotdot(d->d_name))\n \t\t\t\tcontinue;\n \n-\t\t\tif ((linked = get_linked_worktree(d->d_name))) {\n+\t\t\tif ((linked = get_linked_worktree(git_common_dir, d->d_name))) {\n \t\t\t\tALLOC_GROW(list, counter + 1, alloc);\n \t\t\t\tlist[counter++] = linked;\n \t\t\t}\n@@ -209,6 +212,34 @@ struct worktree **get_worktrees(unsigned flags)\n \treturn list;\n }\n \n+struct worktree **get_worktrees(unsigned flags)\n+{\n+\treturn get_worktrees_internal(get_git_common_dir(), flags);\n+}\n+\n+/*\n+ * NEEDSWORK: The values in the returned worktrees are broken, e.g.\n+ * the refs or path resolution is influenced by the current repository.\n+ */\n+static struct worktree **get_submodule_worktrees(const char *path, unsigned flags)\n+{\n+\tchar *submodule_gitdir;\n+\tstruct strbuf sb = STRBUF_INIT;\n+\tstruct worktree **ret;\n+\n+\tsubmodule_gitdir = git_pathdup_submodule(path, \"%s\", \"\");\n+\tif (!submodule_gitdir)\n+\t\treturn NULL;\n+\n+\t/* the env would be set for the superproject */\n+\tget_common_dir_noenv(&sb, submodule_gitdir);\n+\tret = get_worktrees_internal(sb.buf, flags);\n+\n+\tstrbuf_release(&sb);\n+\tfree(submodule_gitdir);\n+\treturn ret;\n+}\n+\n const char *get_worktree_git_dir(const struct worktree *wt)\n {\n \tif (!wt)\n@@ -380,3 +411,20 @@ const struct worktree *find_shared_symref(const char *symref,\n \n \treturn existing;\n }\n+\n+int uses_worktrees(void)\n+{\n+\tstruct worktree **worktrees = get_worktrees(GWT_LINKED_ONLY);\n+\tint retval = (worktrees != NULL && worktrees[0] != NULL);\n+\tfree_worktrees(worktrees);\n+\treturn retval;\n+}\n+\n+int submodule_uses_worktrees(const char *path)\n+{\n+\tstruct worktree **worktrees =\n+\t\t\tget_submodule_worktrees(path, GWT_LINKED_ONLY);\n+\tint retval = (worktrees != NULL && worktrees[0] != NULL);\n+\tfree_worktrees(worktrees);\n+\treturn retval;\n+}\ndiff --git a/worktree.h b/worktree.h\nindex d59ce1fee8..ebe0856330 100644\n--- a/worktree.h\n+++ b/worktree.h\n@@ -16,6 +16,7 @@ struct worktree {\n /* Functions for acting on the information about worktrees. */\n \n #define GWT_SORT_LINKED (1 << 0) /* keeps linked worktrees sorted */\n+#define GWT_LINKED_ONLY (1 << 1) /* do not include the main worktree */\n \n /*\n  * Get the worktrees.  The primary worktree will always be the first returned,\n@@ -27,6 +28,12 @@ struct worktree {\n  */\n extern struct worktree **get_worktrees(unsigned flags);\n \n+/*\n+ * Returns 1 if linked worktrees exist, 0 otherwise.\n+ */\n+extern int uses_worktrees(void);\n+extern int submodule_uses_worktrees(const char *path);\n+\n /*\n  * Return git dir of the worktree. Note that the path may be relative.\n  * If wt is NULL, git dir of current worktree is returned.\n-- \n2.11.0.rc2.29.g7c00390.dirty\n\n"},{"id":"307311","messageId":"20161208210329.12919-7-sbeller@google.com","threadId":"44655","inReplyTo":"20161208210329.12919-1-sbeller@google.com","subject":"[PATCHv7 6/6] submodule: add absorb-git-dir function","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-12-08T21:03:29Z","receivedAt":"2016-12-08T21:04:33Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"When a submodule has its git dir inside the working dir, the submodule\nsupport for checkout that we plan to add in a later patch will fail.\n\nAdd functionality to migrate the git directory to be absorbed\ninto the superprojects git directory.\n\nThe newly added code in this patch is structured such that other areas of\nGit can also make use of it. The code in the submodule--helper is a mere\nwrapper and option parser for the function\n`absorb_git_dir_into_superproject`, that takes care of embedding the\nsubmodules git directory into the superprojects git dir. That function\nmakes use of the more abstract function for this use case\n`relocate_gitdir`, which can be used by e.g. the worktree code eventually\nto move around a git directory.\n\nSigned-off-by: Stefan Beller <sbeller@google.com>\n---\n Documentation/git-submodule.txt    |  15 ++++++\n builtin/submodule--helper.c        |  38 ++++++++++++++\n dir.c                              |  12 +++++\n dir.h                              |   3 ++\n git-submodule.sh                   |   7 ++-\n submodule.c                        | 103 +++++++++++++++++++++++++++++++++++++\n submodule.h                        |   4 ++\n t/t7412-submodule-absorbgitdirs.sh | 101 ++++++++++++++++++++++++++++++++++++\n 8 files changed, 282 insertions(+), 1 deletion(-)\n create mode 100755 t/t7412-submodule-absorbgitdirs.sh\n\ndiff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt\nindex d841573475..918bd1d1bd 100644\n--- a/Documentation/git-submodule.txt\n+++ b/Documentation/git-submodule.txt\n@@ -22,6 +22,7 @@ SYNOPSIS\n \t      [commit] [--] [<path>...]\n 'git submodule' [--quiet] foreach [--recursive] <command>\n 'git submodule' [--quiet] sync [--recursive] [--] [<path>...]\n+'git submodule' [--quiet] absorbgitdirs [--] [<path>...]\n \n \n DESCRIPTION\n@@ -245,6 +246,20 @@ sync::\n If `--recursive` is specified, this command will recurse into the\n registered submodules, and sync any nested submodules within.\n \n+absorbgitdirs::\n+\tIf a git directory of a submodule is inside the submodule,\n+\tmove the git directory of the submodule into its superprojects\n+\t`$GIT_DIR/modules` path and then connect the git directory and\n+\tits working directory by setting the `core.worktree` and adding\n+\ta .git file pointing to the git directory embedded in the\n+\tsuperprojects git directory.\n++\n+A repository that was cloned independently and later added as a submodule or\n+old setups have the submodules git directory inside the submodule instead of\n+embedded into the superprojects git directory.\n++\n+This command is recursive by default.\n+\n OPTIONS\n -------\n -q::\ndiff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c\nindex 5643848667..242d9911a6 100644\n--- a/builtin/submodule--helper.c\n+++ b/builtin/submodule--helper.c\n@@ -1076,6 +1076,43 @@ static int resolve_remote_submodule_branch(int argc, const char **argv,\n \treturn 0;\n }\n \n+static int absorb_git_dirs(int argc, const char **argv, const char *prefix)\n+{\n+\tint i;\n+\tstruct pathspec pathspec;\n+\tstruct module_list list = MODULE_LIST_INIT;\n+\tunsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;\n+\n+\tstruct option embed_gitdir_options[] = {\n+\t\tOPT_STRING(0, \"prefix\", &prefix,\n+\t\t\t   N_(\"path\"),\n+\t\t\t   N_(\"path into the working tree\")),\n+\t\tOPT_BIT(0, \"--recursive\", &flags, N_(\"recurse into submodules\"),\n+\t\t\tABSORB_GITDIR_RECURSE_SUBMODULES),\n+\t\tOPT_END()\n+\t};\n+\n+\tconst char *const git_submodule_helper_usage[] = {\n+\t\tN_(\"git submodule--helper embed-git-dir [<path>...]\"),\n+\t\tNULL\n+\t};\n+\n+\targc = parse_options(argc, argv, prefix, embed_gitdir_options,\n+\t\t\t     git_submodule_helper_usage, 0);\n+\n+\tgitmodules_config();\n+\tgit_config(submodule_config, NULL);\n+\n+\tif (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)\n+\t\treturn 1;\n+\n+\tfor (i = 0; i < list.nr; i++)\n+\t\tabsorb_git_dir_into_superproject(prefix,\n+\t\t\t\tlist.entries[i]->name, flags);\n+\n+\treturn 0;\n+}\n+\n #define SUPPORT_SUPER_PREFIX (1<<0)\n \n struct cmd_struct {\n@@ -1094,6 +1131,7 @@ static struct cmd_struct commands[] = {\n \t{\"resolve-relative-url-test\", resolve_relative_url_test, 0},\n \t{\"init\", module_init, 0},\n \t{\"remote-branch\", resolve_remote_submodule_branch, 0},\n+\t{\"absorb-git-dirs\", absorb_git_dirs, SUPPORT_SUPER_PREFIX},\n };\n \n int cmd_submodule__helper(int argc, const char **argv, const char *prefix)\ndiff --git a/dir.c b/dir.c\nindex e0efd3c2c3..d872cc1570 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -2773,3 +2773,15 @@ void connect_work_tree_and_git_dir(const char *work_tree_, const char *git_dir_)\n \tfree(work_tree);\n \tfree(git_dir);\n }\n+\n+/*\n+ * Migrate the git directory of the given path from old_git_dir to new_git_dir.\n+ */\n+void relocate_gitdir(const char *path, const char *old_git_dir, const char *new_git_dir)\n+{\n+\tif (rename(old_git_dir, new_git_dir) < 0)\n+\t\tdie_errno(_(\"could not migrate git directory from '%s' to '%s'\"),\n+\t\t\told_git_dir, new_git_dir);\n+\n+\tconnect_work_tree_and_git_dir(path, new_git_dir);\n+}\ndiff --git a/dir.h b/dir.h\nindex 051674a431..bf23a470af 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -336,4 +336,7 @@ void write_untracked_extension(struct strbuf *out, struct untracked_cache *untra\n void add_untracked_cache(struct index_state *istate);\n void remove_untracked_cache(struct index_state *istate);\n extern void connect_work_tree_and_git_dir(const char *work_tree, const char *git_dir);\n+extern void relocate_gitdir(const char *path,\n+\t\t\t    const char *old_git_dir,\n+\t\t\t    const char *new_git_dir);\n #endif\ndiff --git a/git-submodule.sh b/git-submodule.sh\nindex a024a135d6..9285b5c43d 100755\n--- a/git-submodule.sh\n+++ b/git-submodule.sh\n@@ -1131,6 +1131,11 @@ cmd_sync()\n \tdone\n }\n \n+cmd_absorbgitdirs()\n+{\n+\tgit submodule--helper absorb-git-dirs --prefix \"$wt_prefix\" \"$@\"\n+}\n+\n # This loop parses the command line arguments to find the\n # subcommand name to dispatch.  Parsing of the subcommand specific\n # options are primarily done by the subcommand implementations.\n@@ -1140,7 +1145,7 @@ cmd_sync()\n while test $# != 0 && test -z \"$command\"\n do\n \tcase \"$1\" in\n-\tadd | foreach | init | deinit | update | status | summary | sync)\n+\tadd | foreach | init | deinit | update | status | summary | sync | absorbgitdirs)\n \t\tcommand=$1\n \t\t;;\n \t-q|--quiet)\ndiff --git a/submodule.c b/submodule.c\nindex 0bb50b4b62..45ccfb7ab4 100644\n--- a/submodule.c\n+++ b/submodule.c\n@@ -14,6 +14,7 @@\n #include \"blob.h\"\n #include \"thread-utils.h\"\n #include \"quote.h\"\n+#include \"worktree.h\"\n \n static int config_fetch_recurse_submodules = RECURSE_SUBMODULES_ON_DEMAND;\n static int parallel_jobs = 1;\n@@ -1237,3 +1238,105 @@ void prepare_submodule_repo_env(struct argv_array *out)\n \t}\n \targv_array_push(out, \"GIT_DIR=.git\");\n }\n+\n+/*\n+ * Embeds a single submodules git directory into the superprojects git dir,\n+ * non recursively.\n+ */\n+static void relocate_single_git_dir_into_superproject(const char *prefix,\n+\t\t\t\t\t\t      const char *path)\n+{\n+\tchar *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL;\n+\tconst char *new_git_dir;\n+\tconst struct submodule *sub;\n+\n+\tif (submodule_uses_worktrees(path))\n+\t\tdie(_(\"relocate_gitdir for submodule '%s' with \"\n+\t\t      \"more than one worktree not supported\"), path);\n+\n+\told_git_dir = xstrfmt(\"%s/.git\", path);\n+\tif (read_gitfile(old_git_dir))\n+\t\t/* If it is an actual gitfile, it doesn't need migration. */\n+\t\treturn;\n+\n+\treal_old_git_dir = xstrdup(real_path(old_git_dir));\n+\n+\tsub = submodule_from_path(null_sha1, path);\n+\tif (!sub)\n+\t\tdie(_(\"could not lookup name for submodule '%s'\"), path);\n+\n+\tnew_git_dir = git_path(\"modules/%s\", sub->name);\n+\tif (safe_create_leading_directories_const(new_git_dir) < 0)\n+\t\tdie(_(\"could not create directory '%s'\"), new_git_dir);\n+\treal_new_git_dir = xstrdup(real_path(new_git_dir));\n+\n+\tif (!prefix)\n+\t\tprefix = get_super_prefix();\n+\n+\tfprintf(stderr, _(\"Migrating git directory of '%s%s' from\\n'%s' to\\n'%s'\\n\"),\n+\t\tprefix ? prefix : \"\", path,\n+\t\treal_old_git_dir, real_new_git_dir);\n+\n+\trelocate_gitdir(path, real_old_git_dir, real_new_git_dir);\n+\n+\tfree(old_git_dir);\n+\tfree(real_old_git_dir);\n+\tfree(real_new_git_dir);\n+}\n+\n+/*\n+ * Migrate the git directory of the submodule given by path from\n+ * having its git directory within the working tree to the git dir nested\n+ * in its superprojects git dir under modules/.\n+ */\n+void absorb_git_dir_into_superproject(const char *prefix,\n+\t\t\t\t      const char *path,\n+\t\t\t\t      unsigned flags)\n+{\n+\tconst char *sub_git_dir, *v;\n+\tchar *real_sub_git_dir = NULL, *real_common_git_dir = NULL;\n+\tstruct strbuf gitdir = STRBUF_INIT;\n+\n+\tstrbuf_addf(&gitdir, \"%s/.git\", path);\n+\tsub_git_dir = resolve_gitdir(gitdir.buf);\n+\n+\t/* Not populated? */\n+\tif (!sub_git_dir)\n+\t\tgoto out;\n+\n+\t/* Is it already absorbed into the superprojects git dir? */\n+\treal_sub_git_dir = xstrdup(real_path(sub_git_dir));\n+\treal_common_git_dir = xstrdup(real_path(get_git_common_dir()));\n+\tif (!skip_prefix(real_sub_git_dir, real_common_git_dir, &v))\n+\t\trelocate_single_git_dir_into_superproject(prefix, path);\n+\n+\tif (flags & ABSORB_GITDIR_RECURSE_SUBMODULES) {\n+\t\tstruct child_process cp = CHILD_PROCESS_INIT;\n+\t\tstruct strbuf sb = STRBUF_INIT;\n+\n+\t\tif (flags & ~ABSORB_GITDIR_RECURSE_SUBMODULES)\n+\t\t\tdie(\"BUG: we don't know how to pass the flags down?\");\n+\n+\t\tif (get_super_prefix())\n+\t\t\tstrbuf_addstr(&sb, get_super_prefix());\n+\t\tstrbuf_addstr(&sb, path);\n+\t\tstrbuf_addch(&sb, '/');\n+\n+\t\tcp.dir = path;\n+\t\tcp.git_cmd = 1;\n+\t\tcp.no_stdin = 1;\n+\t\targv_array_pushl(&cp.args, \"--super-prefix\", sb.buf,\n+\t\t\t\t\t   \"submodule--helper\",\n+\t\t\t\t\t   \"absorb-git-dirs\", NULL);\n+\t\tprepare_submodule_repo_env(&cp.env_array);\n+\t\tif (run_command(&cp))\n+\t\t\tdie(_(\"could not recurse into submodule '%s'\"), path);\n+\n+\t\tstrbuf_release(&sb);\n+\t}\n+\n+out:\n+\tstrbuf_release(&gitdir);\n+\tfree(real_sub_git_dir);\n+\tfree(real_common_git_dir);\n+}\ndiff --git a/submodule.h b/submodule.h\nindex 4e3bf469b4..6229054b99 100644\n--- a/submodule.h\n+++ b/submodule.h\n@@ -74,4 +74,8 @@ int parallel_submodules(void);\n  */\n void prepare_submodule_repo_env(struct argv_array *out);\n \n+#define ABSORB_GITDIR_RECURSE_SUBMODULES (1<<0)\n+extern void absorb_git_dir_into_superproject(const char *prefix,\n+\t\t\t\t\t     const char *path,\n+\t\t\t\t\t     unsigned flags);\n #endif\ndiff --git a/t/t7412-submodule-absorbgitdirs.sh b/t/t7412-submodule-absorbgitdirs.sh\nnew file mode 100755\nindex 0000000000..7c4e8b8d79\n--- /dev/null\n+++ b/t/t7412-submodule-absorbgitdirs.sh\n@@ -0,0 +1,101 @@\n+#!/bin/sh\n+\n+test_description='Test submodule absorbgitdirs\n+\n+This test verifies that `git submodue absorbgitdirs` moves a submodules git\n+directory into the superproject.\n+'\n+\n+. ./test-lib.sh\n+\n+test_expect_success 'setup a real submodule' '\n+\tgit init sub1 &&\n+\ttest_commit -C sub1 first &&\n+\tgit submodule add ./sub1 &&\n+\ttest_tick &&\n+\tgit commit -m superproject\n+'\n+\n+test_expect_success 'absorb the git dir' '\n+\t>expect.1 &&\n+\t>expect.2 &&\n+\t>actual.1 &&\n+\t>actual.2 &&\n+\tgit status >expect.1 &&\n+\tgit -C sub1 rev-parse HEAD >expect.2 &&\n+\tgit submodule absorbgitdirs &&\n+\tgit fsck &&\n+\ttest -f sub1/.git &&\n+\ttest -d .git/modules/sub1 &&\n+\tgit status >actual.1 &&\n+\tgit -C sub1 rev-parse HEAD >actual.2 &&\n+\ttest_cmp expect.1 actual.1 &&\n+\ttest_cmp expect.2 actual.2\n+'\n+\n+test_expect_success 'absorbing does not fail for deinitalized submodules' '\n+\ttest_when_finished \"git submodule update --init\" &&\n+\tgit submodule deinit --all &&\n+\tgit submodule absorbgitdirs &&\n+\ttest -d .git/modules/sub1 &&\n+\ttest -d sub1 &&\n+\t! test -e sub1/.git\n+'\n+\n+test_expect_success 'setup nested submodule' '\n+\tgit init sub1/nested &&\n+\ttest_commit -C sub1/nested first_nested &&\n+\tgit -C sub1 submodule add ./nested &&\n+\ttest_tick &&\n+\tgit -C sub1 commit -m \"add nested\" &&\n+\tgit add sub1 &&\n+\tgit commit -m \"sub1 to include nested submodule\"\n+'\n+\n+test_expect_success 'absorb the git dir in a nested submodule' '\n+\tgit status >expect.1 &&\n+\tgit -C sub1/nested rev-parse HEAD >expect.2 &&\n+\tgit submodule absorbgitdirs &&\n+\ttest -f sub1/nested/.git &&\n+\ttest -d .git/modules/sub1/modules/nested &&\n+\tgit status >actual.1 &&\n+\tgit -C sub1/nested rev-parse HEAD >actual.2 &&\n+\ttest_cmp expect.1 actual.1 &&\n+\ttest_cmp expect.2 actual.2\n+'\n+\n+test_expect_success 'setup a gitlink with missing .gitmodules entry' '\n+\tgit init sub2 &&\n+\ttest_commit -C sub2 first &&\n+\tgit add sub2 &&\n+\tgit commit -m superproject\n+'\n+\n+test_expect_success 'absorbing the git dir fails for incomplete submodules' '\n+\tgit status >expect.1 &&\n+\tgit -C sub2 rev-parse HEAD >expect.2 &&\n+\ttest_must_fail git submodule absorbgitdirs &&\n+\tgit -C sub2 fsck &&\n+\ttest -d sub2/.git &&\n+\tgit status >actual &&\n+\tgit -C sub2 rev-parse HEAD >actual.2 &&\n+\ttest_cmp expect.1 actual.1 &&\n+\ttest_cmp expect.2 actual.2\n+'\n+\n+test_expect_success 'setup a submodule with multiple worktrees' '\n+\t# first create another unembedded git dir in a new submodule\n+\tgit init sub3 &&\n+\ttest_commit -C sub3 first &&\n+\tgit submodule add ./sub3 &&\n+\ttest_tick &&\n+\tgit commit -m \"add another submodule\" &&\n+\tgit -C sub3 worktree add ../sub3_second_work_tree\n+'\n+\n+test_expect_success 'absorb a submodule with multiple worktrees' '\n+\ttest_must_fail git submodule absorbgitdirs sub3 2>error &&\n+\ttest_i18ngrep \"not supported\" error\n+'\n+\n+test_done\n-- \n2.11.0.rc2.29.g7c00390.dirty\n\n"},{"id":"307342","messageId":"20161209120006.GA6609@ash","threadId":"44655","inReplyTo":"20161208210329.12919-5-sbeller@google.com","subject":"Re: [PATCHv7 4/6] worktree: have a function to check if worktrees are in use","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-12-09T12:00:06Z","receivedAt":"2016-12-09T12:00:17Z","isPatch":false,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, Dec 08, 2016 at 01:03:27PM -0800, Stefan Beller wrote:\n> +/*\n> + * NEEDSWORK: The values in the returned worktrees are broken, e.g.\n> + * the refs or path resolution is influenced by the current repository.\n> + */\n> +static struct worktree **get_submodule_worktrees(const char *path, unsigned flags)\n\nI'm ok with this (at least we know the function is half-broken). But I\nwonder if we could go simpler, with something like this. It's more\nefficient as well. And we can replace its implementation with\nget_worktrees() or something when we are able to.\n\nAs long as this function stays in worktree.c I think it's ok for it to\npeek into \"worktrees\" directory directly. That's what all other\nfunctions in this file do after all.\n\nint submodule_uses_worktrees(const char *path)\n{\n\tstruct strbuf path = STRBUF_INIT;\n\tDIR *dir;\n\tstruct dirent *d;\n\tint ret = 0;\n\n\tstrbuf_addf(&path, \"%s/worktrees\", path);\n\tdir = opendir(path.buf);\n\tstrbuf_release(&path);\n\n\tif (!dir)\n\t\treturn 0;\n\n\twhile ((d = readdir(dir)) != NULL) {\n\t\tif (is_dot_or_dotdot(d->d_name))\n\t\t\tcontinue;\n\n\t\tret = 1;\n\t\tbreak;\n\t}\n\tclosedir(dir);\n\treturn ret;\n}\n--\nDuy\n"},{"id":"307377","messageId":"CAGZ79kaV4FYZEbRWQxKBHJg1jVzOkte1QxLZgu75=Jja_BMRGQ@mail.gmail.com","threadId":"44655","inReplyTo":"20161209120006.GA6609@ash","subject":"Re: [PATCHv7 4/6] worktree: have a function to check if worktrees are in use","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-12-09T18:49:28Z","receivedAt":"2016-12-09T18:49:33Z","isPatch":false,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Fri, Dec 9, 2016 at 4:00 AM, Duy Nguyen <pclouds@gmail.com> wrote:\n\n> int submodule_uses_worktrees(const char *path)\n> {\n>         struct strbuf path = STRBUF_INIT;\n>         DIR *dir;\n>         struct dirent *d;\n>         int ret = 0;\n>\n>         strbuf_addf(&path, \"%s/worktrees\", path);\n>         dir = opendir(path.buf);\n>         strbuf_release(&path);\n>\n>         if (!dir)\n>                 return 0;\n\nThe submodule may be one of the linked worktrees, which would be\ncaught if we use the code as I sent it out?\n\nIf this is one of the linked worktrees, we'd rather check if a file\n\"commondir\" or \"gitdir\" exists?\n\nI ask that because I would not know how to relocate such a linked\nworktree gitdir?\n"},{"id":"307436","messageId":"CACsJy8CQD9QOhJzN7Dm8-=bjrZSEWM_03ETgroin-Lm6vKMuRw@mail.gmail.com","threadId":"44655","inReplyTo":"CAGZ79kaV4FYZEbRWQxKBHJg1jVzOkte1QxLZgu75=Jja_BMRGQ@mail.gmail.com","subject":"Re: [PATCHv7 4/6] worktree: have a function to check if worktrees are in use","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-12-10T11:16:08Z","receivedAt":"2016-12-10T11:16:45Z","isPatch":false,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sat, Dec 10, 2016 at 1:49 AM, Stefan Beller <sbeller@google.com> wrote:\n> On Fri, Dec 9, 2016 at 4:00 AM, Duy Nguyen <pclouds@gmail.com> wrote:\n>\n>> int submodule_uses_worktrees(const char *path)\n>> {\n>>         struct strbuf path = STRBUF_INIT;\n>>         DIR *dir;\n>>         struct dirent *d;\n>>         int ret = 0;\n>>\n>>         strbuf_addf(&path, \"%s/worktrees\", path);\n>>         dir = opendir(path.buf);\n>>         strbuf_release(&path);\n>>\n>>         if (!dir)\n>>                 return 0;\n>\n> The submodule may be one of the linked worktrees, which would be\n> caught if we use the code as I sent it out?\n\nI think I simplified it too much, there should still be a\ngit_common_dir_noenv() to retrieve the correct common dir in the\nsubmodule from gitdir. Then, if the repo in question has any linked\nworktrees, you probably can't handle it. It does not matter if the\nsubmodule gitdir in question is a linked or main worktree.\n\n> If this is one of the linked worktrees, we'd rather check if a file\n> \"commondir\" or \"gitdir\" exists?\n\nWell, in theory yes. But we're apparently not ready for that. If you\ncheck the files exist, but the files are not valid, then it's still\nnot considered a worktree. Which is not much different from what we do\nhere (if the directory exists, assuming it's a worktree). It should\ncatch all the valid worktrees. But yes it could give some false\npositive. Since we're just using this function to stop the aborbing\ngit dir operation, i think this is acceptable.\n\n(It goes even trickier, even get_worktrees() won't detect if the\nlinked worktree is already dead, .e.g. deleted manually, I believe.\nThose have to be cleaned up by 'git worktree prune' which does even\nmore checks before it declare \"this directory is garbage\").\n\n> I ask that because I would not know how to relocate such a linked\n> worktree gitdir?\n\n\n\n-- \nDuy\n"}]}