t1006: don't strip timestamps from expected results
[alt-git.git] / environment.c
blob28d18eaca8ee0a1edd33f1faebae9f8c19fbba4c
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 "git-compat-util.h"
11 #include "abspath.h"
12 #include "branch.h"
13 #include "convert.h"
14 #include "environment.h"
15 #include "gettext.h"
16 #include "repository.h"
17 #include "config.h"
18 #include "refs.h"
19 #include "fmt-merge-msg.h"
20 #include "commit.h"
21 #include "strvec.h"
22 #include "object-file.h"
23 #include "object-store.h"
24 #include "replace-object.h"
25 #include "tmp-objdir.h"
26 #include "chdir-notify.h"
27 #include "setup.h"
28 #include "shallow.h"
29 #include "trace.h"
30 #include "wrapper.h"
31 #include "write-or-die.h"
33 int trust_executable_bit = 1;
34 int trust_ctime = 1;
35 int check_stat = 1;
36 int has_symlinks = 1;
37 int minimum_abbrev = 4, default_abbrev = -1;
38 int ignore_case;
39 int assume_unchanged;
40 int prefer_symlink_refs;
41 int is_bare_repository_cfg = -1; /* unspecified */
42 int warn_ambiguous_refs = 1;
43 int warn_on_object_refname_ambiguity = 1;
44 int repository_format_precious_objects;
45 int repository_format_worktree_config;
46 const char *git_commit_encoding;
47 const char *git_log_output_encoding;
48 char *apply_default_whitespace;
49 char *apply_default_ignorewhitespace;
50 const char *git_attributes_file;
51 const char *git_hooks_path;
52 int zlib_compression_level = Z_BEST_SPEED;
53 int pack_compression_level = Z_DEFAULT_COMPRESSION;
54 int fsync_object_files = -1;
55 int use_fsync = -1;
56 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
57 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
58 size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
59 size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
60 size_t delta_base_cache_limit = 96 * 1024 * 1024;
61 unsigned long big_file_threshold = 512 * 1024 * 1024;
62 const char *editor_program;
63 const char *askpass_program;
64 const char *excludes_file;
65 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
66 int read_replace_refs = 1;
67 enum eol core_eol = EOL_UNSET;
68 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
69 char *check_roundtrip_encoding = "SHIFT-JIS";
70 enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
71 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
72 enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
73 #ifndef OBJECT_CREATION_MODE
74 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
75 #endif
76 enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
77 char *notes_ref_name;
78 int grafts_replace_parents = 1;
79 int core_apply_sparse_checkout;
80 int core_sparse_checkout_cone;
81 int sparse_expect_files_outside_of_patterns;
82 int merge_log_config = -1;
83 int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
84 unsigned long pack_size_limit_cfg;
85 enum log_refs_config log_all_ref_updates = LOG_REFS_UNSET;
87 #ifndef PROTECT_HFS_DEFAULT
88 #define PROTECT_HFS_DEFAULT 0
89 #endif
90 int protect_hfs = PROTECT_HFS_DEFAULT;
92 #ifndef PROTECT_NTFS_DEFAULT
93 #define PROTECT_NTFS_DEFAULT 1
94 #endif
95 int protect_ntfs = PROTECT_NTFS_DEFAULT;
98 * The character that begins a commented line in user-editable file
99 * that is subject to stripspace.
101 char comment_line_char = '#';
102 int auto_comment_line_char;
104 /* Parallel index stat data preload? */
105 int core_preload_index = 1;
107 /* This is set by setup_git_dir_gently() and/or git_default_config() */
108 char *git_work_tree_cfg;
110 static char *git_namespace;
113 * Repository-local GIT_* environment variables; see cache.h for details.
115 const char * const local_repo_env[] = {
116 ALTERNATE_DB_ENVIRONMENT,
117 CONFIG_ENVIRONMENT,
118 CONFIG_DATA_ENVIRONMENT,
119 CONFIG_COUNT_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_SHALLOW_FILE_ENVIRONMENT,
130 GIT_COMMON_DIR_ENVIRONMENT,
131 NULL
134 static char *expand_namespace(const char *raw_namespace)
136 struct strbuf buf = STRBUF_INIT;
137 struct strbuf **components, **c;
139 if (!raw_namespace || !*raw_namespace)
140 return xstrdup("");
142 strbuf_addstr(&buf, raw_namespace);
143 components = strbuf_split(&buf, '/');
144 strbuf_reset(&buf);
145 for (c = components; *c; c++)
146 if (strcmp((*c)->buf, "/") != 0)
147 strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
148 strbuf_list_free(components);
149 if (check_refname_format(buf.buf, 0))
150 die(_("bad git namespace path \"%s\""), raw_namespace);
151 strbuf_addch(&buf, '/');
152 return strbuf_detach(&buf, NULL);
155 const char *getenv_safe(struct strvec *argv, const char *name)
157 const char *value = getenv(name);
159 if (!value)
160 return NULL;
162 strvec_push(argv, value);
163 return argv->v[argv->nr - 1];
166 void setup_git_env(const char *git_dir)
168 char *git_replace_ref_base;
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;
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 if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
180 args.disable_ref_updates = 1;
183 repo_set_gitdir(the_repository, git_dir, &args);
184 strvec_clear(&to_free);
186 if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
187 read_replace_refs = 0;
188 replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
189 git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
190 : "refs/replace/");
191 update_ref_namespace(NAMESPACE_REPLACE, git_replace_ref_base);
193 free(git_namespace);
194 git_namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
195 shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
196 if (shallow_file)
197 set_alternate_shallow_file(the_repository, shallow_file, 0);
200 int is_bare_repository(void)
202 /* if core.bare is not 'false', let's see if there is a work tree */
203 return is_bare_repository_cfg && !get_git_work_tree();
206 int have_git_dir(void)
208 return startup_info->have_repository
209 || the_repository->gitdir;
212 const char *get_git_dir(void)
214 if (!the_repository->gitdir)
215 BUG("git environment hasn't been setup");
216 return the_repository->gitdir;
219 const char *get_git_common_dir(void)
221 if (!the_repository->commondir)
222 BUG("git environment hasn't been setup");
223 return the_repository->commondir;
226 const char *get_git_namespace(void)
228 if (!git_namespace)
229 BUG("git environment hasn't been setup");
230 return git_namespace;
233 const char *strip_namespace(const char *namespaced_ref)
235 const char *out;
236 if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
237 return out;
238 return NULL;
241 static int git_work_tree_initialized;
244 * Note. This works only before you used a work tree. This was added
245 * primarily to support git-clone to work in a new repository it just
246 * created, and is not meant to flip between different work trees.
248 void set_git_work_tree(const char *new_work_tree)
250 if (git_work_tree_initialized) {
251 struct strbuf realpath = STRBUF_INIT;
253 strbuf_realpath(&realpath, new_work_tree, 1);
254 new_work_tree = realpath.buf;
255 if (strcmp(new_work_tree, the_repository->worktree))
256 die("internal error: work tree has already been set\n"
257 "Current worktree: %s\nNew worktree: %s",
258 the_repository->worktree, new_work_tree);
259 strbuf_release(&realpath);
260 return;
262 git_work_tree_initialized = 1;
263 repo_set_worktree(the_repository, new_work_tree);
266 const char *get_git_work_tree(void)
268 return the_repository->worktree;
271 const char *get_object_directory(void)
273 if (!the_repository->objects->odb)
274 BUG("git environment hasn't been setup");
275 return the_repository->objects->odb->path;
278 int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
280 int fd;
282 * we let the umask do its job, don't try to be more
283 * restrictive except to remove write permission.
285 int mode = 0444;
286 git_path_buf(temp_filename, "objects/%s", pattern);
287 fd = git_mkstemp_mode(temp_filename->buf, mode);
288 if (0 <= fd)
289 return fd;
291 /* slow path */
292 /* some mkstemp implementations erase temp_filename on failure */
293 git_path_buf(temp_filename, "objects/%s", pattern);
294 safe_create_leading_directories(temp_filename->buf);
295 return xmkstemp_mode(temp_filename->buf, mode);
298 int odb_pack_keep(const char *name)
300 int fd;
302 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
303 if (0 <= fd)
304 return fd;
306 /* slow path */
307 safe_create_leading_directories_const(name);
308 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
311 char *get_index_file(void)
313 if (!the_repository->index_file)
314 BUG("git environment hasn't been setup");
315 return the_repository->index_file;
318 char *get_graft_file(struct repository *r)
320 if (!r->graft_file)
321 BUG("git environment hasn't been setup");
322 return r->graft_file;
325 static void set_git_dir_1(const char *path)
327 xsetenv(GIT_DIR_ENVIRONMENT, path, 1);
328 setup_git_env(path);
331 static void update_relative_gitdir(const char *name UNUSED,
332 const char *old_cwd,
333 const char *new_cwd,
334 void *data UNUSED)
336 char *path = reparent_relative_path(old_cwd, new_cwd, get_git_dir());
337 struct tmp_objdir *tmp_objdir = tmp_objdir_unapply_primary_odb();
339 trace_printf_key(&trace_setup_key,
340 "setup: move $GIT_DIR to '%s'",
341 path);
342 set_git_dir_1(path);
343 if (tmp_objdir)
344 tmp_objdir_reapply_primary_odb(tmp_objdir, old_cwd, new_cwd);
345 free(path);
348 void set_git_dir(const char *path, int make_realpath)
350 struct strbuf realpath = STRBUF_INIT;
352 if (make_realpath) {
353 strbuf_realpath(&realpath, path, 1);
354 path = realpath.buf;
357 set_git_dir_1(path);
358 if (!is_absolute_path(path))
359 chdir_notify_register(NULL, update_relative_gitdir, NULL);
361 strbuf_release(&realpath);
364 const char *get_log_output_encoding(void)
366 return git_log_output_encoding ? git_log_output_encoding
367 : get_commit_output_encoding();
370 const char *get_commit_output_encoding(void)
372 return git_commit_encoding ? git_commit_encoding : "UTF-8";
375 static int the_shared_repository = PERM_UMASK;
376 static int need_shared_repository_from_config = 1;
378 void set_shared_repository(int value)
380 the_shared_repository = value;
381 need_shared_repository_from_config = 0;
384 int get_shared_repository(void)
386 if (need_shared_repository_from_config) {
387 const char *var = "core.sharedrepository";
388 const char *value;
389 if (!git_config_get_value(var, &value))
390 the_shared_repository = git_config_perm(var, value);
391 need_shared_repository_from_config = 0;
393 return the_shared_repository;
396 void reset_shared_repository(void)
398 need_shared_repository_from_config = 1;
401 int use_optional_locks(void)
403 return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
406 int print_sha1_ellipsis(void)
409 * Determine if the calling environment contains the variable
410 * GIT_PRINT_SHA1_ELLIPSIS set to "yes".
412 static int cached_result = -1; /* unknown */
414 if (cached_result < 0) {
415 const char *v = getenv("GIT_PRINT_SHA1_ELLIPSIS");
416 cached_result = (v && !strcasecmp(v, "yes"));
418 return cached_result;