gitgit-fetch

ALTERNATE_DB_ENVIRONMENT environment variable does not work for setting alternate object directories in git


According to this comment in object-file.c in the source tree of git, setting the ALTERNATE_DB_ENVIRONMENT environment variable should work instead of using the .git/objects/info/alternates file.

The elements on this list come from non-empty elements from colon separated ALTERNATE_DB_ENVIRONMENT environment variable, and $GIT_OBJECT_DIRECTORY/info/alternates, whose contents is similar to that environment variable but can be LF separated.
Its base points at a statically allocated buffer that contains "/the/directory/corresponding/to/.git/objects/...", while its name points just after the slash at the end of ".git/objects/" in the example above, and has enough space to hold all hex characters of the object ID, an extra slash for the first level indirection, and the terminating NUL.

However, I have been unable to get the ALTERNATE_DB_ENVIRONMENT environment variable to work, even though the .git/objects/info/alternates method works fine.

Steps to reproduce: (Create alternate object database)

cd $HOME
mkdir -p .cache/git
cd .cache/git
git init --bare
git remote add bdwgc https://github.com/ivmai/bdwgc
git fetch --all

(Broken method using ALTERNATE_DB_ENVIRONMENT environment variable)

export ALTERNATE_DB_ENVIRONMENT=$HOME/.cache/git/objects
cd /tmp
mkdir bdwgc
cd bdwgc
git init
git fetch https://github.com/ivmai/bdwgc

(Functional method using .git/objects/info/alternates file)

cd /tmp
mkdir bdwgc
cd bdwgc
git init
echo "$HOME/.cache/git/objects" >> .git/objects/info/alternates
git fetch https://github.com/ivmai/bdwgc

(Both times it expands to an absolute path, in this case /home/nfl/.cache/git/objects.)

What am I doing wrong?


Solution

  • ALTERNATE_DB_ENVIRONMENT is the C source macro that expands to the shell environment variable name. It's defined in environment.h as

    #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
    

    so that's the variable name you should set.