Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 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" |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 11 | #include "repository.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 12 | #include "config.h" |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 13 | #include "refs.h" |
Ramsay Jones | 273c703 | 2011-10-09 18:33:34 +0100 | [diff] [blame] | 14 | #include "fmt-merge-msg.h" |
Nguyễn Thái Ngọc Duy | 069c053 | 2013-12-05 20:02:45 +0700 | [diff] [blame] | 15 | #include "commit.h" |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 16 | #include "argv-array.h" |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 17 | #include "object-store.h" |
Jeff King | 8500e0d | 2018-03-30 14:35:08 -0400 | [diff] [blame] | 18 | #include "chdir-notify.h" |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 19 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 20 | int trust_executable_bit = 1; |
Alex Riesen | 1ce4790 | 2008-07-28 08:31:28 +0200 | [diff] [blame] | 21 | int trust_ctime = 1; |
Robin Rosenberg | c08e4d5 | 2013-01-22 08:49:22 +0100 | [diff] [blame] | 22 | int check_stat = 1; |
Johannes Sixt | 78a8d64 | 2007-03-02 22:11:30 +0100 | [diff] [blame] | 23 | int has_symlinks = 1; |
Linus Torvalds | e6c587c | 2016-09-30 17:19:37 -0700 | [diff] [blame] | 24 | int minimum_abbrev = 4, default_abbrev = -1; |
Linus Torvalds | 0a9b88b | 2008-03-21 16:52:46 -0700 | [diff] [blame] | 25 | int ignore_case; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 26 | int assume_unchanged; |
| 27 | int prefer_symlink_refs; |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 28 | int is_bare_repository_cfg = -1; /* unspecified */ |
Junio C Hamano | 1b371f5 | 2006-03-23 23:42:40 -0800 | [diff] [blame] | 29 | int warn_ambiguous_refs = 1; |
Jeff King | 25fba78 | 2013-07-12 02:20:05 -0400 | [diff] [blame] | 30 | int warn_on_object_refname_ambiguity = 1; |
Jeff King | 49672f2 | 2015-03-20 14:43:06 -0400 | [diff] [blame] | 31 | int ref_paranoia = -1; |
Jeff King | 067fbd4 | 2015-06-23 06:54:11 -0400 | [diff] [blame] | 32 | int repository_format_precious_objects; |
Jonathan Tan | 75b97fe | 2017-12-05 16:58:43 +0000 | [diff] [blame] | 33 | char *repository_format_partial_clone; |
Jeff Hostetler | 1e1e39b | 2017-12-08 15:58:45 +0000 | [diff] [blame] | 34 | const char *core_partial_clone_filter_default; |
Shawn O. Pearce | 1a8f274 | 2007-03-12 15:33:18 -0400 | [diff] [blame] | 35 | const char *git_commit_encoding; |
Shawn O. Pearce | 3a55602 | 2007-03-06 20:44:17 -0500 | [diff] [blame] | 36 | const char *git_log_output_encoding; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 37 | const char *apply_default_whitespace; |
Giuseppe Bilotta | 86c91f9 | 2009-08-04 13:16:49 +0200 | [diff] [blame] | 38 | const char *apply_default_ignorewhitespace; |
Junio C Hamano | 64589a0 | 2011-10-06 13:22:24 -0500 | [diff] [blame] | 39 | const char *git_attributes_file; |
Ævar Arnfjörð Bjarmason | 867ad08 | 2016-05-04 22:58:12 +0000 | [diff] [blame] | 40 | const char *git_hooks_path; |
Dana How | 960ccca | 2007-05-09 13:56:50 -0700 | [diff] [blame] | 41 | int zlib_compression_level = Z_BEST_SPEED; |
| 42 | int core_compression_level; |
Junio C Hamano | 8de7eeb | 2016-11-15 17:42:40 -0800 | [diff] [blame] | 43 | int pack_compression_level = Z_DEFAULT_COMPRESSION; |
Linus Torvalds | aafe9fb | 2008-06-18 15:18:44 -0700 | [diff] [blame] | 44 | int fsync_object_files; |
Shawn O. Pearce | 8c82534 | 2006-12-24 00:46:13 -0500 | [diff] [blame] | 45 | size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE; |
| 46 | size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT; |
David Kastrup | 4874f54 | 2014-05-04 19:13:57 +0200 | [diff] [blame] | 47 | size_t delta_base_cache_limit = 96 * 1024 * 1024; |
Junio C Hamano | 1536628 | 2011-04-05 10:44:11 -0700 | [diff] [blame] | 48 | unsigned long big_file_threshold = 512 * 1024 * 1024; |
Matthias Lederhofer | aa086eb | 2006-07-30 00:27:43 +0200 | [diff] [blame] | 49 | int pager_use_color = 1; |
Christian Couder | ee9601e | 2008-02-16 06:01:41 +0100 | [diff] [blame] | 50 | const char *editor_program; |
Anselm Kruis | d3e7da8 | 2010-08-30 15:38:38 +0200 | [diff] [blame] | 51 | const char *askpass_program; |
Christian Couder | dfb068b | 2008-02-16 06:01:59 +0100 | [diff] [blame] | 52 | const char *excludes_file; |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 53 | enum auto_crlf auto_crlf = AUTO_CRLF_FALSE; |
Stefan Beller | c3c36d7 | 2018-04-11 17:21:08 -0700 | [diff] [blame] | 54 | int check_replace_refs = 1; /* NEEDSWORK: rename to read_replace_refs */ |
Mike Hommey | 58d121b | 2015-06-12 06:34:59 +0900 | [diff] [blame] | 55 | char *git_replace_ref_base; |
Junio C Hamano | ec70f52 | 2011-05-09 12:52:12 -0700 | [diff] [blame] | 56 | enum eol core_eol = EOL_UNSET; |
Torsten Bögershausen | 8462ff4 | 2018-01-13 23:49:31 +0100 | [diff] [blame] | 57 | int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN; |
Lars Schneider | e92d622 | 2018-04-15 20:16:10 +0200 | [diff] [blame] | 58 | char *check_roundtrip_encoding = "SHIFT-JIS"; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 59 | unsigned whitespace_rule_cfg = WS_DEFAULT_RULE; |
Jay Soffian | 9ed36cf | 2008-02-19 11:24:37 -0500 | [diff] [blame] | 60 | enum branch_track git_branch_track = BRANCH_TRACK_REMOTE; |
Dustin Sallings | c998ae9 | 2008-05-10 15:36:29 -0700 | [diff] [blame] | 61 | enum rebase_setup_type autorebase = AUTOREBASE_NEVER; |
Christopher Tiwald | f25950f | 2012-03-20 00:31:33 -0400 | [diff] [blame] | 62 | enum push_default_type push_default = PUSH_DEFAULT_UNSPECIFIED; |
Johannes Schindelin | 348df16 | 2009-04-28 00:32:25 +0200 | [diff] [blame] | 63 | #ifndef OBJECT_CREATION_MODE |
| 64 | #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS |
Johannes Schindelin | be66a6c | 2009-04-25 11:57:14 +0200 | [diff] [blame] | 65 | #endif |
Johannes Schindelin | 348df16 | 2009-04-28 00:32:25 +0200 | [diff] [blame] | 66 | enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE; |
Johannes Schindelin | a97a746 | 2009-10-09 12:21:57 +0200 | [diff] [blame] | 67 | char *notes_ref_name; |
Johannes Schindelin | 7f3140c | 2009-07-23 17:33:49 +0200 | [diff] [blame] | 68 | int grafts_replace_parents = 1; |
Derrick Stolee | 1b70dfd | 2018-04-10 08:56:03 -0400 | [diff] [blame] | 69 | int core_commit_graph; |
Nguyễn Thái Ngọc Duy | 08aefc9 | 2009-08-20 20:47:08 +0700 | [diff] [blame] | 70 | int core_apply_sparse_checkout; |
Junio C Hamano | 898eacd | 2011-10-06 23:12:09 -0700 | [diff] [blame] | 71 | int merge_log_config = -1; |
Torsten Bögershausen | 76759c7 | 2012-07-08 15:50:25 +0200 | [diff] [blame] | 72 | int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */ |
Junio C Hamano | 568508e | 2011-10-28 14:48:40 -0700 | [diff] [blame] | 73 | unsigned long pack_size_limit_cfg; |
Johannes Schindelin | f30afda | 2016-05-11 10:43:37 +0200 | [diff] [blame] | 74 | enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY; |
Cornelius Weig | 341fb28 | 2017-01-27 11:09:47 +0100 | [diff] [blame] | 75 | enum log_refs_config log_all_ref_updates = LOG_REFS_UNSET; |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 76 | |
Jeff King | a42643a | 2014-12-15 18:15:20 -0500 | [diff] [blame] | 77 | #ifndef PROTECT_HFS_DEFAULT |
| 78 | #define PROTECT_HFS_DEFAULT 0 |
| 79 | #endif |
| 80 | int protect_hfs = PROTECT_HFS_DEFAULT; |
| 81 | |
Johannes Schindelin | 2b4c6ef | 2014-12-16 23:46:59 +0100 | [diff] [blame] | 82 | #ifndef PROTECT_NTFS_DEFAULT |
| 83 | #define PROTECT_NTFS_DEFAULT 0 |
| 84 | #endif |
| 85 | int protect_ntfs = PROTECT_NTFS_DEFAULT; |
Ben Peart | 883e248 | 2017-09-22 12:35:40 -0400 | [diff] [blame] | 86 | const char *core_fsmonitor; |
Johannes Schindelin | 2b4c6ef | 2014-12-16 23:46:59 +0100 | [diff] [blame] | 87 | |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 88 | /* |
| 89 | * The character that begins a commented line in user-editable file |
| 90 | * that is subject to stripspace. |
| 91 | */ |
| 92 | char comment_line_char = '#'; |
Nguyễn Thái Ngọc Duy | 84c9dc2 | 2014-05-17 08:52:23 +0700 | [diff] [blame] | 93 | int auto_comment_line_char; |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 94 | |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 95 | /* Parallel index stat data preload? */ |
Steve Hoelzer | 299e298 | 2014-06-02 11:43:00 -0500 | [diff] [blame] | 96 | int core_preload_index = 1; |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 97 | |
Christian Couder | dae6c32 | 2016-01-27 07:58:06 +0100 | [diff] [blame] | 98 | /* |
| 99 | * This is a hack for test programs like test-dump-untracked-cache to |
| 100 | * ensure that they do not modify the untracked cache when reading it. |
| 101 | * Do not use it otherwise! |
| 102 | */ |
| 103 | int ignore_untracked_cache_config; |
| 104 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 105 | /* This is set by setup_git_dir_gently() and/or git_default_config() */ |
| 106 | char *git_work_tree_cfg; |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 107 | |
Brandon Williams | 38f3f09 | 2018-02-14 10:59:55 -0800 | [diff] [blame] | 108 | static char *git_namespace; |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 109 | |
Brandon Williams | 74866d7 | 2016-10-07 11:18:48 -0700 | [diff] [blame] | 110 | static const char *super_prefix; |
| 111 | |
Giuseppe Bilotta | 48a7c1c | 2010-02-25 00:34:14 +0100 | [diff] [blame] | 112 | /* |
Jeff King | 2163e5d | 2013-03-08 04:29:08 -0500 | [diff] [blame] | 113 | * Repository-local GIT_* environment variables; see cache.h for details. |
Giuseppe Bilotta | 48a7c1c | 2010-02-25 00:34:14 +0100 | [diff] [blame] | 114 | */ |
Jeff King | 2163e5d | 2013-03-08 04:29:08 -0500 | [diff] [blame] | 115 | const char * const local_repo_env[] = { |
Giuseppe Bilotta | 48a7c1c | 2010-02-25 00:34:14 +0100 | [diff] [blame] | 116 | ALTERNATE_DB_ENVIRONMENT, |
| 117 | CONFIG_ENVIRONMENT, |
Jonathan Nieder | 655e8d9 | 2010-08-24 01:41:14 -0500 | [diff] [blame] | 118 | CONFIG_DATA_ENVIRONMENT, |
Giuseppe Bilotta | 48a7c1c | 2010-02-25 00:34:14 +0100 | [diff] [blame] | 119 | DB_ENVIRONMENT, |
| 120 | GIT_DIR_ENVIRONMENT, |
| 121 | GIT_WORK_TREE_ENVIRONMENT, |
Jeff King | 2cd83d1 | 2013-03-08 04:32:22 -0500 | [diff] [blame] | 122 | GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, |
Giuseppe Bilotta | 48a7c1c | 2010-02-25 00:34:14 +0100 | [diff] [blame] | 123 | GRAFT_ENVIRONMENT, |
| 124 | INDEX_ENVIRONMENT, |
| 125 | NO_REPLACE_OBJECTS_ENVIRONMENT, |
Mike Hommey | 58d121b | 2015-06-12 06:34:59 +0900 | [diff] [blame] | 126 | GIT_REPLACE_REF_BASE_ENVIRONMENT, |
Jeff King | a6f7f9a | 2013-03-08 04:30:25 -0500 | [diff] [blame] | 127 | GIT_PREFIX_ENVIRONMENT, |
Brandon Williams | 74866d7 | 2016-10-07 11:18:48 -0700 | [diff] [blame] | 128 | GIT_SUPER_PREFIX_ENVIRONMENT, |
Nguyễn Thái Ngọc Duy | 069c053 | 2013-12-05 20:02:45 +0700 | [diff] [blame] | 129 | GIT_SHALLOW_FILE_ENVIRONMENT, |
Nguyễn Thái Ngọc Duy | c7b3a3d | 2014-11-30 15:24:36 +0700 | [diff] [blame] | 130 | GIT_COMMON_DIR_ENVIRONMENT, |
Giuseppe Bilotta | 48a7c1c | 2010-02-25 00:34:14 +0100 | [diff] [blame] | 131 | NULL |
| 132 | }; |
| 133 | |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 134 | static char *expand_namespace(const char *raw_namespace) |
| 135 | { |
| 136 | struct strbuf buf = STRBUF_INIT; |
| 137 | struct strbuf **components, **c; |
| 138 | |
| 139 | if (!raw_namespace || !*raw_namespace) |
| 140 | return xstrdup(""); |
| 141 | |
| 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); |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 149 | if (check_refname_format(buf.buf, 0)) |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 150 | die("bad git namespace path \"%s\"", raw_namespace); |
| 151 | strbuf_addch(&buf, '/'); |
| 152 | return strbuf_detach(&buf, NULL); |
| 153 | } |
| 154 | |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 155 | /* |
| 156 | * Wrapper of getenv() that returns a strdup value. This value is kept |
| 157 | * in argv to be freed later. |
| 158 | */ |
| 159 | static const char *getenv_safe(struct argv_array *argv, const char *name) |
| 160 | { |
| 161 | const char *value = getenv(name); |
| 162 | |
| 163 | if (!value) |
| 164 | return NULL; |
| 165 | |
| 166 | argv_array_push(argv, value); |
| 167 | return argv->argv[argv->argc - 1]; |
| 168 | } |
| 169 | |
| 170 | void setup_git_env(const char *git_dir) |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 171 | { |
Nguyễn Thái Ngọc Duy | 069c053 | 2013-12-05 20:02:45 +0700 | [diff] [blame] | 172 | const char *shallow_file; |
Mike Hommey | 58d121b | 2015-06-12 06:34:59 +0900 | [diff] [blame] | 173 | const char *replace_ref_base; |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 174 | struct set_gitdir_args args = { NULL }; |
| 175 | struct argv_array to_free = ARGV_ARRAY_INIT; |
| 176 | |
| 177 | args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT); |
| 178 | args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT); |
| 179 | args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT); |
| 180 | args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT); |
Nguyễn Thái Ngọc Duy | 7bc0dca | 2018-03-03 18:35:57 +0700 | [diff] [blame] | 181 | args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT); |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 182 | repo_set_gitdir(the_repository, git_dir, &args); |
| 183 | argv_array_clear(&to_free); |
Nguyễn Thái Ngọc Duy | 487a2b7 | 2013-08-31 08:04:14 +0700 | [diff] [blame] | 184 | |
Christian Couder | 6476b38 | 2009-11-18 07:50:58 +0100 | [diff] [blame] | 185 | if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT)) |
Michael Haggerty | afc711b | 2014-02-18 12:24:55 +0100 | [diff] [blame] | 186 | check_replace_refs = 0; |
Mike Hommey | 58d121b | 2015-06-12 06:34:59 +0900 | [diff] [blame] | 187 | replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT); |
Jeff King | f9b7573 | 2017-09-05 09:04:57 -0400 | [diff] [blame] | 188 | free(git_replace_ref_base); |
Mike Hommey | 58d121b | 2015-06-12 06:34:59 +0900 | [diff] [blame] | 189 | git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base |
| 190 | : "refs/replace/"); |
Brandon Williams | 38f3f09 | 2018-02-14 10:59:55 -0800 | [diff] [blame] | 191 | free(git_namespace); |
| 192 | git_namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT)); |
Nguyễn Thái Ngọc Duy | 069c053 | 2013-12-05 20:02:45 +0700 | [diff] [blame] | 193 | shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT); |
| 194 | if (shallow_file) |
Stefan Beller | 6a2df51 | 2018-05-17 15:51:43 -0700 | [diff] [blame] | 195 | set_alternate_shallow_file(the_repository, shallow_file, 0); |
Shawn O. Pearce | 0bee591 | 2006-12-14 17:41:17 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 198 | int is_bare_repository(void) |
Shawn O. Pearce | 0bee591 | 2006-12-14 17:41:17 -0500 | [diff] [blame] | 199 | { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 200 | /* if core.bare is not 'false', let's see if there is a work tree */ |
| 201 | return is_bare_repository_cfg && !get_git_work_tree(); |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Jeff King | b9605bc | 2016-09-12 20:24:15 -0700 | [diff] [blame] | 204 | int have_git_dir(void) |
| 205 | { |
| 206 | return startup_info->have_repository |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 207 | || the_repository->gitdir; |
Jeff King | b9605bc | 2016-09-12 20:24:15 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Pierre Habouzit | c5fba16 | 2006-08-23 12:39:11 +0200 | [diff] [blame] | 210 | const char *get_git_dir(void) |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 211 | { |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 212 | if (!the_repository->gitdir) |
Brandon Williams | 73f192c | 2017-06-20 12:19:32 -0700 | [diff] [blame] | 213 | BUG("git environment hasn't been setup"); |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 214 | return the_repository->gitdir; |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Nguyễn Thái Ngọc Duy | c7b3a3d | 2014-11-30 15:24:36 +0700 | [diff] [blame] | 217 | const char *get_git_common_dir(void) |
| 218 | { |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 219 | if (!the_repository->commondir) |
Brandon Williams | 73f192c | 2017-06-20 12:19:32 -0700 | [diff] [blame] | 220 | BUG("git environment hasn't been setup"); |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 221 | return the_repository->commondir; |
Nguyễn Thái Ngọc Duy | c7b3a3d | 2014-11-30 15:24:36 +0700 | [diff] [blame] | 222 | } |
| 223 | |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 224 | const char *get_git_namespace(void) |
| 225 | { |
Brandon Williams | 38f3f09 | 2018-02-14 10:59:55 -0800 | [diff] [blame] | 226 | if (!git_namespace) |
Brandon Williams | 73f192c | 2017-06-20 12:19:32 -0700 | [diff] [blame] | 227 | BUG("git environment hasn't been setup"); |
Brandon Williams | 38f3f09 | 2018-02-14 10:59:55 -0800 | [diff] [blame] | 228 | return git_namespace; |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | const char *strip_namespace(const char *namespaced_ref) |
| 232 | { |
Brandon Williams | bf08c8c | 2017-06-20 12:19:34 -0700 | [diff] [blame] | 233 | const char *out; |
| 234 | if (skip_prefix(namespaced_ref, get_git_namespace(), &out)) |
| 235 | return out; |
| 236 | return NULL; |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Brandon Williams | 74866d7 | 2016-10-07 11:18:48 -0700 | [diff] [blame] | 239 | const char *get_super_prefix(void) |
| 240 | { |
| 241 | static int initialized; |
| 242 | if (!initialized) { |
| 243 | super_prefix = getenv(GIT_SUPER_PREFIX_ENVIRONMENT); |
| 244 | initialized = 1; |
| 245 | } |
| 246 | return super_prefix; |
| 247 | } |
| 248 | |
Daniel Barkalow | 19757d8 | 2008-04-27 13:39:21 -0400 | [diff] [blame] | 249 | static int git_work_tree_initialized; |
| 250 | |
| 251 | /* |
| 252 | * Note. This works only before you used a work tree. This was added |
| 253 | * primarily to support git-clone to work in a new repository it just |
| 254 | * created, and is not meant to flip between different work trees. |
| 255 | */ |
| 256 | void set_git_work_tree(const char *new_work_tree) |
| 257 | { |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 258 | if (git_work_tree_initialized) { |
Carlos Martín Nieto | e2a57aa | 2011-03-17 12:26:46 +0100 | [diff] [blame] | 259 | new_work_tree = real_path(new_work_tree); |
Brandon Williams | b415873 | 2017-06-22 11:43:34 -0700 | [diff] [blame] | 260 | if (strcmp(new_work_tree, the_repository->worktree)) |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 261 | die("internal error: work tree has already been set\n" |
| 262 | "Current worktree: %s\nNew worktree: %s", |
Brandon Williams | b415873 | 2017-06-22 11:43:34 -0700 | [diff] [blame] | 263 | the_repository->worktree, new_work_tree); |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 264 | return; |
| 265 | } |
Daniel Barkalow | 19757d8 | 2008-04-27 13:39:21 -0400 | [diff] [blame] | 266 | git_work_tree_initialized = 1; |
Brandon Williams | b415873 | 2017-06-22 11:43:34 -0700 | [diff] [blame] | 267 | repo_set_worktree(the_repository, new_work_tree); |
Daniel Barkalow | 19757d8 | 2008-04-27 13:39:21 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 270 | const char *get_git_work_tree(void) |
| 271 | { |
Brandon Williams | b415873 | 2017-06-22 11:43:34 -0700 | [diff] [blame] | 272 | return the_repository->worktree; |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 273 | } |
| 274 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 275 | char *get_object_directory(void) |
| 276 | { |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 277 | if (!the_repository->objects->objectdir) |
Brandon Williams | 73f192c | 2017-06-20 12:19:32 -0700 | [diff] [blame] | 278 | BUG("git environment hasn't been setup"); |
Stefan Beller | 90c6215 | 2018-03-23 18:20:55 +0100 | [diff] [blame] | 279 | return the_repository->objects->objectdir; |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Brandon Williams | a63b5fc | 2018-02-14 10:59:53 -0800 | [diff] [blame] | 282 | int odb_mkstemp(struct strbuf *temp_filename, const char *pattern) |
Jonathan Nieder | 463db9b | 2010-11-06 06:45:38 -0500 | [diff] [blame] | 283 | { |
| 284 | int fd; |
| 285 | /* |
| 286 | * we let the umask do its job, don't try to be more |
| 287 | * restrictive except to remove write permission. |
| 288 | */ |
| 289 | int mode = 0444; |
Brandon Williams | a63b5fc | 2018-02-14 10:59:53 -0800 | [diff] [blame] | 290 | git_path_buf(temp_filename, "objects/%s", pattern); |
| 291 | fd = git_mkstemp_mode(temp_filename->buf, mode); |
Jonathan Nieder | 463db9b | 2010-11-06 06:45:38 -0500 | [diff] [blame] | 292 | if (0 <= fd) |
| 293 | return fd; |
| 294 | |
| 295 | /* slow path */ |
Brandon Williams | a63b5fc | 2018-02-14 10:59:53 -0800 | [diff] [blame] | 296 | /* some mkstemp implementations erase temp_filename on failure */ |
| 297 | git_path_buf(temp_filename, "objects/%s", pattern); |
| 298 | safe_create_leading_directories(temp_filename->buf); |
| 299 | return xmkstemp_mode(temp_filename->buf, mode); |
Jonathan Nieder | 463db9b | 2010-11-06 06:45:38 -0500 | [diff] [blame] | 300 | } |
| 301 | |
Jeff King | eaeefc3 | 2017-03-16 10:27:12 -0400 | [diff] [blame] | 302 | int odb_pack_keep(const char *name) |
Jonathan Nieder | 463db9b | 2010-11-06 06:45:38 -0500 | [diff] [blame] | 303 | { |
| 304 | int fd; |
| 305 | |
Jonathan Nieder | 463db9b | 2010-11-06 06:45:38 -0500 | [diff] [blame] | 306 | fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600); |
| 307 | if (0 <= fd) |
| 308 | return fd; |
| 309 | |
| 310 | /* slow path */ |
Jeff King | eaeefc3 | 2017-03-16 10:27:12 -0400 | [diff] [blame] | 311 | safe_create_leading_directories_const(name); |
Jonathan Nieder | 463db9b | 2010-11-06 06:45:38 -0500 | [diff] [blame] | 312 | return open(name, O_RDWR|O_CREAT|O_EXCL, 0600); |
| 313 | } |
| 314 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 315 | char *get_index_file(void) |
| 316 | { |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 317 | if (!the_repository->index_file) |
Brandon Williams | 73f192c | 2017-06-20 12:19:32 -0700 | [diff] [blame] | 318 | BUG("git environment hasn't been setup"); |
Brandon Williams | c14c234 | 2017-06-22 11:43:33 -0700 | [diff] [blame] | 319 | return the_repository->index_file; |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Stefan Beller | 0437a2e | 2018-05-17 15:51:50 -0700 | [diff] [blame] | 322 | char *get_graft_file(struct repository *r) |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 323 | { |
Stefan Beller | 0437a2e | 2018-05-17 15:51:50 -0700 | [diff] [blame] | 324 | if (!r->graft_file) |
Brandon Williams | 73f192c | 2017-06-20 12:19:32 -0700 | [diff] [blame] | 325 | BUG("git environment hasn't been setup"); |
Stefan Beller | 0437a2e | 2018-05-17 15:51:50 -0700 | [diff] [blame] | 326 | return r->graft_file; |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 327 | } |
Johannes Schindelin | d7ac12b | 2007-08-01 01:29:38 +0100 | [diff] [blame] | 328 | |
Jeff King | 8500e0d | 2018-03-30 14:35:08 -0400 | [diff] [blame] | 329 | static void set_git_dir_1(const char *path) |
Johannes Schindelin | d7ac12b | 2007-08-01 01:29:38 +0100 | [diff] [blame] | 330 | { |
| 331 | if (setenv(GIT_DIR_ENVIRONMENT, path, 1)) |
Jeff King | 48988c4 | 2018-03-30 14:34:46 -0400 | [diff] [blame] | 332 | die("could not set GIT_DIR to '%s'", path); |
Nguyễn Thái Ngọc Duy | 357a03e | 2018-03-03 18:35:55 +0700 | [diff] [blame] | 333 | setup_git_env(path); |
Johannes Schindelin | d7ac12b | 2007-08-01 01:29:38 +0100 | [diff] [blame] | 334 | } |
Pat Notz | a6fa599 | 2010-11-02 13:59:07 -0600 | [diff] [blame] | 335 | |
Jeff King | 8500e0d | 2018-03-30 14:35:08 -0400 | [diff] [blame] | 336 | static void update_relative_gitdir(const char *name, |
| 337 | const char *old_cwd, |
| 338 | const char *new_cwd, |
| 339 | void *data) |
| 340 | { |
| 341 | char *path = reparent_relative_path(old_cwd, new_cwd, get_git_dir()); |
| 342 | trace_printf_key(&trace_setup_key, |
| 343 | "setup: move $GIT_DIR to '%s'", |
| 344 | path); |
| 345 | set_git_dir_1(path); |
| 346 | free(path); |
| 347 | } |
| 348 | |
| 349 | void set_git_dir(const char *path) |
| 350 | { |
| 351 | set_git_dir_1(path); |
| 352 | if (!is_absolute_path(path)) |
| 353 | chdir_notify_register(NULL, update_relative_gitdir, NULL); |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 354 | } |
Pat Notz | a6fa599 | 2010-11-02 13:59:07 -0600 | [diff] [blame] | 355 | |
| 356 | const char *get_log_output_encoding(void) |
| 357 | { |
| 358 | return git_log_output_encoding ? git_log_output_encoding |
| 359 | : get_commit_output_encoding(); |
| 360 | } |
| 361 | |
| 362 | const char *get_commit_output_encoding(void) |
| 363 | { |
| 364 | return git_commit_encoding ? git_commit_encoding : "UTF-8"; |
| 365 | } |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 366 | |
| 367 | static int the_shared_repository = PERM_UMASK; |
Jeff King | ae5f677 | 2016-03-11 17:36:53 -0500 | [diff] [blame] | 368 | static int need_shared_repository_from_config = 1; |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 369 | |
| 370 | void set_shared_repository(int value) |
| 371 | { |
| 372 | the_shared_repository = value; |
Jeff King | ae5f677 | 2016-03-11 17:36:53 -0500 | [diff] [blame] | 373 | need_shared_repository_from_config = 0; |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | int get_shared_repository(void) |
| 377 | { |
Jeff King | ae5f677 | 2016-03-11 17:36:53 -0500 | [diff] [blame] | 378 | if (need_shared_repository_from_config) { |
| 379 | const char *var = "core.sharedrepository"; |
| 380 | const char *value; |
| 381 | if (!git_config_get_value(var, &value)) |
| 382 | the_shared_repository = git_config_perm(var, value); |
| 383 | need_shared_repository_from_config = 0; |
| 384 | } |
Jeff King | 7875acb | 2016-03-11 17:36:49 -0500 | [diff] [blame] | 385 | return the_shared_repository; |
| 386 | } |
Jeff King | 4543926 | 2016-09-12 20:24:23 -0700 | [diff] [blame] | 387 | |
| 388 | void reset_shared_repository(void) |
| 389 | { |
| 390 | need_shared_repository_from_config = 1; |
| 391 | } |
Jeff King | 27344d6 | 2017-09-27 02:54:30 -0400 | [diff] [blame] | 392 | |
| 393 | int use_optional_locks(void) |
| 394 | { |
| 395 | return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1); |
| 396 | } |
Ann T Ropea | a2cd709 | 2017-12-03 22:27:39 +0100 | [diff] [blame] | 397 | |
| 398 | int print_sha1_ellipsis(void) |
| 399 | { |
| 400 | /* |
| 401 | * Determine if the calling environment contains the variable |
| 402 | * GIT_PRINT_SHA1_ELLIPSIS set to "yes". |
| 403 | */ |
| 404 | static int cached_result = -1; /* unknown */ |
| 405 | |
| 406 | if (cached_result < 0) { |
| 407 | const char *v = getenv("GIT_PRINT_SHA1_ELLIPSIS"); |
| 408 | cached_result = (v && !strcasecmp(v, "yes")); |
| 409 | } |
| 410 | return cached_result; |
| 411 | } |