Hi Duy and Clemens,

I keep forgetting the semantics of the setup vars.  Do you remember?
The result can go in Documentation/technical/api-setup.txt.

Frédéric Brière wrote:

>   $ pwd
>   /tmp/git
> 
>   $ git init
>   Initialized empty Git repository in /tmp/git/.git/
>   
>   $ git --work-tree /tmp/git symbolic-ref HEAD
>   refs/heads/master
> 
>   $ cd .git
>   $ git --work-tree /tmp/git symbolic-ref HEAD
>   fatal: ref HEAD is not a symbolic ref
> 
>   $ cp HEAD ..
>   $ git --work-tree /tmp/git symbolic-ref HEAD
>   refs/heads/master

Yagh.  Given: cwd is .git dir, GIT_DIR unset, GIT_WORK_TREE=/tmp/git

run_builtin() runs the repository setup:

 setup_git_directory() ->
  setup_git_directory_gently(NULL) ->
   setup_git_directory_gently_1(NULL) ->
    setup_bare_git_dir($GIT_WORK_TREE, strlen(cwd), strlen(cwd), cwd, NULL) ->
     set_git_dir(".")

Result:

 GIT_DIR=.
 GIT_WORK_TREE=/tmp/git
 inside_work_tree = -1
 prefix = NULL
 cwd = /tmp/git

Should the result be rather

 GIT_DIR=/tmp/git/.git
 prefix=.git
 inside_work_tree = 1

?  [The following patch is probably broken.  It's more of a "this is
the code path I am talking about" kind of thing than an actual fix.]

Signed-off-by: Jonathan Nieder <jrnie...@gmail.com>
---
Following up on [1].  Sorry to leave it hanging for so long.
[1] http://thread.gmane.org/gmane.comp.version-control.git/147552/focus=147559

diff --git a/setup.c b/setup.c
index a3b76de..c073e0a 100644
--- a/setup.c
+++ b/setup.c
@@ -313,12 +313,30 @@ const char *read_gitfile_gently(const char *path)
        return path;
 }
 
-static const char *setup_explicit_git_dir(const char *gitdirenv,
+static const char *setup_git_dir_with_work_tree(const char *gitdirenv,
                                const char *work_tree_env, int *nongit_ok)
 {
        static char buffer[1024 + 1];
        const char *retval;
 
+       if (check_repository_format_gently(nongit_ok))
+               return NULL;
+       retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
+                       get_git_work_tree());
+       if (!retval || !*retval)
+               return NULL;
+       set_git_dir(make_absolute_path(gitdirenv));
+       if (chdir(work_tree_env) < 0)
+               die_errno ("Could not chdir to '%s'", work_tree_env);
+       strcat(buffer, "/");
+       return retval;
+}
+
+static const char *setup_explicit_git_dir(const char *gitdirenv,
+                               const char *work_tree_env, int *nongit_ok)
+{
+       const char *retval;
+
        if (PATH_MAX - 40 < strlen(gitdirenv))
                die("'$%s' too big", GIT_DIR_ENVIRONMENT);
        if (!is_git_directory(gitdirenv)) {
@@ -328,23 +346,13 @@ static const char *setup_explicit_git_dir(const char 
*gitdirenv,
                }
                die("Not a git repository: '%s'", gitdirenv);
        }
-       if (!work_tree_env) {
-               retval = set_work_tree(gitdirenv);
-               /* config may override worktree */
-               if (check_repository_format_gently(nongit_ok))
-                       return NULL;
-               return retval;
-       }
+       if (work_tree_env)
+               return setup_git_dir_with_work_tree(gitdirenv,
+                               work_tree_env, nongit_ok);
+       retval = set_work_tree(gitdirenv);
+       /* config may override worktree */
        if (check_repository_format_gently(nongit_ok))
                return NULL;
-       retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
-                       get_git_work_tree());
-       if (!retval || !*retval)
-               return NULL;
-       set_git_dir(make_absolute_path(gitdirenv));
-       if (chdir(work_tree_env) < 0)
-               die_errno ("Could not chdir to '%s'", work_tree_env);
-       strcat(buffer, "/");
        return retval;
 }
 
@@ -388,16 +396,19 @@ static const char *setup_bare_git_dir(const char 
*work_tree_env,
        int root_len;
 
        inside_git_dir = 1;
-       if (!work_tree_env)
-               inside_work_tree = 0;
+       inside_work_tree = work_tree_env ? 1 : 0;
        if (offset != len) {
                if (chdir(cwd))
                        die_errno("Cannot come back to cwd");
                root_len = offset_1st_component(cwd);
                cwd[offset > root_len ? offset : root_len] = '\0';
                set_git_dir(cwd);
-       } else
+       } else {
+               if (work_tree_env)
+                       return setup_git_dir_with_work_tree(".",
+                               work_tree_env, nongit_ok);
                set_git_dir(".");
+       }
        check_repository_format_gently(nongit_ok);
        return NULL;
 }



-- 
To UNSUBSCRIBE, email to debian-bugs-dist-requ...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org

Reply via email to