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