blob: 3fd4b1084590d0118cad5e7ae32fb37e570e7523 [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"
Brandon Williamsc14c2342017-06-22 11:43:33 -070011#include "repository.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070012#include "config.h"
Josh Tripletta1bea2c2011-07-05 10:54:44 -070013#include "refs.h"
Ramsay Jones273c7032011-10-09 18:33:34 +010014#include "fmt-merge-msg.h"
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +070015#include "commit.h"
Linus Torvaldse1b10392005-10-11 18:47:34 -070016
Linus Torvaldse1b10392005-10-11 18:47:34 -070017int trust_executable_bit = 1;
Alex Riesen1ce47902008-07-28 08:31:28 +020018int trust_ctime = 1;
Robin Rosenbergc08e4d52013-01-22 08:49:22 +010019int check_stat = 1;
Johannes Sixt78a8d642007-03-02 22:11:30 +010020int has_symlinks = 1;
Linus Torvaldse6c587c2016-09-30 17:19:37 -070021int minimum_abbrev = 4, default_abbrev = -1;
Linus Torvalds0a9b88b2008-03-21 16:52:46 -070022int ignore_case;
David Rientjes96f1e582006-08-15 10:23:48 -070023int assume_unchanged;
24int prefer_symlink_refs;
Junio C Hamano7d1864c2007-01-07 02:00:28 -080025int is_bare_repository_cfg = -1; /* unspecified */
Junio C Hamano1b371f52006-03-23 23:42:40 -080026int warn_ambiguous_refs = 1;
Jeff King25fba782013-07-12 02:20:05 -040027int warn_on_object_refname_ambiguity = 1;
Jeff King49672f22015-03-20 14:43:06 -040028int ref_paranoia = -1;
Jeff King067fbd42015-06-23 06:54:11 -040029int repository_format_precious_objects;
Shawn O. Pearce1a8f2742007-03-12 15:33:18 -040030const char *git_commit_encoding;
Shawn O. Pearce3a556022007-03-06 20:44:17 -050031const char *git_log_output_encoding;
David Rientjes96f1e582006-08-15 10:23:48 -070032const char *apply_default_whitespace;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +020033const char *apply_default_ignorewhitespace;
Junio C Hamano64589a02011-10-06 13:22:24 -050034const char *git_attributes_file;
Ævar Arnfjörð Bjarmason867ad082016-05-04 22:58:12 +000035const char *git_hooks_path;
Dana How960ccca2007-05-09 13:56:50 -070036int zlib_compression_level = Z_BEST_SPEED;
37int core_compression_level;
Junio C Hamano8de7eeb2016-11-15 17:42:40 -080038int pack_compression_level = Z_DEFAULT_COMPRESSION;
Linus Torvaldsaafe9fb2008-06-18 15:18:44 -070039int fsync_object_files;
Shawn O. Pearce8c825342006-12-24 00:46:13 -050040size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
41size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
David Kastrup4874f542014-05-04 19:13:57 +020042size_t delta_base_cache_limit = 96 * 1024 * 1024;
Junio C Hamano15366282011-04-05 10:44:11 -070043unsigned long big_file_threshold = 512 * 1024 * 1024;
Matthias Lederhoferaa086eb2006-07-30 00:27:43 +020044int pager_use_color = 1;
Christian Couderee9601e2008-02-16 06:01:41 +010045const char *editor_program;
Anselm Kruisd3e7da82010-08-30 15:38:38 +020046const char *askpass_program;
Christian Couderdfb068b2008-02-16 06:01:59 +010047const char *excludes_file;
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +020048enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
Michael Haggertyafc711b2014-02-18 12:24:55 +010049int check_replace_refs = 1;
Mike Hommey58d121b2015-06-12 06:34:59 +090050char *git_replace_ref_base;
Junio C Hamanoec70f522011-05-09 12:52:12 -070051enum eol core_eol = EOL_UNSET;
Steffen Prohaska21e5ad52008-02-06 12:25:58 +010052enum safe_crlf safe_crlf = SAFE_CRLF_WARN;
Junio C Hamanocf1b7862007-12-06 00:14:14 -080053unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
Jay Soffian9ed36cf2008-02-19 11:24:37 -050054enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
Dustin Sallingsc998ae92008-05-10 15:36:29 -070055enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
Christopher Tiwaldf25950f2012-03-20 00:31:33 -040056enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
Johannes Schindelin348df162009-04-28 00:32:25 +020057#ifndef OBJECT_CREATION_MODE
58#define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
Johannes Schindelinbe66a6c2009-04-25 11:57:14 +020059#endif
Johannes Schindelin348df162009-04-28 00:32:25 +020060enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
Johannes Schindelina97a7462009-10-09 12:21:57 +020061char *notes_ref_name;
Johannes Schindelin7f3140c2009-07-23 17:33:49 +020062int grafts_replace_parents = 1;
Nguyễn Thái Ngọc Duy08aefc92009-08-20 20:47:08 +070063int core_apply_sparse_checkout;
Junio C Hamano898eacd2011-10-06 23:12:09 -070064int merge_log_config = -1;
Torsten Bögershausen76759c72012-07-08 15:50:25 +020065int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
Junio C Hamano568508e2011-10-28 14:48:40 -070066unsigned long pack_size_limit_cfg;
Johannes Schindelinf30afda2016-05-11 10:43:37 +020067enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
Cornelius Weig341fb282017-01-27 11:09:47 +010068enum log_refs_config log_all_ref_updates = LOG_REFS_UNSET;
Linus Torvaldse1b10392005-10-11 18:47:34 -070069
Jeff Kinga42643a2014-12-15 18:15:20 -050070#ifndef PROTECT_HFS_DEFAULT
71#define PROTECT_HFS_DEFAULT 0
72#endif
73int protect_hfs = PROTECT_HFS_DEFAULT;
74
Johannes Schindelin2b4c6ef2014-12-16 23:46:59 +010075#ifndef PROTECT_NTFS_DEFAULT
76#define PROTECT_NTFS_DEFAULT 0
77#endif
78int protect_ntfs = PROTECT_NTFS_DEFAULT;
79
Junio C Hamanoeff80a92013-01-16 20:18:48 +010080/*
81 * The character that begins a commented line in user-editable file
82 * that is subject to stripspace.
83 */
84char comment_line_char = '#';
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 08:52:23 +070085int auto_comment_line_char;
Junio C Hamanoeff80a92013-01-16 20:18:48 +010086
Linus Torvalds671c9b72008-11-13 16:36:30 -080087/* Parallel index stat data preload? */
Steve Hoelzer299e2982014-06-02 11:43:00 -050088int core_preload_index = 1;
Linus Torvalds671c9b72008-11-13 16:36:30 -080089
Christian Couderdae6c322016-01-27 07:58:06 +010090/*
91 * This is a hack for test programs like test-dump-untracked-cache to
92 * ensure that they do not modify the untracked cache when reading it.
93 * Do not use it otherwise!
94 */
95int ignore_untracked_cache_config;
96
Johannes Schindeline90fdc32007-08-01 01:30:14 +010097/* This is set by setup_git_dir_gently() and/or git_default_config() */
98char *git_work_tree_cfg;
Johannes Schindeline90fdc32007-08-01 01:30:14 +010099
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700100static const char *namespace;
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700101
Brandon Williams74866d72016-10-07 11:18:48 -0700102static const char *super_prefix;
103
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100104/*
Jeff King2163e5d2013-03-08 04:29:08 -0500105 * Repository-local GIT_* environment variables; see cache.h for details.
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100106 */
Jeff King2163e5d2013-03-08 04:29:08 -0500107const char * const local_repo_env[] = {
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100108 ALTERNATE_DB_ENVIRONMENT,
109 CONFIG_ENVIRONMENT,
Jonathan Nieder655e8d92010-08-24 01:41:14 -0500110 CONFIG_DATA_ENVIRONMENT,
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100111 DB_ENVIRONMENT,
112 GIT_DIR_ENVIRONMENT,
113 GIT_WORK_TREE_ENVIRONMENT,
Jeff King2cd83d12013-03-08 04:32:22 -0500114 GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100115 GRAFT_ENVIRONMENT,
116 INDEX_ENVIRONMENT,
117 NO_REPLACE_OBJECTS_ENVIRONMENT,
Mike Hommey58d121b2015-06-12 06:34:59 +0900118 GIT_REPLACE_REF_BASE_ENVIRONMENT,
Jeff Kinga6f7f9a2013-03-08 04:30:25 -0500119 GIT_PREFIX_ENVIRONMENT,
Brandon Williams74866d72016-10-07 11:18:48 -0700120 GIT_SUPER_PREFIX_ENVIRONMENT,
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +0700121 GIT_SHALLOW_FILE_ENVIRONMENT,
Nguyễn Thái Ngọc Duyc7b3a3d2014-11-30 15:24:36 +0700122 GIT_COMMON_DIR_ENVIRONMENT,
Giuseppe Bilotta48a7c1c2010-02-25 00:34:14 +0100123 NULL
124};
125
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700126static char *expand_namespace(const char *raw_namespace)
127{
128 struct strbuf buf = STRBUF_INIT;
129 struct strbuf **components, **c;
130
131 if (!raw_namespace || !*raw_namespace)
132 return xstrdup("");
133
134 strbuf_addstr(&buf, raw_namespace);
135 components = strbuf_split(&buf, '/');
136 strbuf_reset(&buf);
137 for (c = components; *c; c++)
138 if (strcmp((*c)->buf, "/") != 0)
139 strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
140 strbuf_list_free(components);
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200141 if (check_refname_format(buf.buf, 0))
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700142 die("bad git namespace path \"%s\"", raw_namespace);
143 strbuf_addch(&buf, '/');
144 return strbuf_detach(&buf, NULL);
145}
146
Brandon Williams73f192c2017-06-20 12:19:32 -0700147void setup_git_env(void)
Linus Torvaldse1b10392005-10-11 18:47:34 -0700148{
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +0700149 const char *shallow_file;
Mike Hommey58d121b2015-06-12 06:34:59 +0900150 const char *replace_ref_base;
Nguyễn Thái Ngọc Duy487a2b72013-08-31 08:04:14 +0700151
Christian Couder6476b382009-11-18 07:50:58 +0100152 if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
Michael Haggertyafc711b2014-02-18 12:24:55 +0100153 check_replace_refs = 0;
Mike Hommey58d121b2015-06-12 06:34:59 +0900154 replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
155 git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
156 : "refs/replace/");
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700157 namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
Nguyễn Thái Ngọc Duy069c0532013-12-05 20:02:45 +0700158 shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
159 if (shallow_file)
160 set_alternate_shallow_file(shallow_file, 0);
Shawn O. Pearce0bee5912006-12-14 17:41:17 -0500161}
162
Junio C Hamano7d1864c2007-01-07 02:00:28 -0800163int is_bare_repository(void)
Shawn O. Pearce0bee5912006-12-14 17:41:17 -0500164{
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100165 /* if core.bare is not 'false', let's see if there is a work tree */
166 return is_bare_repository_cfg && !get_git_work_tree();
Linus Torvaldse1b10392005-10-11 18:47:34 -0700167}
168
Jeff Kingb9605bc2016-09-12 20:24:15 -0700169int have_git_dir(void)
170{
171 return startup_info->have_repository
Brandon Williamsc14c2342017-06-22 11:43:33 -0700172 || the_repository->gitdir;
Jeff Kingb9605bc2016-09-12 20:24:15 -0700173}
174
Pierre Habouzitc5fba162006-08-23 12:39:11 +0200175const char *get_git_dir(void)
Linus Torvaldse1b10392005-10-11 18:47:34 -0700176{
Brandon Williamsc14c2342017-06-22 11:43:33 -0700177 if (!the_repository->gitdir)
Brandon Williams73f192c2017-06-20 12:19:32 -0700178 BUG("git environment hasn't been setup");
Brandon Williamsc14c2342017-06-22 11:43:33 -0700179 return the_repository->gitdir;
Linus Torvaldse1b10392005-10-11 18:47:34 -0700180}
181
Nguyễn Thái Ngọc Duyc7b3a3d2014-11-30 15:24:36 +0700182const char *get_git_common_dir(void)
183{
Brandon Williamsc14c2342017-06-22 11:43:33 -0700184 if (!the_repository->commondir)
Brandon Williams73f192c2017-06-20 12:19:32 -0700185 BUG("git environment hasn't been setup");
Brandon Williamsc14c2342017-06-22 11:43:33 -0700186 return the_repository->commondir;
Nguyễn Thái Ngọc Duyc7b3a3d2014-11-30 15:24:36 +0700187}
188
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700189const char *get_git_namespace(void)
190{
191 if (!namespace)
Brandon Williams73f192c2017-06-20 12:19:32 -0700192 BUG("git environment hasn't been setup");
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700193 return namespace;
194}
195
196const char *strip_namespace(const char *namespaced_ref)
197{
Brandon Williamsbf08c8c2017-06-20 12:19:34 -0700198 const char *out;
199 if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
200 return out;
201 return NULL;
Josh Tripletta1bea2c2011-07-05 10:54:44 -0700202}
203
Brandon Williams74866d72016-10-07 11:18:48 -0700204const char *get_super_prefix(void)
205{
206 static int initialized;
207 if (!initialized) {
208 super_prefix = getenv(GIT_SUPER_PREFIX_ENVIRONMENT);
209 initialized = 1;
210 }
211 return super_prefix;
212}
213
Daniel Barkalow19757d82008-04-27 13:39:21 -0400214static int git_work_tree_initialized;
215
216/*
217 * Note. This works only before you used a work tree. This was added
218 * primarily to support git-clone to work in a new repository it just
219 * created, and is not meant to flip between different work trees.
220 */
221void set_git_work_tree(const char *new_work_tree)
222{
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700223 if (git_work_tree_initialized) {
Carlos Martín Nietoe2a57aa2011-03-17 12:26:46 +0100224 new_work_tree = real_path(new_work_tree);
Brandon Williamsb4158732017-06-22 11:43:34 -0700225 if (strcmp(new_work_tree, the_repository->worktree))
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700226 die("internal error: work tree has already been set\n"
227 "Current worktree: %s\nNew worktree: %s",
Brandon Williamsb4158732017-06-22 11:43:34 -0700228 the_repository->worktree, new_work_tree);
Nguyễn Thái Ngọc Duy83518362010-11-26 22:32:40 +0700229 return;
230 }
Daniel Barkalow19757d82008-04-27 13:39:21 -0400231 git_work_tree_initialized = 1;
Brandon Williamsb4158732017-06-22 11:43:34 -0700232 repo_set_worktree(the_repository, new_work_tree);
Daniel Barkalow19757d82008-04-27 13:39:21 -0400233}
234
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100235const char *get_git_work_tree(void)
236{
Brandon Williamsb4158732017-06-22 11:43:34 -0700237 return the_repository->worktree;
Johannes Schindeline90fdc32007-08-01 01:30:14 +0100238}
239
Linus Torvaldse1b10392005-10-11 18:47:34 -0700240char *get_object_directory(void)
241{
Brandon Williamsc14c2342017-06-22 11:43:33 -0700242 if (!the_repository->objectdir)
Brandon Williams73f192c2017-06-20 12:19:32 -0700243 BUG("git environment hasn't been setup");
Brandon Williamsc14c2342017-06-22 11:43:33 -0700244 return the_repository->objectdir;
Linus Torvaldse1b10392005-10-11 18:47:34 -0700245}
246
Jeff King594fa992017-03-28 15:45:43 -0400247int odb_mkstemp(struct strbuf *template, const char *pattern)
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500248{
249 int fd;
250 /*
251 * we let the umask do its job, don't try to be more
252 * restrictive except to remove write permission.
253 */
254 int mode = 0444;
Jeff King4aa7d752017-03-28 15:45:52 -0400255 git_path_buf(template, "objects/%s", pattern);
Jeff King594fa992017-03-28 15:45:43 -0400256 fd = git_mkstemp_mode(template->buf, mode);
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500257 if (0 <= fd)
258 return fd;
259
260 /* slow path */
261 /* some mkstemp implementations erase template on failure */
Jeff King4aa7d752017-03-28 15:45:52 -0400262 git_path_buf(template, "objects/%s", pattern);
Jeff King594fa992017-03-28 15:45:43 -0400263 safe_create_leading_directories(template->buf);
264 return xmkstemp_mode(template->buf, mode);
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500265}
266
Jeff Kingeaeefc32017-03-16 10:27:12 -0400267int odb_pack_keep(const char *name)
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500268{
269 int fd;
270
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500271 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
272 if (0 <= fd)
273 return fd;
274
275 /* slow path */
Jeff Kingeaeefc32017-03-16 10:27:12 -0400276 safe_create_leading_directories_const(name);
Jonathan Nieder463db9b2010-11-06 06:45:38 -0500277 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
278}
279
Linus Torvaldse1b10392005-10-11 18:47:34 -0700280char *get_index_file(void)
281{
Brandon Williamsc14c2342017-06-22 11:43:33 -0700282 if (!the_repository->index_file)
Brandon Williams73f192c2017-06-20 12:19:32 -0700283 BUG("git environment hasn't been setup");
Brandon Williamsc14c2342017-06-22 11:43:33 -0700284 return the_repository->index_file;
Linus Torvaldse1b10392005-10-11 18:47:34 -0700285}
286
287char *get_graft_file(void)
288{
Brandon Williamsc14c2342017-06-22 11:43:33 -0700289 if (!the_repository->graft_file)
Brandon Williams73f192c2017-06-20 12:19:32 -0700290 BUG("git environment hasn't been setup");
Brandon Williamsc14c2342017-06-22 11:43:33 -0700291 return the_repository->graft_file;
Linus Torvaldse1b10392005-10-11 18:47:34 -0700292}
Johannes Schindelind7ac12b2007-08-01 01:29:38 +0100293
294int set_git_dir(const char *path)
295{
296 if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
297 return error("Could not set GIT_DIR to '%s'", path);
Brandon Williamsc14c2342017-06-22 11:43:33 -0700298 repo_set_gitdir(the_repository, path);
Johannes Schindelind7ac12b2007-08-01 01:29:38 +0100299 setup_git_env();
300 return 0;
301}
Pat Notza6fa5992010-11-02 13:59:07 -0600302
303const char *get_log_output_encoding(void)
304{
305 return git_log_output_encoding ? git_log_output_encoding
306 : get_commit_output_encoding();
307}
308
309const char *get_commit_output_encoding(void)
310{
311 return git_commit_encoding ? git_commit_encoding : "UTF-8";
312}
Jeff King7875acb2016-03-11 17:36:49 -0500313
314static int the_shared_repository = PERM_UMASK;
Jeff Kingae5f6772016-03-11 17:36:53 -0500315static int need_shared_repository_from_config = 1;
Jeff King7875acb2016-03-11 17:36:49 -0500316
317void set_shared_repository(int value)
318{
319 the_shared_repository = value;
Jeff Kingae5f6772016-03-11 17:36:53 -0500320 need_shared_repository_from_config = 0;
Jeff King7875acb2016-03-11 17:36:49 -0500321}
322
323int get_shared_repository(void)
324{
Jeff Kingae5f6772016-03-11 17:36:53 -0500325 if (need_shared_repository_from_config) {
326 const char *var = "core.sharedrepository";
327 const char *value;
328 if (!git_config_get_value(var, &value))
329 the_shared_repository = git_config_perm(var, value);
330 need_shared_repository_from_config = 0;
331 }
Jeff King7875acb2016-03-11 17:36:49 -0500332 return the_shared_repository;
333}
Jeff King45439262016-09-12 20:24:23 -0700334
335void reset_shared_repository(void)
336{
337 need_shared_repository_from_config = 1;
338}