t2400: drop no-op `--sq` from rev-parse call
[git.git] / environment.c
blob63c697e7e97cca35e52df3847bb017713f6940ef
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.
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 "abspath.h"
12 #include "branch.h"
13 #include "environment.h"
14 #include "gettext.h"
15 #include "repository.h"
16 #include "config.h"
17 #include "refs.h"
18 #include "fmt-merge-msg.h"
19 #include "commit.h"
20 #include "strvec.h"
21 #include "object-store.h"
22 #include "replace-object.h"
23 #include "tmp-objdir.h"
24 #include "chdir-notify.h"
25 #include "setup.h"
26 #include "shallow.h"
27 #include "wrapper.h"
28 #include "write-or-die.h"
30 int trust_executable_bit = 1;
31 int trust_ctime = 1;
32 int check_stat = 1;
33 int has_symlinks = 1;
34 int minimum_abbrev = 4, default_abbrev = -1;
35 int ignore_case;
36 int assume_unchanged;
37 int prefer_symlink_refs;
38 int is_bare_repository_cfg = -1; /* unspecified */
39 int warn_ambiguous_refs = 1;
40 int warn_on_object_refname_ambiguity = 1;
41 int repository_format_precious_objects;
42 int repository_format_worktree_config;
43 const char *git_commit_encoding;
44 const char *git_log_output_encoding;
45 char *apply_default_whitespace;
46 char *apply_default_ignorewhitespace;
47 const char *git_attributes_file;
48 const char *git_hooks_path;
49 int zlib_compression_level = Z_BEST_SPEED;
50 int pack_compression_level = Z_DEFAULT_COMPRESSION;
51 int fsync_object_files = -1;
52 int use_fsync = -1;
53 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
54 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
55 size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
56 size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
57 size_t delta_base_cache_limit = 96 * 1024 * 1024;
58 unsigned long big_file_threshold = 512 * 1024 * 1024;
59 int pager_use_color = 1;
60 const char *editor_program;
61 const char *askpass_program;
62 const char *excludes_file;
63 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
64 int read_replace_refs = 1;
65 enum eol core_eol = EOL_UNSET;
66 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
67 char *check_roundtrip_encoding = "SHIFT-JIS";
68 unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
69 enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
70 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
71 enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
72 #ifndef OBJECT_CREATION_MODE
73 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
74 #endif
75 enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
76 char *notes_ref_name;
77 int grafts_replace_parents = 1;
78 int core_apply_sparse_checkout;
79 int core_sparse_checkout_cone;
80 int sparse_expect_files_outside_of_patterns;
81 int merge_log_config = -1;
82 int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
83 unsigned long pack_size_limit_cfg;
84 enum log_refs_config log_all_ref_updates = LOG_REFS_UNSET;
86 #ifndef PROTECT_HFS_DEFAULT
87 #define PROTECT_HFS_DEFAULT 0
88 #endif
89 int protect_hfs = PROTECT_HFS_DEFAULT;
91 #ifndef PROTECT_NTFS_DEFAULT
92 #define PROTECT_NTFS_DEFAULT 1
93 #endif
94 int protect_ntfs = PROTECT_NTFS_DEFAULT;
97 * The character that begins a commented line in user-editable file
98 * that is subject to stripspace.
100 char comment_line_char = '#';
101 int auto_comment_line_char;
103 /* Parallel index stat data preload? */
104 int core_preload_index = 1;
106 /* This is set by setup_git_dir_gently() and/or git_default_config() */
107 char *git_work_tree_cfg;
109 static char *git_namespace;
112 * Repository-local GIT_* environment variables; see cache.h for details.
114 const char * const local_repo_env[] = {
115 ALTERNATE_DB_ENVIRONMENT,
116 CONFIG_ENVIRONMENT,
117 CONFIG_DATA_ENVIRONMENT,
118 CONFIG_COUNT_ENVIRONMENT,
119 DB_ENVIRONMENT,
120 GIT_DIR_ENVIRONMENT,
121 GIT_WORK_TREE_ENVIRONMENT,
122 GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
123 GRAFT_ENVIRONMENT,
124 INDEX_ENVIRONMENT,
125 NO_REPLACE_OBJECTS_ENVIRONMENT,
126 GIT_REPLACE_REF_BASE_ENVIRONMENT,
127 GIT_PREFIX_ENVIRONMENT,
128 GIT_SHALLOW_FILE_ENVIRONMENT,
129 GIT_COMMON_DIR_ENVIRONMENT,
130 NULL
133 static char *expand_namespace(const char *raw_namespace)
135 struct strbuf buf = STRBUF_INIT;
136 struct strbuf **components, **c;
138 if (!raw_namespace || !*raw_namespace)
139 return xstrdup("");
141 strbuf_addstr(&buf, raw_namespace);
142 components = strbuf_split(&buf, '/');
143 strbuf_reset(&buf);
144 for (c = components; *c; c++)
145 if (strcmp((*c)->buf, "/") != 0)
146 strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
147 strbuf_list_free(components);
148 if (check_refname_format(buf.buf, 0))
149 die(_("bad git namespace path \"%s\""), raw_namespace);
150 strbuf_addch(&buf, '/');
151 return strbuf_detach(&buf, NULL);
154 const char *getenv_safe(struct strvec *argv, const char *name)
156 const char *value = getenv(name);
158 if (!value)
159 return NULL;
161 strvec_push(argv, value);
162 return argv->v[argv->nr - 1];
165 void setup_git_env(const char *git_dir)
167 char *git_replace_ref_base;
168 const char *shallow_file;
169 const char *replace_ref_base;
170 struct set_gitdir_args args = { NULL };
171 struct strvec to_free = STRVEC_INIT;
173 args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT);
174 args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT);
175 args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT);
176 args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT);
177 args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT);
178 if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
179 args.disable_ref_updates = 1;
182 repo_set_gitdir(the_repository, git_dir, &args);
183 strvec_clear(&to_free);
185 if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
186 read_replace_refs = 0;
187 replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
188 git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
189 : "refs/replace/");
190 update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base);
192 free(git_namespace);
193 git_namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
194 shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
195 if (shallow_file)
196 set_alternate_shallow_file(the_repository, shallow_file, 0);
199 int is_bare_repository(void)
201 /* if core.bare is not 'false', let's see if there is a work tree */
202 return is_bare_repository_cfg && !get_git_work_tree();
205 int have_git_dir(void)
207 return startup_info->have_repository
208 || the_repository->gitdir;
211 const char *get_git_dir(void)
213 if (!the_repository->gitdir)
214 BUG("git environment hasn't been setup");
215 return the_repository->gitdir;
218 const char *get_git_common_dir(void)
220 if (!the_repository->commondir)
221 BUG("git environment hasn't been setup");
222 return the_repository->commondir;
225 const char *get_git_namespace(void)
227 if (!git_namespace)
228 BUG("git environment hasn't been setup");
229 return git_namespace;
232 const char *strip_namespace(const char *namespaced_ref)
234 const char *out;
235 if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
236 return out;
237 return NULL;
240 static int git_work_tree_initialized;
243 * Note. This works only before you used a work tree. This was added
244 * primarily to support git-clone to work in a new repository it just
245 * created, and is not meant to flip between different work trees.
247 void set_git_work_tree(const char *new_work_tree)
249 if (git_work_tree_initialized) {
250 struct strbuf realpath = STRBUF_INIT;
252 strbuf_realpath(&realpath, new_work_tree, 1);
253 new_work_tree = realpath.buf;
254 if (strcmp(new_work_tree, the_repository->worktree))
255 die("internal error: work tree has already been set\n"
256 "Current worktree: %s\nNew worktree: %s",
257 the_repository->worktree, new_work_tree);
258 strbuf_release(&realpath);
259 return;
261 git_work_tree_initialized = 1;
262 repo_set_worktree(the_repository, new_work_tree);
265 const char *get_git_work_tree(void)
267 return the_repository->worktree;
270 const char *get_object_directory(void)
272 if (!the_repository->objects->odb)
273 BUG("git environment hasn't been setup");
274 return the_repository->objects->odb->path;
277 int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
279 int fd;
281 * we let the umask do its job, don't try to be more
282 * restrictive except to remove write permission.
284 int mode = 0444;
285 git_path_buf(temp_filename, "objects/%s", pattern);
286 fd = git_mkstemp_mode(temp_filename->buf, mode);
287 if (0 <= fd)
288 return fd;
290 /* slow path */
291 /* some mkstemp implementations erase temp_filename on failure */
292 git_path_buf(temp_filename, "objects/%s", pattern);
293 safe_create_leading_directories(temp_filename->buf);
294 return xmkstemp_mode(temp_filename->buf, mode);
297 int odb_pack_keep(const char *name)
299 int fd;
301 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
302 if (0 <= fd)
303 return fd;
305 /* slow path */
306 safe_create_leading_directories_const(name);
307 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
310 char *get_index_file(void)
312 if (!the_repository->index_file)
313 BUG("git environment hasn't been setup");
314 return the_repository->index_file;
317 char *get_graft_file(struct repository *r)
319 if (!r->graft_file)
320 BUG("git environment hasn't been setup");
321 return r->graft_file;
324 static void set_git_dir_1(const char *path)
326 xsetenv(GIT_DIR_ENVIRONMENT, path, 1);
327 setup_git_env(path);
330 static void update_relative_gitdir(const char *name UNUSED,
331 const char *old_cwd,
332 const char *new_cwd,
333 void *data UNUSED)
335 char *path = reparent_relative_path(old_cwd, new_cwd, get_git_dir());
336 struct tmp_objdir *tmp_objdir = tmp_objdir_unapply_primary_odb();
338 trace_printf_key(&trace_setup_key,
339 "setup: move $GIT_DIR to '%s'",
340 path);
341 set_git_dir_1(path);
342 if (tmp_objdir)
343 tmp_objdir_reapply_primary_odb(tmp_objdir, old_cwd, new_cwd);
344 free(path);
347 void set_git_dir(const char *path, int make_realpath)
349 struct strbuf realpath = STRBUF_INIT;
351 if (make_realpath) {
352 strbuf_realpath(&realpath, path, 1);
353 path = realpath.buf;
356 set_git_dir_1(path);
357 if (!is_absolute_path(path))
358 chdir_notify_register(NULL, update_relative_gitdir, NULL);
360 strbuf_release(&realpath);
363 const char *get_log_output_encoding(void)
365 return git_log_output_encoding ? git_log_output_encoding
366 : get_commit_output_encoding();
369 const char *get_commit_output_encoding(void)
371 return git_commit_encoding ? git_commit_encoding : "UTF-8";
374 static int the_shared_repository = PERM_UMASK;
375 static int need_shared_repository_from_config = 1;
377 void set_shared_repository(int value)
379 the_shared_repository = value;
380 need_shared_repository_from_config = 0;
383 int get_shared_repository(void)
385 if (need_shared_repository_from_config) {
386 const char *var = "core.sharedrepository";
387 const char *value;
388 if (!git_config_get_value(var, &value))
389 the_shared_repository = git_config_perm(var, value);
390 need_shared_repository_from_config = 0;
392 return the_shared_repository;
395 void reset_shared_repository(void)
397 need_shared_repository_from_config = 1;
400 int use_optional_locks(void)
402 return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
405 int print_sha1_ellipsis(void)
408 * Determine if the calling environment contains the variable
409 * GIT_PRINT_SHA1_ELLIPSIS set to "yes".
411 static int cached_result = -1; /* unknown */
413 if (cached_result < 0) {
414 const char *v = getenv("GIT_PRINT_SHA1_ELLIPSIS");
415 cached_result = (v && !strcasecmp(v, "yes"));
417 return cached_result;