git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Cc: git@vger.kernel.org, git@drmicha.warpmail.net, max.nordlund@sqore.com
Subject: Re: [PATCH v2 4/3] init: combine set_git_dir_init() and init_db() into one
Date: Sat, 24 Sep 2016 11:55:33 -0700	[thread overview]
Message-ID: <xmqq8tuhyvoa.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <xmqqshsqz0s1.fsf@gitster.mtv.corp.google.com> (Junio C. Hamano's message of "Fri, 23 Sep 2016 15:53:02 -0700")

Junio C Hamano <gitster@pobox.com> writes:

> I think this 4/3 is not quite enough to fix the damage to the code
> caused by 2/3.
> ...
> after 4/3 is applied, we should be able to remove the global
> variable 2/3 introduced, make init_db() receive that information as
> the return value of set_git_dir_init(), and pass that as a parameter
> to create_default_files().

That would look something like this squashed into 4/3, I think.  I
am not sure if a commit that squashes 2/3, 3/3, 4/3 and this update
together is harder to understand than keeping 2/3, 3/3 and a fixed
4/3 separate, though.  The end result looks much better structured
than before 2/3 is applied to my quick scan-through of the code.

In any case, the log message of 2/3 needs to be updated to retitle
it, I think.  "do not ... more often than necessary" makes it sound
as if we were doing things that did not make any difference in the
end result, wasting cycles.  But what you actually wanted to achieve
was not to "avoid unnecessary work"--doing so gave a broken
behaviour and that was what you were fixing, "do not record broken
core.worktree", perhaps?

The solution (if we squash 2-4 and the fixup below) is to stop
feeding get_git_dir() to needs_work_tree_config(), because the
parameter to the latter is the path to ".git" that presumably is at
the top of the working tree, and get_git_dir() is not that when
"gitdir" file is involved.  So a rewritten log message may say
something like...

    init: do not set unnecessary core.worktree

    The function needs_work_tree_config() that is called from
    create_default_files() is supposed to be fed the path to ".git"
    that looks as if it is at the top of the working tree, and
    decide if that location matches the actual worktree being used.
    This comparison allows "git init" to decide if core.worktree
    needs to be recorded in the working tree.

    In the current code, however, we feed the return value from
    get_git_dir(), which can be totally different from what the
    function expects when "gitdir" file is involved.  Instead of
    giving the path to the ".git" at the top of the working tree, we
    end up feeding the actual path that the file points at.

    This original location of ".git" however is only known to a
    helper function set_git_dir_init() that must be called before
    init_db() is called (they both have only two callsites, one in
    "git init" and the other in "git clone"), and in the current
    code, this original location is not visible to its callers.

    By doing the following two things:

    * Move call to set_git_dir_init() to init_db(), as the two must
      always be called in this order, and adjust its current
      callers.

    * Make set_git_dir_init() return the original location of ".git"
      to the caller, which is init_db(), and have it passed to
      create_default_files() as a new parameter.

   pass the correct location down to needs_work_tree_config() to fix
   this.

This suggests that 2/3, 3/3 and fixed 4/3 could be done in two
logical steps.  The first bullet point can be done as a separate
preparatory step, and on top of that, the second bullet point can be
done as a separate "fix".



 builtin/init-db.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/builtin/init-db.c b/builtin/init-db.c
index ee7942f..527722c 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -23,7 +23,6 @@ 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)
@@ -172,7 +171,8 @@ static int needs_work_tree_config(const char *git_dir, const char *work_tree)
 	return 1;
 }
 
-static int create_default_files(const char *template_path)
+static int create_default_files(const char *template_path,
+				const char *original_git_dir)
 {
 	struct stat st1;
 	struct strbuf buf = STRBUF_INIT;
@@ -312,11 +312,11 @@ static void create_object_directory(void)
 	strbuf_release(&path);
 }
 
-static int set_git_dir_init(const char *git_dir,
-			    const char *real_git_dir,
-			    int exist_ok)
+static char *set_git_dir_init(const char *git_dir,
+			      const char *real_git_dir,
+			      int exist_ok)
 {
-	original_git_dir = xstrdup(real_path(git_dir));
+	char *original_git_dir = xstrdup(real_path(git_dir));
 
 	if (real_git_dir) {
 		struct stat st;
@@ -339,7 +339,7 @@ static int set_git_dir_init(const char *git_dir,
 		git_link = NULL;
 	}
 	startup_info->have_repository = 1;
-	return 0;
+	return original_git_dir;
 }
 
 static void separate_git_dir(const char *git_dir)
@@ -367,9 +367,10 @@ int init_db(const char *git_dir, const char *real_git_dir,
 	    const char *template_dir, unsigned int flags)
 {
 	int reinit;
+	char *original_git_dir;
 
-	set_git_dir_init(git_dir, real_git_dir, flags & INIT_DB_EXIST_OK);
-
+	flags |= INIT_DB_EXIST_OK;
+	original_git_dir = set_git_dir_init(git_dir, real_git_dir, flags);
 	git_dir = get_git_dir();
 
 	if (git_link)
@@ -386,7 +387,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
 	 */
 	check_repository_format();
 
-	reinit = create_default_files(template_dir);
+	reinit = create_default_files(template_dir, original_git_dir);
 
 	create_object_directory();
 

  reply	other threads:[~2016-09-24 18:55 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-23 12:35 Bug with git worktrees and git init Max Nordlund
2016-08-23 15:21 ` Michael J Gruber
2016-08-24  9:35   ` Duy Nguyen
2016-09-08 13:47     ` [PATCH 0/3] Fix git-init in linked worktrees Nguyễn Thái Ngọc Duy
2016-09-08 13:47       ` [PATCH 1/3] init: correct re-initialization from a linked worktree Nguyễn Thái Ngọc Duy
2016-09-08 19:37         ` Junio C Hamano
2016-09-09 10:36           ` Duy Nguyen
2016-09-08 13:47       ` [PATCH 2/3] t0001: work around the bug that reads config file before repo setup Nguyễn Thái Ngọc Duy
2016-09-08 19:44         ` Junio C Hamano
2016-09-08 20:02         ` Jeff King
2016-09-09 10:32           ` Duy Nguyen
2016-09-09 11:22             ` Jeff King
2016-09-09 17:45               ` Jacob Keller
2016-09-08 13:47       ` [PATCH 3/3] init: do not set core.worktree more often than necessary Nguyễn Thái Ngọc Duy
2016-09-08 19:54         ` Junio C Hamano
2016-09-09 10:33           ` Duy Nguyen
2016-09-21 11:29       ` [PATCH v2 0/3] Fix git-init in linked worktrees Nguyễn Thái Ngọc Duy
2016-09-21 11:29         ` [PATCH v2 1/3] init: correct re-initialization from a linked worktree Nguyễn Thái Ngọc Duy
2016-09-21 11:29         ` [PATCH v2 2/3] init: do not set core.worktree more often than necessary Nguyễn Thái Ngọc Duy
2016-09-21 18:44           ` Junio C Hamano
2016-09-22 10:06             ` Duy Nguyen
2016-09-22 17:27               ` Junio C Hamano
2016-09-23 11:12                 ` [PATCH v2 4/3] init: combine set_git_dir_init() and init_db() into one Nguyễn Thái Ngọc Duy
2016-09-23 15:18                   ` Junio C Hamano
2016-09-23 22:53                   ` Junio C Hamano
2016-09-24 18:55                     ` Junio C Hamano [this message]
2016-09-25  3:13                       ` Duy Nguyen
2016-09-25  3:14                         ` [PATCH v3 1/5] init: correct re-initialization from a linked worktree Nguyễn Thái Ngọc Duy
2016-09-25  3:14                           ` [PATCH v3 2/5] init: call set_git_dir_init() from within init_db() Nguyễn Thái Ngọc Duy
2016-09-25  3:14                           ` [PATCH v3 3/5] init: kill set_git_dir_init() Nguyễn Thái Ngọc Duy
2016-09-25  3:14                           ` [PATCH v3 4/5] init: do not set unnecessary core.worktree Nguyễn Thái Ngọc Duy
2016-09-25  3:14                           ` [PATCH v3 5/5] init: kill git_link variable Nguyễn Thái Ngọc Duy
2016-09-21 11:29         ` [PATCH v2 3/3] init: reuse original_git_dir in set_git_dir_init() Nguyễn Thái Ngọc Duy
2016-09-21 18:18         ` [PATCH v2 0/3] Fix git-init in linked worktrees Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq8tuhyvoa.fsf@gitster.mtv.corp.google.com \
    --to=gitster@pobox.com \
    --cc=git@drmicha.warpmail.net \
    --cc=git@vger.kernel.org \
    --cc=max.nordlund@sqore.com \
    --cc=pclouds@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).