setup.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. #include "cache.h"
  2. #include "repository.h"
  3. #include "config.h"
  4. #include "dir.h"
  5. #include "string-list.h"
  6. #include "chdir-notify.h"
  7. #include "promisor-remote.h"
  8. static int inside_git_dir = -1;
  9. static int inside_work_tree = -1;
  10. static int work_tree_config_is_bogus;
  11. static struct startup_info the_startup_info;
  12. struct startup_info *startup_info = &the_startup_info;
  13. /*
  14. * The input parameter must contain an absolute path, and it must already be
  15. * normalized.
  16. *
  17. * Find the part of an absolute path that lies inside the work tree by
  18. * dereferencing symlinks outside the work tree, for example:
  19. * /dir1/repo/dir2/file (work tree is /dir1/repo) -> dir2/file
  20. * /dir/file (work tree is /) -> dir/file
  21. * /dir/symlink1/symlink2 (symlink1 points to work tree) -> symlink2
  22. * /dir/repolink/file (repolink points to /dir/repo) -> file
  23. * /dir/repo (exactly equal to work tree) -> (empty string)
  24. */
  25. static int abspath_part_inside_repo(char *path)
  26. {
  27. size_t len;
  28. size_t wtlen;
  29. char *path0;
  30. int off;
  31. const char *work_tree = get_git_work_tree();
  32. struct strbuf realpath = STRBUF_INIT;
  33. if (!work_tree)
  34. return -1;
  35. wtlen = strlen(work_tree);
  36. len = strlen(path);
  37. off = offset_1st_component(path);
  38. /* check if work tree is already the prefix */
  39. if (wtlen <= len && !fspathncmp(path, work_tree, wtlen)) {
  40. if (path[wtlen] == '/') {
  41. memmove(path, path + wtlen + 1, len - wtlen);
  42. return 0;
  43. } else if (path[wtlen - 1] == '/' || path[wtlen] == '\0') {
  44. /* work tree is the root, or the whole path */
  45. memmove(path, path + wtlen, len - wtlen + 1);
  46. return 0;
  47. }
  48. /* work tree might match beginning of a symlink to work tree */
  49. off = wtlen;
  50. }
  51. path0 = path;
  52. path += off;
  53. /* check each '/'-terminated level */
  54. while (*path) {
  55. path++;
  56. if (*path == '/') {
  57. *path = '\0';
  58. strbuf_realpath(&realpath, path0, 1);
  59. if (fspathcmp(realpath.buf, work_tree) == 0) {
  60. memmove(path0, path + 1, len - (path - path0));
  61. strbuf_release(&realpath);
  62. return 0;
  63. }
  64. *path = '/';
  65. }
  66. }
  67. /* check whole path */
  68. strbuf_realpath(&realpath, path0, 1);
  69. if (fspathcmp(realpath.buf, work_tree) == 0) {
  70. *path0 = '\0';
  71. strbuf_release(&realpath);
  72. return 0;
  73. }
  74. strbuf_release(&realpath);
  75. return -1;
  76. }
  77. /*
  78. * Normalize "path", prepending the "prefix" for relative paths. If
  79. * remaining_prefix is not NULL, return the actual prefix still
  80. * remains in the path. For example, prefix = sub1/sub2/ and path is
  81. *
  82. * foo -> sub1/sub2/foo (full prefix)
  83. * ../foo -> sub1/foo (remaining prefix is sub1/)
  84. * ../../bar -> bar (no remaining prefix)
  85. * ../../sub1/sub2/foo -> sub1/sub2/foo (but no remaining prefix)
  86. * `pwd`/../bar -> sub1/bar (no remaining prefix)
  87. */
  88. char *prefix_path_gently(const char *prefix, int len,
  89. int *remaining_prefix, const char *path)
  90. {
  91. const char *orig = path;
  92. char *sanitized;
  93. if (is_absolute_path(orig)) {
  94. sanitized = xmallocz(strlen(path));
  95. if (remaining_prefix)
  96. *remaining_prefix = 0;
  97. if (normalize_path_copy_len(sanitized, path, remaining_prefix)) {
  98. free(sanitized);
  99. return NULL;
  100. }
  101. if (abspath_part_inside_repo(sanitized)) {
  102. free(sanitized);
  103. return NULL;
  104. }
  105. } else {
  106. sanitized = xstrfmt("%.*s%s", len, len ? prefix : "", path);
  107. if (remaining_prefix)
  108. *remaining_prefix = len;
  109. if (normalize_path_copy_len(sanitized, sanitized, remaining_prefix)) {
  110. free(sanitized);
  111. return NULL;
  112. }
  113. }
  114. return sanitized;
  115. }
  116. char *prefix_path(const char *prefix, int len, const char *path)
  117. {
  118. char *r = prefix_path_gently(prefix, len, NULL, path);
  119. if (!r) {
  120. const char *hint_path = get_git_work_tree();
  121. if (!hint_path)
  122. hint_path = get_git_dir();
  123. die(_("'%s' is outside repository at '%s'"), path,
  124. absolute_path(hint_path));
  125. }
  126. return r;
  127. }
  128. int path_inside_repo(const char *prefix, const char *path)
  129. {
  130. int len = prefix ? strlen(prefix) : 0;
  131. char *r = prefix_path_gently(prefix, len, NULL, path);
  132. if (r) {
  133. free(r);
  134. return 1;
  135. }
  136. return 0;
  137. }
  138. int check_filename(const char *prefix, const char *arg)
  139. {
  140. char *to_free = NULL;
  141. struct stat st;
  142. if (skip_prefix(arg, ":/", &arg)) {
  143. if (!*arg) /* ":/" is root dir, always exists */
  144. return 1;
  145. prefix = NULL;
  146. } else if (skip_prefix(arg, ":!", &arg) ||
  147. skip_prefix(arg, ":^", &arg)) {
  148. if (!*arg) /* excluding everything is silly, but allowed */
  149. return 1;
  150. }
  151. if (prefix)
  152. arg = to_free = prefix_filename(prefix, arg);
  153. if (!lstat(arg, &st)) {
  154. free(to_free);
  155. return 1; /* file exists */
  156. }
  157. if (is_missing_file_error(errno)) {
  158. free(to_free);
  159. return 0; /* file does not exist */
  160. }
  161. die_errno(_("failed to stat '%s'"), arg);
  162. }
  163. static void NORETURN die_verify_filename(struct repository *r,
  164. const char *prefix,
  165. const char *arg,
  166. int diagnose_misspelt_rev)
  167. {
  168. if (!diagnose_misspelt_rev)
  169. die(_("%s: no such path in the working tree.\n"
  170. "Use 'git <command> -- <path>...' to specify paths that do not exist locally."),
  171. arg);
  172. /*
  173. * Saying "'(icase)foo' does not exist in the index" when the
  174. * user gave us ":(icase)foo" is just stupid. A magic pathspec
  175. * begins with a colon and is followed by a non-alnum; do not
  176. * let maybe_die_on_misspelt_object_name() even trigger.
  177. */
  178. if (!(arg[0] == ':' && !isalnum(arg[1])))
  179. maybe_die_on_misspelt_object_name(r, arg, prefix);
  180. /* ... or fall back the most general message. */
  181. die(_("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
  182. "Use '--' to separate paths from revisions, like this:\n"
  183. "'git <command> [<revision>...] -- [<file>...]'"), arg);
  184. }
  185. /*
  186. * Check for arguments that don't resolve as actual files,
  187. * but which look sufficiently like pathspecs that we'll consider
  188. * them such for the purposes of rev/pathspec DWIM parsing.
  189. */
  190. static int looks_like_pathspec(const char *arg)
  191. {
  192. const char *p;
  193. int escaped = 0;
  194. /*
  195. * Wildcard characters imply the user is looking to match pathspecs
  196. * that aren't in the filesystem. Note that this doesn't include
  197. * backslash even though it's a glob special; by itself it doesn't
  198. * cause any increase in the match. Likewise ignore backslash-escaped
  199. * wildcard characters.
  200. */
  201. for (p = arg; *p; p++) {
  202. if (escaped) {
  203. escaped = 0;
  204. } else if (is_glob_special(*p)) {
  205. if (*p == '\\')
  206. escaped = 1;
  207. else
  208. return 1;
  209. }
  210. }
  211. /* long-form pathspec magic */
  212. if (starts_with(arg, ":("))
  213. return 1;
  214. return 0;
  215. }
  216. /*
  217. * Verify a filename that we got as an argument for a pathspec
  218. * entry. Note that a filename that begins with "-" never verifies
  219. * as true, because even if such a filename were to exist, we want
  220. * it to be preceded by the "--" marker (or we want the user to
  221. * use a format like "./-filename")
  222. *
  223. * The "diagnose_misspelt_rev" is used to provide a user-friendly
  224. * diagnosis when dying upon finding that "name" is not a pathname.
  225. * If set to 1, the diagnosis will try to diagnose "name" as an
  226. * invalid object name (e.g. HEAD:foo). If set to 0, the diagnosis
  227. * will only complain about an inexisting file.
  228. *
  229. * This function is typically called to check that a "file or rev"
  230. * argument is unambiguous. In this case, the caller will want
  231. * diagnose_misspelt_rev == 1 when verifying the first non-rev
  232. * argument (which could have been a revision), and
  233. * diagnose_misspelt_rev == 0 for the next ones (because we already
  234. * saw a filename, there's not ambiguity anymore).
  235. */
  236. void verify_filename(const char *prefix,
  237. const char *arg,
  238. int diagnose_misspelt_rev)
  239. {
  240. if (*arg == '-')
  241. die(_("option '%s' must come before non-option arguments"), arg);
  242. if (looks_like_pathspec(arg) || check_filename(prefix, arg))
  243. return;
  244. die_verify_filename(the_repository, prefix, arg, diagnose_misspelt_rev);
  245. }
  246. /*
  247. * Opposite of the above: the command line did not have -- marker
  248. * and we parsed the arg as a refname. It should not be interpretable
  249. * as a filename.
  250. */
  251. void verify_non_filename(const char *prefix, const char *arg)
  252. {
  253. if (!is_inside_work_tree() || is_inside_git_dir())
  254. return;
  255. if (*arg == '-')
  256. return; /* flag */
  257. if (!check_filename(prefix, arg))
  258. return;
  259. die(_("ambiguous argument '%s': both revision and filename\n"
  260. "Use '--' to separate paths from revisions, like this:\n"
  261. "'git <command> [<revision>...] -- [<file>...]'"), arg);
  262. }
  263. int get_common_dir(struct strbuf *sb, const char *gitdir)
  264. {
  265. const char *git_env_common_dir = getenv(GIT_COMMON_DIR_ENVIRONMENT);
  266. if (git_env_common_dir) {
  267. strbuf_addstr(sb, git_env_common_dir);
  268. return 1;
  269. } else {
  270. return get_common_dir_noenv(sb, gitdir);
  271. }
  272. }
  273. int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
  274. {
  275. struct strbuf data = STRBUF_INIT;
  276. struct strbuf path = STRBUF_INIT;
  277. int ret = 0;
  278. strbuf_addf(&path, "%s/commondir", gitdir);
  279. if (file_exists(path.buf)) {
  280. if (strbuf_read_file(&data, path.buf, 0) <= 0)
  281. die_errno(_("failed to read %s"), path.buf);
  282. while (data.len && (data.buf[data.len - 1] == '\n' ||
  283. data.buf[data.len - 1] == '\r'))
  284. data.len--;
  285. data.buf[data.len] = '\0';
  286. strbuf_reset(&path);
  287. if (!is_absolute_path(data.buf))
  288. strbuf_addf(&path, "%s/", gitdir);
  289. strbuf_addbuf(&path, &data);
  290. strbuf_add_real_path(sb, path.buf);
  291. ret = 1;
  292. } else {
  293. strbuf_addstr(sb, gitdir);
  294. }
  295. strbuf_release(&data);
  296. strbuf_release(&path);
  297. return ret;
  298. }
  299. /*
  300. * Test if it looks like we're at a git directory.
  301. * We want to see:
  302. *
  303. * - either an objects/ directory _or_ the proper
  304. * GIT_OBJECT_DIRECTORY environment variable
  305. * - a refs/ directory
  306. * - either a HEAD symlink or a HEAD file that is formatted as
  307. * a proper "ref:", or a regular file HEAD that has a properly
  308. * formatted sha1 object name.
  309. */
  310. int is_git_directory(const char *suspect)
  311. {
  312. struct strbuf path = STRBUF_INIT;
  313. int ret = 0;
  314. size_t len;
  315. /* Check worktree-related signatures */
  316. strbuf_addstr(&path, suspect);
  317. strbuf_complete(&path, '/');
  318. strbuf_addstr(&path, "HEAD");
  319. if (validate_headref(path.buf))
  320. goto done;
  321. strbuf_reset(&path);
  322. get_common_dir(&path, suspect);
  323. len = path.len;
  324. /* Check non-worktree-related signatures */
  325. if (getenv(DB_ENVIRONMENT)) {
  326. if (access(getenv(DB_ENVIRONMENT), X_OK))
  327. goto done;
  328. }
  329. else {
  330. strbuf_setlen(&path, len);
  331. strbuf_addstr(&path, "/objects");
  332. if (access(path.buf, X_OK))
  333. goto done;
  334. }
  335. strbuf_setlen(&path, len);
  336. strbuf_addstr(&path, "/refs");
  337. if (access(path.buf, X_OK))
  338. goto done;
  339. ret = 1;
  340. done:
  341. strbuf_release(&path);
  342. return ret;
  343. }
  344. int is_nonbare_repository_dir(struct strbuf *path)
  345. {
  346. int ret = 0;
  347. int gitfile_error;
  348. size_t orig_path_len = path->len;
  349. assert(orig_path_len != 0);
  350. strbuf_complete(path, '/');
  351. strbuf_addstr(path, ".git");
  352. if (read_gitfile_gently(path->buf, &gitfile_error) || is_git_directory(path->buf))
  353. ret = 1;
  354. if (gitfile_error == READ_GITFILE_ERR_OPEN_FAILED ||
  355. gitfile_error == READ_GITFILE_ERR_READ_FAILED)
  356. ret = 1;
  357. strbuf_setlen(path, orig_path_len);
  358. return ret;
  359. }
  360. int is_inside_git_dir(void)
  361. {
  362. if (inside_git_dir < 0)
  363. inside_git_dir = is_inside_dir(get_git_dir());
  364. return inside_git_dir;
  365. }
  366. int is_inside_work_tree(void)
  367. {
  368. if (inside_work_tree < 0)
  369. inside_work_tree = is_inside_dir(get_git_work_tree());
  370. return inside_work_tree;
  371. }
  372. void setup_work_tree(void)
  373. {
  374. const char *work_tree;
  375. static int initialized = 0;
  376. if (initialized)
  377. return;
  378. if (work_tree_config_is_bogus)
  379. die(_("unable to set up work tree using invalid config"));
  380. work_tree = get_git_work_tree();
  381. if (!work_tree || chdir_notify(work_tree))
  382. die(_("this operation must be run in a work tree"));
  383. /*
  384. * Make sure subsequent git processes find correct worktree
  385. * if $GIT_WORK_TREE is set relative
  386. */
  387. if (getenv(GIT_WORK_TREE_ENVIRONMENT))
  388. setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
  389. initialized = 1;
  390. }
  391. static int read_worktree_config(const char *var, const char *value, void *vdata)
  392. {
  393. struct repository_format *data = vdata;
  394. if (strcmp(var, "core.bare") == 0) {
  395. data->is_bare = git_config_bool(var, value);
  396. } else if (strcmp(var, "core.worktree") == 0) {
  397. if (!value)
  398. return config_error_nonbool(var);
  399. free(data->work_tree);
  400. data->work_tree = xstrdup(value);
  401. }
  402. return 0;
  403. }
  404. enum extension_result {
  405. EXTENSION_ERROR = -1, /* compatible with error(), etc */
  406. EXTENSION_UNKNOWN = 0,
  407. EXTENSION_OK = 1
  408. };
  409. /*
  410. * Do not add new extensions to this function. It handles extensions which are
  411. * respected even in v0-format repositories for historical compatibility.
  412. */
  413. static enum extension_result handle_extension_v0(const char *var,
  414. const char *value,
  415. const char *ext,
  416. struct repository_format *data)
  417. {
  418. if (!strcmp(ext, "noop")) {
  419. return EXTENSION_OK;
  420. } else if (!strcmp(ext, "preciousobjects")) {
  421. data->precious_objects = git_config_bool(var, value);
  422. return EXTENSION_OK;
  423. } else if (!strcmp(ext, "partialclone")) {
  424. if (!value)
  425. return config_error_nonbool(var);
  426. data->partial_clone = xstrdup(value);
  427. return EXTENSION_OK;
  428. } else if (!strcmp(ext, "worktreeconfig")) {
  429. data->worktree_config = git_config_bool(var, value);
  430. return EXTENSION_OK;
  431. }
  432. return EXTENSION_UNKNOWN;
  433. }
  434. /*
  435. * Record any new extensions in this function.
  436. */
  437. static enum extension_result handle_extension(const char *var,
  438. const char *value,
  439. const char *ext,
  440. struct repository_format *data)
  441. {
  442. if (!strcmp(ext, "noop-v1")) {
  443. return EXTENSION_OK;
  444. } else if (!strcmp(ext, "objectformat")) {
  445. int format;
  446. if (!value)
  447. return config_error_nonbool(var);
  448. format = hash_algo_by_name(value);
  449. if (format == GIT_HASH_UNKNOWN)
  450. return error("invalid value for 'extensions.objectformat'");
  451. data->hash_algo = format;
  452. return EXTENSION_OK;
  453. }
  454. return EXTENSION_UNKNOWN;
  455. }
  456. static int check_repo_format(const char *var, const char *value, void *vdata)
  457. {
  458. struct repository_format *data = vdata;
  459. const char *ext;
  460. if (strcmp(var, "core.repositoryformatversion") == 0)
  461. data->version = git_config_int(var, value);
  462. else if (skip_prefix(var, "extensions.", &ext)) {
  463. switch (handle_extension_v0(var, value, ext, data)) {
  464. case EXTENSION_ERROR:
  465. return -1;
  466. case EXTENSION_OK:
  467. return 0;
  468. case EXTENSION_UNKNOWN:
  469. break;
  470. }
  471. switch (handle_extension(var, value, ext, data)) {
  472. case EXTENSION_ERROR:
  473. return -1;
  474. case EXTENSION_OK:
  475. string_list_append(&data->v1_only_extensions, ext);
  476. return 0;
  477. case EXTENSION_UNKNOWN:
  478. string_list_append(&data->unknown_extensions, ext);
  479. return 0;
  480. }
  481. }
  482. return read_worktree_config(var, value, vdata);
  483. }
  484. static int check_repository_format_gently(const char *gitdir, struct repository_format *candidate, int *nongit_ok)
  485. {
  486. struct strbuf sb = STRBUF_INIT;
  487. struct strbuf err = STRBUF_INIT;
  488. int has_common;
  489. has_common = get_common_dir(&sb, gitdir);
  490. strbuf_addstr(&sb, "/config");
  491. read_repository_format(candidate, sb.buf);
  492. strbuf_release(&sb);
  493. /*
  494. * For historical use of check_repository_format() in git-init,
  495. * we treat a missing config as a silent "ok", even when nongit_ok
  496. * is unset.
  497. */
  498. if (candidate->version < 0)
  499. return 0;
  500. if (verify_repository_format(candidate, &err) < 0) {
  501. if (nongit_ok) {
  502. warning("%s", err.buf);
  503. strbuf_release(&err);
  504. *nongit_ok = -1;
  505. return -1;
  506. }
  507. die("%s", err.buf);
  508. }
  509. repository_format_precious_objects = candidate->precious_objects;
  510. set_repository_format_partial_clone(candidate->partial_clone);
  511. repository_format_worktree_config = candidate->worktree_config;
  512. string_list_clear(&candidate->unknown_extensions, 0);
  513. string_list_clear(&candidate->v1_only_extensions, 0);
  514. if (repository_format_worktree_config) {
  515. /*
  516. * pick up core.bare and core.worktree from per-worktree
  517. * config if present
  518. */
  519. strbuf_addf(&sb, "%s/config.worktree", gitdir);
  520. git_config_from_file(read_worktree_config, sb.buf, candidate);
  521. strbuf_release(&sb);
  522. has_common = 0;
  523. }
  524. if (!has_common) {
  525. if (candidate->is_bare != -1) {
  526. is_bare_repository_cfg = candidate->is_bare;
  527. if (is_bare_repository_cfg == 1)
  528. inside_work_tree = -1;
  529. }
  530. if (candidate->work_tree) {
  531. free(git_work_tree_cfg);
  532. git_work_tree_cfg = xstrdup(candidate->work_tree);
  533. inside_work_tree = -1;
  534. }
  535. }
  536. return 0;
  537. }
  538. int upgrade_repository_format(int target_version)
  539. {
  540. struct strbuf sb = STRBUF_INIT;
  541. struct strbuf err = STRBUF_INIT;
  542. struct strbuf repo_version = STRBUF_INIT;
  543. struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
  544. strbuf_git_common_path(&sb, the_repository, "config");
  545. read_repository_format(&repo_fmt, sb.buf);
  546. strbuf_release(&sb);
  547. if (repo_fmt.version >= target_version)
  548. return 0;
  549. if (verify_repository_format(&repo_fmt, &err) < 0) {
  550. error("cannot upgrade repository format from %d to %d: %s",
  551. repo_fmt.version, target_version, err.buf);
  552. strbuf_release(&err);
  553. return -1;
  554. }
  555. if (!repo_fmt.version && repo_fmt.unknown_extensions.nr)
  556. return error("cannot upgrade repository format: "
  557. "unknown extension %s",
  558. repo_fmt.unknown_extensions.items[0].string);
  559. strbuf_addf(&repo_version, "%d", target_version);
  560. git_config_set("core.repositoryformatversion", repo_version.buf);
  561. strbuf_release(&repo_version);
  562. return 1;
  563. }
  564. static void init_repository_format(struct repository_format *format)
  565. {
  566. const struct repository_format fresh = REPOSITORY_FORMAT_INIT;
  567. memcpy(format, &fresh, sizeof(fresh));
  568. }
  569. int read_repository_format(struct repository_format *format, const char *path)
  570. {
  571. clear_repository_format(format);
  572. git_config_from_file(check_repo_format, path, format);
  573. if (format->version == -1)
  574. clear_repository_format(format);
  575. return format->version;
  576. }
  577. void clear_repository_format(struct repository_format *format)
  578. {
  579. string_list_clear(&format->unknown_extensions, 0);
  580. string_list_clear(&format->v1_only_extensions, 0);
  581. free(format->work_tree);
  582. free(format->partial_clone);
  583. init_repository_format(format);
  584. }
  585. int verify_repository_format(const struct repository_format *format,
  586. struct strbuf *err)
  587. {
  588. if (GIT_REPO_VERSION_READ < format->version) {
  589. strbuf_addf(err, _("Expected git repo version <= %d, found %d"),
  590. GIT_REPO_VERSION_READ, format->version);
  591. return -1;
  592. }
  593. if (format->version >= 1 && format->unknown_extensions.nr) {
  594. int i;
  595. strbuf_addstr(err, _("unknown repository extensions found:"));
  596. for (i = 0; i < format->unknown_extensions.nr; i++)
  597. strbuf_addf(err, "\n\t%s",
  598. format->unknown_extensions.items[i].string);
  599. return -1;
  600. }
  601. if (format->version == 0 && format->v1_only_extensions.nr) {
  602. int i;
  603. strbuf_addstr(err,
  604. _("repo version is 0, but v1-only extensions found:"));
  605. for (i = 0; i < format->v1_only_extensions.nr; i++)
  606. strbuf_addf(err, "\n\t%s",
  607. format->v1_only_extensions.items[i].string);
  608. return -1;
  609. }
  610. return 0;
  611. }
  612. void read_gitfile_error_die(int error_code, const char *path, const char *dir)
  613. {
  614. switch (error_code) {
  615. case READ_GITFILE_ERR_STAT_FAILED:
  616. case READ_GITFILE_ERR_NOT_A_FILE:
  617. /* non-fatal; follow return path */
  618. break;
  619. case READ_GITFILE_ERR_OPEN_FAILED:
  620. die_errno(_("error opening '%s'"), path);
  621. case READ_GITFILE_ERR_TOO_LARGE:
  622. die(_("too large to be a .git file: '%s'"), path);
  623. case READ_GITFILE_ERR_READ_FAILED:
  624. die(_("error reading %s"), path);
  625. case READ_GITFILE_ERR_INVALID_FORMAT:
  626. die(_("invalid gitfile format: %s"), path);
  627. case READ_GITFILE_ERR_NO_PATH:
  628. die(_("no path in gitfile: %s"), path);
  629. case READ_GITFILE_ERR_NOT_A_REPO:
  630. die(_("not a git repository: %s"), dir);
  631. default:
  632. BUG("unknown error code");
  633. }
  634. }
  635. /*
  636. * Try to read the location of the git directory from the .git file,
  637. * return path to git directory if found. The return value comes from
  638. * a shared buffer.
  639. *
  640. * On failure, if return_error_code is not NULL, return_error_code
  641. * will be set to an error code and NULL will be returned. If
  642. * return_error_code is NULL the function will die instead (for most
  643. * cases).
  644. */
  645. const char *read_gitfile_gently(const char *path, int *return_error_code)
  646. {
  647. const int max_file_size = 1 << 20; /* 1MB */
  648. int error_code = 0;
  649. char *buf = NULL;
  650. char *dir = NULL;
  651. const char *slash;
  652. struct stat st;
  653. int fd;
  654. ssize_t len;
  655. static struct strbuf realpath = STRBUF_INIT;
  656. if (stat(path, &st)) {
  657. /* NEEDSWORK: discern between ENOENT vs other errors */
  658. error_code = READ_GITFILE_ERR_STAT_FAILED;
  659. goto cleanup_return;
  660. }
  661. if (!S_ISREG(st.st_mode)) {
  662. error_code = READ_GITFILE_ERR_NOT_A_FILE;
  663. goto cleanup_return;
  664. }
  665. if (st.st_size > max_file_size) {
  666. error_code = READ_GITFILE_ERR_TOO_LARGE;
  667. goto cleanup_return;
  668. }
  669. fd = open(path, O_RDONLY);
  670. if (fd < 0) {
  671. error_code = READ_GITFILE_ERR_OPEN_FAILED;
  672. goto cleanup_return;
  673. }
  674. buf = xmallocz(st.st_size);
  675. len = read_in_full(fd, buf, st.st_size);
  676. close(fd);
  677. if (len != st.st_size) {
  678. error_code = READ_GITFILE_ERR_READ_FAILED;
  679. goto cleanup_return;
  680. }
  681. if (!starts_with(buf, "gitdir: ")) {
  682. error_code = READ_GITFILE_ERR_INVALID_FORMAT;
  683. goto cleanup_return;
  684. }
  685. while (buf[len - 1] == '\n' || buf[len - 1] == '\r')
  686. len--;
  687. if (len < 9) {
  688. error_code = READ_GITFILE_ERR_NO_PATH;
  689. goto cleanup_return;
  690. }
  691. buf[len] = '\0';
  692. dir = buf + 8;
  693. if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) {
  694. size_t pathlen = slash+1 - path;
  695. dir = xstrfmt("%.*s%.*s", (int)pathlen, path,
  696. (int)(len - 8), buf + 8);
  697. free(buf);
  698. buf = dir;
  699. }
  700. if (!is_git_directory(dir)) {
  701. error_code = READ_GITFILE_ERR_NOT_A_REPO;
  702. goto cleanup_return;
  703. }
  704. strbuf_realpath(&realpath, dir, 1);
  705. path = realpath.buf;
  706. cleanup_return:
  707. if (return_error_code)
  708. *return_error_code = error_code;
  709. else if (error_code)
  710. read_gitfile_error_die(error_code, path, dir);
  711. free(buf);
  712. return error_code ? NULL : path;
  713. }
  714. static const char *setup_explicit_git_dir(const char *gitdirenv,
  715. struct strbuf *cwd,
  716. struct repository_format *repo_fmt,
  717. int *nongit_ok)
  718. {
  719. const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
  720. const char *worktree;
  721. char *gitfile;
  722. int offset;
  723. if (PATH_MAX - 40 < strlen(gitdirenv))
  724. die(_("'$%s' too big"), GIT_DIR_ENVIRONMENT);
  725. gitfile = (char*)read_gitfile(gitdirenv);
  726. if (gitfile) {
  727. gitfile = xstrdup(gitfile);
  728. gitdirenv = gitfile;
  729. }
  730. if (!is_git_directory(gitdirenv)) {
  731. if (nongit_ok) {
  732. *nongit_ok = 1;
  733. free(gitfile);
  734. return NULL;
  735. }
  736. die(_("not a git repository: '%s'"), gitdirenv);
  737. }
  738. if (check_repository_format_gently(gitdirenv, repo_fmt, nongit_ok)) {
  739. free(gitfile);
  740. return NULL;
  741. }
  742. /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
  743. if (work_tree_env)
  744. set_git_work_tree(work_tree_env);
  745. else if (is_bare_repository_cfg > 0) {
  746. if (git_work_tree_cfg) {
  747. /* #22.2, #30 */
  748. warning("core.bare and core.worktree do not make sense");
  749. work_tree_config_is_bogus = 1;
  750. }
  751. /* #18, #26 */
  752. set_git_dir(gitdirenv, 0);
  753. free(gitfile);
  754. return NULL;
  755. }
  756. else if (git_work_tree_cfg) { /* #6, #14 */
  757. if (is_absolute_path(git_work_tree_cfg))
  758. set_git_work_tree(git_work_tree_cfg);
  759. else {
  760. char *core_worktree;
  761. if (chdir(gitdirenv))
  762. die_errno(_("cannot chdir to '%s'"), gitdirenv);
  763. if (chdir(git_work_tree_cfg))
  764. die_errno(_("cannot chdir to '%s'"), git_work_tree_cfg);
  765. core_worktree = xgetcwd();
  766. if (chdir(cwd->buf))
  767. die_errno(_("cannot come back to cwd"));
  768. set_git_work_tree(core_worktree);
  769. free(core_worktree);
  770. }
  771. }
  772. else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
  773. /* #16d */
  774. set_git_dir(gitdirenv, 0);
  775. free(gitfile);
  776. return NULL;
  777. }
  778. else /* #2, #10 */
  779. set_git_work_tree(".");
  780. /* set_git_work_tree() must have been called by now */
  781. worktree = get_git_work_tree();
  782. /* both get_git_work_tree() and cwd are already normalized */
  783. if (!strcmp(cwd->buf, worktree)) { /* cwd == worktree */
  784. set_git_dir(gitdirenv, 0);
  785. free(gitfile);
  786. return NULL;
  787. }
  788. offset = dir_inside_of(cwd->buf, worktree);
  789. if (offset >= 0) { /* cwd inside worktree? */
  790. set_git_dir(gitdirenv, 1);
  791. if (chdir(worktree))
  792. die_errno(_("cannot chdir to '%s'"), worktree);
  793. strbuf_addch(cwd, '/');
  794. free(gitfile);
  795. return cwd->buf + offset;
  796. }
  797. /* cwd outside worktree */
  798. set_git_dir(gitdirenv, 0);
  799. free(gitfile);
  800. return NULL;
  801. }
  802. static const char *setup_discovered_git_dir(const char *gitdir,
  803. struct strbuf *cwd, int offset,
  804. struct repository_format *repo_fmt,
  805. int *nongit_ok)
  806. {
  807. if (check_repository_format_gently(gitdir, repo_fmt, nongit_ok))
  808. return NULL;
  809. /* --work-tree is set without --git-dir; use discovered one */
  810. if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
  811. char *to_free = NULL;
  812. const char *ret;
  813. if (offset != cwd->len && !is_absolute_path(gitdir))
  814. gitdir = to_free = real_pathdup(gitdir, 1);
  815. if (chdir(cwd->buf))
  816. die_errno(_("cannot come back to cwd"));
  817. ret = setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok);
  818. free(to_free);
  819. return ret;
  820. }
  821. /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
  822. if (is_bare_repository_cfg > 0) {
  823. set_git_dir(gitdir, (offset != cwd->len));
  824. if (chdir(cwd->buf))
  825. die_errno(_("cannot come back to cwd"));
  826. return NULL;
  827. }
  828. /* #0, #1, #5, #8, #9, #12, #13 */
  829. set_git_work_tree(".");
  830. if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
  831. set_git_dir(gitdir, 0);
  832. inside_git_dir = 0;
  833. inside_work_tree = 1;
  834. if (offset >= cwd->len)
  835. return NULL;
  836. /* Make "offset" point past the '/' (already the case for root dirs) */
  837. if (offset != offset_1st_component(cwd->buf))
  838. offset++;
  839. /* Add a '/' at the end */
  840. strbuf_addch(cwd, '/');
  841. return cwd->buf + offset;
  842. }
  843. /* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
  844. static const char *setup_bare_git_dir(struct strbuf *cwd, int offset,
  845. struct repository_format *repo_fmt,
  846. int *nongit_ok)
  847. {
  848. int root_len;
  849. if (check_repository_format_gently(".", repo_fmt, nongit_ok))
  850. return NULL;
  851. setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
  852. /* --work-tree is set without --git-dir; use discovered one */
  853. if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
  854. static const char *gitdir;
  855. gitdir = offset == cwd->len ? "." : xmemdupz(cwd->buf, offset);
  856. if (chdir(cwd->buf))
  857. die_errno(_("cannot come back to cwd"));
  858. return setup_explicit_git_dir(gitdir, cwd, repo_fmt, nongit_ok);
  859. }
  860. inside_git_dir = 1;
  861. inside_work_tree = 0;
  862. if (offset != cwd->len) {
  863. if (chdir(cwd->buf))
  864. die_errno(_("cannot come back to cwd"));
  865. root_len = offset_1st_component(cwd->buf);
  866. strbuf_setlen(cwd, offset > root_len ? offset : root_len);
  867. set_git_dir(cwd->buf, 0);
  868. }
  869. else
  870. set_git_dir(".", 0);
  871. return NULL;
  872. }
  873. static dev_t get_device_or_die(const char *path, const char *prefix, int prefix_len)
  874. {
  875. struct stat buf;
  876. if (stat(path, &buf)) {
  877. die_errno(_("failed to stat '%*s%s%s'"),
  878. prefix_len,
  879. prefix ? prefix : "",
  880. prefix ? "/" : "", path);
  881. }
  882. return buf.st_dev;
  883. }
  884. /*
  885. * A "string_list_each_func_t" function that canonicalizes an entry
  886. * from GIT_CEILING_DIRECTORIES using real_pathdup(), or
  887. * discards it if unusable. The presence of an empty entry in
  888. * GIT_CEILING_DIRECTORIES turns off canonicalization for all
  889. * subsequent entries.
  890. */
  891. static int canonicalize_ceiling_entry(struct string_list_item *item,
  892. void *cb_data)
  893. {
  894. int *empty_entry_found = cb_data;
  895. char *ceil = item->string;
  896. if (!*ceil) {
  897. *empty_entry_found = 1;
  898. return 0;
  899. } else if (!is_absolute_path(ceil)) {
  900. return 0;
  901. } else if (*empty_entry_found) {
  902. /* Keep entry but do not canonicalize it */
  903. return 1;
  904. } else {
  905. char *real_path = real_pathdup(ceil, 0);
  906. if (!real_path) {
  907. return 0;
  908. }
  909. free(item->string);
  910. item->string = real_path;
  911. return 1;
  912. }
  913. }
  914. enum discovery_result {
  915. GIT_DIR_NONE = 0,
  916. GIT_DIR_EXPLICIT,
  917. GIT_DIR_DISCOVERED,
  918. GIT_DIR_BARE,
  919. /* these are errors */
  920. GIT_DIR_HIT_CEILING = -1,
  921. GIT_DIR_HIT_MOUNT_POINT = -2,
  922. GIT_DIR_INVALID_GITFILE = -3
  923. };
  924. /*
  925. * We cannot decide in this function whether we are in the work tree or
  926. * not, since the config can only be read _after_ this function was called.
  927. *
  928. * Also, we avoid changing any global state (such as the current working
  929. * directory) to allow early callers.
  930. *
  931. * The directory where the search should start needs to be passed in via the
  932. * `dir` parameter; upon return, the `dir` buffer will contain the path of
  933. * the directory where the search ended, and `gitdir` will contain the path of
  934. * the discovered .git/ directory, if any. If `gitdir` is not absolute, it
  935. * is relative to `dir` (i.e. *not* necessarily the cwd).
  936. */
  937. static enum discovery_result setup_git_directory_gently_1(struct strbuf *dir,
  938. struct strbuf *gitdir,
  939. int die_on_error)
  940. {
  941. const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
  942. struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
  943. const char *gitdirenv;
  944. int ceil_offset = -1, min_offset = offset_1st_component(dir->buf);
  945. dev_t current_device = 0;
  946. int one_filesystem = 1;
  947. /*
  948. * If GIT_DIR is set explicitly, we're not going
  949. * to do any discovery, but we still do repository
  950. * validation.
  951. */
  952. gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
  953. if (gitdirenv) {
  954. strbuf_addstr(gitdir, gitdirenv);
  955. return GIT_DIR_EXPLICIT;
  956. }
  957. if (env_ceiling_dirs) {
  958. int empty_entry_found = 0;
  959. string_list_split(&ceiling_dirs, env_ceiling_dirs, PATH_SEP, -1);
  960. filter_string_list(&ceiling_dirs, 0,
  961. canonicalize_ceiling_entry, &empty_entry_found);
  962. ceil_offset = longest_ancestor_length(dir->buf, &ceiling_dirs);
  963. string_list_clear(&ceiling_dirs, 0);
  964. }
  965. if (ceil_offset < 0)
  966. ceil_offset = min_offset - 2;
  967. if (min_offset && min_offset == dir->len &&
  968. !is_dir_sep(dir->buf[min_offset - 1])) {
  969. strbuf_addch(dir, '/');
  970. min_offset++;
  971. }
  972. /*
  973. * Test in the following order (relative to the dir):
  974. * - .git (file containing "gitdir: <path>")
  975. * - .git/
  976. * - ./ (bare)
  977. * - ../.git
  978. * - ../.git/
  979. * - ../ (bare)
  980. * - ../../.git
  981. * etc.
  982. */
  983. one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
  984. if (one_filesystem)
  985. current_device = get_device_or_die(dir->buf, NULL, 0);
  986. for (;;) {
  987. int offset = dir->len, error_code = 0;
  988. if (offset > min_offset)
  989. strbuf_addch(dir, '/');
  990. strbuf_addstr(dir, DEFAULT_GIT_DIR_ENVIRONMENT);
  991. gitdirenv = read_gitfile_gently(dir->buf, die_on_error ?
  992. NULL : &error_code);
  993. if (!gitdirenv) {
  994. if (die_on_error ||
  995. error_code == READ_GITFILE_ERR_NOT_A_FILE) {
  996. /* NEEDSWORK: fail if .git is not file nor dir */
  997. if (is_git_directory(dir->buf))
  998. gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
  999. } else if (error_code != READ_GITFILE_ERR_STAT_FAILED)
  1000. return GIT_DIR_INVALID_GITFILE;
  1001. }
  1002. strbuf_setlen(dir, offset);
  1003. if (gitdirenv) {
  1004. strbuf_addstr(gitdir, gitdirenv);
  1005. return GIT_DIR_DISCOVERED;
  1006. }
  1007. if (is_git_directory(dir->buf)) {
  1008. strbuf_addstr(gitdir, ".");
  1009. return GIT_DIR_BARE;
  1010. }
  1011. if (offset <= min_offset)
  1012. return GIT_DIR_HIT_CEILING;
  1013. while (--offset > ceil_offset && !is_dir_sep(dir->buf[offset]))
  1014. ; /* continue */
  1015. if (offset <= ceil_offset)
  1016. return GIT_DIR_HIT_CEILING;
  1017. strbuf_setlen(dir, offset > min_offset ? offset : min_offset);
  1018. if (one_filesystem &&
  1019. current_device != get_device_or_die(dir->buf, NULL, offset))
  1020. return GIT_DIR_HIT_MOUNT_POINT;
  1021. }
  1022. }
  1023. int discover_git_directory(struct strbuf *commondir,
  1024. struct strbuf *gitdir)
  1025. {
  1026. struct strbuf dir = STRBUF_INIT, err = STRBUF_INIT;
  1027. size_t gitdir_offset = gitdir->len, cwd_len;
  1028. size_t commondir_offset = commondir->len;
  1029. struct repository_format candidate = REPOSITORY_FORMAT_INIT;
  1030. if (strbuf_getcwd(&dir))
  1031. return -1;
  1032. cwd_len = dir.len;
  1033. if (setup_git_directory_gently_1(&dir, gitdir, 0) <= 0) {
  1034. strbuf_release(&dir);
  1035. return -1;
  1036. }
  1037. /*
  1038. * The returned gitdir is relative to dir, and if dir does not reflect
  1039. * the current working directory, we simply make the gitdir absolute.
  1040. */
  1041. if (dir.len < cwd_len && !is_absolute_path(gitdir->buf + gitdir_offset)) {
  1042. /* Avoid a trailing "/." */
  1043. if (!strcmp(".", gitdir->buf + gitdir_offset))
  1044. strbuf_setlen(gitdir, gitdir_offset);
  1045. else
  1046. strbuf_addch(&dir, '/');
  1047. strbuf_insert(gitdir, gitdir_offset, dir.buf, dir.len);
  1048. }
  1049. get_common_dir(commondir, gitdir->buf + gitdir_offset);
  1050. strbuf_reset(&dir);
  1051. strbuf_addf(&dir, "%s/config", commondir->buf + commondir_offset);
  1052. read_repository_format(&candidate, dir.buf);
  1053. strbuf_release(&dir);
  1054. if (verify_repository_format(&candidate, &err) < 0) {
  1055. warning("ignoring git dir '%s': %s",
  1056. gitdir->buf + gitdir_offset, err.buf);
  1057. strbuf_release(&err);
  1058. strbuf_setlen(commondir, commondir_offset);
  1059. strbuf_setlen(gitdir, gitdir_offset);
  1060. clear_repository_format(&candidate);
  1061. return -1;
  1062. }
  1063. clear_repository_format(&candidate);
  1064. return 0;
  1065. }
  1066. const char *setup_git_directory_gently(int *nongit_ok)
  1067. {
  1068. static struct strbuf cwd = STRBUF_INIT;
  1069. struct strbuf dir = STRBUF_INIT, gitdir = STRBUF_INIT;
  1070. const char *prefix = NULL;
  1071. struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
  1072. /*
  1073. * We may have read an incomplete configuration before
  1074. * setting-up the git directory. If so, clear the cache so
  1075. * that the next queries to the configuration reload complete
  1076. * configuration (including the per-repo config file that we
  1077. * ignored previously).
  1078. */
  1079. git_config_clear();
  1080. /*
  1081. * Let's assume that we are in a git repository.
  1082. * If it turns out later that we are somewhere else, the value will be
  1083. * updated accordingly.
  1084. */
  1085. if (nongit_ok)
  1086. *nongit_ok = 0;
  1087. if (strbuf_getcwd(&cwd))
  1088. die_errno(_("Unable to read current working directory"));
  1089. strbuf_addbuf(&dir, &cwd);
  1090. switch (setup_git_directory_gently_1(&dir, &gitdir, 1)) {
  1091. case GIT_DIR_EXPLICIT:
  1092. prefix = setup_explicit_git_dir(gitdir.buf, &cwd, &repo_fmt, nongit_ok);
  1093. break;
  1094. case GIT_DIR_DISCOVERED:
  1095. if (dir.len < cwd.len && chdir(dir.buf))
  1096. die(_("cannot change to '%s'"), dir.buf);
  1097. prefix = setup_discovered_git_dir(gitdir.buf, &cwd, dir.len,
  1098. &repo_fmt, nongit_ok);
  1099. break;
  1100. case GIT_DIR_BARE:
  1101. if (dir.len < cwd.len && chdir(dir.buf))
  1102. die(_("cannot change to '%s'"), dir.buf);
  1103. prefix = setup_bare_git_dir(&cwd, dir.len, &repo_fmt, nongit_ok);
  1104. break;
  1105. case GIT_DIR_HIT_CEILING:
  1106. if (!nongit_ok)
  1107. die(_("not a git repository (or any of the parent directories): %s"),
  1108. DEFAULT_GIT_DIR_ENVIRONMENT);
  1109. *nongit_ok = 1;
  1110. break;
  1111. case GIT_DIR_HIT_MOUNT_POINT:
  1112. if (!nongit_ok)
  1113. die(_("not a git repository (or any parent up to mount point %s)\n"
  1114. "Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)."),
  1115. dir.buf);
  1116. *nongit_ok = 1;
  1117. break;
  1118. case GIT_DIR_NONE:
  1119. /*
  1120. * As a safeguard against setup_git_directory_gently_1 returning
  1121. * this value, fallthrough to BUG. Otherwise it is possible to
  1122. * set startup_info->have_repository to 1 when we did nothing to
  1123. * find a repository.
  1124. */
  1125. default:
  1126. BUG("unhandled setup_git_directory_1() result");
  1127. }
  1128. /*
  1129. * At this point, nongit_ok is stable. If it is non-NULL and points
  1130. * to a non-zero value, then this means that we haven't found a
  1131. * repository and that the caller expects startup_info to reflect
  1132. * this.
  1133. *
  1134. * Regardless of the state of nongit_ok, startup_info->prefix and
  1135. * the GIT_PREFIX environment variable must always match. For details
  1136. * see Documentation/config/alias.txt.
  1137. */
  1138. if (nongit_ok && *nongit_ok) {
  1139. startup_info->have_repository = 0;
  1140. startup_info->prefix = NULL;
  1141. setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
  1142. } else {
  1143. startup_info->have_repository = 1;
  1144. startup_info->prefix = prefix;
  1145. if (prefix)
  1146. setenv(GIT_PREFIX_ENVIRONMENT, prefix, 1);
  1147. else
  1148. setenv(GIT_PREFIX_ENVIRONMENT, "", 1);
  1149. }
  1150. /*
  1151. * Not all paths through the setup code will call 'set_git_dir()' (which
  1152. * directly sets up the environment) so in order to guarantee that the
  1153. * environment is in a consistent state after setup, explicitly setup
  1154. * the environment if we have a repository.
  1155. *
  1156. * NEEDSWORK: currently we allow bogus GIT_DIR values to be set in some
  1157. * code paths so we also need to explicitly setup the environment if
  1158. * the user has set GIT_DIR. It may be beneficial to disallow bogus
  1159. * GIT_DIR values at some point in the future.
  1160. */
  1161. if (/* GIT_DIR_EXPLICIT, GIT_DIR_DISCOVERED, GIT_DIR_BARE */
  1162. startup_info->have_repository ||
  1163. /* GIT_DIR_EXPLICIT */
  1164. getenv(GIT_DIR_ENVIRONMENT)) {
  1165. if (!the_repository->gitdir) {
  1166. const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
  1167. if (!gitdir)
  1168. gitdir = DEFAULT_GIT_DIR_ENVIRONMENT;
  1169. setup_git_env(gitdir);
  1170. }
  1171. if (startup_info->have_repository)
  1172. repo_set_hash_algo(the_repository, repo_fmt.hash_algo);
  1173. }
  1174. strbuf_release(&dir);
  1175. strbuf_release(&gitdir);
  1176. clear_repository_format(&repo_fmt);
  1177. return prefix;
  1178. }
  1179. int git_config_perm(const char *var, const char *value)
  1180. {
  1181. int i;
  1182. char *endptr;
  1183. if (value == NULL)
  1184. return PERM_GROUP;
  1185. if (!strcmp(value, "umask"))
  1186. return PERM_UMASK;
  1187. if (!strcmp(value, "group"))
  1188. return PERM_GROUP;
  1189. if (!strcmp(value, "all") ||
  1190. !strcmp(value, "world") ||
  1191. !strcmp(value, "everybody"))
  1192. return PERM_EVERYBODY;
  1193. /* Parse octal numbers */
  1194. i = strtol(value, &endptr, 8);
  1195. /* If not an octal number, maybe true/false? */
  1196. if (*endptr != 0)
  1197. return git_config_bool(var, value) ? PERM_GROUP : PERM_UMASK;
  1198. /*
  1199. * Treat values 0, 1 and 2 as compatibility cases, otherwise it is
  1200. * a chmod value to restrict to.
  1201. */
  1202. switch (i) {
  1203. case PERM_UMASK: /* 0 */
  1204. return PERM_UMASK;
  1205. case OLD_PERM_GROUP: /* 1 */
  1206. return PERM_GROUP;
  1207. case OLD_PERM_EVERYBODY: /* 2 */
  1208. return PERM_EVERYBODY;
  1209. }
  1210. /* A filemode value was given: 0xxx */
  1211. if ((i & 0600) != 0600)
  1212. die(_("problem with core.sharedRepository filemode value "
  1213. "(0%.3o).\nThe owner of files must always have "
  1214. "read and write permissions."), i);
  1215. /*
  1216. * Mask filemode value. Others can not get write permission.
  1217. * x flags for directories are handled separately.
  1218. */
  1219. return -(i & 0666);
  1220. }
  1221. void check_repository_format(struct repository_format *fmt)
  1222. {
  1223. struct repository_format repo_fmt = REPOSITORY_FORMAT_INIT;
  1224. if (!fmt)
  1225. fmt = &repo_fmt;
  1226. check_repository_format_gently(get_git_dir(), fmt, NULL);
  1227. startup_info->have_repository = 1;
  1228. repo_set_hash_algo(the_repository, fmt->hash_algo);
  1229. clear_repository_format(&repo_fmt);
  1230. }
  1231. /*
  1232. * Returns the "prefix", a path to the current working directory
  1233. * relative to the work tree root, or NULL, if the current working
  1234. * directory is not a strict subdirectory of the work tree root. The
  1235. * prefix always ends with a '/' character.
  1236. */
  1237. const char *setup_git_directory(void)
  1238. {
  1239. return setup_git_directory_gently(NULL);
  1240. }
  1241. const char *resolve_gitdir_gently(const char *suspect, int *return_error_code)
  1242. {
  1243. if (is_git_directory(suspect))
  1244. return suspect;
  1245. return read_gitfile_gently(suspect, return_error_code);
  1246. }
  1247. /* if any standard file descriptor is missing open it to /dev/null */
  1248. void sanitize_stdfds(void)
  1249. {
  1250. int fd = open("/dev/null", O_RDWR, 0);
  1251. while (fd != -1 && fd < 2)
  1252. fd = dup(fd);
  1253. if (fd == -1)
  1254. die_errno(_("open /dev/null or dup failed"));
  1255. if (fd > 2)
  1256. close(fd);
  1257. }
  1258. int daemonize(void)
  1259. {
  1260. #ifdef NO_POSIX_GOODIES
  1261. errno = ENOSYS;
  1262. return -1;
  1263. #else
  1264. switch (fork()) {
  1265. case 0:
  1266. break;
  1267. case -1:
  1268. die_errno(_("fork failed"));
  1269. default:
  1270. exit(0);
  1271. }
  1272. if (setsid() == -1)
  1273. die_errno(_("setsid failed"));
  1274. close(0);
  1275. close(1);
  1276. close(2);
  1277. sanitize_stdfds();
  1278. return 0;
  1279. #endif
  1280. }