Skip to content

Commit

Permalink
Merge branch 'jn/maint-setup-fix'
Browse files Browse the repository at this point in the history
* jn/maint-setup-fix:
  setup: split off a function to handle ordinary .git directories
  Revert "rehabilitate 'git index-pack' inside the object store"
  setup: do not forget working dir from subdir of gitdir
  t4111 (apply): refresh index before applying patches to it
  setup: split off get_device_or_die helper
  setup: split off a function to handle hitting ceiling in repo search
  setup: split off code to handle stumbling upon a repository
  setup: split off a function to checks working dir for .git file
  setup: split off $GIT_DIR-set case from setup_git_directory_gently
  tests: try git apply from subdir of toplevel
  t1501 (rev-parse): clarify

Conflicts:
	builtin/index-pack.c
  • Loading branch information
Junio C Hamano committed Aug 31, 2010
2 parents 1253164 + 98937be commit 693fefe
Show file tree
Hide file tree
Showing 4 changed files with 581 additions and 282 deletions.
24 changes: 5 additions & 19 deletions builtin/index-pack.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,31 +880,17 @@ int cmd_index_pack(int argc, const char **argv, const char *prefix)
char *index_name_buf = NULL, *keep_name_buf = NULL;
struct pack_idx_entry **idx_objects;
unsigned char pack_sha1[20];
int nongit;

if (argc == 2 && !strcmp(argv[1], "-h"))
usage(index_pack_usage);

read_replace_refs = 0;

/*
* We wish to read the repository's config file if any, and
* for that it is necessary to call setup_git_directory_gently().
* However if the cwd was inside .git/objects/pack/ then we need
* to go back there or all the pack name arguments will be wrong.
* And in that case we cannot rely on any prefix returned by
* setup_git_directory_gently() either.
*/
{
char cwd[PATH_MAX+1];
int nongit;

if (!getcwd(cwd, sizeof(cwd)-1))
die("Unable to get current working directory");
setup_git_directory_gently(&nongit);
git_config(git_index_pack_config, NULL);
if (chdir(cwd))
die("Cannot come back to cwd");
}
prefix = setup_git_directory_gently(&nongit);
git_config(git_index_pack_config, NULL);
if (prefix && chdir(prefix))
die("Cannot come back to cwd");

for (i = 1; i < argc; i++) {
const char *arg = argv[i];
Expand Down
214 changes: 124 additions & 90 deletions setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,115 @@ const char *read_gitfile_gently(const char *path)
return path;
}

static const char *setup_explicit_git_dir(const char *gitdirenv,
const char *work_tree_env, int *nongit_ok)
{
static char buffer[1024 + 1];
const char *retval;

if (PATH_MAX - 40 < strlen(gitdirenv))
die("'$%s' too big", GIT_DIR_ENVIRONMENT);
if (!is_git_directory(gitdirenv)) {
if (nongit_ok) {
*nongit_ok = 1;
return NULL;
}
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 (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 int cwd_contains_git_dir(const char **gitfile_dirp)
{
const char *gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
*gitfile_dirp = gitfile_dir;
if (gitfile_dir) {
if (set_git_dir(gitfile_dir))
die("Repository setup failed");
return 1;
}
return is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT);
}

static const char *setup_discovered_git_dir(const char *work_tree_env,
int offset, int len, char *cwd, int *nongit_ok)
{
int root_len;

inside_git_dir = 0;
if (!work_tree_env)
inside_work_tree = 1;
root_len = offset_1st_component(cwd);
git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
if (check_repository_format_gently(nongit_ok))
return NULL;
if (offset == len)
return NULL;

/* Make "offset" point to past the '/', and add a '/' at the end */
offset++;
cwd[len++] = '/';
cwd[len] = 0;
return cwd + offset;
}

static const char *setup_bare_git_dir(const char *work_tree_env,
int offset, int len, char *cwd, int *nongit_ok)
{
int root_len;

inside_git_dir = 1;
if (!work_tree_env)
inside_work_tree = 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
set_git_dir(".");
check_repository_format_gently(nongit_ok);
return NULL;
}

static const char *setup_nongit(const char *cwd, int *nongit_ok)
{
if (!nongit_ok)
die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
if (chdir(cwd))
die_errno("Cannot come back to cwd");
*nongit_ok = 1;
return NULL;
}

static dev_t get_device_or_die(const char *path, const char *prefix)
{
struct stat buf;
if (stat(path, &buf))
die_errno("failed to stat '%s%s%s'",
prefix ? prefix : "",
prefix ? "/" : "", path);
return buf.st_dev;
}

/*
* We cannot decide in this function whether we are in the work tree or
* not, since the config can only be read _after_ this function was called.
Expand All @@ -324,10 +433,9 @@ const char *setup_git_directory_gently(int *nongit_ok)
static char cwd[PATH_MAX+1];
const char *gitdirenv;
const char *gitfile_dir;
int len, offset, ceil_offset, root_len;
int len, offset, ceil_offset;
dev_t current_device = 0;
int one_filesystem = 1;
struct stat buf;

/*
* Let's assume that we are in a git repository.
Expand All @@ -343,38 +451,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
* validation.
*/
gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
if (gitdirenv) {
if (PATH_MAX - 40 < strlen(gitdirenv))
die("'$%s' too big", GIT_DIR_ENVIRONMENT);
if (is_git_directory(gitdirenv)) {
static char buffer[1024 + 1];
const char *retval;

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 (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;
}
if (nongit_ok) {
*nongit_ok = 1;
return NULL;
}
die("Not a git repository: '%s'", gitdirenv);
}
if (gitdirenv)
return setup_explicit_git_dir(gitdirenv, work_tree_env, nongit_ok);

if (!getcwd(cwd, sizeof(cwd)-1))
die_errno("Unable to read current working directory");
Expand All @@ -396,49 +474,21 @@ const char *setup_git_directory_gently(int *nongit_ok)
*/
offset = len = strlen(cwd);
one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
if (one_filesystem) {
if (stat(".", &buf))
die_errno("failed to stat '.'");
current_device = buf.st_dev;
}
if (one_filesystem)
current_device = get_device_or_die(".", NULL);
for (;;) {
gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
if (gitfile_dir) {
if (set_git_dir(gitfile_dir))
die("Repository setup failed");
break;
}
if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
break;
if (is_git_directory(".")) {
inside_git_dir = 1;
if (!work_tree_env)
inside_work_tree = 0;
if (offset != len) {
root_len = offset_1st_component(cwd);
cwd[offset > root_len ? offset : root_len] = '\0';
set_git_dir(cwd);
} else
set_git_dir(".");
check_repository_format_gently(nongit_ok);
return NULL;
}
if (cwd_contains_git_dir(&gitfile_dir))
return setup_discovered_git_dir(work_tree_env, offset,
len, cwd, nongit_ok);
if (is_git_directory("."))
return setup_bare_git_dir(work_tree_env, offset,
len, cwd, nongit_ok);
while (--offset > ceil_offset && cwd[offset] != '/');
if (offset <= ceil_offset) {
if (nongit_ok) {
if (chdir(cwd))
die_errno("Cannot come back to cwd");
*nongit_ok = 1;
return NULL;
}
die("Not a git repository (or any of the parent directories): %s", DEFAULT_GIT_DIR_ENVIRONMENT);
}
if (offset <= ceil_offset)
return setup_nongit(cwd, nongit_ok);
if (one_filesystem) {
if (stat("..", &buf)) {
cwd[offset] = '\0';
die_errno("failed to stat '%s/..'", cwd);
}
if (buf.st_dev != current_device) {
dev_t parent_device = get_device_or_die("..", cwd);
if (parent_device != current_device) {
if (nongit_ok) {
if (chdir(cwd))
die_errno("Cannot come back to cwd");
Expand All @@ -455,22 +505,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
die_errno("Cannot change to '%s/..'", cwd);
}
}

inside_git_dir = 0;
if (!work_tree_env)
inside_work_tree = 1;
root_len = offset_1st_component(cwd);
git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
if (check_repository_format_gently(nongit_ok))
return NULL;
if (offset == len)
return NULL;

/* Make "offset" point to past the '/', and add a '/' at the end */
offset++;
cwd[len++] = '/';
cwd[len] = 0;
return cwd + offset;
}

int git_config_perm(const char *var, const char *value)
Expand Down
Loading

0 comments on commit 693fefe

Please sign in to comment.