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

Re: [PATCH 05/15] connect_work_tree_and_git_dir: safely create leading directories

From
Stefan Beller <sbeller@google.com>
Date
Feb 16, 2017, 21:16 UTC
Message-ID
<CAGZ79kbMmPQ1ZxtdaoFof36g1An-xsAx85etiskirJfY4tGHaQ@mail.gmail.com>
In-Reply-To
<xmqqh93tx3f3.fsf@gitster.mtv.corp.google.com>
On Thu, Feb 16, 2017 at 12:54 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 60 quoted lines
> Stefan Beller <sbeller@google.com> writes:
>
>> In a later patch we'll use connect_work_tree_and_git_dir when the
>> directory for the gitlink file doesn't exist yet. Safely create
>> the directory first.
>>
>> One of the two users of 'connect_work_tree_and_git_dir' already checked
>> for the directory being there, so we can loose that check.
>>
>> Signed-off-by: Stefan Beller <sbeller@google.com>
>> ---
>>  dir.c       | 32 +++++++++++++++++++++-----------
>>  submodule.c | 11 ++---------
>>  2 files changed, 23 insertions(+), 20 deletions(-)
>>
>> diff --git a/dir.c b/dir.c
>> index 4541f9e146..6f52af7abb 100644
>> --- a/dir.c
>> +++ b/dir.c
>> @@ -2728,23 +2728,33 @@ void untracked_cache_add_to_index(struct index_state *istate,
>>  /* Update gitfile and core.worktree setting to connect work tree and git dir */
>>  void connect_work_tree_and_git_dir(const char *work_tree_, const char *git_dir_)
>>  {
>> -     struct strbuf file_name = STRBUF_INIT;
>> +     struct strbuf gitfile_sb = STRBUF_INIT;
>> +     struct strbuf cfg_sb = STRBUF_INIT;
>>       struct strbuf rel_path = STRBUF_INIT;
>> -     char *git_dir = real_pathdup(git_dir_);
>> -     char *work_tree = real_pathdup(work_tree_);
>> +     char *git_dir, *work_tree;
>>
>> -     /* Update gitfile */
>> -     strbuf_addf(&file_name, "%s/.git", work_tree);
>> -     write_file(file_name.buf, "gitdir: %s",
>> -                relative_path(git_dir, work_tree, &rel_path));
>> +     /* Prepare .git file */
>> +     strbuf_addf(&gitfile_sb, "%s/.git", work_tree_);
>> +     if (safe_create_leading_directories_const(gitfile_sb.buf))
>> +             die(_("could not create directories for %s"), gitfile_sb.buf);
>> +
>> +     /* Prepare config file */
>> +     strbuf_addf(&cfg_sb, "%s/config", git_dir_);
>> +     if (safe_create_leading_directories_const(cfg_sb.buf))
>> +             die(_("could not create directories for %s"), cfg_sb.buf);
>>
>> +     git_dir = real_pathdup(git_dir_);
>> +     work_tree = real_pathdup(work_tree_);
>> +
>> +     /* Write .git file */
>> +     write_file(gitfile_sb.buf, "gitdir: %s",
>> +                relative_path(git_dir, work_tree, &rel_path));
>
> The above does somewhat more than advertised and was a bit hard to
> grok.  Initially I thought the reason why pathdup()s were delayed
> was perhaps because you pathdup() something potentially different
> from the given parameter to the function (i.e. new code before
> pathdup() may tweak what is pathdup()ed).
>
> But that is not what is happening.  I suspect that you did this to
> avoid leaking allocated memory when the code calls die().

That is not what is happening, either. AFAICT real_pathdup resolves a path to its real path. But the path *must* exist already (except the very last part, i.e. the file name). So the SCLD must come before the real_pathdup, which has to come before its consumers, which are the relative_path calls for write_file as well as git_config_set_in_file.

So structurally we need to:

1a) construct $GIT_DIR/modules/<name>/config 1b) SCLD 1a) 1c) get absolute path of 1a)

2a) construct <submodule path>/.git file 2b) SCLD 2a) 2c) get absolute path of 2a)

3) compute relative path of 1c and 2c
    both ways, write to appropriate places.
I chose to structure it as:

1a 1b 2a 2b

1c 2c 3

because the a/b steps seemed very related and the order is valid.
Show 7 quoted lines
>
> If the code was written like so from the beginning, I do not see a
> reason to move the pathdup() up to deliberately make it leak [*1*].
> But as a part of this change, I found it distracting and getting in
> the way of understanding the change.  If you really care, it is
> nicer to do it to reviewers as a separate preparatory clean-up step,
> or follow-up standalone clean-up patch after the series settles.
I considered doing it in multiple patches as we have different things
going on here:
1) as the commit claims have SCLD in connect_work_tree_and_git_dir
2) keep the dependencies of SCLD and real_pathdup in order.  C.f.
  the deleted lines of code in submodule.c
Show 5 quoted lines
> It is a good thing to do to make sure git_dir_ exists and it should
> be mentioned in the log message.  Adding "Do the same for the place
> where the per-repo config file is created". at the end of the first
> paragraph should be sufficient.
>
will do.

Thanks for thorough review! Stefan

Previous: Junio C HamanoNext: Junio C Hamano
Message 35 of 94 in “Checkout aware of Submodules!”
  1. 00/14 Checkout aware of Submodules!Stefan Beller, Feb 15, 2017
  2. 01/14 lib-submodule-update.sh: reorder create_lib_submodule_repoStefan Beller, Feb 15, 2017
  3. brian m. carlsonFeb 15, 2017
  4. 02/14 lib-submodule-update.sh: define tests for recursing into submodulesStefan Beller, Feb 15, 2017
  5. Brandon WilliamsFeb 15, 2017
  6. Stefan BellerFeb 15, 2017
  7. 07/14 update submodules: introduce is_interesting_submoduleStefan Beller, Feb 15, 2017
  8. Brandon WilliamsFeb 15, 2017
  9. Stefan BellerFeb 15, 2017
  10. 06/14 update submodules: add a config option to determine if submodules are updatedStefan Beller, Feb 15, 2017
  11. 05/14 update submodules: add submodule config parsingStefan Beller, Feb 15, 2017
  12. Junio C HamanoFeb 15, 2017
  13. 11/14 unpack-trees: check if we can perform the operation for submodulesStefan Beller, Feb 15, 2017
  14. 10/14 unpack-trees: pass old oid to verify_clean_submoduleStefan Beller, Feb 15, 2017
  15. 09/14 update submodules: add submodule_go_from_toStefan Beller, Feb 15, 2017
  16. brian m. carlsonFeb 15, 2017
  17. 04/14 connect_work_tree_and_git_dir: safely create leading directoriesStefan Beller, Feb 15, 2017
  18. Junio C HamanoFeb 15, 2017
  19. Stefan BellerFeb 15, 2017
  20. Junio C HamanoFeb 15, 2017
  21. 13/14 entry.c: update submodules when interestingStefan Beller, Feb 15, 2017
  22. 14/14 builtin/checkout: add --recurse-submodules switchStefan Beller, Feb 15, 2017
  23. brian m. carlsonFeb 15, 2017
  24. Stefan BellerFeb 16, 2017
  25. 12/14 read-cache: remove_marked_cache_entries to wipe selected submodules.Stefan Beller, Feb 15, 2017
  26. 03/14 make is_submodule_populated gentlyStefan Beller, Feb 15, 2017
  27. Junio C HamanoFeb 15, 2017
  28. Stefan BellerFeb 15, 2017
  29. 08/14 update submodules: move up prepare_submodule_repo_envStefan Beller, Feb 15, 2017
  30. brian m. carlsonFeb 15, 2017
  31. Junio C HamanoFeb 15, 2017
  32. 00/14 Checkout aware of Submodules!Stefan Beller, Feb 16, 2017
  33. 05/15 connect_work_tree_and_git_dir: safely create leading directoriesStefan Beller, Feb 16, 2017
  34. Junio C HamanoFeb 16, 2017
  35. Stefan BellerFeb 16, 2017
  36. Junio C HamanoFeb 16, 2017
  37. 07/15 update submodules: add a config option to determine if submodules are updatedStefan Beller, Feb 16, 2017
  38. 03/15 lib-submodule-update.sh: define tests for recursing into submodulesStefan Beller, Feb 16, 2017
  39. Junio C HamanoFeb 16, 2017
  40. Stefan BellerFeb 22, 2017
  41. 02/15 lib-submodule-update.sh: do not use ./. as submodule remoteStefan Beller, Feb 16, 2017
  42. 08/15 submodules: introduce check to see whether to touch a submoduleStefan Beller, Feb 16, 2017
  43. Junio C HamanoFeb 16, 2017
  44. Jacob KellerFeb 16, 2017
  45. Jacob KellerFeb 17, 2017
  46. Stefan BellerFeb 21, 2017
  47. 04/15 make is_submodule_populated gentlyStefan Beller, Feb 16, 2017
  48. 09/15 update submodules: move up prepare_submodule_repo_envStefan Beller, Feb 16, 2017
  49. 10/15 update submodules: add submodule_go_from_toStefan Beller, Feb 16, 2017
  50. Junio C HamanoFeb 16, 2017
  51. Stefan BellerFeb 16, 2017
  52. 06/15 update submodules: add submodule config parsingStefan Beller, Feb 16, 2017
  53. Jacob KellerFeb 17, 2017
  54. Stefan BellerFeb 21, 2017
  55. Jacob KellerFeb 21, 2017
  56. 13/15 read-cache: remove_marked_cache_entries to wipe selected submodules.Stefan Beller, Feb 16, 2017
  57. Junio C HamanoFeb 16, 2017
  58. 15/15 builtin/checkout: add --recurse-submodules switchStefan Beller, Feb 16, 2017
  59. 11/15 unpack-trees: pass old oid to verify_clean_submoduleStefan Beller, Feb 16, 2017
  60. Junio C HamanoFeb 16, 2017
  61. 12/15 unpack-trees: check if we can perform the operation for submodulesStefan Beller, Feb 16, 2017
  62. Brandon WilliamsFeb 16, 2017
  63. Junio C HamanoFeb 16, 2017
  64. Jacob KellerFeb 17, 2017
  65. Stefan BellerFeb 21, 2017
  66. Jacob KellerFeb 21, 2017
  67. Stefan BellerFeb 21, 2017
  68. Jacob KellerFeb 22, 2017
  69. 14/15 entry.c: update submodules when interestingStefan Beller, Feb 16, 2017
  70. 01/15 lib-submodule-update.sh: reorder create_lib_submodule_repoStefan Beller, Feb 16, 2017
  71. Junio C HamanoFeb 16, 2017
  72. Jacob KellerFeb 16, 2017
  73. Stefan BellerFeb 16, 2017
  74. Jacob KellerFeb 16, 2017
  75. 00/14 Checkout aware of Submodules!Stefan Beller, Feb 23, 2017
  76. 08/15 submodules: introduce check to see whether to touch a submoduleStefan Beller, Feb 23, 2017
  77. 09/15 update submodules: move up prepare_submodule_repo_envStefan Beller, Feb 23, 2017
  78. 01/15 lib-submodule-update.sh: reorder create_lib_submodule_repoStefan Beller, Feb 23, 2017
  79. 04/15 make is_submodule_populated gentlyStefan Beller, Feb 23, 2017
  80. 03/15 lib-submodule-update.sh: define tests for recursing into submodulesStefan Beller, Feb 23, 2017
  81. 11/15 unpack-trees: pass old oid to verify_clean_submoduleStefan Beller, Feb 23, 2017
  82. 02/15 lib-submodule-update.sh: do not use ./. as submodule remoteStefan Beller, Feb 23, 2017
  83. 06/15 update submodules: add submodule config parsingStefan Beller, Feb 23, 2017
  84. 14/15 entry.c: update submodules when interestingStefan Beller, Feb 23, 2017
  85. 12/15 unpack-trees: check if we can perform the operation for submodulesStefan Beller, Feb 23, 2017
  86. 05/15 connect_work_tree_and_git_dir: safely create leading directoriesStefan Beller, Feb 23, 2017
  87. 07/15 update submodules: add a config option to determine if submodules are updatedStefan Beller, Feb 23, 2017
  88. 10/15 update submodules: add submodule_move_headStefan Beller, Feb 23, 2017
  89. Ramsay JonesFeb 24, 2017
  90. Stefan BellerFeb 24, 2017
  91. 13/15 read-cache, remove_marked_cache_entries: wipe selected submodules.Stefan Beller, Feb 23, 2017
  92. 15/15 builtin/checkout: add --recurse-submodules switchStefan Beller, Feb 23, 2017
  93. Ramsay JonesFeb 24, 2017
  94. Stefan BellerFeb 24, 2017

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.