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