Skip to content

Commit

Permalink
Clean up work-tree handling
Browse files Browse the repository at this point in the history
The old version of work-tree support was an unholy mess, barely readable,
and not to the point.

For example, why do you have to provide a worktree, when it is not used?
As in "git status".  Now it works.

Another riddle was: if you can have work trees inside the git dir, why
are some programs complaining that they need a work tree?

IOW it is allowed to call

	$ git --git-dir=../ --work-tree=. bla

when you really want to.  In this case, you are both in the git directory
and in the working tree.  So, programs have to actually test for the right
thing, namely if they are inside a working tree, and not if they are
inside a git directory.

Also, GIT_DIR=../.git should behave the same as if no GIT_DIR was
specified, unless there is a repository in the current working directory.
It does now.

The logic to determine if a repository is bare, or has a work tree
(tertium non datur), is this:

--work-tree=bla overrides GIT_WORK_TREE, which overrides core.bare = true,
which overrides core.worktree, which overrides GIT_DIR/.. when GIT_DIR
ends in /.git, which overrides the directory in which .git/ was found.

In related news, a long standing bug was fixed: when in .git/bla/x.git/,
which is a bare repository, git formerly assumed ../.. to be the
appropriate git dir.  This problem was reported by Shawn Pearce to have
caused much pain, where a colleague mistakenly ran "git init" in "/" a
long time ago, and bare repositories just would not work.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Johannes Schindelin authored and Junio C Hamano committed Aug 1, 2007
1 parent d7ac12b commit e90fdc3
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 221 deletions.
48 changes: 11 additions & 37 deletions builtin-init-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,7 @@ static void copy_templates(const char *git_dir, int len, const char *template_di
closedir(dir);
}

/*
* Get the full path to the working tree specified in $GIT_WORK_TREE
* or NULL if no working tree is specified.
*/
static const char *get_work_tree(void)
{
const char *git_work_tree;
char cwd[PATH_MAX];
static char worktree[PATH_MAX];

git_work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
if (!git_work_tree)
return NULL;
if (!getcwd(cwd, sizeof(cwd)))
die("Unable to read current working directory");
if (chdir(git_work_tree))
die("Cannot change directory to specified working tree '%s'",
git_work_tree);
if (git_work_tree[0] != '/') {
if (!getcwd(worktree, sizeof(worktree)))
die("Unable to read current working directory");
git_work_tree = worktree;
}
if (chdir(cwd))
die("Cannot come back to cwd");
return git_work_tree;
}

static int create_default_files(const char *git_dir, const char *git_work_tree,
const char *template_path)
static int create_default_files(const char *git_dir, const char *template_path)
{
unsigned len = strlen(git_dir);
static char path[PATH_MAX];
Expand Down Expand Up @@ -282,16 +253,16 @@ static int create_default_files(const char *git_dir, const char *git_work_tree,
}
git_config_set("core.filemode", filemode ? "true" : "false");

if (is_bare_repository() && !git_work_tree) {
if (is_bare_repository())
git_config_set("core.bare", "true");
}
else {
const char *work_tree = get_git_work_tree();
git_config_set("core.bare", "false");
/* allow template config file to override the default */
if (log_all_ref_updates == -1)
git_config_set("core.logallrefupdates", "true");
if (git_work_tree)
git_config_set("core.worktree", git_work_tree);
if (work_tree != git_work_tree_cfg)
git_config_set("core.worktree", work_tree);
}
return reinit;
}
Expand All @@ -308,7 +279,6 @@ static const char init_db_usage[] =
int cmd_init_db(int argc, const char **argv, const char *prefix)
{
const char *git_dir;
const char *git_work_tree;
const char *sha1_dir;
const char *template_dir = NULL;
char *path;
Expand All @@ -329,7 +299,11 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
usage(init_db_usage);
}

git_work_tree = get_work_tree();
git_work_tree_cfg = xcalloc(PATH_MAX, 1);
if (!getcwd(git_work_tree_cfg, PATH_MAX))
die ("Cannot access current working directory.");
if (access(get_git_work_tree(), X_OK))
die ("Cannot access work tree '%s'", get_git_work_tree());

/*
* Set up the default .git directory contents
Expand All @@ -346,7 +320,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
*/
check_repository_format();

reinit = create_default_files(git_dir, git_work_tree, template_dir);
reinit = create_default_files(git_dir, template_dir);

/*
* And set up the object store.
Expand Down
8 changes: 5 additions & 3 deletions builtin-ls-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,11 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
break;
}

if (require_work_tree &&
(!is_inside_work_tree() || is_inside_git_dir()))
die("This operation must be run in a work tree");
if (require_work_tree && !is_inside_work_tree()) {
const char *work_tree = get_git_work_tree();
if (!work_tree || chdir(work_tree))
die("This operation must be run in a work tree");
}

pathspec = get_pathspec(prefix, argv + i);

Expand Down
7 changes: 7 additions & 0 deletions builtin-rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,13 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
}
if (!strcmp(arg, "--show-cdup")) {
const char *pfx = prefix;
if (!is_inside_work_tree()) {
const char *work_tree =
get_git_work_tree();
if (work_tree)
printf("%s\n", work_tree);
continue;
}
while (pfx) {
pfx = strchr(pfx, '/');
if (pfx) {
Expand Down
2 changes: 2 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,15 @@ enum object_type {
extern int is_bare_repository_cfg;
extern int is_bare_repository(void);
extern int is_inside_git_dir(void);
extern char *git_work_tree_cfg;
extern int is_inside_work_tree(void);
extern const char *get_git_dir(void);
extern char *get_object_directory(void);
extern char *get_refs_directory(void);
extern char *get_index_file(void);
extern char *get_graft_file(void);
extern int set_git_dir(const char *path);
extern const char *get_git_work_tree(void);

#define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"

Expand Down
35 changes: 26 additions & 9 deletions environment.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ int pager_in_use;
int pager_use_color = 1;
int auto_crlf = 0; /* 1: both ways, -1: only when adding git objects */

/* This is set by setup_git_dir_gently() and/or git_default_config() */
char *git_work_tree_cfg;
static const char *work_tree;

static const char *git_dir;
static char *git_object_dir, *git_index_file, *git_refs_dir, *git_graft_file;

Expand Down Expand Up @@ -62,15 +66,8 @@ static void setup_git_env(void)

int is_bare_repository(void)
{
const char *dir, *s;
if (0 <= is_bare_repository_cfg)
return is_bare_repository_cfg;

dir = get_git_dir();
if (!strcmp(dir, DEFAULT_GIT_DIR_ENVIRONMENT))
return 0;
s = strrchr(dir, '/');
return !s || strcmp(s + 1, DEFAULT_GIT_DIR_ENVIRONMENT);
/* if core.bare is not 'false', let's see if there is a work tree */
return is_bare_repository_cfg && !get_git_work_tree();
}

const char *get_git_dir(void)
Expand All @@ -80,6 +77,26 @@ const char *get_git_dir(void)
return git_dir;
}

const char *get_git_work_tree(void)
{
static int initialized = 0;
if (!initialized) {
work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
/* core.bare = true overrides implicit and config work tree */
if (!work_tree && is_bare_repository_cfg < 1) {
work_tree = git_work_tree_cfg;
/* make_absolute_path also normalizes the path */
if (work_tree && !is_absolute_path(work_tree))
work_tree = xstrdup(make_absolute_path(git_path(work_tree)));
} else if (work_tree)
work_tree = xstrdup(make_absolute_path(work_tree));
initialized = 1;
if (work_tree)
is_bare_repository_cfg = 0;
}
return work_tree;
}

char *get_object_directory(void)
{
if (!git_object_dir)
Expand Down
3 changes: 1 addition & 2 deletions git-sh-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ cd_to_toplevel () {
}

require_work_tree () {
test $(git rev-parse --is-inside-work-tree) = true &&
test $(git rev-parse --is-inside-git-dir) = false ||
test $(git rev-parse --is-inside-work-tree) = true ||
die "fatal: $0 cannot be used without a working tree."
}

Expand Down
11 changes: 8 additions & 3 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,14 @@ static int run_command(struct cmd_struct *p, int argc, const char **argv)
prefix = setup_git_directory();
if (p->option & USE_PAGER)
setup_pager();
if ((p->option & NEED_WORK_TREE) &&
(!is_inside_work_tree() || is_inside_git_dir()))
die("%s must be run in a work tree", p->cmd);
if (p->option & NEED_WORK_TREE) {
const char *work_tree = get_git_work_tree();
const char *git_dir = get_git_dir();
if (!is_absolute_path(git_dir))
set_git_dir(make_absolute_path(git_dir));
if (!work_tree || chdir(work_tree))
die("%s must be run in a work tree", p->cmd);
}
trace_argv_printf(argv, argc, "trace: built-in: git");

status = p->fn(argc, argv, prefix);
Expand Down
Loading

0 comments on commit e90fdc3

Please sign in to comment.