]> git.ipfire.org Git - thirdparty/git.git/blob - environment.c
7bf0390a3355d3838c1b3baab93051bed5686790
[thirdparty/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
11 #define USE_THE_REPOSITORY_VARIABLE
12
13 #include "git-compat-util.h"
14 #include "abspath.h"
15 #include "branch.h"
16 #include "convert.h"
17 #include "environment.h"
18 #include "gettext.h"
19 #include "git-zlib.h"
20 #include "repository.h"
21 #include "config.h"
22 #include "refs.h"
23 #include "fmt-merge-msg.h"
24 #include "commit.h"
25 #include "strvec.h"
26 #include "path.h"
27 #include "chdir-notify.h"
28 #include "setup.h"
29 #include "write-or-die.h"
30
31 int trust_executable_bit = 1;
32 int trust_ctime = 1;
33 int check_stat = 1;
34 int has_symlinks = 1;
35 int minimum_abbrev = 4, default_abbrev = -1;
36 int ignore_case;
37 int assume_unchanged;
38 int is_bare_repository_cfg = -1; /* unspecified */
39 int warn_on_object_refname_ambiguity = 1;
40 int repository_format_precious_objects;
41 char *git_commit_encoding;
42 char *git_log_output_encoding;
43 char *apply_default_whitespace;
44 char *apply_default_ignorewhitespace;
45 char *git_attributes_file;
46 int zlib_compression_level = Z_BEST_SPEED;
47 int pack_compression_level = Z_DEFAULT_COMPRESSION;
48 int fsync_object_files = -1;
49 int use_fsync = -1;
50 enum fsync_method fsync_method = FSYNC_METHOD_DEFAULT;
51 enum fsync_component fsync_components = FSYNC_COMPONENTS_DEFAULT;
52 char *editor_program;
53 char *askpass_program;
54 char *excludes_file;
55 enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
56 enum eol core_eol = EOL_UNSET;
57 int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
58 char *check_roundtrip_encoding;
59 enum branch_track git_branch_track = BRANCH_TRACK_REMOTE;
60 enum rebase_setup_type autorebase = AUTOREBASE_NEVER;
61 enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED;
62 #ifndef OBJECT_CREATION_MODE
63 #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS
64 #endif
65 enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE;
66 int grafts_keep_true_parents;
67 int core_apply_sparse_checkout;
68 int core_sparse_checkout_cone;
69 int sparse_expect_files_outside_of_patterns;
70 int merge_log_config = -1;
71 int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
72 unsigned long pack_size_limit_cfg;
73 int max_allowed_tree_depth =
74 #ifdef _MSC_VER
75 /*
76 * When traversing into too-deep trees, Visual C-compiled Git seems to
77 * run into some internal stack overflow detection in the
78 * `RtlpAllocateHeap()` function that is called from within
79 * `git_inflate_init()`'s call tree. The following value seems to be
80 * low enough to avoid that by letting Git exit with an error before
81 * the stack overflow can occur.
82 */
83 512;
84 #elif defined(GIT_WINDOWS_NATIVE) && defined(__clang__) && defined(__aarch64__)
85 /*
86 * Similar to Visual C, it seems that on Windows/ARM64 the clang-based
87 * builds have a smaller stack space available. When running out of
88 * that stack space, a `STATUS_STACK_OVERFLOW` is produced. When the
89 * Git command was run from an MSYS2 Bash, this unfortunately results
90 * in an exit code 127. Let's prevent that by lowering the maximal
91 * tree depth; This value seems to be low enough.
92 */
93 1280;
94 #else
95 2048;
96 #endif
97
98 #ifndef PROTECT_HFS_DEFAULT
99 #define PROTECT_HFS_DEFAULT 0
100 #endif
101 int protect_hfs = PROTECT_HFS_DEFAULT;
102
103 #ifndef PROTECT_NTFS_DEFAULT
104 #define PROTECT_NTFS_DEFAULT 1
105 #endif
106 int protect_ntfs = PROTECT_NTFS_DEFAULT;
107
108 /*
109 * The character that begins a commented line in user-editable file
110 * that is subject to stripspace.
111 */
112 const char *comment_line_str = "#";
113 char *comment_line_str_to_free;
114 int auto_comment_line_char;
115
116 /* This is set by setup_git_directory_gently() and/or git_default_config() */
117 char *git_work_tree_cfg;
118
119 /*
120 * Repository-local GIT_* environment variables; see environment.h for details.
121 */
122 const char * const local_repo_env[] = {
123 ALTERNATE_DB_ENVIRONMENT,
124 CONFIG_ENVIRONMENT,
125 CONFIG_DATA_ENVIRONMENT,
126 CONFIG_COUNT_ENVIRONMENT,
127 DB_ENVIRONMENT,
128 GIT_DIR_ENVIRONMENT,
129 GIT_WORK_TREE_ENVIRONMENT,
130 GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
131 GRAFT_ENVIRONMENT,
132 INDEX_ENVIRONMENT,
133 NO_REPLACE_OBJECTS_ENVIRONMENT,
134 GIT_REPLACE_REF_BASE_ENVIRONMENT,
135 GIT_PREFIX_ENVIRONMENT,
136 GIT_SHALLOW_FILE_ENVIRONMENT,
137 GIT_COMMON_DIR_ENVIRONMENT,
138 NULL
139 };
140
141 const char *getenv_safe(struct strvec *argv, const char *name)
142 {
143 const char *value = getenv(name);
144
145 if (!value)
146 return NULL;
147
148 strvec_push(argv, value);
149 return argv->v[argv->nr - 1];
150 }
151
152 int is_bare_repository(void)
153 {
154 /* if core.bare is not 'false', let's see if there is a work tree */
155 return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
156 }
157
158 int have_git_dir(void)
159 {
160 return startup_info->have_repository
161 || the_repository->gitdir;
162 }
163
164 const char *get_git_namespace(void)
165 {
166 static const char *namespace;
167
168 struct strbuf buf = STRBUF_INIT;
169 struct strbuf **components, **c;
170 const char *raw_namespace;
171
172 if (namespace)
173 return namespace;
174
175 raw_namespace = getenv(GIT_NAMESPACE_ENVIRONMENT);
176 if (!raw_namespace || !*raw_namespace) {
177 namespace = "";
178 return namespace;
179 }
180
181 strbuf_addstr(&buf, raw_namespace);
182 components = strbuf_split(&buf, '/');
183 strbuf_reset(&buf);
184 for (c = components; *c; c++)
185 if (strcmp((*c)->buf, "/") != 0)
186 strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
187 strbuf_list_free(components);
188 if (check_refname_format(buf.buf, 0))
189 die(_("bad git namespace path \"%s\""), raw_namespace);
190 strbuf_addch(&buf, '/');
191
192 namespace = strbuf_detach(&buf, NULL);
193
194 return namespace;
195 }
196
197 const char *strip_namespace(const char *namespaced_ref)
198 {
199 const char *out;
200 if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
201 return out;
202 return NULL;
203 }
204
205 const char *get_log_output_encoding(void)
206 {
207 return git_log_output_encoding ? git_log_output_encoding
208 : get_commit_output_encoding();
209 }
210
211 const char *get_commit_output_encoding(void)
212 {
213 return git_commit_encoding ? git_commit_encoding : "UTF-8";
214 }
215
216 int use_optional_locks(void)
217 {
218 return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
219 }
220
221 int print_sha1_ellipsis(void)
222 {
223 /*
224 * Determine if the calling environment contains the variable
225 * GIT_PRINT_SHA1_ELLIPSIS set to "yes".
226 */
227 static int cached_result = -1; /* unknown */
228
229 if (cached_result < 0) {
230 const char *v = getenv("GIT_PRINT_SHA1_ELLIPSIS");
231 cached_result = (v && !strcasecmp(v, "yes"));
232 }
233 return cached_result;
234 }