Merge branch 'ag/userdiff-go-funcname'
[git/git.git] / environment.c
1 /*
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"
11 #include "repository.h"
12 #include "config.h"
13 #include "refs.h"
14 #include "fmt-merge-msg.h"
15 #include "commit.h"
16
17 int trust_executable_bit = 1;
18 int trust_ctime = 1;
19 int check_stat = 1;
20 int has_symlinks = 1;
21 int minimum_abbrev = 4, default_abbrev = -1;
22 int ignore_case;
23 int assume_unchanged;
24 int prefer_symlink_refs;
25 int is_bare_repository_cfg = -1; /* unspecified */
26 int warn_ambiguous_refs = 1;
27 int warn_on_object_refname_ambiguity = 1;
28 int ref_paranoia = -1;
29 int repository_format_precious_objects;
30 char *repository_format_partial_clone;
31 const char *core_partial_clone_filter_default;
32 const char *git_commit_encoding;
33 const char *git_log_output_encoding;
34 const char *apply_default_whitespace;
35 const char *apply_default_ignorewhitespace;
36 const char *git_attributes_file;
37 const char *git_hooks_path;
38 int zlib_compression_level = Z_BEST_SPEED;
39 int core_compression_level;
40 int pack_compression_level = Z_DEFAULT_COMPRESSION;
41 int fsync_object_files;
42 size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
43 size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
44 size_t delta_base_cache_limit = 96 * 1024 * 1024;
45 unsigned long big_file_threshold = 512 * 1024 * 1024;
46 int pager_use_color = 1;
47 const char *editor_program;
48 const char *askpass_program;
49 const char *excludes_file;
50 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
51 int check_replace_refs = 1;
52 char *git_replace_ref_base;
53 enum eol core_eol = EOL_UNSET;
54 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
55 unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
56 enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
57 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
58 enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
59 #ifndef OBJECT_CREATION_MODE
60 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
61 #endif
62 enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
63 char *notes_ref_name;
64 int grafts_replace_parents = 1;
65 int core_apply_sparse_checkout;
66 int merge_log_config = -1;
67 int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
68 unsigned long pack_size_limit_cfg;
69 enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
70 enum log_refs_config log_all_ref_updates = LOG_REFS_UNSET;
71
72 #ifndef PROTECT_HFS_DEFAULT
73 #define PROTECT_HFS_DEFAULT 0
74 #endif
75 int protect_hfs = PROTECT_HFS_DEFAULT;
76
77 #ifndef PROTECT_NTFS_DEFAULT
78 #define PROTECT_NTFS_DEFAULT 0
79 #endif
80 int protect_ntfs = PROTECT_NTFS_DEFAULT;
81 const char *core_fsmonitor;
82
83 /*
84 * The character that begins a commented line in user-editable file
85 * that is subject to stripspace.
86 */
87 char comment_line_char = '#';
88 int auto_comment_line_char;
89
90 /* Parallel index stat data preload? */
91 int core_preload_index = 1;
92
93 /*
94 * This is a hack for test programs like test-dump-untracked-cache to
95 * ensure that they do not modify the untracked cache when reading it.
96 * Do not use it otherwise!
97 */
98 int ignore_untracked_cache_config;
99
100 /* This is set by setup_git_dir_gently() and/or git_default_config() */
101 char *git_work_tree_cfg;
102
103 static char *git_namespace;
104
105 static const char *super_prefix;
106
107 /*
108 * Repository-local GIT_* environment variables; see cache.h for details.
109 */
110 const char * const local_repo_env[] = {
111 ALTERNATE_DB_ENVIRONMENT,
112 CONFIG_ENVIRONMENT,
113 CONFIG_DATA_ENVIRONMENT,
114 DB_ENVIRONMENT,
115 GIT_DIR_ENVIRONMENT,
116 GIT_WORK_TREE_ENVIRONMENT,
117 GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
118 GRAFT_ENVIRONMENT,
119 INDEX_ENVIRONMENT,
120 NO_REPLACE_OBJECTS_ENVIRONMENT,
121 GIT_REPLACE_REF_BASE_ENVIRONMENT,
122 GIT_PREFIX_ENVIRONMENT,
123 GIT_SUPER_PREFIX_ENVIRONMENT,
124 GIT_SHALLOW_FILE_ENVIRONMENT,
125 GIT_COMMON_DIR_ENVIRONMENT,
126 NULL
127 };
128
129 static char *expand_namespace(const char *raw_namespace)
130 {
131 struct strbuf buf = STRBUF_INIT;
132 struct strbuf **components, **c;
133
134 if (!raw_namespace || !*raw_namespace)
135 return xstrdup("");
136
137 strbuf_addstr(&buf, raw_namespace);
138 components = strbuf_split(&buf, '/');
139 strbuf_reset(&buf);
140 for (c = components; *c; c++)
141 if (strcmp((*c)->buf, "/") != 0)
142 strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
143 strbuf_list_free(components);
144 if (check_refname_format(buf.buf, 0))
145 die("bad git namespace path \"%s\"", raw_namespace);
146 strbuf_addch(&buf, '/');
147 return strbuf_detach(&buf, NULL);
148 }
149
150 void setup_git_env(void)
151 {
152 const char *shallow_file;
153 const char *replace_ref_base;
154
155 if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
156 check_replace_refs = 0;
157 replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
158 free(git_replace_ref_base);
159 git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
160 : "refs/replace/");
161 free(git_namespace);
162 git_namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
163 shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
164 if (shallow_file)
165 set_alternate_shallow_file(shallow_file, 0);
166 }
167
168 int is_bare_repository(void)
169 {
170 /* if core.bare is not 'false', let's see if there is a work tree */
171 return is_bare_repository_cfg && !get_git_work_tree();
172 }
173
174 int have_git_dir(void)
175 {
176 return startup_info->have_repository
177 || the_repository->gitdir;
178 }
179
180 const char *get_git_dir(void)
181 {
182 if (!the_repository->gitdir)
183 BUG("git environment hasn't been setup");
184 return the_repository->gitdir;
185 }
186
187 const char *get_git_common_dir(void)
188 {
189 if (!the_repository->commondir)
190 BUG("git environment hasn't been setup");
191 return the_repository->commondir;
192 }
193
194 const char *get_git_namespace(void)
195 {
196 if (!git_namespace)
197 BUG("git environment hasn't been setup");
198 return git_namespace;
199 }
200
201 const char *strip_namespace(const char *namespaced_ref)
202 {
203 const char *out;
204 if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
205 return out;
206 return NULL;
207 }
208
209 const char *get_super_prefix(void)
210 {
211 static int initialized;
212 if (!initialized) {
213 super_prefix = getenv(GIT_SUPER_PREFIX_ENVIRONMENT);
214 initialized = 1;
215 }
216 return super_prefix;
217 }
218
219 static int git_work_tree_initialized;
220
221 /*
222 * Note. This works only before you used a work tree. This was added
223 * primarily to support git-clone to work in a new repository it just
224 * created, and is not meant to flip between different work trees.
225 */
226 void set_git_work_tree(const char *new_work_tree)
227 {
228 if (git_work_tree_initialized) {
229 new_work_tree = real_path(new_work_tree);
230 if (strcmp(new_work_tree, the_repository->worktree))
231 die("internal error: work tree has already been set\n"
232 "Current worktree: %s\nNew worktree: %s",
233 the_repository->worktree, new_work_tree);
234 return;
235 }
236 git_work_tree_initialized = 1;
237 repo_set_worktree(the_repository, new_work_tree);
238 }
239
240 const char *get_git_work_tree(void)
241 {
242 return the_repository->worktree;
243 }
244
245 char *get_object_directory(void)
246 {
247 if (!the_repository->objectdir)
248 BUG("git environment hasn't been setup");
249 return the_repository->objectdir;
250 }
251
252 int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
253 {
254 int fd;
255 /*
256 * we let the umask do its job, don't try to be more
257 * restrictive except to remove write permission.
258 */
259 int mode = 0444;
260 git_path_buf(temp_filename, "objects/%s", pattern);
261 fd = git_mkstemp_mode(temp_filename->buf, mode);
262 if (0 <= fd)
263 return fd;
264
265 /* slow path */
266 /* some mkstemp implementations erase temp_filename on failure */
267 git_path_buf(temp_filename, "objects/%s", pattern);
268 safe_create_leading_directories(temp_filename->buf);
269 return xmkstemp_mode(temp_filename->buf, mode);
270 }
271
272 int odb_pack_keep(const char *name)
273 {
274 int fd;
275
276 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
277 if (0 <= fd)
278 return fd;
279
280 /* slow path */
281 safe_create_leading_directories_const(name);
282 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
283 }
284
285 char *get_index_file(void)
286 {
287 if (!the_repository->index_file)
288 BUG("git environment hasn't been setup");
289 return the_repository->index_file;
290 }
291
292 char *get_graft_file(void)
293 {
294 if (!the_repository->graft_file)
295 BUG("git environment hasn't been setup");
296 return the_repository->graft_file;
297 }
298
299 int set_git_dir(const char *path)
300 {
301 if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
302 return error("Could not set GIT_DIR to '%s'", path);
303 repo_set_gitdir(the_repository, path);
304 setup_git_env();
305 return 0;
306 }
307
308 const char *get_log_output_encoding(void)
309 {
310 return git_log_output_encoding ? git_log_output_encoding
311 : get_commit_output_encoding();
312 }
313
314 const char *get_commit_output_encoding(void)
315 {
316 return git_commit_encoding ? git_commit_encoding : "UTF-8";
317 }
318
319 static int the_shared_repository = PERM_UMASK;
320 static int need_shared_repository_from_config = 1;
321
322 void set_shared_repository(int value)
323 {
324 the_shared_repository = value;
325 need_shared_repository_from_config = 0;
326 }
327
328 int get_shared_repository(void)
329 {
330 if (need_shared_repository_from_config) {
331 const char *var = "core.sharedrepository";
332 const char *value;
333 if (!git_config_get_value(var, &value))
334 the_shared_repository = git_config_perm(var, value);
335 need_shared_repository_from_config = 0;
336 }
337 return the_shared_repository;
338 }
339
340 void reset_shared_repository(void)
341 {
342 need_shared_repository_from_config = 1;
343 }
344
345 int use_optional_locks(void)
346 {
347 return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
348 }
349
350 int print_sha1_ellipsis(void)
351 {
352 /*
353 * Determine if the calling environment contains the variable
354 * GIT_PRINT_SHA1_ELLIPSIS set to "yes".
355 */
356 static int cached_result = -1; /* unknown */
357
358 if (cached_result < 0) {
359 const char *v = getenv("GIT_PRINT_SHA1_ELLIPSIS");
360 cached_result = (v && !strcasecmp(v, "yes"));
361 }
362 return cached_result;
363 }