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

Re: [PATCH] setup.c: set workdir when gitdir is not default

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Sep 4, 2014, 10:44 UTC
Message-ID
<CAPig+cQMNyurCB73CzGBLcfXqap5=kZsYq0O8nD29S8QsHtrBA@mail.gmail.com>
In-Reply-To
<1409784120-2228-1-git-send-email-max@max630.net>
On Wed, Sep 3, 2014 at 6:42 PM, Max Kirillov <max@max630.net> wrote:
Show 43 quoted lines
> When gitfile is used, git sets GIT_DIR environment variable for
> subsequent commands, and that commands start working in mode "GIT_DIR
> set, workdir current", which is incorrect for the case when git runs
> from subdirectory of repository. This can be observed at least for
> running aliases - git fails with message "internal error: work tree has
> already been set"
>
> Fix by setting GIT_WORK_TREE environment also.
>
> Add test which demonstrates problem with alias.
>
> Signed-off-by: Max Kirillov <max@max630.net>
> ---
>  setup.c            | 4 +++-
>  t/t0002-gitfile.sh | 7 +++++++
>  2 files changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/setup.c b/setup.c
> index 0a22f8b..bcf4e31 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -508,8 +508,10 @@ static const char *setup_discovered_git_dir(const char *gitdir,
>
>         /* #0, #1, #5, #8, #9, #12, #13 */
>         set_git_work_tree(".");
> -       if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
> +       if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) {
>                 set_git_dir(gitdir);
> +               setenv(GIT_WORK_TREE_ENVIRONMENT, get_git_work_tree(), 1);
> +       }
>         inside_git_dir = 0;
>         inside_work_tree = 1;
>         if (offset == len)
> diff --git a/t/t0002-gitfile.sh b/t/t0002-gitfile.sh
> index 37e9396..428cfdc 100755
> --- a/t/t0002-gitfile.sh
> +++ b/t/t0002-gitfile.sh
> @@ -99,4 +99,11 @@ test_expect_success 'check rev-list' '
>         test "$SHA" = "$(git rev-list HEAD)"
>  '
>
> +test_expect_success 'check alias call from subdirectory' '
> +       git config alias.testalias "rev-parse HEAD" &&
Perhaps use test_config here?
> +       mkdir -p subdir &&
> +       cd subdir &&
> +       git testalias

If a new test is added following this one, it will be run from within 'subdir', which might come as a surprise as the author of the new test. Wrapping the 'cd' and following code in a subshell is a good way to avoid the problem.

    mkdir -p subdir &&
    (
        cd subdir &&
        git testalias
    )
Show 5 quoted lines
> +'
> +
>  test_done
> --
> 2.0.1.1697.g73c6810
Previous: Max KirillovNext: Max Kirillov
Message 2 of 5 in “setup.c: set workdir when gitdir is not default”
  1. setup.c: set workdir when gitdir is not defaultMax Kirillov, Sep 3, 2014
  2. Eric SunshineSep 4, 2014
  3. Max KirillovSep 4, 2014
  4. Duy NguyenSep 4, 2014
  5. Max KirillovSep 4, 2014

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.