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" |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 11 | #include "refs.h" |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 12 | |
| 13 | char git_default_email[MAX_GITNAME]; |
| 14 | char git_default_name[MAX_GITNAME]; |
Santi Béjar | bb1ae3f | 2008-05-04 18:04:51 +0200 | [diff] [blame] | 15 | int user_ident_explicitly_given; |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 16 | int trust_executable_bit = 1; |
Alex Riesen | 1ce4790 | 2008-07-28 08:31:28 +0200 | [diff] [blame] | 17 | int trust_ctime = 1; |
Johannes Sixt | 78a8d64 | 2007-03-02 22:11:30 +0100 | [diff] [blame] | 18 | int has_symlinks = 1; |
Linus Torvalds | dce9648 | 2010-10-28 11:28:04 -0700 | [diff] [blame] | 19 | int minimum_abbrev = 4, default_abbrev = 7; |
Linus Torvalds | 0a9b88b | 2008-03-21 16:52:46 -0700 | [diff] [blame] | 20 | int ignore_case; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 21 | int assume_unchanged; |
| 22 | int prefer_symlink_refs; |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 23 | int is_bare_repository_cfg = -1; /* unspecified */ |
Junio C Hamano | 510c5a8 | 2007-01-07 01:35:34 -0800 | [diff] [blame] | 24 | int log_all_ref_updates = -1; /* unspecified */ |
Junio C Hamano | 1b371f5 | 2006-03-23 23:42:40 -0800 | [diff] [blame] | 25 | int warn_ambiguous_refs = 1; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 26 | int repository_format_version; |
Shawn O. Pearce | 1a8f274 | 2007-03-12 15:33:18 -0400 | [diff] [blame] | 27 | const char *git_commit_encoding; |
Shawn O. Pearce | 3a55602 | 2007-03-06 20:44:17 -0500 | [diff] [blame] | 28 | const char *git_log_output_encoding; |
Junio C Hamano | 94df250 | 2006-06-09 23:09:49 -0700 | [diff] [blame] | 29 | int shared_repository = PERM_UMASK; |
David Rientjes | 96f1e58 | 2006-08-15 10:23:48 -0700 | [diff] [blame] | 30 | const char *apply_default_whitespace; |
Giuseppe Bilotta | 86c91f9 | 2009-08-04 13:16:49 +0200 | [diff] [blame] | 31 | const char *apply_default_ignorewhitespace; |
Junio C Hamano | 64589a0 | 2011-10-06 13:22:24 -0500 | [diff] [blame] | 32 | const char *git_attributes_file; |
Dana How | 960ccca | 2007-05-09 13:56:50 -0700 | [diff] [blame] | 33 | int zlib_compression_level = Z_BEST_SPEED; |
| 34 | int core_compression_level; |
| 35 | int core_compression_seen; |
Linus Torvalds | aafe9fb | 2008-06-18 15:18:44 -0700 | [diff] [blame] | 36 | int fsync_object_files; |
Shawn O. Pearce | 8c82534 | 2006-12-24 00:46:13 -0500 | [diff] [blame] | 37 | size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE; |
| 38 | size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT; |
Shawn O. Pearce | 18bdec1 | 2007-03-19 01:14:37 -0400 | [diff] [blame] | 39 | size_t delta_base_cache_limit = 16 * 1024 * 1024; |
Junio C Hamano | 1536628 | 2011-04-05 10:44:11 -0700 | [diff] [blame] | 40 | unsigned long big_file_threshold = 512 * 1024 * 1024; |
Junio C Hamano | 5f44324 | 2011-07-06 19:08:55 -0700 | [diff] [blame] | 41 | const char *log_pack_access; |
Christian Couder | 872da32 | 2008-02-16 06:01:11 +0100 | [diff] [blame] | 42 | const char *pager_program; |
Matthias Lederhofer | aa086eb | 2006-07-30 00:27:43 +0200 | [diff] [blame] | 43 | int pager_use_color = 1; |
Christian Couder | ee9601e | 2008-02-16 06:01:41 +0100 | [diff] [blame] | 44 | const char *editor_program; |
Anselm Kruis | d3e7da8 | 2010-08-30 15:38:38 +0200 | [diff] [blame] | 45 | const char *askpass_program; |
Christian Couder | dfb068b | 2008-02-16 06:01:59 +0100 | [diff] [blame] | 46 | const char *excludes_file; |
Eyvind Bernhardsen | fd6cce9 | 2010-05-19 22:43:10 +0200 | [diff] [blame] | 47 | enum auto_crlf auto_crlf = AUTO_CRLF_FALSE; |
Junio C Hamano | e1111ce | 2011-05-15 12:54:53 -0700 | [diff] [blame] | 48 | int read_replace_refs = 1; /* NEEDSWORK: rename to use_replace_refs */ |
Junio C Hamano | ec70f52 | 2011-05-09 12:52:12 -0700 | [diff] [blame] | 49 | enum eol core_eol = EOL_UNSET; |
Steffen Prohaska | 21e5ad5 | 2008-02-06 12:25:58 +0100 | [diff] [blame] | 50 | enum safe_crlf safe_crlf = SAFE_CRLF_WARN; |
Junio C Hamano | cf1b786 | 2007-12-06 00:14:14 -0800 | [diff] [blame] | 51 | unsigned whitespace_rule_cfg = WS_DEFAULT_RULE; |
Jay Soffian | 9ed36cf | 2008-02-19 11:24:37 -0500 | [diff] [blame] | 52 | enum branch_track git_branch_track = BRANCH_TRACK_REMOTE; |
Dustin Sallings | c998ae9 | 2008-05-10 15:36:29 -0700 | [diff] [blame] | 53 | enum rebase_setup_type autorebase = AUTOREBASE_NEVER; |
Junio C Hamano | bba0fd2 | 2009-07-18 17:19:47 -0700 | [diff] [blame] | 54 | enum push_default_type push_default = PUSH_DEFAULT_MATCHING; |
Johannes Schindelin | 348df16 | 2009-04-28 00:32:25 +0200 | [diff] [blame] | 55 | #ifndef OBJECT_CREATION_MODE |
| 56 | #define OBJECT_CREATION_MODE OBJECT_CREATION_USES_HARDLINKS |
Johannes Schindelin | be66a6c | 2009-04-25 11:57:14 +0200 | [diff] [blame] | 57 | #endif |
Johannes Schindelin | 348df16 | 2009-04-28 00:32:25 +0200 | [diff] [blame] | 58 | enum object_creation_mode object_creation_mode = OBJECT_CREATION_MODE; |
Johannes Schindelin | a97a746 | 2009-10-09 12:21:57 +0200 | [diff] [blame] | 59 | char *notes_ref_name; |
Johannes Schindelin | 7f3140c | 2009-07-23 17:33:49 +0200 | [diff] [blame] | 60 | int grafts_replace_parents = 1; |
Nguyễn Thái Ngọc Duy | 08aefc9 | 2009-08-20 20:47:08 +0700 | [diff] [blame] | 61 | int core_apply_sparse_checkout; |
Nguyễn Thái Ngọc Duy | e37c132 | 2010-08-05 21:40:35 -0500 | [diff] [blame] | 62 | struct startup_info *startup_info; |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 63 | |
Linus Torvalds | 671c9b7 | 2008-11-13 16:36:30 -0800 | [diff] [blame] | 64 | /* Parallel index stat data preload? */ |
| 65 | int core_preload_index = 0; |
| 66 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 67 | /* This is set by setup_git_dir_gently() and/or git_default_config() */ |
| 68 | char *git_work_tree_cfg; |
Daniel Barkalow | 19757d8 | 2008-04-27 13:39:21 -0400 | [diff] [blame] | 69 | static char *work_tree; |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 70 | |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 71 | static const char *namespace; |
| 72 | static size_t namespace_len; |
| 73 | |
Pierre Habouzit | c5fba16 | 2006-08-23 12:39:11 +0200 | [diff] [blame] | 74 | static const char *git_dir; |
Jonathan Nieder | 7f7868e | 2010-10-02 03:35:29 -0500 | [diff] [blame] | 75 | static char *git_object_dir, *git_index_file, *git_graft_file; |
Pierre Habouzit | c5fba16 | 2006-08-23 12:39:11 +0200 | [diff] [blame] | 76 | |
Giuseppe Bilotta | 48a7c1c | 2010-02-25 00:34:14 +0100 | [diff] [blame] | 77 | /* |
| 78 | * Repository-local GIT_* environment variables |
| 79 | * Remember to update local_repo_env_size in cache.h when |
| 80 | * the size of the list changes |
| 81 | */ |
| 82 | const char * const local_repo_env[LOCAL_REPO_ENV_SIZE + 1] = { |
| 83 | ALTERNATE_DB_ENVIRONMENT, |
| 84 | CONFIG_ENVIRONMENT, |
Jonathan Nieder | 655e8d9 | 2010-08-24 01:41:14 -0500 | [diff] [blame] | 85 | CONFIG_DATA_ENVIRONMENT, |
Giuseppe Bilotta | 48a7c1c | 2010-02-25 00:34:14 +0100 | [diff] [blame] | 86 | DB_ENVIRONMENT, |
| 87 | GIT_DIR_ENVIRONMENT, |
| 88 | GIT_WORK_TREE_ENVIRONMENT, |
| 89 | GRAFT_ENVIRONMENT, |
| 90 | INDEX_ENVIRONMENT, |
| 91 | NO_REPLACE_OBJECTS_ENVIRONMENT, |
| 92 | NULL |
| 93 | }; |
| 94 | |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 95 | static char *expand_namespace(const char *raw_namespace) |
| 96 | { |
| 97 | struct strbuf buf = STRBUF_INIT; |
| 98 | struct strbuf **components, **c; |
| 99 | |
| 100 | if (!raw_namespace || !*raw_namespace) |
| 101 | return xstrdup(""); |
| 102 | |
| 103 | strbuf_addstr(&buf, raw_namespace); |
| 104 | components = strbuf_split(&buf, '/'); |
| 105 | strbuf_reset(&buf); |
| 106 | for (c = components; *c; c++) |
| 107 | if (strcmp((*c)->buf, "/") != 0) |
| 108 | strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf); |
| 109 | strbuf_list_free(components); |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 110 | if (check_refname_format(buf.buf, 0)) |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 111 | die("bad git namespace path \"%s\"", raw_namespace); |
| 112 | strbuf_addch(&buf, '/'); |
| 113 | return strbuf_detach(&buf, NULL); |
| 114 | } |
| 115 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 116 | static void setup_git_env(void) |
| 117 | { |
| 118 | git_dir = getenv(GIT_DIR_ENVIRONMENT); |
Kirill Smelkov | 15431ca | 2010-11-11 21:08:23 +0300 | [diff] [blame] | 119 | git_dir = git_dir ? xstrdup(git_dir) : NULL; |
Jonathan Nieder | 06f3549 | 2010-10-02 03:36:52 -0500 | [diff] [blame] | 120 | if (!git_dir) { |
Junio C Hamano | 13d6ec9 | 2011-08-22 14:04:56 -0700 | [diff] [blame] | 121 | git_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT); |
Jonathan Nieder | 06f3549 | 2010-10-02 03:36:52 -0500 | [diff] [blame] | 122 | git_dir = git_dir ? xstrdup(git_dir) : NULL; |
| 123 | } |
Lars Hjemli | b44ebb1 | 2008-02-20 23:13:13 +0100 | [diff] [blame] | 124 | if (!git_dir) |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 125 | git_dir = DEFAULT_GIT_DIR_ENVIRONMENT; |
| 126 | git_object_dir = getenv(DB_ENVIRONMENT); |
| 127 | if (!git_object_dir) { |
| 128 | git_object_dir = xmalloc(strlen(git_dir) + 9); |
| 129 | sprintf(git_object_dir, "%s/objects", git_dir); |
| 130 | } |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 131 | git_index_file = getenv(INDEX_ENVIRONMENT); |
| 132 | if (!git_index_file) { |
| 133 | git_index_file = xmalloc(strlen(git_dir) + 7); |
| 134 | sprintf(git_index_file, "%s/index", git_dir); |
| 135 | } |
| 136 | git_graft_file = getenv(GRAFT_ENVIRONMENT); |
| 137 | if (!git_graft_file) |
Alex Riesen | a4f34cb | 2008-10-27 11:22:09 +0100 | [diff] [blame] | 138 | git_graft_file = git_pathdup("info/grafts"); |
Christian Couder | 6476b38 | 2009-11-18 07:50:58 +0100 | [diff] [blame] | 139 | if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT)) |
| 140 | read_replace_refs = 0; |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 141 | namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT)); |
| 142 | namespace_len = strlen(namespace); |
Shawn O. Pearce | 0bee591 | 2006-12-14 17:41:17 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Junio C Hamano | 7d1864c | 2007-01-07 02:00:28 -0800 | [diff] [blame] | 145 | int is_bare_repository(void) |
Shawn O. Pearce | 0bee591 | 2006-12-14 17:41:17 -0500 | [diff] [blame] | 146 | { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 147 | /* if core.bare is not 'false', let's see if there is a work tree */ |
| 148 | return is_bare_repository_cfg && !get_git_work_tree(); |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Dmitry Potapov | d2b0708 | 2008-09-27 12:41:50 +0400 | [diff] [blame] | 151 | int have_git_dir(void) |
| 152 | { |
| 153 | return !!git_dir; |
| 154 | } |
| 155 | |
Pierre Habouzit | c5fba16 | 2006-08-23 12:39:11 +0200 | [diff] [blame] | 156 | const char *get_git_dir(void) |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 157 | { |
| 158 | if (!git_dir) |
| 159 | setup_git_env(); |
| 160 | return git_dir; |
| 161 | } |
| 162 | |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 163 | const char *get_git_namespace(void) |
| 164 | { |
| 165 | if (!namespace) |
| 166 | setup_git_env(); |
| 167 | return namespace; |
| 168 | } |
| 169 | |
| 170 | const char *strip_namespace(const char *namespaced_ref) |
| 171 | { |
| 172 | if (prefixcmp(namespaced_ref, get_git_namespace()) != 0) |
| 173 | return NULL; |
| 174 | return namespaced_ref + namespace_len; |
| 175 | } |
| 176 | |
Daniel Barkalow | 19757d8 | 2008-04-27 13:39:21 -0400 | [diff] [blame] | 177 | static int git_work_tree_initialized; |
| 178 | |
| 179 | /* |
| 180 | * Note. This works only before you used a work tree. This was added |
| 181 | * primarily to support git-clone to work in a new repository it just |
| 182 | * created, and is not meant to flip between different work trees. |
| 183 | */ |
| 184 | void set_git_work_tree(const char *new_work_tree) |
| 185 | { |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 186 | if (git_work_tree_initialized) { |
Carlos Martín Nieto | e2a57aa | 2011-03-17 12:26:46 +0100 | [diff] [blame] | 187 | new_work_tree = real_path(new_work_tree); |
Nguyễn Thái Ngọc Duy | 8351836 | 2010-11-26 22:32:40 +0700 | [diff] [blame] | 188 | if (strcmp(new_work_tree, work_tree)) |
| 189 | die("internal error: work tree has already been set\n" |
| 190 | "Current worktree: %s\nNew worktree: %s", |
| 191 | work_tree, new_work_tree); |
| 192 | return; |
| 193 | } |
Daniel Barkalow | 19757d8 | 2008-04-27 13:39:21 -0400 | [diff] [blame] | 194 | git_work_tree_initialized = 1; |
Carlos Martín Nieto | e2a57aa | 2011-03-17 12:26:46 +0100 | [diff] [blame] | 195 | work_tree = xstrdup(real_path(new_work_tree)); |
Daniel Barkalow | 19757d8 | 2008-04-27 13:39:21 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 198 | const char *get_git_work_tree(void) |
| 199 | { |
Johannes Schindelin | e90fdc3 | 2007-08-01 01:30:14 +0100 | [diff] [blame] | 200 | return work_tree; |
| 201 | } |
| 202 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 203 | char *get_object_directory(void) |
| 204 | { |
| 205 | if (!git_object_dir) |
| 206 | setup_git_env(); |
| 207 | return git_object_dir; |
| 208 | } |
| 209 | |
Jonathan Nieder | 463db9b | 2010-11-06 06:45:38 -0500 | [diff] [blame] | 210 | int odb_mkstemp(char *template, size_t limit, const char *pattern) |
| 211 | { |
| 212 | int fd; |
| 213 | /* |
| 214 | * we let the umask do its job, don't try to be more |
| 215 | * restrictive except to remove write permission. |
| 216 | */ |
| 217 | int mode = 0444; |
| 218 | snprintf(template, limit, "%s/%s", |
| 219 | get_object_directory(), pattern); |
| 220 | fd = git_mkstemp_mode(template, mode); |
| 221 | if (0 <= fd) |
| 222 | return fd; |
| 223 | |
| 224 | /* slow path */ |
| 225 | /* some mkstemp implementations erase template on failure */ |
| 226 | snprintf(template, limit, "%s/%s", |
| 227 | get_object_directory(), pattern); |
| 228 | safe_create_leading_directories(template); |
| 229 | return xmkstemp_mode(template, mode); |
| 230 | } |
| 231 | |
| 232 | int odb_pack_keep(char *name, size_t namesz, unsigned char *sha1) |
| 233 | { |
| 234 | int fd; |
| 235 | |
| 236 | snprintf(name, namesz, "%s/pack/pack-%s.keep", |
| 237 | get_object_directory(), sha1_to_hex(sha1)); |
| 238 | fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600); |
| 239 | if (0 <= fd) |
| 240 | return fd; |
| 241 | |
| 242 | /* slow path */ |
| 243 | safe_create_leading_directories(name); |
| 244 | return open(name, O_RDWR|O_CREAT|O_EXCL, 0600); |
| 245 | } |
| 246 | |
Linus Torvalds | e1b1039 | 2005-10-11 18:47:34 -0700 | [diff] [blame] | 247 | char *get_index_file(void) |
| 248 | { |
| 249 | if (!git_index_file) |
| 250 | setup_git_env(); |
| 251 | return git_index_file; |
| 252 | } |
| 253 | |
| 254 | char *get_graft_file(void) |
| 255 | { |
| 256 | if (!git_graft_file) |
| 257 | setup_git_env(); |
| 258 | return git_graft_file; |
| 259 | } |
Johannes Schindelin | d7ac12b | 2007-08-01 01:29:38 +0100 | [diff] [blame] | 260 | |
| 261 | int set_git_dir(const char *path) |
| 262 | { |
| 263 | if (setenv(GIT_DIR_ENVIRONMENT, path, 1)) |
| 264 | return error("Could not set GIT_DIR to '%s'", path); |
| 265 | setup_git_env(); |
| 266 | return 0; |
| 267 | } |
Pat Notz | a6fa599 | 2010-11-02 13:59:07 -0600 | [diff] [blame] | 268 | |
| 269 | const char *get_log_output_encoding(void) |
| 270 | { |
| 271 | return git_log_output_encoding ? git_log_output_encoding |
| 272 | : get_commit_output_encoding(); |
| 273 | } |
| 274 | |
| 275 | const char *get_commit_output_encoding(void) |
| 276 | { |
| 277 | return git_commit_encoding ? git_commit_encoding : "UTF-8"; |
| 278 | } |