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

Re: [PATCH v2 2/3] init: do not set core.worktree more often than necessary

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 21, 2016, 18:44 UTC
Message-ID
<xmqqd1jx854z.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20160921112939.3444-3-pclouds@gmail.com>
Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:
Show 7 quoted lines
> When "git init" is called with GIT_WORK_TREE environment set, we want to
> keep this worktree's location in core.worktree so the user does not have
> to set the environment again and again. See ef6f0af (git-init: set
> core.worktree if GIT_WORK_TREE is specified - 2007-07-04)
>
> We detect that by this logic (in needs_work_tree_config): normally
> worktree's top dir would contains ".git" directory, if this is not true,
s/contains/contain/;
Show 10 quoted lines
> worktree is probably set to elsewhere by the user.
>
> Unfortunately when it calls get_git_dir() it does not take ".git" files
> into account. When we find a .git file, we immediately follow the file
> until we find the real ".git" directory. The location of this first
> ".git" file is lost.
>
> The .git file would satisfy the logic above and not create
> core.worktree (correct). But because the final .git's location is used,
> needs_work_tree_config() is misled and creates core.worktree anyway.

The above explanation makes it sound like the correct fix belongs to needs_work_tree_config(), though.

I am starting to wonder if what ef6f0af did was misguided and we are better off without setting core.worktree ourselves, but that is a different issue.

Show 29 quoted lines
> diff --git a/builtin/init-db.c b/builtin/init-db.c
> index d5d7558..0d5cc76 100644
> --- a/builtin/init-db.c
> +++ b/builtin/init-db.c
> @@ -23,6 +23,7 @@ static int init_is_bare_repository = 0;
>  static int init_shared_repository = -1;
>  static const char *init_db_template_dir;
>  static const char *git_link;
> +static const char *original_git_dir;
>  
>  static void copy_templates_1(struct strbuf *path, struct strbuf *template,
>  			     DIR *dir)
> @@ -263,7 +264,7 @@ static int create_default_files(const char *template_path)
>  		/* allow template config file to override the default */
>  		if (log_all_ref_updates == -1)
>  			git_config_set("core.logallrefupdates", "true");
> -		if (needs_work_tree_config(get_git_dir(), work_tree))
> +		if (needs_work_tree_config(original_git_dir, work_tree))
>  			git_config_set("core.worktree", work_tree);
>  	}
>  
> @@ -314,6 +315,8 @@ static void create_object_directory(void)
>  int set_git_dir_init(const char *git_dir, const char *real_git_dir,
>  		     int exist_ok)
>  {
> +	original_git_dir = xstrdup(real_path(git_dir));
> +
>  	if (real_git_dir) {
>  		struct stat st;

The function being extern bothers me. The create_default_files() function, which is the only thing consumes this variable, is called only from init_db(), and I'd prefer to see some way to guarantee that everybody who calls init_db() calls set_git_dir_init() beforehand. Right now, cmd_init_db() and cmd_clone() are the only ones that call init_db() and they both call set_dir_git_init(); if a new caller starts calling init_db() and forgets to call the other one, that caller will be buggy.

Perhaps a comment before init_db() to tell callers to always call the other one is the least thing necessary?

Show 16 quoted lines
> diff --git a/t/t0001-init.sh b/t/t0001-init.sh
> index 488564b..b8fc588 100755
> --- a/t/t0001-init.sh
> +++ b/t/t0001-init.sh
> @@ -400,9 +400,11 @@ test_expect_success 're-init from a linked worktree' '
>  		test_commit first &&
>  		git worktree add ../linked-worktree &&
>  		mv .git/info/exclude expected-exclude &&
> +		cp .git/config expected-config &&
>  		find .git/worktrees -print | sort >expected &&
>  		git -C ../linked-worktree init &&
>  		test_cmp expected-exclude .git/info/exclude &&
> +		test_cmp expected-config .git/config &&
>  		find .git/worktrees -print | sort >actual &&
>  		test_cmp expected actual
>  	)
Previous: Nguyễn Thái Ngọc DuyNext: Duy Nguyen
Message 20 of 34 in “Bug with git worktrees and git init”
  1. Max NordlundAug 23, 2016
  2. Michael J GruberAug 23, 2016
  3. Duy NguyenAug 24, 2016
  4. 0/3 Fix git-init in linked worktreesNguyễn Thái Ngọc Duy, Sep 8, 2016
  5. 1/3 init: correct re-initialization from a linked worktreeNguyễn Thái Ngọc Duy, Sep 8, 2016
  6. Junio C HamanoSep 8, 2016
  7. Duy NguyenSep 9, 2016
  8. 2/3 t0001: work around the bug that reads config file before repo setupNguyễn Thái Ngọc Duy, Sep 8, 2016
  9. Junio C HamanoSep 8, 2016
  10. Jeff KingSep 8, 2016
  11. Duy NguyenSep 9, 2016
  12. Jeff KingSep 9, 2016
  13. Jacob KellerSep 9, 2016
  14. 3/3 init: do not set core.worktree more often than necessaryNguyễn Thái Ngọc Duy, Sep 8, 2016
  15. Junio C HamanoSep 8, 2016
  16. Duy NguyenSep 9, 2016
  17. 0/3 Fix git-init in linked worktreesNguyễn Thái Ngọc Duy, Sep 21, 2016
  18. 1/3 init: correct re-initialization from a linked worktreeNguyễn Thái Ngọc Duy, Sep 21, 2016
  19. 2/3 init: do not set core.worktree more often than necessaryNguyễn Thái Ngọc Duy, Sep 21, 2016
  20. Junio C HamanoSep 21, 2016
  21. Duy NguyenSep 22, 2016
  22. Junio C HamanoSep 22, 2016
  23. 4/3 init: combine set_git_dir_init() and init_db() into oneNguyễn Thái Ngọc Duy, Sep 23, 2016
  24. Junio C HamanoSep 23, 2016
  25. Junio C HamanoSep 23, 2016
  26. Junio C HamanoSep 24, 2016
  27. Duy NguyenSep 25, 2016
  28. 1/5 init: correct re-initialization from a linked worktreeNguyễn Thái Ngọc Duy, Sep 25, 2016
  29. 2/5 init: call set_git_dir_init() from within init_db()Nguyễn Thái Ngọc Duy, Sep 25, 2016
  30. 3/5 init: kill set_git_dir_init()Nguyễn Thái Ngọc Duy, Sep 25, 2016
  31. 4/5 init: do not set unnecessary core.worktreeNguyễn Thái Ngọc Duy, Sep 25, 2016
  32. 5/5 init: kill git_link variableNguyễn Thái Ngọc Duy, Sep 25, 2016
  33. 3/3 init: reuse original_git_dir in set_git_dir_init()Nguyễn Thái Ngọc Duy, Sep 21, 2016
  34. Junio C HamanoSep 21, 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.