Blame environment.c

Packit 4511e4
/*
Packit 4511e4
 * We put all the git config variables in this same object
Packit 4511e4
 * file, so that programs can link against the config parser
Packit 4511e4
 * without having to link against all the rest of git.
Packit 4511e4
 *
Packit 4511e4
 * In particular, no need to bring in libz etc unless needed,
Packit 4511e4
 * even if you might want to know where the git directory etc
Packit 4511e4
 * are.
Packit 4511e4
 */
Packit 4511e4
#include "cache.h"
Packit Service b5fd21
#include "branch.h"
Packit 4511e4
#include "repository.h"
Packit 4511e4
#include "config.h"
Packit 4511e4
#include "refs.h"
Packit 4511e4
#include "fmt-merge-msg.h"
Packit 4511e4
#include "commit.h"
Packit 4511e4
#include "argv-array.h"
Packit 4511e4
#include "object-store.h"
Packit 4511e4
#include "chdir-notify.h"
Packit Service b5fd21
#include "shallow.h"
Packit 4511e4
Packit 4511e4
int trust_executable_bit = 1;
Packit 4511e4
int trust_ctime = 1;
Packit 4511e4
int check_stat = 1;
Packit 4511e4
int has_symlinks = 1;
Packit 4511e4
int minimum_abbrev = 4, default_abbrev = -1;
Packit 4511e4
int ignore_case;
Packit 4511e4
int assume_unchanged;
Packit 4511e4
int prefer_symlink_refs;
Packit 4511e4
int is_bare_repository_cfg = -1; /* unspecified */
Packit 4511e4
int warn_ambiguous_refs = 1;
Packit 4511e4
int warn_on_object_refname_ambiguity = 1;
Packit 4511e4
int ref_paranoia = -1;
Packit 4511e4
int repository_format_precious_objects;
Packit Service b5fd21
int repository_format_worktree_config;
Packit 4511e4
const char *git_commit_encoding;
Packit 4511e4
const char *git_log_output_encoding;
Packit 4511e4
const char *apply_default_whitespace;
Packit 4511e4
const char *apply_default_ignorewhitespace;
Packit 4511e4
const char *git_attributes_file;
Packit 4511e4
const char *git_hooks_path;
Packit 4511e4
int zlib_compression_level = Z_BEST_SPEED;
Packit 4511e4
int core_compression_level;
Packit 4511e4
int pack_compression_level = Z_DEFAULT_COMPRESSION;
Packit 4511e4
int fsync_object_files;
Packit 4511e4
size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
Packit 4511e4
size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
Packit 4511e4
size_t delta_base_cache_limit = 96 * 1024 * 1024;
Packit 4511e4
unsigned long big_file_threshold = 512 * 1024 * 1024;
Packit 4511e4
int pager_use_color = 1;
Packit 4511e4
const char *editor_program;
Packit 4511e4
const char *askpass_program;
Packit 4511e4
const char *excludes_file;
Packit 4511e4
enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
Packit Service b5fd21
int read_replace_refs = 1;
Packit 4511e4
char *git_replace_ref_base;
Packit 4511e4
enum eol core_eol = EOL_UNSET;
Packit 4511e4
int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
Packit 4511e4
char *check_roundtrip_encoding = "SHIFT-JIS";
Packit 4511e4
unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
Packit 4511e4
enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
Packit 4511e4
enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
Packit 4511e4
enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
Packit 4511e4
#ifndef OBJECT_CREATION_MODE
Packit 4511e4
#define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
Packit 4511e4
#endif
Packit 4511e4
enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
Packit 4511e4
char *notes_ref_name;
Packit 4511e4
int grafts_replace_parents = 1;
Packit 4511e4
int core_apply_sparse_checkout;
Packit Service b5fd21
int core_sparse_checkout_cone;
Packit 4511e4
int merge_log_config = -1;
Packit 4511e4
int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
Packit 4511e4
unsigned long pack_size_limit_cfg;
Packit 4511e4
enum log_refs_config log_all_ref_updates = LOG_REFS_UNSET;
Packit 4511e4
Packit 4511e4
#ifndef PROTECT_HFS_DEFAULT
Packit 4511e4
#define PROTECT_HFS_DEFAULT 0
Packit 4511e4
#endif
Packit 4511e4
int protect_hfs = PROTECT_HFS_DEFAULT;
Packit 4511e4
Packit 4511e4
#ifndef PROTECT_NTFS_DEFAULT
Packit 4511e4
#define PROTECT_NTFS_DEFAULT 1
Packit 4511e4
#endif
Packit 4511e4
int protect_ntfs = PROTECT_NTFS_DEFAULT;
Packit 4511e4
const char *core_fsmonitor;
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * The character that begins a commented line in user-editable file
Packit 4511e4
 * that is subject to stripspace.
Packit 4511e4
 */
Packit 4511e4
char comment_line_char = '#';
Packit 4511e4
int auto_comment_line_char;
Packit 4511e4
Packit 4511e4
/* Parallel index stat data preload? */
Packit 4511e4
int core_preload_index = 1;
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * This is a hack for test programs like test-dump-untracked-cache to
Packit 4511e4
 * ensure that they do not modify the untracked cache when reading it.
Packit 4511e4
 * Do not use it otherwise!
Packit 4511e4
 */
Packit 4511e4
int ignore_untracked_cache_config;
Packit 4511e4
Packit 4511e4
/* This is set by setup_git_dir_gently() and/or git_default_config() */
Packit 4511e4
char *git_work_tree_cfg;
Packit 4511e4
Packit 4511e4
static char *git_namespace;
Packit 4511e4
Packit Service b5fd21
static char *super_prefix;
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Repository-local GIT_* environment variables; see cache.h for details.
Packit 4511e4
 */
Packit 4511e4
const char * const local_repo_env[] = {
Packit 4511e4
	ALTERNATE_DB_ENVIRONMENT,
Packit 4511e4
	CONFIG_ENVIRONMENT,
Packit 4511e4
	CONFIG_DATA_ENVIRONMENT,
Packit 4511e4
	DB_ENVIRONMENT,
Packit 4511e4
	GIT_DIR_ENVIRONMENT,
Packit 4511e4
	GIT_WORK_TREE_ENVIRONMENT,
Packit 4511e4
	GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
Packit 4511e4
	GRAFT_ENVIRONMENT,
Packit 4511e4
	INDEX_ENVIRONMENT,
Packit 4511e4
	NO_REPLACE_OBJECTS_ENVIRONMENT,
Packit 4511e4
	GIT_REPLACE_REF_BASE_ENVIRONMENT,
Packit 4511e4
	GIT_PREFIX_ENVIRONMENT,
Packit 4511e4
	GIT_SUPER_PREFIX_ENVIRONMENT,
Packit 4511e4
	GIT_SHALLOW_FILE_ENVIRONMENT,
Packit 4511e4
	GIT_COMMON_DIR_ENVIRONMENT,
Packit 4511e4
	NULL
Packit 4511e4
};
Packit 4511e4
Packit 4511e4
static char *expand_namespace(const char *raw_namespace)
Packit 4511e4
{
Packit 4511e4
	struct strbuf buf = STRBUF_INIT;
Packit 4511e4
	struct strbuf **components, **c;
Packit 4511e4
Packit 4511e4
	if (!raw_namespace || !*raw_namespace)
Packit 4511e4
		return xstrdup("");
Packit 4511e4
Packit 4511e4
	strbuf_addstr(&buf, raw_namespace);
Packit 4511e4
	components = strbuf_split(&buf, '/');
Packit 4511e4
	strbuf_reset(&buf;;
Packit 4511e4
	for (c = components; *c; c++)
Packit 4511e4
		if (strcmp((*c)->buf, "/") != 0)
Packit 4511e4
			strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
Packit 4511e4
	strbuf_list_free(components);
Packit 4511e4
	if (check_refname_format(buf.buf, 0))
Packit Service b5fd21
		die(_("bad git namespace path \"%s\""), raw_namespace);
Packit 4511e4
	strbuf_addch(&buf, '/');
Packit 4511e4
	return strbuf_detach(&buf, NULL);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Wrapper of getenv() that returns a strdup value. This value is kept
Packit 4511e4
 * in argv to be freed later.
Packit 4511e4
 */
Packit 4511e4
static const char *getenv_safe(struct argv_array *argv, const char *name)
Packit 4511e4
{
Packit 4511e4
	const char *value = getenv(name);
Packit 4511e4
Packit 4511e4
	if (!value)
Packit 4511e4
		return NULL;
Packit 4511e4
Packit 4511e4
	argv_array_push(argv, value);
Packit 4511e4
	return argv->argv[argv->argc - 1];
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void setup_git_env(const char *git_dir)
Packit 4511e4
{
Packit 4511e4
	const char *shallow_file;
Packit 4511e4
	const char *replace_ref_base;
Packit 4511e4
	struct set_gitdir_args args = { NULL };
Packit 4511e4
	struct argv_array to_free = ARGV_ARRAY_INIT;
Packit 4511e4
Packit 4511e4
	args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT);
Packit 4511e4
	args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT);
Packit 4511e4
	args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT);
Packit 4511e4
	args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT);
Packit 4511e4
	args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT);
Packit 4511e4
	repo_set_gitdir(the_repository, git_dir, &args);
Packit 4511e4
	argv_array_clear(&to_free);
Packit 4511e4
Packit 4511e4
	if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
Packit Service b5fd21
		read_replace_refs = 0;
Packit 4511e4
	replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
Packit 4511e4
	free(git_replace_ref_base);
Packit 4511e4
	git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
Packit 4511e4
							  : "refs/replace/");
Packit 4511e4
	free(git_namespace);
Packit 4511e4
	git_namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
Packit 4511e4
	shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
Packit 4511e4
	if (shallow_file)
Packit Service b5fd21
		set_alternate_shallow_file(the_repository, shallow_file, 0);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int is_bare_repository(void)
Packit 4511e4
{
Packit 4511e4
	/* if core.bare is not 'false', let's see if there is a work tree */
Packit 4511e4
	return is_bare_repository_cfg && !get_git_work_tree();
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int have_git_dir(void)
Packit 4511e4
{
Packit 4511e4
	return startup_info->have_repository
Packit 4511e4
		|| the_repository->gitdir;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
const char *get_git_dir(void)
Packit 4511e4
{
Packit 4511e4
	if (!the_repository->gitdir)
Packit 4511e4
		BUG("git environment hasn't been setup");
Packit 4511e4
	return the_repository->gitdir;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
const char *get_git_common_dir(void)
Packit 4511e4
{
Packit 4511e4
	if (!the_repository->commondir)
Packit 4511e4
		BUG("git environment hasn't been setup");
Packit 4511e4
	return the_repository->commondir;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
const char *get_git_namespace(void)
Packit 4511e4
{
Packit 4511e4
	if (!git_namespace)
Packit 4511e4
		BUG("git environment hasn't been setup");
Packit 4511e4
	return git_namespace;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
const char *strip_namespace(const char *namespaced_ref)
Packit 4511e4
{
Packit 4511e4
	const char *out;
Packit 4511e4
	if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
Packit 4511e4
		return out;
Packit 4511e4
	return NULL;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
const char *get_super_prefix(void)
Packit 4511e4
{
Packit 4511e4
	static int initialized;
Packit 4511e4
	if (!initialized) {
Packit Service b5fd21
		super_prefix = xstrdup_or_null(getenv(GIT_SUPER_PREFIX_ENVIRONMENT));
Packit 4511e4
		initialized = 1;
Packit 4511e4
	}
Packit 4511e4
	return super_prefix;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int git_work_tree_initialized;
Packit 4511e4
Packit 4511e4
/*
Packit 4511e4
 * Note.  This works only before you used a work tree.  This was added
Packit 4511e4
 * primarily to support git-clone to work in a new repository it just
Packit 4511e4
 * created, and is not meant to flip between different work trees.
Packit 4511e4
 */
Packit 4511e4
void set_git_work_tree(const char *new_work_tree)
Packit 4511e4
{
Packit Service b5fd21
	struct strbuf realpath = STRBUF_INIT;
Packit Service b5fd21
Packit 4511e4
	if (git_work_tree_initialized) {
Packit Service b5fd21
		strbuf_realpath(&realpath, new_work_tree, 1);
Packit Service b5fd21
		new_work_tree = realpath.buf;
Packit 4511e4
		if (strcmp(new_work_tree, the_repository->worktree))
Packit 4511e4
			die("internal error: work tree has already been set\n"
Packit 4511e4
			    "Current worktree: %s\nNew worktree: %s",
Packit 4511e4
			    the_repository->worktree, new_work_tree);
Packit 4511e4
		return;
Packit 4511e4
	}
Packit 4511e4
	git_work_tree_initialized = 1;
Packit 4511e4
	repo_set_worktree(the_repository, new_work_tree);
Packit Service b5fd21
Packit Service b5fd21
	strbuf_release(&realpath);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
const char *get_git_work_tree(void)
Packit 4511e4
{
Packit 4511e4
	return the_repository->worktree;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
char *get_object_directory(void)
Packit 4511e4
{
Packit Service b5fd21
	if (!the_repository->objects->odb)
Packit 4511e4
		BUG("git environment hasn't been setup");
Packit Service b5fd21
	return the_repository->objects->odb->path;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
Packit 4511e4
{
Packit 4511e4
	int fd;
Packit 4511e4
	/*
Packit 4511e4
	 * we let the umask do its job, don't try to be more
Packit 4511e4
	 * restrictive except to remove write permission.
Packit 4511e4
	 */
Packit 4511e4
	int mode = 0444;
Packit 4511e4
	git_path_buf(temp_filename, "objects/%s", pattern);
Packit 4511e4
	fd = git_mkstemp_mode(temp_filename->buf, mode);
Packit 4511e4
	if (0 <= fd)
Packit 4511e4
		return fd;
Packit 4511e4
Packit 4511e4
	/* slow path */
Packit 4511e4
	/* some mkstemp implementations erase temp_filename on failure */
Packit 4511e4
	git_path_buf(temp_filename, "objects/%s", pattern);
Packit 4511e4
	safe_create_leading_directories(temp_filename->buf);
Packit 4511e4
	return xmkstemp_mode(temp_filename->buf, mode);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int odb_pack_keep(const char *name)
Packit 4511e4
{
Packit 4511e4
	int fd;
Packit 4511e4
Packit 4511e4
	fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
Packit 4511e4
	if (0 <= fd)
Packit 4511e4
		return fd;
Packit 4511e4
Packit 4511e4
	/* slow path */
Packit 4511e4
	safe_create_leading_directories_const(name);
Packit 4511e4
	return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
char *get_index_file(void)
Packit 4511e4
{
Packit 4511e4
	if (!the_repository->index_file)
Packit 4511e4
		BUG("git environment hasn't been setup");
Packit 4511e4
	return the_repository->index_file;
Packit 4511e4
}
Packit 4511e4
Packit Service b5fd21
char *get_graft_file(struct repository *r)
Packit 4511e4
{
Packit Service b5fd21
	if (!r->graft_file)
Packit 4511e4
		BUG("git environment hasn't been setup");
Packit Service b5fd21
	return r->graft_file;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void set_git_dir_1(const char *path)
Packit 4511e4
{
Packit 4511e4
	if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
Packit Service b5fd21
		die(_("could not set GIT_DIR to '%s'"), path);
Packit 4511e4
	setup_git_env(path);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static void update_relative_gitdir(const char *name,
Packit 4511e4
				   const char *old_cwd,
Packit 4511e4
				   const char *new_cwd,
Packit 4511e4
				   void *data)
Packit 4511e4
{
Packit 4511e4
	char *path = reparent_relative_path(old_cwd, new_cwd, get_git_dir());
Packit 4511e4
	trace_printf_key(&trace_setup_key,
Packit 4511e4
			 "setup: move $GIT_DIR to '%s'",
Packit 4511e4
			 path);
Packit 4511e4
	set_git_dir_1(path);
Packit 4511e4
	free(path);
Packit 4511e4
}
Packit 4511e4
Packit Service b5fd21
void set_git_dir(const char *path, int make_realpath)
Packit 4511e4
{
Packit Service b5fd21
	struct strbuf realpath = STRBUF_INIT;
Packit Service b5fd21
Packit Service b5fd21
	if (make_realpath) {
Packit Service b5fd21
		strbuf_realpath(&realpath, path, 1);
Packit Service b5fd21
		path = realpath.buf;
Packit Service b5fd21
	}
Packit Service b5fd21
Packit 4511e4
	set_git_dir_1(path);
Packit 4511e4
	if (!is_absolute_path(path))
Packit 4511e4
		chdir_notify_register(NULL, update_relative_gitdir, NULL);
Packit Service b5fd21
Packit Service b5fd21
	strbuf_release(&realpath);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
const char *get_log_output_encoding(void)
Packit 4511e4
{
Packit 4511e4
	return git_log_output_encoding ? git_log_output_encoding
Packit 4511e4
		: get_commit_output_encoding();
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
const char *get_commit_output_encoding(void)
Packit 4511e4
{
Packit 4511e4
	return git_commit_encoding ? git_commit_encoding : "UTF-8";
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
static int the_shared_repository = PERM_UMASK;
Packit 4511e4
static int need_shared_repository_from_config = 1;
Packit 4511e4
Packit 4511e4
void set_shared_repository(int value)
Packit 4511e4
{
Packit 4511e4
	the_shared_repository = value;
Packit 4511e4
	need_shared_repository_from_config = 0;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int get_shared_repository(void)
Packit 4511e4
{
Packit 4511e4
	if (need_shared_repository_from_config) {
Packit 4511e4
		const char *var = "core.sharedrepository";
Packit 4511e4
		const char *value;
Packit 4511e4
		if (!git_config_get_value(var, &value))
Packit 4511e4
			the_shared_repository = git_config_perm(var, value);
Packit 4511e4
		need_shared_repository_from_config = 0;
Packit 4511e4
	}
Packit 4511e4
	return the_shared_repository;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
void reset_shared_repository(void)
Packit 4511e4
{
Packit 4511e4
	need_shared_repository_from_config = 1;
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int use_optional_locks(void)
Packit 4511e4
{
Packit 4511e4
	return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
Packit 4511e4
}
Packit 4511e4
Packit 4511e4
int print_sha1_ellipsis(void)
Packit 4511e4
{
Packit 4511e4
	/*
Packit 4511e4
	 * Determine if the calling environment contains the variable
Packit 4511e4
	 * GIT_PRINT_SHA1_ELLIPSIS set to "yes".
Packit 4511e4
	 */
Packit 4511e4
	static int cached_result = -1; /* unknown */
Packit 4511e4
Packit 4511e4
	if (cached_result < 0) {
Packit 4511e4
		const char *v = getenv("GIT_PRINT_SHA1_ELLIPSIS");
Packit 4511e4
		cached_result = (v && !strcasecmp(v, "yes"));
Packit 4511e4
	}
Packit 4511e4
	return cached_result;
Packit 4511e4
}