THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

410 lines
11KB

  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 "branch.h"
  12. #include "repository.h"
  13. #include "config.h"
  14. #include "refs.h"
  15. #include "fmt-merge-msg.h"
  16. #include "commit.h"
  17. #include "argv-array.h"
  18. #include "object-store.h"
  19. #include "chdir-notify.h"
  20. int trust_executable_bit = 1;
  21. int trust_ctime = 1;
  22. int check_stat = 1;
  23. int has_symlinks = 1;
  24. int minimum_abbrev = 4, default_abbrev = -1;
  25. int ignore_case;
  26. int assume_unchanged;
  27. int prefer_symlink_refs;
  28. int is_bare_repository_cfg = -1; /* unspecified */
  29. int warn_ambiguous_refs = 1;
  30. int warn_on_object_refname_ambiguity = 1;
  31. int ref_paranoia = -1;
  32. int repository_format_precious_objects;
  33. int repository_format_worktree_config;
  34. const char *git_commit_encoding;
  35. const char *git_log_output_encoding;
  36. const char *apply_default_whitespace;
  37. const char *apply_default_ignorewhitespace;
  38. const char *git_attributes_file;
  39. const char *git_hooks_path;
  40. int zlib_compression_level = Z_BEST_SPEED;
  41. int core_compression_level;
  42. int pack_compression_level = Z_DEFAULT_COMPRESSION;
  43. int fsync_object_files;
  44. size_t packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE;
  45. size_t packed_git_limit = DEFAULT_PACKED_GIT_LIMIT;
  46. size_t delta_base_cache_limit = 96 * 1024 * 1024;
  47. unsigned long big_file_threshold = 512 * 1024 * 1024;
  48. int pager_use_color = 1;
  49. const char *editor_program;
  50. const char *askpass_program;
  51. const char *excludes_file;
  52. enum auto_crlf auto_crlf = AUTO_CRLF_FALSE;
  53. int read_replace_refs = 1;
  54. char *git_replace_ref_base;
  55. enum eol core_eol = EOL_UNSET;
  56. int global_conv_flags_eol = CONV_EOL_RNDTRP_WARN;
  57. char *check_roundtrip_encoding = "SHIFT-JIS";
  58. unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
  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. char *notes_ref_name;
  67. int grafts_replace_parents = 1;
  68. int core_apply_sparse_checkout;
  69. int merge_log_config = -1;
  70. int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
  71. unsigned long pack_size_limit_cfg;
  72. 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;
  77. #ifndef PROTECT_NTFS_DEFAULT
  78. #define PROTECT_NTFS_DEFAULT 0
  79. #endif
  80. int protect_ntfs = PROTECT_NTFS_DEFAULT;
  81. const char *core_fsmonitor;
  82. /*
  83. * The character that begins a commented line in user-editable file
  84. * that is subject to stripspace.
  85. */
  86. char comment_line_char = '#';
  87. int auto_comment_line_char;
  88. /* Parallel index stat data preload? */
  89. int core_preload_index = 1;
  90. /*
  91. * This is a hack for test programs like test-dump-untracked-cache to
  92. * ensure that they do not modify the untracked cache when reading it.
  93. * Do not use it otherwise!
  94. */
  95. int ignore_untracked_cache_config;
  96. /* This is set by setup_git_dir_gently() and/or git_default_config() */
  97. char *git_work_tree_cfg;
  98. static char *git_namespace;
  99. static char *super_prefix;
  100. /*
  101. * Repository-local GIT_* environment variables; see cache.h for details.
  102. */
  103. const char * const local_repo_env[] = {
  104. ALTERNATE_DB_ENVIRONMENT,
  105. CONFIG_ENVIRONMENT,
  106. CONFIG_DATA_ENVIRONMENT,
  107. DB_ENVIRONMENT,
  108. GIT_DIR_ENVIRONMENT,
  109. GIT_WORK_TREE_ENVIRONMENT,
  110. GIT_IMPLICIT_WORK_TREE_ENVIRONMENT,
  111. GRAFT_ENVIRONMENT,
  112. INDEX_ENVIRONMENT,
  113. NO_REPLACE_OBJECTS_ENVIRONMENT,
  114. GIT_REPLACE_REF_BASE_ENVIRONMENT,
  115. GIT_PREFIX_ENVIRONMENT,
  116. GIT_SUPER_PREFIX_ENVIRONMENT,
  117. GIT_SHALLOW_FILE_ENVIRONMENT,
  118. GIT_COMMON_DIR_ENVIRONMENT,
  119. NULL
  120. };
  121. static char *expand_namespace(const char *raw_namespace)
  122. {
  123. struct strbuf buf = STRBUF_INIT;
  124. struct strbuf **components, **c;
  125. if (!raw_namespace || !*raw_namespace)
  126. return xstrdup("");
  127. strbuf_addstr(&buf, raw_namespace);
  128. components = strbuf_split(&buf, '/');
  129. strbuf_reset(&buf);
  130. for (c = components; *c; c++)
  131. if (strcmp((*c)->buf, "/") != 0)
  132. strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
  133. strbuf_list_free(components);
  134. if (check_refname_format(buf.buf, 0))
  135. die(_("bad git namespace path \"%s\""), raw_namespace);
  136. strbuf_addch(&buf, '/');
  137. return strbuf_detach(&buf, NULL);
  138. }
  139. /*
  140. * Wrapper of getenv() that returns a strdup value. This value is kept
  141. * in argv to be freed later.
  142. */
  143. static const char *getenv_safe(struct argv_array *argv, const char *name)
  144. {
  145. const char *value = getenv(name);
  146. if (!value)
  147. return NULL;
  148. argv_array_push(argv, value);
  149. return argv->argv[argv->argc - 1];
  150. }
  151. void setup_git_env(const char *git_dir)
  152. {
  153. const char *shallow_file;
  154. const char *replace_ref_base;
  155. struct set_gitdir_args args = { NULL };
  156. struct argv_array to_free = ARGV_ARRAY_INIT;
  157. args.commondir = getenv_safe(&to_free, GIT_COMMON_DIR_ENVIRONMENT);
  158. args.object_dir = getenv_safe(&to_free, DB_ENVIRONMENT);
  159. args.graft_file = getenv_safe(&to_free, GRAFT_ENVIRONMENT);
  160. args.index_file = getenv_safe(&to_free, INDEX_ENVIRONMENT);
  161. args.alternate_db = getenv_safe(&to_free, ALTERNATE_DB_ENVIRONMENT);
  162. repo_set_gitdir(the_repository, git_dir, &args);
  163. argv_array_clear(&to_free);
  164. if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
  165. read_replace_refs = 0;
  166. replace_ref_base = getenv(GIT_REPLACE_REF_BASE_ENVIRONMENT);
  167. free(git_replace_ref_base);
  168. git_replace_ref_base = xstrdup(replace_ref_base ? replace_ref_base
  169. : "refs/replace/");
  170. free(git_namespace);
  171. git_namespace = expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT));
  172. shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
  173. if (shallow_file)
  174. set_alternate_shallow_file(the_repository, shallow_file, 0);
  175. }
  176. int is_bare_repository(void)
  177. {
  178. /* if core.bare is not 'false', let's see if there is a work tree */
  179. return is_bare_repository_cfg && !get_git_work_tree();
  180. }
  181. int have_git_dir(void)
  182. {
  183. return startup_info->have_repository
  184. || the_repository->gitdir;
  185. }
  186. const char *get_git_dir(void)
  187. {
  188. if (!the_repository->gitdir)
  189. BUG("git environment hasn't been setup");
  190. return the_repository->gitdir;
  191. }
  192. const char *get_git_common_dir(void)
  193. {
  194. if (!the_repository->commondir)
  195. BUG("git environment hasn't been setup");
  196. return the_repository->commondir;
  197. }
  198. const char *get_git_namespace(void)
  199. {
  200. if (!git_namespace)
  201. BUG("git environment hasn't been setup");
  202. return git_namespace;
  203. }
  204. const char *strip_namespace(const char *namespaced_ref)
  205. {
  206. const char *out;
  207. if (skip_prefix(namespaced_ref, get_git_namespace(), &out))
  208. return out;
  209. return NULL;
  210. }
  211. const char *get_super_prefix(void)
  212. {
  213. static int initialized;
  214. if (!initialized) {
  215. super_prefix = xstrdup_or_null(getenv(GIT_SUPER_PREFIX_ENVIRONMENT));
  216. initialized = 1;
  217. }
  218. return super_prefix;
  219. }
  220. static int git_work_tree_initialized;
  221. /*
  222. * Note. This works only before you used a work tree. This was added
  223. * primarily to support git-clone to work in a new repository it just
  224. * created, and is not meant to flip between different work trees.
  225. */
  226. void set_git_work_tree(const char *new_work_tree)
  227. {
  228. if (git_work_tree_initialized) {
  229. new_work_tree = real_path(new_work_tree);
  230. if (strcmp(new_work_tree, the_repository->worktree))
  231. die("internal error: work tree has already been set\n"
  232. "Current worktree: %s\nNew worktree: %s",
  233. the_repository->worktree, new_work_tree);
  234. return;
  235. }
  236. git_work_tree_initialized = 1;
  237. repo_set_worktree(the_repository, new_work_tree);
  238. }
  239. const char *get_git_work_tree(void)
  240. {
  241. return the_repository->worktree;
  242. }
  243. char *get_object_directory(void)
  244. {
  245. if (!the_repository->objects->odb)
  246. BUG("git environment hasn't been setup");
  247. return the_repository->objects->odb->path;
  248. }
  249. int odb_mkstemp(struct strbuf *temp_filename, const char *pattern)
  250. {
  251. int fd;
  252. /*
  253. * we let the umask do its job, don't try to be more
  254. * restrictive except to remove write permission.
  255. */
  256. int mode = 0444;
  257. git_path_buf(temp_filename, "objects/%s", pattern);
  258. fd = git_mkstemp_mode(temp_filename->buf, mode);
  259. if (0 <= fd)
  260. return fd;
  261. /* slow path */
  262. /* some mkstemp implementations erase temp_filename on failure */
  263. git_path_buf(temp_filename, "objects/%s", pattern);
  264. safe_create_leading_directories(temp_filename->buf);
  265. return xmkstemp_mode(temp_filename->buf, mode);
  266. }
  267. int odb_pack_keep(const char *name)
  268. {
  269. int fd;
  270. fd = open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
  271. if (0 <= fd)
  272. return fd;
  273. /* slow path */
  274. safe_create_leading_directories_const(name);
  275. return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
  276. }
  277. char *get_index_file(void)
  278. {
  279. if (!the_repository->index_file)
  280. BUG("git environment hasn't been setup");
  281. return the_repository->index_file;
  282. }
  283. char *get_graft_file(struct repository *r)
  284. {
  285. if (!r->graft_file)
  286. BUG("git environment hasn't been setup");
  287. return r->graft_file;
  288. }
  289. static void set_git_dir_1(const char *path)
  290. {
  291. if (setenv(GIT_DIR_ENVIRONMENT, path, 1))
  292. die(_("could not set GIT_DIR to '%s'"), path);
  293. setup_git_env(path);
  294. }
  295. static void update_relative_gitdir(const char *name,
  296. const char *old_cwd,
  297. const char *new_cwd,
  298. void *data)
  299. {
  300. char *path = reparent_relative_path(old_cwd, new_cwd, get_git_dir());
  301. trace_printf_key(&trace_setup_key,
  302. "setup: move $GIT_DIR to '%s'",
  303. path);
  304. set_git_dir_1(path);
  305. free(path);
  306. }
  307. void set_git_dir(const char *path)
  308. {
  309. set_git_dir_1(path);
  310. if (!is_absolute_path(path))
  311. chdir_notify_register(NULL, update_relative_gitdir, NULL);
  312. }
  313. const char *get_log_output_encoding(void)
  314. {
  315. return git_log_output_encoding ? git_log_output_encoding
  316. : get_commit_output_encoding();
  317. }
  318. const char *get_commit_output_encoding(void)
  319. {
  320. return git_commit_encoding ? git_commit_encoding : "UTF-8";
  321. }
  322. static int the_shared_repository = PERM_UMASK;
  323. static int need_shared_repository_from_config = 1;
  324. void set_shared_repository(int value)
  325. {
  326. the_shared_repository = value;
  327. need_shared_repository_from_config = 0;
  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. void reset_shared_repository(void)
  341. {
  342. need_shared_repository_from_config = 1;
  343. }
  344. int use_optional_locks(void)
  345. {
  346. return git_env_bool(GIT_OPTIONAL_LOCKS_ENVIRONMENT, 1);
  347. }
  348. int print_sha1_ellipsis(void)
  349. {
  350. /*
  351. * Determine if the calling environment contains the variable
  352. * GIT_PRINT_SHA1_ELLIPSIS set to "yes".
  353. */
  354. static int cached_result = -1; /* unknown */
  355. if (cached_result < 0) {
  356. const char *v = getenv("GIT_PRINT_SHA1_ELLIPSIS");
  357. cached_result = (v && !strcasecmp(v, "yes"));
  358. }
  359. return cached_result;
  360. }