🌐 AI搜索 & 代理 主页
blob: a40044c3bf8040a36ddfb5c260b389ec63683abd [file] [log] [blame]
Linus Torvaldse1b10392005-10-12 01:47:341/*
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"
Josh Tripletta1bea2c2011-07-05 17:54:4411#include "refs.h"
Ramsay Jones273c7032011-10-09 17:33:3412#include "fmt-merge-msg.h"
Nguyễn Thái Ngọc Duy069c0532013-12-05 13:02:4513#include "commit.h"
Linus Torvaldse1b10392005-10-12 01:47:3414
Linus Torvaldse1b10392005-10-12 01:47:3415int trust_executable_bit = 1;
Alex Riesen1ce47902008-07-28 06:31:2816int trust_ctime = 1;
Robin Rosenbergc08e4d52013-01-22 07:49:2217int check_stat = 1;
Johannes Sixt78a8d642007-03-02 21:11:3018int has_symlinks = 1;
Linus Torvaldsdce96482010-10-28 18:28:0419int minimum_abbrev = 4, default_abbrev = 7;
Linus Torvalds0a9b88b2008-03-21 23:52:4620int ignore_case;
David Rientjes96f1e582006-08-15 17:23:4821int assume_unchanged;
22int prefer_symlink_refs;
Junio C Hamano7d1864c2007-01-07 10:00:2823int is_bare_repository_cfg = -1; /* unspecified */
Junio C Hamano510c5a82007-01-07 09:35:3424int log_all_ref_updates = -1; /* unspecified */
Junio C Hamano1b371f52006-03-24 07:42:4025int warn_ambiguous_refs = 1;
Jeff King25fba782013-07-12 06:20:0526int warn_on_object_refname_ambiguity = 1;
Jeff King49672f22015-03-20 18:43:0627int ref_paranoia = -1;
David Rientjes96f1e582006-08-15 17:23:4828int repository_format_version;
Shawn O. Pearce1a8f2742007-03-12 19:33:1829const char *git_commit_encoding;
Shawn O. Pearce3a556022007-03-07 01:44:1730const char *git_log_output_encoding;
Junio C Hamano94df2502006-06-10 06:09:4931int shared_repository = PERM_UMASK;
David Rientjes96f1e582006-08-15 17:23:4832const char *apply_default_whitespace;
Giuseppe Bilotta86c91f92009-08-04 11:16:4933const char *apply_default_ignorewhitespace;
Junio C Hamano64589a02011-10-06 18:22:2434const char *git_attributes_file;
Dana How960ccca2007-05-09 20:56:5035int zlib_compression_level = Z_BEST_SPEED;
36int core_compression_level;
37int core_compression_seen;
Linus Torvaldsaafe9fb2008-06-18 22:18:4438int fsync_object_files;
Shawn O. Pearce8c825342006-12-24 05:46:1339size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
40size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
David Kastrup4874f542014-05-04 17:13:5741size_t delta_base_cache_limit = 96 * 1024 * 1024;
Junio C Hamano15366282011-04-05 17:44:1142unsigned long big_file_threshold = 512 * 1024 * 1024;
Christian Couder872da322008-02-16 05:01:1143const char *pager_program;
Matthias Lederhoferaa086eb2006-07-29 22:27:4344int pager_use_color = 1;
Christian Couderee9601e2008-02-16 05:01:4145const char *editor_program;
Anselm Kruisd3e7da82010-08-30 13:38:3846const char *askpass_program;
Christian Couderdfb068b2008-02-16 05:01:5947const char *excludes_file;
Eyvind Bernhardsenfd6cce92010-05-19 20:43:1048enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
Michael Haggertyafc711b2014-02-18 11:24:5549int check_replace_refs = 1;
Junio C Hamanoec70f522011-05-09 19:52:1250enum eol core_eol = EOL_UNSET;
Steffen Prohaska21e5ad52008-02-06 11:25:5851enum safe_crlf safe_crlf = SAFE_CRLF_WARN;
Junio C Hamanocf1b7862007-12-06 08:14:1452unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
Jay Soffian9ed36cf2008-02-19 16:24:3753enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
Dustin Sallingsc998ae92008-05-10 22:36:2954enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
Christopher Tiwaldf25950f2012-03-20 04:31:3355enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
Johannes Schindelin348df162009-04-27 22:32:2556#ifndef OBJECT_CREATION_MODE
57#define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
Johannes Schindelinbe66a6c2009-04-25 09:57:1458#endif
Johannes Schindelin348df162009-04-27 22:32:2559enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
Johannes Schindelina97a7462009-10-09 10:21:5760char *notes_ref_name;
Johannes Schindelin7f3140c2009-07-23 15:33:4961int grafts_replace_parents = 1;
Nguyễn Thái Ngọc Duy08aefc92009-08-20 13:47:0862int core_apply_sparse_checkout;
Junio C Hamano898eacd2011-10-07 06:12:0963int merge_log_config = -1;
Torsten Bögershausen76759c72012-07-08 13:50:2564int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
Nguyễn Thái Ngọc Duye37c1322010-08-06 02:40:3565struct startup_info *startup_info;
Junio C Hamano568508e2011-10-28 21:48:4066unsigned long pack_size_limit_cfg;
Linus Torvaldse1b10392005-10-12 01:47:3467
Jeff Kinga42643a2014-12-15 23:15:2068#ifndef PROTECT_HFS_DEFAULT
69#define PROTECT_HFS_DEFAULT 0
70#endif
71int protect_hfs = PROTECT_HFS_DEFAULT;
72
Johannes Schindelin2b4c6ef2014-12-16 22:46:5973#ifndef PROTECT_NTFS_DEFAULT
74#define PROTECT_NTFS_DEFAULT 0
75#endif
76int protect_ntfs = PROTECT_NTFS_DEFAULT;
77
Junio C Hamanoeff80a92013-01-16 19:18:4878/*
79 * The character that begins a commented line in user-editable file
80 * that is subject to stripspace.
81 */
82char comment_line_char = '#';
Nguyễn Thái Ngọc Duy84c9dc22014-05-17 01:52:2383int auto_comment_line_char;
Junio C Hamanoeff80a92013-01-16 19:18:4884
Linus Torvalds671c9b72008-11-14 00:36:3085/* Parallel index stat data preload? */
Steve Hoelzer299e2982014-06-02 16:43:0086int core_preload_index = 1;
Linus Torvalds671c9b72008-11-14 00:36:3087
Johannes Schindeline90fdc32007-08-01 00:30:1488/* This is set by setup_git_dir_gently() and/or git_default_config() */
89char *git_work_tree_cfg;
Daniel Barkalow19757d82008-04-27 17:39:2190static char *work_tree;
Johannes Schindeline90fdc32007-08-01 00:30:1491
Josh Tripletta1bea2c2011-07-05 17:54:4492static const char *namespace;
93static size_t namespace_len;
94
Pierre Habouzitc5fba162006-08-23 10:39:1195static const char *git_dir;
Jonathan Nieder7f7868e2010-10-02 08:35:2996static char *git_object_dir, *git_index_file, *git_graft_file;
Pierre Habouzitc5fba162006-08-23 10:39:1197
Giuseppe Bilotta48a7c1c2010-02-24 23:34:1498/*
Jeff King2163e5d2013-03-08 09:29:0899 * Repository-local GIT_* environment variables; see cache.h for details.
Giuseppe Bilotta48a7c1c2010-02-24 23:34:14100 */
Jeff King2163e5d2013-03-08 09:29:08101const char * const local_repo_env[] = {
Giuseppe Bilotta48a7c1c2010-02-24 23:34:14102 ALTERNATE_DB_ENVIRONMENT,
103 CONFIG_ENVIRONMENT,
Jonathan Nieder655e8d92010-08-24 06:41:14104 CONFIG_DATA_ENVIRONMENT,
Giuseppe Bilotta48a7c1c2010-02-24 23:34:14105 DB_ENVIRONMENT,
106 GIT_DIR_ENVIRONMENT,
107 GIT_WORK_TREE_ENVIRONMENT,
Jeff King2cd83d12013-03-08 09:32:22108 GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
Giuseppe Bilotta48a7c1c2010-02-24 23:34:14109 GRAFT_ENVIRONMENT,
110 INDEX_ENVIRONMENT,
111 NO_REPLACE_OBJECTS_ENVIRONMENT,
Jeff Kinga6f7f9a2013-03-08 09:30:25112 GIT_PREFIX_ENVIRONMENT,
Nguyễn Thái Ngọc Duy069c0532013-12-05 13:02:45113 GIT_SHALLOW_FILE_ENVIRONMENT,
Giuseppe Bilotta48a7c1c2010-02-24 23:34:14114 NULL
115};
116
Josh Tripletta1bea2c2011-07-05 17:54:44117static char *expand_namespace(const char *raw_namespace)
118{
119 struct strbuf buf = STRBUF_INIT;
120 struct strbuf **components, **c;
121
122 if (!raw_namespace || !*raw_namespace)
123 return xstrdup("");
124
125 strbuf_addstr(&buf, raw_namespace);
126 components = strbuf_split(&buf, '/');
127 strbuf_reset(&buf);
128 for (c = components; *c; c++)
129 if (strcmp((*c)->buf, "/") != 0)
130 strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
131 strbuf_list_free(components);
Michael Haggerty8d9c5012011-09-15 21:10:25132 if (check_refname_format(buf.buf, 0))
Josh Tripletta1bea2c2011-07-05 17:54:44133 die("bad git namespace path \"%s\"", raw_namespace);
134 strbuf_addch(&buf, '/');
135 return strbuf_detach(&buf, NULL);
136}
137
Jeff Kingcb6c38d2014-06-24 20:58:15138static char *git_path_from_env(const char *envvar, const char *path)
139{
140 const char *value = getenv(envvar);
141 return value ? xstrdup(value) : git_pathdup("%s", path);
142}
143
Linus Torvaldse1b10392005-10-12 01:47:34144static void setup_git_env(void)
145{
Nguyễn Thái Ngọc Duy487a2b72013-08-31 01:04:14146 const char *gitfile;
Nguyễn Thái Ngọc Duy069c0532013-12-05 13:02:45147 const char *shallow_file;
Nguyễn Thái Ngọc Duy487a2b72013-08-31 01:04:14148
Linus Torvaldse1b10392005-10-12 01:47:34149 git_dir = getenv(GIT_DIR_ENVIRONMENT);
Lars Hjemlib44ebb12008-02-20 22:13:13150 if (!git_dir)
Linus Torvaldse1b10392005-10-12 01:47:34151 git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
Nguyễn Thái Ngọc Duy487a2b72013-08-31 01:04:14152 gitfile = read_gitfile(git_dir);
153 git_dir = xstrdup(gitfile ? gitfile : git_dir);
Jeff Kingcb6c38d2014-06-24 20:58:15154 git_object_dir = git_path_from_env(DB_ENVIRONMENT, "objects");
155 git_index_file = git_path_from_env(INDEX_ENVIRONMENT, "index");
156 git_graft_file = git_path_from_env(GRAFT_ENVIRONMENT, "info/grafts");
Christian Couder6476b382009-11-18 06:50:58157 if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
Michael Haggertyafc711b2014-02-18 11:24:55158 check_replace_refs = 0;
Josh Tripletta1bea2c2011-07-05 17:54:44159 namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
160 namespace_len = strlen(namespace);
Nguyễn Thái Ngọc Duy069c0532013-12-05 13:02:45161 shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
162 if (shallow_file)
163 set_alternate_shallow_file(shallow_file, 0);
Shawn O. Pearce0bee5912006-12-14 22:41:17164}
165
Junio C Hamano7d1864c2007-01-07 10:00:28166int is_bare_repository(void)
Shawn O. Pearce0bee5912006-12-14 22:41:17167{
Johannes Schindeline90fdc32007-08-01 00:30:14168 /* if core.bare is not 'false', let's see if there is a work tree */
169 return is_bare_repository_cfg && !get_git_work_tree();
Linus Torvaldse1b10392005-10-12 01:47:34170}
171
Pierre Habouzitc5fba162006-08-23 10:39:11172const char *get_git_dir(void)
Linus Torvaldse1b10392005-10-12 01:47:34173{
174 if (!git_dir)
175 setup_git_env();
176 return git_dir;
177}
178
Josh Tripletta1bea2c2011-07-05 17:54:44179const char *get_git_namespace(void)
180{
181 if (!namespace)
182 setup_git_env();
183 return namespace;
184}
185
186const char *strip_namespace(const char *namespaced_ref)
187{
Christian Couder59556542013-11-30 20:55:40188 if (!starts_with(namespaced_ref, get_git_namespace()))
Josh Tripletta1bea2c2011-07-05 17:54:44189 return NULL;
190 return namespaced_ref + namespace_len;
191}
192
Daniel Barkalow19757d82008-04-27 17:39:21193static int git_work_tree_initialized;
194
195/*
196 * Note. This works only before you used a work tree. This was added
197 * primarily to support git-clone to work in a new repository it just
198 * created, and is not meant to flip between different work trees.
199 */
200void set_git_work_tree(const char *new_work_tree)
201{
Nguyễn Thái Ngọc Duy83518362010-11-26 15:32:40202 if (git_work_tree_initialized) {
Carlos Martín Nietoe2a57aa2011-03-17 11:26:46203 new_work_tree = real_path(new_work_tree);
Nguyễn Thái Ngọc Duy83518362010-11-26 15:32:40204 if (strcmp(new_work_tree, work_tree))
205 die("internal error: work tree has already been set\n"
206 "Current worktree: %s\nNew worktree: %s",
207 work_tree, new_work_tree);
208 return;
209 }
Daniel Barkalow19757d82008-04-27 17:39:21210 git_work_tree_initialized = 1;
Carlos Martín Nietoe2a57aa2011-03-17 11:26:46211 work_tree = xstrdup(real_path(new_work_tree));
Daniel Barkalow19757d82008-04-27 17:39:21212}
213
Johannes Schindeline90fdc32007-08-01 00:30:14214const char *get_git_work_tree(void)
215{
Johannes Schindeline90fdc32007-08-01 00:30:14216 return work_tree;
217}
218
Linus Torvaldse1b10392005-10-12 01:47:34219char *get_object_directory(void)
220{
221 if (!git_object_dir)
222 setup_git_env();
223 return git_object_dir;
224}
225
Jonathan Nieder463db9b2010-11-06 11:45:38226int odb_mkstemp(char *template, size_t limit, const char *pattern)
227{
228 int fd;
229 /*
230 * we let the umask do its job, don't try to be more
231 * restrictive except to remove write permission.
232 */
233 int mode = 0444;
234 snprintf(template, limit, "%s/%s",
235 get_object_directory(), pattern);
236 fd = git_mkstemp_mode(template, mode);
237 if (0 <= fd)
238 return fd;
239
240 /* slow path */
241 /* some mkstemp implementations erase template on failure */
242 snprintf(template, limit, "%s/%s",
243 get_object_directory(), pattern);
244 safe_create_leading_directories(template);
245 return xmkstemp_mode(template, mode);
246}
247
Nguyễn Thái Ngọc Duy8640d492014-03-16 13:35:00248int odb_pack_keep(char *name, size_t namesz, const unsigned char *sha1)
Jonathan Nieder463db9b2010-11-06 11:45:38249{
250 int fd;
251
252 snprintf(name, namesz, "%s/pack/pack-%s.keep",
253 get_object_directory(), sha1_to_hex(sha1));
254 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
255 if (0 <= fd)
256 return fd;
257
258 /* slow path */
259 safe_create_leading_directories(name);
260 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
261}
262
Linus Torvaldse1b10392005-10-12 01:47:34263char *get_index_file(void)
264{
265 if (!git_index_file)
266 setup_git_env();
267 return git_index_file;
268}
269
270char *get_graft_file(void)
271{
272 if (!git_graft_file)
273 setup_git_env();
274 return git_graft_file;
275}
Johannes Schindelind7ac12b2007-08-01 00:29:38276
277int set_git_dir(const char *path)
278{
279 if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
280 return error("Could not set GIT_DIR to '%s'", path);
281 setup_git_env();
282 return 0;
283}
Pat Notza6fa5992010-11-02 19:59:07284
285const char *get_log_output_encoding(void)
286{
287 return git_log_output_encoding ? git_log_output_encoding
288 : get_commit_output_encoding();
289}
290
291const char *get_commit_output_encoding(void)
292{
293 return git_commit_encoding ? git_commit_encoding : "UTF-8";
294}