blob: 2f27008424a074c451a5037e40e241a9e8fccfa9 [file] [log] [blame]
Linus Torvaldse1b10392005-10-11 18:47:34 -07001/*
2 * We put all the git config variables in this same object
3 * file, so that programs can link against the config parser
4 * without having to link against all the rest of git.
5 *
6 * In particular, no need to bring in libz etc unless needed,
7 * even if you might want to know where the git directory etc
8 * are.
9 */
10#include "cache.h"
Elijah Newrene730b812018-08-15 10:54:07 -070011#include "branch.h"
Patrick Steinhardtb9d147f2021-01-12 13:27:10 +010012#include "environment.h"
Brandon Williamsc14c2342017-06-22 11:43:33 -070013#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070014#include "config.h"
Josh Tripletta1bea2c2011-07-05 10:54:44 -070015#include "refs.h"
Ramsay Jones273c7032011-10-09 18:33:34 +010016#include "fmt-merge-msg.h"
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +070017#include "commit.h"
Jeff Kingdbbcd442020-07-28 16:23:39 -040018#include "strvec.h"
Stefan Beller90c62152018-03-23 18:20:55 +010019#include "object-store.h"
Jeff King8500e0d2018-03-30 14:35:08 -040020#include "chdir-notify.h"
Taylor Blau120ad2b2020-04-30 13:48:50 -060021#include "shallow.h"
Linus Torvaldse1b10392005-10-11 18:47:34 -070022
Linus Torvaldse1b10392005-10-11 18:47:34 -070023int trust_executable_bit = 1;
Alex Riesen1ce47902008-07-28 08:31:28 +020024int trust_ctime = 1;
Robin Rosenbergc08e4d52013-01-22 08:49:22 +010025int check_stat = 1;
Johannes Sixt78a8d642007-03-02 22:11:30 +010026int has_symlinks = 1;
Linus Torvaldse6c587c2016-09-30 17:19:37 -070027int minimum_abbrev = 4, default_abbrev = -1;
Linus Torvalds0a9b88b2008-03-21 16:52:46 -070028int ignore_case;
David Rientjes96f1e582006-08-15 10:23:48 -070029int assume_unchanged;
30int prefer_symlink_refs;
Junio C Hamano7d1864c2007-01-07 02:00:28 -080031int is_bare_repository_cfg = -1; /* unspecified */
Junio C Hamano1b371f52006-03-23 23:42:40 -080032int warn_ambiguous_refs = 1;
Jeff King25fba782013-07-12 02:20:05 -040033int warn_on_object_refname_ambiguity = 1;
Jeff King49672f22015-03-20 14:43:06 -040034int ref_paranoia = -1;
Jeff King067fbd42015-06-23 06:54:11 -040035int repository_format_precious_objects;
Nguyễn Thái Ngọc Duy58b284a2018-10-21 16:02:28 +020036int repository_format_worktree_config;
Shawn O. Pearce1a8f2742007-03-12 15:33:18 -040037const char *git_commit_encoding;
Shawn O. Pearce3a556022007-03-06 20:44:17 -050038const char *git_log_output_encoding;
Jeff King9a532192020-08-17 17:33:11 -040039char *apply_default_whitespace;
40char *apply_default_ignorewhitespace;
Junio C Hamano64589a02011-10-06 13:22:24 -050041const char *git_attributes_file;
Ævar Arnfjörð Bjarmason867ad082016-05-04 22:58:12 +000042const char *git_hooks_path;
Dana How960ccca2007-05-09 13:56:50 -070043int zlib_compression_level = Z_BEST_SPEED;
44int core_compression_level;
Junio C Hamano8de7eeb2016-11-15 17:42:40 -080045int pack_compression_level = Z_DEFAULT_COMPRESSION;
Linus Torvaldsaafe9fb2008-06-18 15:18:44 -070046int fsync_object_files;
Shawn O. Pearce8c825342006-12-24 00:46:13 -050047size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
48size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
David Kastrup4874f542014-05-04 19:13:57 +020049size_t delta_base_cache_limit = 96 * 1024 * 1024;
Junio C Hamano15366282011-04-05 10:44:11 -070050unsigned long big_file_threshold = 512 * 1024 * 1024;
Matthias Lederhoferaa086eb2006-07-30 00:27:43 +020051int pager_use_color = 1;
Christian Couderee9601e2008-02-16 06:01:41 +010052const char *editor_program;
Anselm Kruisd3e7da82010-08-30 15:38:38 +020053const char *askpass_program;
Christian Couderdfb068b2008-02-16 06:01:59 +010054const char *excludes_file;
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +020055enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
Jeff King6ebd1ca2018-07-18 16:45:20 -040056int read_replace_refs = 1;
Mike Hommey58d121b2015-06-12 06:34:59 +090057char *git_replace_ref_base;
Junio C Hamanoec70f522011-05-09 12:52:12 -070058enum eol core_eol = EOL_UNSET;
Torsten Bögershausen8462ff42018-01-13 23:49:31 +010059int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
Lars Schneidere92d6222018-04-15 20:16:10 +020060char *check_roundtrip_encoding = "SHIFT-JIS";
Junio C Hamanocf1b7862007-12-06 00:14:14 -080061unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
Jay Soffian9ed36cf2008-02-19 11:24:37 -050062enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
Dustin Sallingsc998ae92008-05-10 15:36:29 -070063enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
Christopher Tiwaldf25950f2012-03-20 00:31:33 -040064enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
Johannes Schindelin348df162009-04-28 00:32:25 +020065#ifndef OBJECT_CREATION_MODE
66#define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +020067#endif
Johannes Schindelin348df162009-04-28 00:32:25 +020068enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
Johannes Schindelina97a7462009-10-09 12:21:57 +020069char *notes_ref_name;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +020070int grafts_replace_parents = 1;
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +070071int core_apply_sparse_checkout;
Derrick Stolee879321e2019-11-21 22:04:40 +000072int core_sparse_checkout_cone;
Junio C Hamano898eacd2011-10-06 23:12:09 -070073int merge_log_config = -1;
Torsten Bögershausen76759c72012-07-08 15:50:25 +020074int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
Junio C Hamano568508e2011-10-28 14:48:40 -070075unsigned long pack_size_limit_cfg;
Cornelius Weig341fb282017-01-27 11:09:47 +010076enum log_refs_config log_all_ref_updates = LOG_REFS_UNSET;
Linus Torvaldse1b10392005-10-11 18:47:34 -070077
Jeff Kinga42643a2014-12-15 18:15:20 -050078#ifndef PROTECT_HFS_DEFAULT
79#define PROTECT_HFS_DEFAULT 0
80#endif
81int protect_hfs = PROTECT_HFS_DEFAULT;
82
Johannes Schindelin2b4c6ef2014-12-16 23:46:59 +010083#ifndef PROTECT_NTFS_DEFAULT
Johannes Schindelin9102f952019-09-09 21:04:41 +020084#define PROTECT_NTFS_DEFAULT 1
Johannes Schindelin2b4c6ef2014-12-16 23:46:59 +010085#endif
86int protect_ntfs = PROTECT_NTFS_DEFAULT;
Ben Peart883e2482017-09-22 12:35:40 -040087const char *core_fsmonitor;
Johannes Schindelin2b4c6ef2014-12-16 23:46:59 +010088
Junio C Hamanoeff80a92013-01-16 20:18:48 +010089/*
90 * The character that begins a commented line in user-editable file
91 * that is subject to stripspace.
92 */
93char comment_line_char = '#';
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +070094int auto_comment_line_char;
Junio C Hamanoeff80a92013-01-16 20:18:48 +010095
Linus Torvalds671c9b72008-11-13 16:36:30 -080096/* Parallel index stat data preload? */
Steve Hoelzer299e2982014-06-02 11:43:00 -050097int core_preload_index = 1;
Linus Torvalds671c9b72008-11-13 16:36:30 -080098
Christian Couderdae6c322016-01-27 07:58:06 +010099/*
100 * This is a hack for test programs like test-dump-untracked-cache to
101 * ensure that they do not modify the untracked cache when reading it.
102 * Do not use it otherwise!
103 */
104int ignore_untracked_cache_config;
105
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100106/* This is set by setup_git_dir_gently() and/or git_default_config() */
107char *git_work_tree_cfg;
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100108
Brandon Williams38f3f092018-02-14 10:59:55 -0800109static char *git_namespace;
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700110
Jeff King8aac6902019-01-11 17:15:00 -0500111static char *super_prefix;
Brandon Williams74866d72016-10-07 11:18:48 -0700112
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100113/*
Jeff King2163e5d2013-03-08 04:29:08 -0500114 * Repository-local GIT_* environment variables; see cache.h for details.
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100115 */
Jeff King2163e5d2013-03-08 04:29:08 -0500116const char * const local_repo_env[] = {
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100117 ALTERNATE_DB_ENVIRONMENT,
118 CONFIG_ENVIRONMENT,
Jonathan Nieder655e8d92010-08-24 01:41:14 -0500119 CONFIG_DATA_ENVIRONMENT,
Patrick Steinhardtd8d77152021-01-12 13:27:14 +0100120 CONFIG_COUNT_ENVIRONMENT,
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100121 DB_ENVIRONMENT,
122 GIT_DIR_ENVIRONMENT,
123 GIT_WORK_TREE_ENVIRONMENT,
Jeff King2cd83d12013-03-08 04:32:22 -0500124 GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100125 GRAFT_ENVIRONMENT,
126 INDEX_ENVIRONMENT,
127 NO_REPLACE_OBJECTS_ENVIRONMENT,
Mike Hommey58d121b2015-06-12 06:34:59 +0900128 GIT_REPLACE_REF_BASE_ENVIRONMENT,
Jeff Kinga6f7f9a2013-03-08 04:30:25 -0500129 GIT_PREFIX_ENVIRONMENT,
Brandon Williams74866d72016-10-07 11:18:48 -0700130 GIT_SUPER_PREFIX_ENVIRONMENT,
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +0700131 GIT_SHALLOW_FILE_ENVIRONMENT,
Nguyễn Thái Ngọc Duyc7b3a3d2014-11-30 15:24:36 +0700132 GIT_COMMON_DIR_ENVIRONMENT,
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100133 NULL
134};
135
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700136static char *expand_namespace(const char *raw_namespace)
137{
138 struct strbuf buf = STRBUF_INIT;
139 struct strbuf **components, **c;
140
141 if (!raw_namespace || !*raw_namespace)
142 return xstrdup("");
143
144 strbuf_addstr(&buf, raw_namespace);
145 components = strbuf_split(&buf, '/');
146 strbuf_reset(&buf);
147 for (c = components; *c; c++)
148 if (strcmp((*c)->buf, "/") != 0)
149 strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
150 strbuf_list_free(components);
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200151 if (check_refname_format(buf.buf, 0))
Nguyễn Thái Ngọc Duycbb46ca2018-07-21 09:49:31 +0200152 die(_("bad git namespace path \"%s\""), raw_namespace);
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700153 strbuf_addch(&buf, '/');
154 return strbuf_detach(&buf, NULL);
155}
156
Patrick Steinhardtb9d147f2021-01-12 13:27:10 +0100157const char *getenv_safe(struct strvec *argv, const char *name)
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700158{
159 const char *value = getenv(name);
160
161 if (!value)
162 return NULL;
163
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400164 strvec_push(argv, value);
Jeff Kingd70a9eb2020-07-28 20:37:20 -0400165 return argv->v[argv->nr - 1];
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700166}
167
168void setup_git_env(const char *git_dir)
Linus Torvaldse1b10392005-10-11 18:47:34 -0700169{
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +0700170 const char *shallow_file;
Mike Hommey58d121b2015-06-12 06:34:59 +0900171 const char *replace_ref_base;
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700172 struct set_gitdir_args args = { NULL };
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400173 struct strvec to_free = STRVEC_INIT;
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700174
175 args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT);
176 args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT);
177 args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT);
178 args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT);
Nguyễn Thái Ngọc Duy7bc0dca2018-03-03 18:35:57 +0700179 args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT);
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700180 repo_set_gitdir(the_repository, git_dir, &args);
Jeff Kingef8d7ac2020-07-28 16:24:53 -0400181 strvec_clear(&to_free);
Nguyễn Thái Ngọc Duy487a2b72013-08-31 08:04:14 +0700182
Christian Couder6476b382009-11-18 07:50:58 +0100183 if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
Jeff King6ebd1ca2018-07-18 16:45:20 -0400184 read_replace_refs = 0;
Mike Hommey58d121b2015-06-12 06:34:59 +0900185 replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
Jeff Kingf9b75732017-09-05 09:04:57 -0400186 free(git_replace_ref_base);
Mike Hommey58d121b2015-06-12 06:34:59 +0900187 git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
188 : "refs/replace/");
Brandon Williams38f3f092018-02-14 10:59:55 -0800189 free(git_namespace);
190 git_namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +0700191 shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
192 if (shallow_file)
Stefan Beller6a2df512018-05-17 15:51:43 -0700193 set_alternate_shallow_file(the_repository, shallow_file, 0);
Shawn O. Pearce0bee5912006-12-14 17:41:17 -0500194}
195
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800196int is_bare_repository(void)
Shawn O. Pearce0bee5912006-12-14 17:41:17 -0500197{
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100198 /* if core.bare is not 'false', let's see if there is a work tree */
199 return is_bare_repository_cfg && !get_git_work_tree();
Linus Torvaldse1b10392005-10-11 18:47:34 -0700200}
201
Jeff Kingb9605bc2016-09-12 20:24:15 -0700202int have_git_dir(void)
203{
204 return startup_info->have_repository
Brandon Williamsc14c2342017-06-22 11:43:33 -0700205 || the_repository->gitdir;
Jeff Kingb9605bc2016-09-12 20:24:15 -0700206}
207
Pierre Habouzitc5fba162006-08-23 12:39:11 +0200208const char *get_git_dir(void)
Linus Torvaldse1b10392005-10-11 18:47:34 -0700209{
Brandon Williamsc14c2342017-06-22 11:43:33 -0700210 if (!the_repository->gitdir)
Brandon Williams73f192c2017-06-20 12:19:32 -0700211 BUG("git environment hasn't been setup");
Brandon Williamsc14c2342017-06-22 11:43:33 -0700212 return the_repository->gitdir;
Linus Torvaldse1b10392005-10-11 18:47:34 -0700213}
214
Nguyễn Thái Ngọc Duyc7b3a3d2014-11-30 15:24:36 +0700215const char *get_git_common_dir(void)
216{
Brandon Williamsc14c2342017-06-22 11:43:33 -0700217 if (!the_repository->commondir)
Brandon Williams73f192c2017-06-20 12:19:32 -0700218 BUG("git environment hasn't been setup");
Brandon Williamsc14c2342017-06-22 11:43:33 -0700219 return the_repository->commondir;
Nguyễn Thái Ngọc Duyc7b3a3d2014-11-30 15:24:36 +0700220}
221
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700222const char *get_git_namespace(void)
223{
Brandon Williams38f3f092018-02-14 10:59:55 -0800224 if (!git_namespace)
Brandon Williams73f192c2017-06-20 12:19:32 -0700225 BUG("git environment hasn't been setup");
Brandon Williams38f3f092018-02-14 10:59:55 -0800226 return git_namespace;
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700227}
228
229const char *strip_namespace(const char *namespaced_ref)
230{
Brandon Williamsbf08c8c2017-06-20 12:19:34 -0700231 const char *out;
232 if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
233 return out;
234 return NULL;
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700235}
236
Brandon Williams74866d72016-10-07 11:18:48 -0700237const char *get_super_prefix(void)
238{
239 static int initialized;
240 if (!initialized) {
Jeff King8aac6902019-01-11 17:15:00 -0500241 super_prefix = xstrdup_or_null(getenv(GIT_SUPER_PREFIX_ENVIRONMENT));
Brandon Williams74866d72016-10-07 11:18:48 -0700242 initialized = 1;
243 }
244 return super_prefix;
245}
246
Daniel Barkalow19757d82008-04-27 13:39:21 -0400247static int git_work_tree_initialized;
248
249/*
250 * Note. This works only before you used a work tree. This was added
251 * primarily to support git-clone to work in a new repository it just
252 * created, and is not meant to flip between different work trees.
253 */
254void set_git_work_tree(const char *new_work_tree)
255{
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000256 struct strbuf realpath = STRBUF_INIT;
257
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700258 if (git_work_tree_initialized) {
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000259 strbuf_realpath(&realpath, new_work_tree, 1);
260 new_work_tree = realpath.buf;
Brandon Williamsb4158732017-06-22 11:43:34 -0700261 if (strcmp(new_work_tree, the_repository->worktree))
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700262 die("internal error: work tree has already been set\n"
263 "Current worktree: %s\nNew worktree: %s",
Brandon Williamsb4158732017-06-22 11:43:34 -0700264 the_repository->worktree, new_work_tree);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700265 return;
266 }
Daniel Barkalow19757d82008-04-27 13:39:21 -0400267 git_work_tree_initialized = 1;
Brandon Williamsb4158732017-06-22 11:43:34 -0700268 repo_set_worktree(the_repository, new_work_tree);
Alexandr Miloslavskiy3d7747e2020-03-10 13:11:22 +0000269
270 strbuf_release(&realpath);
Daniel Barkalow19757d82008-04-27 13:39:21 -0400271}
272
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100273const char *get_git_work_tree(void)
274{
Brandon Williamsb4158732017-06-22 11:43:34 -0700275 return the_repository->worktree;
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100276}
277
Linus Torvaldse1b10392005-10-11 18:47:34 -0700278char *get_object_directory(void)
279{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500280 if (!the_repository->objects->odb)
Brandon Williams73f192c2017-06-20 12:19:32 -0700281 BUG("git environment hasn't been setup");
Jeff Kingf0eaf632018-11-12 09:50:39 -0500282 return the_repository->objects->odb->path;
Linus Torvaldse1b10392005-10-11 18:47:34 -0700283}
284
Brandon Williamsa63b5fc2018-02-14 10:59:53 -0800285int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500286{
287 int fd;
288 /*
289 * we let the umask do its job, don't try to be more
290 * restrictive except to remove write permission.
291 */
292 int mode = 0444;
Brandon Williamsa63b5fc2018-02-14 10:59:53 -0800293 git_path_buf(temp_filename, "objects/%s", pattern);
294 fd = git_mkstemp_mode(temp_filename->buf, mode);
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500295 if (0 <= fd)
296 return fd;
297
298 /* slow path */
Brandon Williamsa63b5fc2018-02-14 10:59:53 -0800299 /* some mkstemp implementations erase temp_filename on failure */
300 git_path_buf(temp_filename, "objects/%s", pattern);
301 safe_create_leading_directories(temp_filename->buf);
302 return xmkstemp_mode(temp_filename->buf, mode);
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500303}
304
Jeff Kingeaeefc32017-03-16 10:27:12 -0400305int odb_pack_keep(const char *name)
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500306{
307 int fd;
308
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500309 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
310 if (0 <= fd)
311 return fd;
312
313 /* slow path */
Jeff Kingeaeefc32017-03-16 10:27:12 -0400314 safe_create_leading_directories_const(name);
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500315 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
316}
317
Linus Torvaldse1b10392005-10-11 18:47:34 -0700318char *get_index_file(void)
319{
Brandon Williamsc14c2342017-06-22 11:43:33 -0700320 if (!the_repository->index_file)
Brandon Williams73f192c2017-06-20 12:19:32 -0700321 BUG("git environment hasn't been setup");
Brandon Williamsc14c2342017-06-22 11:43:33 -0700322 return the_repository->index_file;
Linus Torvaldse1b10392005-10-11 18:47:34 -0700323}
324
Stefan Beller0437a2e2018-05-17 15:51:50 -0700325char *get_graft_file(struct repository *r)
Linus Torvaldse1b10392005-10-11 18:47:34 -0700326{
Stefan Beller0437a2e2018-05-17 15:51:50 -0700327 if (!r->graft_file)
Brandon Williams73f192c2017-06-20 12:19:32 -0700328 BUG("git environment hasn't been setup");
Stefan Beller0437a2e2018-05-17 15:51:50 -0700329 return r->graft_file;
Linus Torvaldse1b10392005-10-11 18:47:34 -0700330}
Johannes Schindelind7ac12b2007-08-01 01:29:38 +0100331
Jeff King8500e0d2018-03-30 14:35:08 -0400332static void set_git_dir_1(const char *path)
Johannes Schindelind7ac12b2007-08-01 01:29:38 +0100333{
334 if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
Nguyễn Thái Ngọc Duycbb46ca2018-07-21 09:49:31 +0200335 die(_("could not set GIT_DIR to '%s'"), path);
Nguyễn Thái Ngọc Duy357a03e2018-03-03 18:35:55 +0700336 setup_git_env(path);
Johannes Schindelind7ac12b2007-08-01 01:29:38 +0100337}
Pat Notza6fa5992010-11-02 13:59:07 -0600338
Jeff King8500e0d2018-03-30 14:35:08 -0400339static void update_relative_gitdir(const char *name,
340 const char *old_cwd,
341 const char *new_cwd,
342 void *data)
343{
344 char *path = reparent_relative_path(old_cwd, new_cwd, get_git_dir());
345 trace_printf_key(&trace_setup_key,
346 "setup: move $GIT_DIR to '%s'",
347 path);
348 set_git_dir_1(path);
349 free(path);
350}
351
Alexandr Miloslavskiy0915a5b2020-03-06 19:03:13 +0000352void set_git_dir(const char *path, int make_realpath)
Jeff King8500e0d2018-03-30 14:35:08 -0400353{
Alexandr Miloslavskiy0915a5b2020-03-06 19:03:13 +0000354 struct strbuf realpath = STRBUF_INIT;
355
356 if (make_realpath) {
357 strbuf_realpath(&realpath, path, 1);
358 path = realpath.buf;
359 }
360
Jeff King8500e0d2018-03-30 14:35:08 -0400361 set_git_dir_1(path);
362 if (!is_absolute_path(path))
363 chdir_notify_register(NULL, update_relative_gitdir, NULL);
Alexandr Miloslavskiy0915a5b2020-03-06 19:03:13 +0000364
365 strbuf_release(&realpath);
Linus Torvaldse1b10392005-10-11 18:47:34 -0700366}
Pat Notza6fa5992010-11-02 13:59:07 -0600367
368const char *get_log_output_encoding(void)
369{
370 return git_log_output_encoding ? git_log_output_encoding
371 : get_commit_output_encoding();
372}
373
374const char *get_commit_output_encoding(void)
375{
376 return git_commit_encoding ? git_commit_encoding : "UTF-8";
377}
Jeff King7875acb2016-03-11 17:36:49 -0500378
379static int the_shared_repository = PERM_UMASK;
Jeff Kingae5f6772016-03-11 17:36:53 -0500380static int need_shared_repository_from_config = 1;
Jeff King7875acb2016-03-11 17:36:49 -0500381
382void set_shared_repository(int value)
383{
384 the_shared_repository = value;
Jeff Kingae5f6772016-03-11 17:36:53 -0500385 need_shared_repository_from_config = 0;
Jeff King7875acb2016-03-11 17:36:49 -0500386}
387
388int get_shared_repository(void)
389{
Jeff Kingae5f6772016-03-11 17:36:53 -0500390 if (need_shared_repository_from_config) {
391 const char *var = "core.sharedrepository";
392 const char *value;
393 if (!git_config_get_value(var, &value))
394 the_shared_repository = git_config_perm(var, value);
395 need_shared_repository_from_config = 0;
396 }
Jeff King7875acb2016-03-11 17:36:49 -0500397 return the_shared_repository;
398}
Jeff King45439262016-09-12 20:24:23 -0700399
400void reset_shared_repository(void)
401{
402 need_shared_repository_from_config = 1;
403}
Jeff King27344d62017-09-27 02:54:30 -0400404
405int use_optional_locks(void)
406{
407 return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
408}
Ann T Ropeaa2cd7092017-12-03 22:27:39 +0100409
410int print_sha1_ellipsis(void)
411{
412 /*
413 * Determine if the calling environment contains the variable
414 * GIT_PRINT_SHA1_ELLIPSIS set to "yes".
415 */
416 static int cached_result = -1; /* unknown */
417
418 if (cached_result < 0) {
419 const char *v = getenv("GIT_PRINT_SHA1_ELLIPSIS");
420 cached_result = (v && !strcasecmp(v, "yes"));
421 }
422 return cached_result;
423}