summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--builtin/grep.c3
-rw-r--r--builtin/ls-files.c192
-rw-r--r--builtin/submodule--helper.c9
-rw-r--r--cache.h62
-rw-r--r--config.c216
-rw-r--r--config.h24
-rw-r--r--environment.c91
-rw-r--r--git.c2
-rw-r--r--path.c130
-rw-r--r--path.h82
-rw-r--r--repository.c242
-rw-r--r--repository.h97
-rw-r--r--setup.c33
-rw-r--r--submodule-config.c70
-rw-r--r--submodule-config.h10
-rw-r--r--submodule.c35
-rw-r--r--submodule.h4
-rwxr-xr-xt/t3007-ls-files-recurse-submodules.sh39
-rw-r--r--worktree.c3
20 files changed, 972 insertions, 373 deletions
diff --git a/Makefile b/Makefile
index b94cd5633c..9c9c42f8f9 100644
--- a/Makefile
+++ b/Makefile
@@ -840,6 +840,7 @@ LIB_OBJS += refs/ref-cache.o
LIB_OBJS += ref-filter.o
LIB_OBJS += remote.o
LIB_OBJS += replace_object.o
+LIB_OBJS += repository.o
LIB_OBJS += rerere.o
LIB_OBJS += resolve-undo.o
LIB_OBJS += revision.o
diff --git a/builtin/grep.c b/builtin/grep.c
index f752f642ff..fa351c49f4 100644
--- a/builtin/grep.c
+++ b/builtin/grep.c
@@ -4,6 +4,7 @@
* Copyright (c) 2006 Junio C Hamano
*/
#include "cache.h"
+#include "repository.h"
#include "config.h"
#include "blob.h"
#include "tree.h"
@@ -643,7 +644,7 @@ static int grep_submodule_launch(struct grep_opt *opt,
static int grep_submodule(struct grep_opt *opt, const struct object_id *oid,
const char *filename, const char *path)
{
- if (!is_submodule_initialized(path))
+ if (!is_submodule_active(the_repository, path))
return 0;
if (!is_submodule_populated_gently(path, NULL)) {
/*
diff --git a/builtin/ls-files.c b/builtin/ls-files.c
index b12d0bb612..b8514a0029 100644
--- a/builtin/ls-files.c
+++ b/builtin/ls-files.c
@@ -5,7 +5,9 @@
*
* Copyright (C) Linus Torvalds, 2005
*/
+#define NO_THE_INDEX_COMPATIBILITY_MACROS
#include "cache.h"
+#include "repository.h"
#include "config.h"
#include "quote.h"
#include "dir.h"
@@ -32,10 +34,8 @@ static int line_terminator = '\n';
static int debug_mode;
static int show_eol;
static int recurse_submodules;
-static struct argv_array submodule_options = ARGV_ARRAY_INIT;
static const char *prefix;
-static const char *super_prefix;
static int max_prefix_len;
static int prefix_len;
static struct pathspec pathspec;
@@ -74,24 +74,11 @@ static void write_eolinfo(const struct index_state *istate,
static void write_name(const char *name)
{
/*
- * Prepend the super_prefix to name to construct the full_name to be
- * written.
- */
- struct strbuf full_name = STRBUF_INIT;
- if (super_prefix) {
- strbuf_addstr(&full_name, super_prefix);
- strbuf_addstr(&full_name, name);
- name = full_name.buf;
- }
-
- /*
* With "--full-name", prefix_len=0; this caller needs to pass
* an empty string in that case (a NULL is good for "").
*/
write_name_quoted_relative(name, prefix_len ? prefix : NULL,
stdout, line_terminator);
-
- strbuf_release(&full_name);
}
static const char *get_tag(const struct cache_entry *ce, const char *tag)
@@ -210,83 +197,38 @@ static void show_killed_files(const struct index_state *istate,
}
}
-/*
- * Compile an argv_array with all of the options supported by --recurse_submodules
- */
-static void compile_submodule_options(const char **argv,
- const struct dir_struct *dir,
- int show_tag)
-{
- if (line_terminator == '\0')
- argv_array_push(&submodule_options, "-z");
- if (show_tag)
- argv_array_push(&submodule_options, "-t");
- if (show_valid_bit)
- argv_array_push(&submodule_options, "-v");
- if (show_cached)
- argv_array_push(&submodule_options, "--cached");
- if (show_eol)
- argv_array_push(&submodule_options, "--eol");
- if (debug_mode)
- argv_array_push(&submodule_options, "--debug");
-
- /* Add Pathspecs */
- argv_array_push(&submodule_options, "--");
- for (; *argv; argv++)
- argv_array_push(&submodule_options, *argv);
-}
+static void show_files(struct repository *repo, struct dir_struct *dir);
-/**
- * Recursively call ls-files on a submodule
- */
-static void show_gitlink(const struct cache_entry *ce)
+static void show_submodule(struct repository *superproject,
+ struct dir_struct *dir, const char *path)
{
- struct child_process cp = CHILD_PROCESS_INIT;
- int status;
- char *dir;
-
- prepare_submodule_repo_env(&cp.env_array);
- argv_array_push(&cp.env_array, GIT_DIR_ENVIRONMENT);
-
- if (prefix_len)
- argv_array_pushf(&cp.env_array, "%s=%s",
- GIT_TOPLEVEL_PREFIX_ENVIRONMENT,
- prefix);
- argv_array_pushf(&cp.args, "--super-prefix=%s%s/",
- super_prefix ? super_prefix : "",
- ce->name);
- argv_array_push(&cp.args, "ls-files");
- argv_array_push(&cp.args, "--recurse-submodules");
-
- /* add supported options */
- argv_array_pushv(&cp.args, submodule_options.argv);
-
- cp.git_cmd = 1;
- dir = mkpathdup("%s/%s", get_git_work_tree(), ce->name);
- cp.dir = dir;
- status = run_command(&cp);
- free(dir);
- if (status)
- exit(status);
+ struct repository submodule;
+
+ if (repo_submodule_init(&submodule, superproject, path))
+ return;
+
+ if (repo_read_index(&submodule) < 0)
+ die("index file corrupt");
+
+ repo_read_gitmodules(&submodule);
+
+ show_files(&submodule, dir);
+
+ repo_clear(&submodule);
}
-static void show_ce_entry(const struct index_state *istate,
- const char *tag, const struct cache_entry *ce)
+static void show_ce(struct repository *repo, struct dir_struct *dir,
+ const struct cache_entry *ce, const char *fullname,
+ const char *tag)
{
- struct strbuf name = STRBUF_INIT;
- int len = max_prefix_len;
- if (super_prefix)
- strbuf_addstr(&name, super_prefix);
- strbuf_addstr(&name, ce->name);
-
- if (len > ce_namelen(ce))
+ if (max_prefix_len > strlen(fullname))
die("git ls-files: internal error - cache entry not superset of prefix");
if (recurse_submodules && S_ISGITLINK(ce->ce_mode) &&
- submodule_path_match(&pathspec, name.buf, ps_matched)) {
- show_gitlink(ce);
- } else if (match_pathspec(&pathspec, name.buf, name.len,
- len, ps_matched,
+ is_submodule_active(repo, ce->name)) {
+ show_submodule(repo, dir, ce->name);
+ } else if (match_pathspec(&pathspec, fullname, strlen(fullname),
+ max_prefix_len, ps_matched,
S_ISDIR(ce->ce_mode) ||
S_ISGITLINK(ce->ce_mode))) {
tag = get_tag(ce, tag);
@@ -300,12 +242,10 @@ static void show_ce_entry(const struct index_state *istate,
find_unique_abbrev(ce->oid.hash, abbrev),
ce_stage(ce));
}
- write_eolinfo(istate, ce, ce->name);
- write_name(ce->name);
+ write_eolinfo(repo->index, ce, fullname);
+ write_name(fullname);
print_debug(ce);
}
-
- strbuf_release(&name);
}
static void show_ru_info(const struct index_state *istate)
@@ -338,59 +278,79 @@ static void show_ru_info(const struct index_state *istate)
}
static int ce_excluded(struct dir_struct *dir, struct index_state *istate,
- const struct cache_entry *ce)
+ const char *fullname, const struct cache_entry *ce)
{
int dtype = ce_to_dtype(ce);
- return is_excluded(dir, istate, ce->name, &dtype);
+ return is_excluded(dir, istate, fullname, &dtype);
+}
+
+static void construct_fullname(struct strbuf *out, const struct repository *repo,
+ const struct cache_entry *ce)
+{
+ strbuf_reset(out);
+ if (repo->submodule_prefix)
+ strbuf_addstr(out, repo->submodule_prefix);
+ strbuf_addstr(out, ce->name);
}
-static void show_files(struct index_state *istate, struct dir_struct *dir)
+static void show_files(struct repository *repo, struct dir_struct *dir)
{
int i;
+ struct strbuf fullname = STRBUF_INIT;
/* For cached/deleted files we don't need to even do the readdir */
if (show_others || show_killed) {
if (!show_others)
dir->flags |= DIR_COLLECT_KILLED_ONLY;
- fill_directory(dir, istate, &pathspec);
+ fill_directory(dir, repo->index, &pathspec);
if (show_others)
- show_other_files(istate, dir);
+ show_other_files(repo->index, dir);
if (show_killed)
- show_killed_files(istate, dir);
+ show_killed_files(repo->index, dir);
}
if (show_cached || show_stage) {
- for (i = 0; i < istate->cache_nr; i++) {
- const struct cache_entry *ce = istate->cache[i];
+ for (i = 0; i < repo->index->cache_nr; i++) {
+ const struct cache_entry *ce = repo->index->cache[i];
+
+ construct_fullname(&fullname, repo, ce);
+
if ((dir->flags & DIR_SHOW_IGNORED) &&
- !ce_excluded(dir, istate, ce))
+ !ce_excluded(dir, repo->index, fullname.buf, ce))
continue;
if (show_unmerged && !ce_stage(ce))
continue;
if (ce->ce_flags & CE_UPDATE)
continue;
- show_ce_entry(istate, ce_stage(ce) ? tag_unmerged :
- (ce_skip_worktree(ce) ? tag_skip_worktree : tag_cached), ce);
+ show_ce(repo, dir, ce, fullname.buf,
+ ce_stage(ce) ? tag_unmerged :
+ (ce_skip_worktree(ce) ? tag_skip_worktree :
+ tag_cached));
}
}
if (show_deleted || show_modified) {
- for (i = 0; i < istate->cache_nr; i++) {
- const struct cache_entry *ce = istate->cache[i];
+ for (i = 0; i < repo->index->cache_nr; i++) {
+ const struct cache_entry *ce = repo->index->cache[i];
struct stat st;
int err;
+
+ construct_fullname(&fullname, repo, ce);
+
if ((dir->flags & DIR_SHOW_IGNORED) &&
- !ce_excluded(dir, istate, ce))
+ !ce_excluded(dir, repo->index, fullname.buf, ce))
continue;
if (ce->ce_flags & CE_UPDATE)
continue;
if (ce_skip_worktree(ce))
continue;
- err = lstat(ce->name, &st);
+ err = lstat(fullname.buf, &st);
if (show_deleted && err)
- show_ce_entry(istate, tag_removed, ce);
- if (show_modified && ie_modified(istate, ce, &st, 0))
- show_ce_entry(istate, tag_modified, ce);
+ show_ce(repo, dir, ce, fullname.buf, tag_removed);
+ if (show_modified && ie_modified(repo->index, ce, &st, 0))
+ show_ce(repo, dir, ce, fullname.buf, tag_modified);
}
}
+
+ strbuf_release(&fullname);
}
/*
@@ -615,10 +575,9 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
prefix = cmd_prefix;
if (prefix)
prefix_len = strlen(prefix);
- super_prefix = get_super_prefix();
git_config(git_default_config, NULL);
- if (read_cache() < 0)
+ if (repo_read_index(the_repository) < 0)
die("index file corrupt");
argc = parse_options(argc, argv, prefix, builtin_ls_files_options,
@@ -652,7 +611,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
setup_work_tree();
if (recurse_submodules)
- compile_submodule_options(argv, &dir, show_tag);
+ repo_read_gitmodules(the_repository);
if (recurse_submodules &&
(show_stage || show_deleted || show_others || show_unmerged ||
@@ -670,7 +629,10 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
/*
* Find common prefix for all pathspec's
* This is used as a performance optimization which unfortunately cannot
- * be done when recursing into submodules
+ * be done when recursing into submodules because when a pathspec is
+ * given which spans repository boundaries you can't simply remove the
+ * submodule entry because the pathspec may match something inside the
+ * submodule.
*/
if (recurse_submodules)
max_prefix = NULL;
@@ -678,7 +640,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
max_prefix = common_prefix(&pathspec);
max_prefix_len = get_common_prefix_len(max_prefix);
- prune_index(&the_index, max_prefix, max_prefix_len);
+ prune_index(the_repository->index, max_prefix, max_prefix_len);
/* Treat unmatching pathspec elements as errors */
if (pathspec.nr && error_unmatch)
@@ -699,11 +661,13 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
*/
if (show_stage || show_unmerged)
die("ls-files --with-tree is incompatible with -s or -u");
- overlay_tree_on_index(&the_index, with_tree, max_prefix);
+ overlay_tree_on_index(the_repository->index, with_tree, max_prefix);
}
- show_files(&the_index, &dir);
+
+ show_files(the_repository, &dir);
+
if (show_resolve_undo)
- show_ru_info(&the_index);
+ show_ru_info(the_repository->index);
if (ps_matched) {
int bad;
diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c
index 8517032b3e..e1b06c41d8 100644
--- a/builtin/submodule--helper.c
+++ b/builtin/submodule--helper.c
@@ -1,4 +1,5 @@
#include "builtin.h"
+#include "repository.h"
#include "cache.h"
#include "config.h"
#include "parse-options.h"
@@ -280,7 +281,7 @@ static void module_list_active(struct module_list *list)
for (i = 0; i < list->nr; i++) {
const struct cache_entry *ce = list->entries[i];
- if (!is_submodule_initialized(ce->name))
+ if (!is_submodule_active(the_repository, ce->name))
continue;
ALLOC_GROW(active_modules.entries,
@@ -362,7 +363,7 @@ static void init_submodule(const char *path, const char *prefix, int quiet)
*
* Set active flag for the submodule being initialized
*/
- if (!is_submodule_initialized(path)) {
+ if (!is_submodule_active(the_repository, path)) {
strbuf_reset(&sb);
strbuf_addf(&sb, "submodule.%s.active", sub->name);
git_config_set_gently(sb.buf, "true");
@@ -817,7 +818,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
}
/* Check if the submodule has been initialized. */
- if (!is_submodule_initialized(ce->name)) {
+ if (!is_submodule_active(the_repository, ce->name)) {
next_submodule_warn_missing(suc, out, displaypath);
goto cleanup;
}
@@ -1193,7 +1194,7 @@ static int is_active(int argc, const char **argv, const char *prefix)
gitmodules_config();
- return !is_submodule_initialized(argv[1]);
+ return !is_submodule_active(the_repository, argv[1]);
}
#define SUPPORT_SUPER_PREFIX (1<<0)
diff --git a/cache.h b/cache.h
index 96055c2229..c958fc3ce5 100644
--- a/cache.h
+++ b/cache.h
@@ -11,6 +11,7 @@
#include "string-list.h"
#include "pack-revindex.h"
#include "hash.h"
+#include "path.h"
#ifndef platform_SHA_CTX
/*
@@ -462,6 +463,8 @@ static inline enum object_type object_type(unsigned int mode)
*/
extern const char * const local_repo_env[];
+extern void setup_git_env(void);
+
/*
* Returns true iff we have a configured git repository (either via
* setup_git_directory, or in the environment via $GIT_DIR).
@@ -769,7 +772,6 @@ extern int core_apply_sparse_checkout;
extern int precomposed_unicode;
extern int protect_hfs;
extern int protect_ntfs;
-extern int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
/*
* Include broken refs in all ref iterations, which will
@@ -892,64 +894,6 @@ extern void check_repository_format(void);
#define TYPE_CHANGED 0x0040
/*
- * Return a statically allocated filename, either generically (mkpath), in
- * the repository directory (git_path), or in a submodule's repository
- * directory (git_path_submodule). In all cases, note that the result
- * may be overwritten by another call to _any_ of the functions. Consider
- * using the safer "dup" or "strbuf" formats below (in some cases, the
- * unsafe versions have already been removed).
- */
-extern const char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
-extern const char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
-extern const char *git_common_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
-
-extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
- __attribute__((format (printf, 3, 4)));
-extern void strbuf_git_path(struct strbuf *sb, const char *fmt, ...)
- __attribute__((format (printf, 2, 3)));
-extern void strbuf_git_common_path(struct strbuf *sb, const char *fmt, ...)
- __attribute__((format (printf, 2, 3)));
-extern char *git_path_buf(struct strbuf *buf, const char *fmt, ...)
- __attribute__((format (printf, 2, 3)));
-extern int strbuf_git_path_submodule(struct strbuf *sb, const char *path,
- const char *fmt, ...)
- __attribute__((format (printf, 3, 4)));
-extern char *git_pathdup(const char *fmt, ...)
- __attribute__((format (printf, 1, 2)));
-extern char *mkpathdup(const char *fmt, ...)
- __attribute__((format (printf, 1, 2)));
-extern char *git_pathdup_submodule(const char *path, const char *fmt, ...)
- __attribute__((format (printf, 2, 3)));
-
-extern void report_linked_checkout_garbage(void);
-
-/*
- * You can define a static memoized git path like:
- *
- * static GIT_PATH_FUNC(git_path_foo, "FOO");
- *
- * or use one of the global ones below.
- */
-#define GIT_PATH_FUNC(func, filename) \
- const char *func(void) \
- { \
- static char *ret; \
- if (!ret) \
- ret = git_pathdup(filename); \
- return ret; \
- }
-
-const char *git_path_cherry_pick_head(void);
-const char *git_path_revert_head(void);
-const char *git_path_squash_msg(void);
-const char *git_path_merge_msg(void);
-const char *git_path_merge_rr(void);
-const char *git_path_merge_mode(void);
-const char *git_path_merge_head(void);
-const char *git_path_fetch_head(void);
-const char *git_path_shallow(void);
-
-/*
* Return the name of the file in the local object database that would
* be used to store a loose object with the specified sha1. The
* return value is a pointer to a statically allocated buffer that is
diff --git a/config.c b/config.c
index 1cd40a5fe6..4638b0696a 100644
--- a/config.c
+++ b/config.c
@@ -7,6 +7,7 @@
*/
#include "cache.h"
#include "config.h"
+#include "repository.h"
#include "lockfile.h"
#include "exec_cmd.h"
#include "strbuf.h"
@@ -72,13 +73,6 @@ static int core_compression_seen;
static int pack_compression_seen;
static int zlib_compression_seen;
-/*
- * Default config_set that contains key-value pairs from the usual set of config
- * config files (i.e repo specific .git/config, user wide ~/.gitconfig, XDG
- * config file and the global /etc/gitconfig)
- */
-static struct config_set the_config_set;
-
static int config_file_fgetc(struct config_source *conf)
{
return getc_unlocked(conf->u.file);
@@ -1604,31 +1598,6 @@ int config_with_options(config_fn_t fn, void *data,
return do_git_config_sequence(opts, fn, data);
}
-static void git_config_raw(config_fn_t fn, void *data)
-{
- struct config_options opts = {0};
-
- opts.respect_includes = 1;
- if (have_git_dir()) {
- opts.commondir = get_git_common_dir();
- opts.git_dir = get_git_dir();
- }
-
- if (config_with_options(fn, data, NULL, &opts) < 0)
- /*
- * config_with_options() normally returns only
- * zero, as most errors are fatal, and
- * non-fatal potential errors are guarded by "if"
- * statements that are entered only when no error is
- * possible.
- *
- * If we ever encounter a non-fatal error, it means
- * something went really wrong and we should stop
- * immediately.
- */
- die(_("unknown error occurred while reading the configuration files"));
-}
-
static void configset_iter(struct config_set *cs, config_fn_t fn, void *data)
{
int i, value_index;
@@ -1682,14 +1651,6 @@ void read_early_config(config_fn_t cb, void *data)
strbuf_release(&gitdir);
}
-static void git_config_check_init(void);
-
-void git_config(config_fn_t fn, void *data)
-{
- git_config_check_init();
- configset_iter(&the_config_set, fn, data);
-}
-
static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
{
struct config_set_element k;
@@ -1899,87 +1860,194 @@ int git_configset_get_pathname(struct config_set *cs, const char *key, const cha
return 1;
}
-static void git_config_check_init(void)
+/* Functions use to read configuration from a repository */
+static void repo_read_config(struct repository *repo)
{
- if (the_config_set.hash_initialized)
+ struct config_options opts;
+
+ opts.respect_includes = 1;
+ opts.commondir = repo->commondir;
+ opts.git_dir = repo->gitdir;
+
+ if (!repo->config)
+ repo->config = xcalloc(1, sizeof(struct config_set));
+ else
+ git_configset_clear(repo->config);
+
+ git_configset_init(repo->config);
+
+ if (config_with_options(config_set_callback, repo->config, NULL, &opts) < 0)
+ /*
+ * config_with_options() normally returns only
+ * zero, as most errors are fatal, and
+ * non-fatal potential errors are guarded by "if"
+ * statements that are entered only when no error is
+ * possible.
+ *
+ * If we ever encounter a non-fatal error, it means
+ * something went really wrong and we should stop
+ * immediately.
+ */
+ die(_("unknown error occurred while reading the configuration files"));
+}
+
+static void git_config_check_init(struct repository *repo)
+{
+ if (repo->config && repo->config->hash_initialized)
return;
- git_configset_init(&the_config_set);
- git_config_raw(config_set_callback, &the_config_set);
+ repo_read_config(repo);
}
-void git_config_clear(void)
+static void repo_config_clear(struct repository *repo)
{
- if (!the_config_set.hash_initialized)
+ if (!repo->config || !repo->config->hash_initialized)
return;
- git_configset_clear(&the_config_set);
+ git_configset_clear(repo->config);
}
-int git_config_get_value(const char *key, const char **value)
+void repo_config(struct repository *repo, config_fn_t fn, void *data)
{
- git_config_check_init();
- return git_configset_get_value(&the_config_set, key, value);
+ git_config_check_init(repo);
+ configset_iter(repo->config, fn, data);
}
-const struct string_list *git_config_get_value_multi(const char *key)
+int repo_config_get_value(struct repository *repo,
+ const char *key, const char **value)
{
- git_config_check_init();
- return git_configset_get_value_multi(&the_config_set, key);
+ git_config_check_init(repo);
+ return git_configset_get_value(repo->config, key, value);
}
-int git_config_get_string_const(const char *key, const char **dest)
+const struct string_list *repo_config_get_value_multi(struct repository *repo,
+ const char *key)
+{
+ git_config_check_init(repo);
+ return git_configset_get_value_multi(repo->config, key);
+}
+
+int repo_config_get_string_const(struct repository *repo,
+ const char *key, const char **dest)
+{
+ int ret;
+ git_config_check_init(repo);
+ ret = git_configset_get_string_const(repo->config, key, dest);
+ if (ret < 0)
+ git_die_config(key, NULL);
+ return ret;
+}
+
+int repo_config_get_string(struct repository *repo,
+ const char *key, char **dest)
+{
+ git_config_check_init(repo);
+ return repo_config_get_string_const(repo, key, (const char **)dest);
+}
+
+int repo_config_get_int(struct repository *repo,
+ const char *key, int *dest)
+{
+ git_config_check_init(repo);
+ return git_configset_get_int(repo->config, key, dest);
+}
+
+int repo_config_get_ulong(struct repository *repo,
+ const char *key, unsigned long *dest)
+{
+ git_config_check_init(repo);
+ return git_configset_get_ulong(repo->config, key, dest);
+}
+
+int repo_config_get_bool(struct repository *repo,
+ const char *key, int *dest)
+{
+ git_config_check_init(repo);
+ return git_configset_get_bool(repo->config, key, dest);
+}
+
+int repo_config_get_bool_or_int(struct repository *repo,
+ const char *key, int *is_bool, int *dest)
+{
+ git_config_check_init(repo);
+ return git_configset_get_bool_or_int(repo->config, key, is_bool, dest);
+}
+
+int repo_config_get_maybe_bool(struct repository *repo,
+ const char *key, int *dest)
+{
+ git_config_check_init(repo);
+ return git_configset_get_maybe_bool(repo->config, key, dest);
+}
+
+int repo_config_get_pathname(struct repository *repo,
+ const char *key, const char **dest)
{
int ret;
- git_config_check_init();
- ret = git_configset_get_string_const(&the_config_set, key, dest);
+ git_config_check_init(repo);
+ ret = git_configset_get_pathname(repo->config, key, dest);
if (ret < 0)
git_die_config(key, NULL);
return ret;
}
+/* Functions used historically to read configuration from 'the_repository' */
+void git_config(config_fn_t fn, void *data)
+{
+ repo_config(the_repository, fn, data);
+}
+
+void git_config_clear(void)
+{
+ repo_config_clear(the_repository);
+}
+
+int git_config_get_value(const char *key, const char **value)
+{
+ return repo_config_get_value(the_repository, key, value);
+}
+
+const struct string_list *git_config_get_value_multi(const char *key)
+{
+ return repo_config_get_value_multi(the_repository, key);
+}
+
+int git_config_get_string_const(const char *key, const char **dest)
+{
+ return repo_config_get_string_const(the_repository, key, dest);
+}
+
int git_config_get_string(const char *key, char **dest)
{
- git_config_check_init();
- return git_config_get_string_const(key, (const char **)dest);
+ return repo_config_get_string(the_repository, key, dest);
}
int git_config_get_int(const char *key, int *dest)
{
- git_config_check_init();
- return git_configset_get_int(&the_config_set, key, dest);
+ return repo_config_get_int(the_repository, key, dest);
}
int git_config_get_ulong(const char *key, unsigned long *dest)
{
- git_config_check_init();
- return git_configset_get_ulong(&the_config_set, key, dest);
+ return repo_config_get_ulong(the_repository, key, dest);
}
int git_config_get_bool(const char *key, int *dest)
{
- git_config_check_init();
- return git_configset_get_bool(&the_config_set, key, dest);
+ return repo_config_get_bool(the_repository, key, dest);
}
int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
{
- git_config_check_init();
- return git_configset_get_bool_or_int(&the_config_set, key, is_bool, dest);
+ return repo_config_get_bool_or_int(the_repository, key, is_bool, dest);
}
int git_config_get_maybe_bool(const char *key, int *dest)
{
- git_config_check_init();
- return git_configset_get_maybe_bool(&the_config_set, key, dest);
+ return repo_config_get_maybe_bool(the_repository, key, dest);
}
int git_config_get_pathname(const char *key, const char **dest)
{
- int ret;
- git_config_check_init();
- ret = git_configset_get_pathname(&the_config_set, key, dest);
- if (ret < 0)
- git_die_config(key, NULL);
- return ret;
+ return repo_config_get_pathname(the_repository, key, dest);
}
int git_config_get_expiry(const char *key, const char **output)
diff --git a/config.h b/config.h
index 9e038cce25..0352da117b 100644
--- a/config.h
+++ b/config.h
@@ -163,6 +163,30 @@ extern int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
extern int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest);
extern int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest);
+/* Functions for reading a repository's config */
+struct repository;
+extern void repo_config(struct repository *repo, config_fn_t fn, void *data);
+extern int repo_config_get_value(struct repository *repo,
+ const char *key, const char **value);
+extern const struct string_list *repo_config_get_value_multi(struct repository *repo,
+ const char *key);
+extern int repo_config_get_string_const(struct repository *repo,
+ const char *key, const char **dest);
+extern int repo_config_get_string(struct repository *repo,
+ const char *key, char **dest);
+extern int repo_config_get_int(struct repository *repo,
+ const char *key, int *dest);
+extern int repo_config_get_ulong(struct repository *repo,
+ const char *key, unsigned long *dest);
+extern int repo_config_get_bool(struct repository *repo,
+ const char *key, int *dest);
+extern int repo_config_get_bool_or_int(struct repository *repo,
+ const char *key, int *is_bool, int *dest);
+extern int repo_config_get_maybe_bool(struct repository *repo,
+ const char *key, int *dest);
+extern int repo_config_get_pathname(struct repository *repo,
+ const char *key, const char **dest);
+
extern int git_config_get_value(const char *key, const char **value);
extern const struct string_list *git_config_get_value_multi(const char *key);
extern void git_config_clear(void);
diff --git a/environment.c b/environment.c
index d40b21fb72..3fd4b10845 100644
--- a/environment.c
+++ b/environment.c
@@ -8,6 +8,7 @@
* are.
*/
#include "cache.h"
+#include "repository.h"
#include "config.h"
#include "refs.h"
#include "fmt-merge-msg.h"
@@ -95,17 +96,11 @@ int ignore_untracked_cache_config;
/* This is set by setup_git_dir_gently() and/or git_default_config() */
char *git_work_tree_cfg;
-static char *work_tree;
static const char *namespace;
-static size_t namespace_len;
static const char *super_prefix;
-static const char *git_dir, *git_common_dir;
-static char *git_object_dir, *git_index_file, *git_graft_file;
-int git_db_env, git_index_env, git_graft_env, git_common_dir_env;
-
/*
* Repository-local GIT_* environment variables; see cache.h for details.
*/
@@ -149,48 +144,17 @@ static char *expand_namespace(const char *raw_namespace)
return strbuf_detach(&buf, NULL);
}
-static char *git_path_from_env(const char *envvar, const char *git_dir,
- const char *path, int *fromenv)
+void setup_git_env(void)
{
- const char *value = getenv(envvar);
- if (!value)
- return xstrfmt("%s/%s", git_dir, path);
- if (fromenv)
- *fromenv = 1;
- return xstrdup(value);
-}
-
-static void setup_git_env(void)
-{
- struct strbuf sb = STRBUF_INIT;
- const char *gitfile;
const char *shallow_file;
const char *replace_ref_base;
- git_dir = getenv(GIT_DIR_ENVIRONMENT);
- if (!git_dir) {
- if (!startup_info->have_repository)
- BUG("setup_git_env called without repository");
- git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
- }
- gitfile = read_gitfile(git_dir);
- git_dir = xstrdup(gitfile ? gitfile : git_dir);
- if (get_common_dir(&sb, git_dir))
- git_common_dir_env = 1;
- git_common_dir = strbuf_detach(&sb, NULL);
- git_object_dir = git_path_from_env(DB_ENVIRONMENT, git_common_dir,
- "objects", &git_db_env);
- git_index_file = git_path_from_env(INDEX_ENVIRONMENT, git_dir,
- "index", &git_index_env);
- git_graft_file = git_path_from_env(GRAFT_ENVIRONMENT, git_common_dir,
- "info/grafts", &git_graft_env);
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
check_replace_refs = 0;
replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
: "refs/replace/");
namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
- namespace_len = strlen(namespace);
shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
if (shallow_file)
set_alternate_shallow_file(shallow_file, 0);
@@ -205,36 +169,36 @@ int is_bare_repository(void)
int have_git_dir(void)
{
return startup_info->have_repository
- || git_dir
- || getenv(GIT_DIR_ENVIRONMENT);
+ || the_repository->gitdir;
}
const char *get_git_dir(void)
{
- if (!git_dir)
- setup_git_env();
- return git_dir;
+ if (!the_repository->gitdir)
+ BUG("git environment hasn't been setup");
+ return the_repository->gitdir;
}
const char *get_git_common_dir(void)
{
- if (!git_dir)
- setup_git_env();
- return git_common_dir;
+ if (!the_repository->commondir)
+ BUG("git environment hasn't been setup");
+ return the_repository->commondir;
}
const char *get_git_namespace(void)
{
if (!namespace)
- setup_git_env();
+ BUG("git environment ha