ref-filter: use generation number for --contains
[git/raj.git] / environment.c
blob8853e2f0ddc808446cc81270af52225c1f9d7e7e
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 "repository.h"
12 #include "config.h"
13 #include "refs.h"
14 #include "fmt-merge-msg.h"
15 #include "commit.h"
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;
73 #ifndef PROTECT_HFS_DEFAULT
74 #define PROTECT_HFS_DEFAULT 0
75 #endif
76 int protect_hfs = PROTECT_HFS_DEFAULT;
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;
85 * The character that begins a commented line in user-editable file
86 * that is subject to stripspace.
88 char comment_line_char = '#';
89 int auto_comment_line_char;
91 /* Parallel index stat data preload? */
92 int core_preload_index = 1;
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!
99 int ignore_untracked_cache_config;
101 /* This is set by setup_git_dir_gently() and/or git_default_config() */
102 char *git_work_tree_cfg;
104 static char *git_namespace;
106 static const char *super_prefix;
109 * Repository-local GIT_* environment variables; see cache.h for details.
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
130 static char *expand_namespace(const char *raw_namespace)
132 struct strbuf buf = STRBUF_INIT;
133 struct strbuf **components, **c;
135 if (!raw_namespace || !*raw_namespace)
136 return xstrdup("");
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);
151 void setup_git_env(void)
153 const char *shallow_file;
154 const char *replace_ref_base;
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);
169 int is_bare_repository(void)
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();
175 int have_git_dir(void)
177 return startup_info->have_repository
178 || the_repository->gitdir;
181 const char *get_git_dir(void)
183 if (!the_repository->gitdir)
184 BUG("git environment hasn't been setup");
185 return the_repository->gitdir;
188 const char *get_git_common_dir(void)
190 if (!the_repository->commondir)
191 BUG("git environment hasn't been setup");
192 return the_repository->commondir;
195 const char *get_git_namespace(void)
197 if (!git_namespace)
198 BUG("git environment hasn't been setup");
199 return git_namespace;
202 const char *strip_namespace(const char *namespaced_ref)
204 const char *out;
205 if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
206 return out;
207 return NULL;
210 const char *get_super_prefix(void)
212 static int initialized;
213 if (!initialized) {
214 super_prefix = getenv(GIT_SUPER_PREFIX_ENVIRONMENT);
215 initialized = 1;
217 return super_prefix;
220 static int git_work_tree_initialized;
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.
227 void set_git_work_tree(const char *new_work_tree)
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;
237 git_work_tree_initialized = 1;
238 repo_set_worktree(the_repository, new_work_tree);
241 const char *get_git_work_tree(void)
243 return the_repository->worktree;
246 char *get_object_directory(void)
248 if (!the_repository->objectdir)
249 BUG("git environment hasn't been setup");
250 return the_repository->objectdir;
253 int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
255 int fd;
257 * we let the umask do its job, don't try to be more
258 * restrictive except to remove write permission.
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;
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);
273 int odb_pack_keep(const char *name)
275 int fd;
277 fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
278 if (0 <= fd)
279 return fd;
281 /* slow path */
282 safe_create_leading_directories_const(name);
283 return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
286 char *get_index_file(void)
288 if (!the_repository->index_file)
289 BUG("git environment hasn't been setup");
290 return the_repository->index_file;
293 char *get_graft_file(void)
295 if (!the_repository->graft_file)
296 BUG("git environment hasn't been setup");
297 return the_repository->graft_file;
300 int set_git_dir(const char *path)
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;
309 const char *get_log_output_encoding(void)
311 return git_log_output_encoding ? git_log_output_encoding
312 : get_commit_output_encoding();
315 const char *get_commit_output_encoding(void)
317 return git_commit_encoding ? git_commit_encoding : "UTF-8";
320 static int the_shared_repository = PERM_UMASK;
321 static int need_shared_repository_from_config = 1;
323 void set_shared_repository(int value)
325 the_shared_repository = value;
326 need_shared_repository_from_config = 0;
329 int get_shared_repository(void)
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;
338 return the_shared_repository;
341 void reset_shared_repository(void)
343 need_shared_repository_from_config = 1;
346 int use_optional_locks(void)
348 return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
351 int print_sha1_ellipsis(void)
354 * Determine if the calling environment contains the variable
355 * GIT_PRINT_SHA1_ELLIPSIS set to "yes".
357 static int cached_result = -1; /* unknown */
359 if (cached_result < 0) {
360 const char *v = getenv("GIT_PRINT_SHA1_ELLIPSIS");
361 cached_result = (v && !strcasecmp(v, "yes"));
363 return cached_result;