PageRenderTime 82ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/config.c

https://gitlab.com/nmusco/git
C | 1910 lines | 1510 code | 228 blank | 172 comment | 444 complexity | b93e91462b04234fe2a2bc377996f795 MD5 | raw file
  1. /*
  2. * GIT - The information manager from hell
  3. *
  4. * Copyright (C) Linus Torvalds, 2005
  5. * Copyright (C) Johannes Schindelin, 2005
  6. *
  7. */
  8. #include "cache.h"
  9. #include "exec_cmd.h"
  10. #include "strbuf.h"
  11. #include "quote.h"
  12. struct config_source {
  13. struct config_source *prev;
  14. union {
  15. FILE *file;
  16. struct config_buf {
  17. const char *buf;
  18. size_t len;
  19. size_t pos;
  20. } buf;
  21. } u;
  22. const char *name;
  23. const char *path;
  24. int die_on_error;
  25. int linenr;
  26. int eof;
  27. struct strbuf value;
  28. struct strbuf var;
  29. int (*do_fgetc)(struct config_source *c);
  30. int (*do_ungetc)(int c, struct config_source *conf);
  31. long (*do_ftell)(struct config_source *c);
  32. };
  33. static struct config_source *cf;
  34. static int zlib_compression_seen;
  35. static int config_file_fgetc(struct config_source *conf)
  36. {
  37. return fgetc(conf->u.file);
  38. }
  39. static int config_file_ungetc(int c, struct config_source *conf)
  40. {
  41. return ungetc(c, conf->u.file);
  42. }
  43. static long config_file_ftell(struct config_source *conf)
  44. {
  45. return ftell(conf->u.file);
  46. }
  47. static int config_buf_fgetc(struct config_source *conf)
  48. {
  49. if (conf->u.buf.pos < conf->u.buf.len)
  50. return conf->u.buf.buf[conf->u.buf.pos++];
  51. return EOF;
  52. }
  53. static int config_buf_ungetc(int c, struct config_source *conf)
  54. {
  55. if (conf->u.buf.pos > 0)
  56. return conf->u.buf.buf[--conf->u.buf.pos];
  57. return EOF;
  58. }
  59. static long config_buf_ftell(struct config_source *conf)
  60. {
  61. return conf->u.buf.pos;
  62. }
  63. #define MAX_INCLUDE_DEPTH 10
  64. static const char include_depth_advice[] =
  65. "exceeded maximum include depth (%d) while including\n"
  66. " %s\n"
  67. "from\n"
  68. " %s\n"
  69. "Do you have circular includes?";
  70. static int handle_path_include(const char *path, struct config_include_data *inc)
  71. {
  72. int ret = 0;
  73. struct strbuf buf = STRBUF_INIT;
  74. char *expanded;
  75. if (!path)
  76. return config_error_nonbool("include.path");
  77. expanded = expand_user_path(path);
  78. if (!expanded)
  79. return error("Could not expand include path '%s'", path);
  80. path = expanded;
  81. /*
  82. * Use an absolute path as-is, but interpret relative paths
  83. * based on the including config file.
  84. */
  85. if (!is_absolute_path(path)) {
  86. char *slash;
  87. if (!cf || !cf->path)
  88. return error("relative config includes must come from files");
  89. slash = find_last_dir_sep(cf->path);
  90. if (slash)
  91. strbuf_add(&buf, cf->path, slash - cf->path + 1);
  92. strbuf_addstr(&buf, path);
  93. path = buf.buf;
  94. }
  95. if (!access_or_die(path, R_OK, 0)) {
  96. if (++inc->depth > MAX_INCLUDE_DEPTH)
  97. die(include_depth_advice, MAX_INCLUDE_DEPTH, path,
  98. cf && cf->name ? cf->name : "the command line");
  99. ret = git_config_from_file(git_config_include, path, inc);
  100. inc->depth--;
  101. }
  102. strbuf_release(&buf);
  103. free(expanded);
  104. return ret;
  105. }
  106. int git_config_include(const char *var, const char *value, void *data)
  107. {
  108. struct config_include_data *inc = data;
  109. const char *type;
  110. int ret;
  111. /*
  112. * Pass along all values, including "include" directives; this makes it
  113. * possible to query information on the includes themselves.
  114. */
  115. ret = inc->fn(var, value, inc->data);
  116. if (ret < 0)
  117. return ret;
  118. type = skip_prefix(var, "include.");
  119. if (!type)
  120. return ret;
  121. if (!strcmp(type, "path"))
  122. ret = handle_path_include(value, inc);
  123. return ret;
  124. }
  125. static void lowercase(char *p)
  126. {
  127. for (; *p; p++)
  128. *p = tolower(*p);
  129. }
  130. void git_config_push_parameter(const char *text)
  131. {
  132. struct strbuf env = STRBUF_INIT;
  133. const char *old = getenv(CONFIG_DATA_ENVIRONMENT);
  134. if (old) {
  135. strbuf_addstr(&env, old);
  136. strbuf_addch(&env, ' ');
  137. }
  138. sq_quote_buf(&env, text);
  139. setenv(CONFIG_DATA_ENVIRONMENT, env.buf, 1);
  140. strbuf_release(&env);
  141. }
  142. int git_config_parse_parameter(const char *text,
  143. config_fn_t fn, void *data)
  144. {
  145. struct strbuf **pair;
  146. pair = strbuf_split_str(text, '=', 2);
  147. if (!pair[0])
  148. return error("bogus config parameter: %s", text);
  149. if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=')
  150. strbuf_setlen(pair[0], pair[0]->len - 1);
  151. strbuf_trim(pair[0]);
  152. if (!pair[0]->len) {
  153. strbuf_list_free(pair);
  154. return error("bogus config parameter: %s", text);
  155. }
  156. lowercase(pair[0]->buf);
  157. if (fn(pair[0]->buf, pair[1] ? pair[1]->buf : NULL, data) < 0) {
  158. strbuf_list_free(pair);
  159. return -1;
  160. }
  161. strbuf_list_free(pair);
  162. return 0;
  163. }
  164. int git_config_from_parameters(config_fn_t fn, void *data)
  165. {
  166. const char *env = getenv(CONFIG_DATA_ENVIRONMENT);
  167. char *envw;
  168. const char **argv = NULL;
  169. int nr = 0, alloc = 0;
  170. int i;
  171. if (!env)
  172. return 0;
  173. /* sq_dequote will write over it */
  174. envw = xstrdup(env);
  175. if (sq_dequote_to_argv(envw, &argv, &nr, &alloc) < 0) {
  176. free(envw);
  177. return error("bogus format in " CONFIG_DATA_ENVIRONMENT);
  178. }
  179. for (i = 0; i < nr; i++) {
  180. if (git_config_parse_parameter(argv[i], fn, data) < 0) {
  181. free(argv);
  182. free(envw);
  183. return -1;
  184. }
  185. }
  186. free(argv);
  187. free(envw);
  188. return nr > 0;
  189. }
  190. static int get_next_char(void)
  191. {
  192. int c = cf->do_fgetc(cf);
  193. if (c == '\r') {
  194. /* DOS like systems */
  195. c = cf->do_fgetc(cf);
  196. if (c != '\n') {
  197. cf->do_ungetc(c, cf);
  198. c = '\r';
  199. }
  200. }
  201. if (c == '\n')
  202. cf->linenr++;
  203. if (c == EOF) {
  204. cf->eof = 1;
  205. c = '\n';
  206. }
  207. return c;
  208. }
  209. static char *parse_value(void)
  210. {
  211. int quote = 0, comment = 0, space = 0;
  212. strbuf_reset(&cf->value);
  213. for (;;) {
  214. int c = get_next_char();
  215. if (c == '\n') {
  216. if (quote) {
  217. cf->linenr--;
  218. return NULL;
  219. }
  220. return cf->value.buf;
  221. }
  222. if (comment)
  223. continue;
  224. if (isspace(c) && !quote) {
  225. if (cf->value.len)
  226. space++;
  227. continue;
  228. }
  229. if (!quote) {
  230. if (c == ';' || c == '#') {
  231. comment = 1;
  232. continue;
  233. }
  234. }
  235. for (; space; space--)
  236. strbuf_addch(&cf->value, ' ');
  237. if (c == '\\') {
  238. c = get_next_char();
  239. switch (c) {
  240. case '\n':
  241. continue;
  242. case 't':
  243. c = '\t';
  244. break;
  245. case 'b':
  246. c = '\b';
  247. break;
  248. case 'n':
  249. c = '\n';
  250. break;
  251. /* Some characters escape as themselves */
  252. case '\\': case '"':
  253. break;
  254. /* Reject unknown escape sequences */
  255. default:
  256. return NULL;
  257. }
  258. strbuf_addch(&cf->value, c);
  259. continue;
  260. }
  261. if (c == '"') {
  262. quote = 1-quote;
  263. continue;
  264. }
  265. strbuf_addch(&cf->value, c);
  266. }
  267. }
  268. static inline int iskeychar(int c)
  269. {
  270. return isalnum(c) || c == '-';
  271. }
  272. static int get_value(config_fn_t fn, void *data, struct strbuf *name)
  273. {
  274. int c;
  275. char *value;
  276. /* Get the full name */
  277. for (;;) {
  278. c = get_next_char();
  279. if (cf->eof)
  280. break;
  281. if (!iskeychar(c))
  282. break;
  283. strbuf_addch(name, tolower(c));
  284. }
  285. while (c == ' ' || c == '\t')
  286. c = get_next_char();
  287. value = NULL;
  288. if (c != '\n') {
  289. if (c != '=')
  290. return -1;
  291. value = parse_value();
  292. if (!value)
  293. return -1;
  294. }
  295. return fn(name->buf, value, data);
  296. }
  297. static int get_extended_base_var(struct strbuf *name, int c)
  298. {
  299. do {
  300. if (c == '\n')
  301. goto error_incomplete_line;
  302. c = get_next_char();
  303. } while (isspace(c));
  304. /* We require the format to be '[base "extension"]' */
  305. if (c != '"')
  306. return -1;
  307. strbuf_addch(name, '.');
  308. for (;;) {
  309. int c = get_next_char();
  310. if (c == '\n')
  311. goto error_incomplete_line;
  312. if (c == '"')
  313. break;
  314. if (c == '\\') {
  315. c = get_next_char();
  316. if (c == '\n')
  317. goto error_incomplete_line;
  318. }
  319. strbuf_addch(name, c);
  320. }
  321. /* Final ']' */
  322. if (get_next_char() != ']')
  323. return -1;
  324. return 0;
  325. error_incomplete_line:
  326. cf->linenr--;
  327. return -1;
  328. }
  329. static int get_base_var(struct strbuf *name)
  330. {
  331. for (;;) {
  332. int c = get_next_char();
  333. if (cf->eof)
  334. return -1;
  335. if (c == ']')
  336. return 0;
  337. if (isspace(c))
  338. return get_extended_base_var(name, c);
  339. if (!iskeychar(c) && c != '.')
  340. return -1;
  341. strbuf_addch(name, tolower(c));
  342. }
  343. }
  344. static int git_parse_source(config_fn_t fn, void *data)
  345. {
  346. int comment = 0;
  347. int baselen = 0;
  348. struct strbuf *var = &cf->var;
  349. /* U+FEFF Byte Order Mark in UTF8 */
  350. static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
  351. const unsigned char *bomptr = utf8_bom;
  352. for (;;) {
  353. int c = get_next_char();
  354. if (bomptr && *bomptr) {
  355. /* We are at the file beginning; skip UTF8-encoded BOM
  356. * if present. Sane editors won't put this in on their
  357. * own, but e.g. Windows Notepad will do it happily. */
  358. if ((unsigned char) c == *bomptr) {
  359. bomptr++;
  360. continue;
  361. } else {
  362. /* Do not tolerate partial BOM. */
  363. if (bomptr != utf8_bom)
  364. break;
  365. /* No BOM at file beginning. Cool. */
  366. bomptr = NULL;
  367. }
  368. }
  369. if (c == '\n') {
  370. if (cf->eof)
  371. return 0;
  372. comment = 0;
  373. continue;
  374. }
  375. if (comment || isspace(c))
  376. continue;
  377. if (c == '#' || c == ';') {
  378. comment = 1;
  379. continue;
  380. }
  381. if (c == '[') {
  382. /* Reset prior to determining a new stem */
  383. strbuf_reset(var);
  384. if (get_base_var(var) < 0 || var->len < 1)
  385. break;
  386. strbuf_addch(var, '.');
  387. baselen = var->len;
  388. continue;
  389. }
  390. if (!isalpha(c))
  391. break;
  392. /*
  393. * Truncate the var name back to the section header
  394. * stem prior to grabbing the suffix part of the name
  395. * and the value.
  396. */
  397. strbuf_setlen(var, baselen);
  398. strbuf_addch(var, tolower(c));
  399. if (get_value(fn, data, var) < 0)
  400. break;
  401. }
  402. if (cf->die_on_error)
  403. die("bad config file line %d in %s", cf->linenr, cf->name);
  404. else
  405. return error("bad config file line %d in %s", cf->linenr, cf->name);
  406. }
  407. static int parse_unit_factor(const char *end, uintmax_t *val)
  408. {
  409. if (!*end)
  410. return 1;
  411. else if (!strcasecmp(end, "k")) {
  412. *val *= 1024;
  413. return 1;
  414. }
  415. else if (!strcasecmp(end, "m")) {
  416. *val *= 1024 * 1024;
  417. return 1;
  418. }
  419. else if (!strcasecmp(end, "g")) {
  420. *val *= 1024 * 1024 * 1024;
  421. return 1;
  422. }
  423. return 0;
  424. }
  425. static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
  426. {
  427. if (value && *value) {
  428. char *end;
  429. intmax_t val;
  430. uintmax_t uval;
  431. uintmax_t factor = 1;
  432. errno = 0;
  433. val = strtoimax(value, &end, 0);
  434. if (errno == ERANGE)
  435. return 0;
  436. if (!parse_unit_factor(end, &factor)) {
  437. errno = EINVAL;
  438. return 0;
  439. }
  440. uval = abs(val);
  441. uval *= factor;
  442. if (uval > max || abs(val) > uval) {
  443. errno = ERANGE;
  444. return 0;
  445. }
  446. val *= factor;
  447. *ret = val;
  448. return 1;
  449. }
  450. errno = EINVAL;
  451. return 0;
  452. }
  453. static int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max)
  454. {
  455. if (value && *value) {
  456. char *end;
  457. uintmax_t val;
  458. uintmax_t oldval;
  459. errno = 0;
  460. val = strtoumax(value, &end, 0);
  461. if (errno == ERANGE)
  462. return 0;
  463. oldval = val;
  464. if (!parse_unit_factor(end, &val)) {
  465. errno = EINVAL;
  466. return 0;
  467. }
  468. if (val > max || oldval > val) {
  469. errno = ERANGE;
  470. return 0;
  471. }
  472. *ret = val;
  473. return 1;
  474. }
  475. errno = EINVAL;
  476. return 0;
  477. }
  478. static int git_parse_int(const char *value, int *ret)
  479. {
  480. intmax_t tmp;
  481. if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int)))
  482. return 0;
  483. *ret = tmp;
  484. return 1;
  485. }
  486. static int git_parse_int64(const char *value, int64_t *ret)
  487. {
  488. intmax_t tmp;
  489. if (!git_parse_signed(value, &tmp, maximum_signed_value_of_type(int64_t)))
  490. return 0;
  491. *ret = tmp;
  492. return 1;
  493. }
  494. int git_parse_ulong(const char *value, unsigned long *ret)
  495. {
  496. uintmax_t tmp;
  497. if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(long)))
  498. return 0;
  499. *ret = tmp;
  500. return 1;
  501. }
  502. NORETURN
  503. static void die_bad_number(const char *name, const char *value)
  504. {
  505. const char *reason = errno == ERANGE ?
  506. "out of range" :
  507. "invalid unit";
  508. if (!value)
  509. value = "";
  510. if (cf && cf->name)
  511. die("bad numeric config value '%s' for '%s' in %s: %s",
  512. value, name, cf->name, reason);
  513. die("bad numeric config value '%s' for '%s': %s", value, name, reason);
  514. }
  515. int git_config_int(const char *name, const char *value)
  516. {
  517. int ret;
  518. if (!git_parse_int(value, &ret))
  519. die_bad_number(name, value);
  520. return ret;
  521. }
  522. int64_t git_config_int64(const char *name, const char *value)
  523. {
  524. int64_t ret;
  525. if (!git_parse_int64(value, &ret))
  526. die_bad_number(name, value);
  527. return ret;
  528. }
  529. unsigned long git_config_ulong(const char *name, const char *value)
  530. {
  531. unsigned long ret;
  532. if (!git_parse_ulong(value, &ret))
  533. die_bad_number(name, value);
  534. return ret;
  535. }
  536. static int git_config_maybe_bool_text(const char *name, const char *value)
  537. {
  538. if (!value)
  539. return 1;
  540. if (!*value)
  541. return 0;
  542. if (!strcasecmp(value, "true")
  543. || !strcasecmp(value, "yes")
  544. || !strcasecmp(value, "on"))
  545. return 1;
  546. if (!strcasecmp(value, "false")
  547. || !strcasecmp(value, "no")
  548. || !strcasecmp(value, "off"))
  549. return 0;
  550. return -1;
  551. }
  552. int git_config_maybe_bool(const char *name, const char *value)
  553. {
  554. int v = git_config_maybe_bool_text(name, value);
  555. if (0 <= v)
  556. return v;
  557. if (git_parse_int(value, &v))
  558. return !!v;
  559. return -1;
  560. }
  561. int git_config_bool_or_int(const char *name, const char *value, int *is_bool)
  562. {
  563. int v = git_config_maybe_bool_text(name, value);
  564. if (0 <= v) {
  565. *is_bool = 1;
  566. return v;
  567. }
  568. *is_bool = 0;
  569. return git_config_int(name, value);
  570. }
  571. int git_config_bool(const char *name, const char *value)
  572. {
  573. int discard;
  574. return !!git_config_bool_or_int(name, value, &discard);
  575. }
  576. int git_config_string(const char **dest, const char *var, const char *value)
  577. {
  578. if (!value)
  579. return config_error_nonbool(var);
  580. *dest = xstrdup(value);
  581. return 0;
  582. }
  583. int git_config_pathname(const char **dest, const char *var, const char *value)
  584. {
  585. if (!value)
  586. return config_error_nonbool(var);
  587. *dest = expand_user_path(value);
  588. if (!*dest)
  589. die("Failed to expand user dir in: '%s'", value);
  590. return 0;
  591. }
  592. static int git_default_core_config(const char *var, const char *value)
  593. {
  594. /* This needs a better name */
  595. if (!strcmp(var, "core.filemode")) {
  596. trust_executable_bit = git_config_bool(var, value);
  597. return 0;
  598. }
  599. if (!strcmp(var, "core.trustctime")) {
  600. trust_ctime = git_config_bool(var, value);
  601. return 0;
  602. }
  603. if (!strcmp(var, "core.checkstat")) {
  604. if (!strcasecmp(value, "default"))
  605. check_stat = 1;
  606. else if (!strcasecmp(value, "minimal"))
  607. check_stat = 0;
  608. }
  609. if (!strcmp(var, "core.quotepath")) {
  610. quote_path_fully = git_config_bool(var, value);
  611. return 0;
  612. }
  613. if (!strcmp(var, "core.symlinks")) {
  614. has_symlinks = git_config_bool(var, value);
  615. return 0;
  616. }
  617. if (!strcmp(var, "core.ignorecase")) {
  618. ignore_case = git_config_bool(var, value);
  619. return 0;
  620. }
  621. if (!strcmp(var, "core.attributesfile"))
  622. return git_config_pathname(&git_attributes_file, var, value);
  623. if (!strcmp(var, "core.bare")) {
  624. is_bare_repository_cfg = git_config_bool(var, value);
  625. return 0;
  626. }
  627. if (!strcmp(var, "core.ignorestat")) {
  628. assume_unchanged = git_config_bool(var, value);
  629. return 0;
  630. }
  631. if (!strcmp(var, "core.prefersymlinkrefs")) {
  632. prefer_symlink_refs = git_config_bool(var, value);
  633. return 0;
  634. }
  635. if (!strcmp(var, "core.logallrefupdates")) {
  636. log_all_ref_updates = git_config_bool(var, value);
  637. return 0;
  638. }
  639. if (!strcmp(var, "core.warnambiguousrefs")) {
  640. warn_ambiguous_refs = git_config_bool(var, value);
  641. return 0;
  642. }
  643. if (!strcmp(var, "core.abbrev")) {
  644. int abbrev = git_config_int(var, value);
  645. if (abbrev < minimum_abbrev || abbrev > 40)
  646. return -1;
  647. default_abbrev = abbrev;
  648. return 0;
  649. }
  650. if (!strcmp(var, "core.loosecompression")) {
  651. int level = git_config_int(var, value);
  652. if (level == -1)
  653. level = Z_DEFAULT_COMPRESSION;
  654. else if (level < 0 || level > Z_BEST_COMPRESSION)
  655. die("bad zlib compression level %d", level);
  656. zlib_compression_level = level;
  657. zlib_compression_seen = 1;
  658. return 0;
  659. }
  660. if (!strcmp(var, "core.compression")) {
  661. int level = git_config_int(var, value);
  662. if (level == -1)
  663. level = Z_DEFAULT_COMPRESSION;
  664. else if (level < 0 || level > Z_BEST_COMPRESSION)
  665. die("bad zlib compression level %d", level);
  666. core_compression_level = level;
  667. core_compression_seen = 1;
  668. if (!zlib_compression_seen)
  669. zlib_compression_level = level;
  670. return 0;
  671. }
  672. if (!strcmp(var, "core.packedgitwindowsize")) {
  673. int pgsz_x2 = getpagesize() * 2;
  674. packed_git_window_size = git_config_ulong(var, value);
  675. /* This value must be multiple of (pagesize * 2) */
  676. packed_git_window_size /= pgsz_x2;
  677. if (packed_git_window_size < 1)
  678. packed_git_window_size = 1;
  679. packed_git_window_size *= pgsz_x2;
  680. return 0;
  681. }
  682. if (!strcmp(var, "core.bigfilethreshold")) {
  683. big_file_threshold = git_config_ulong(var, value);
  684. return 0;
  685. }
  686. if (!strcmp(var, "core.packedgitlimit")) {
  687. packed_git_limit = git_config_ulong(var, value);
  688. return 0;
  689. }
  690. if (!strcmp(var, "core.deltabasecachelimit")) {
  691. delta_base_cache_limit = git_config_ulong(var, value);
  692. return 0;
  693. }
  694. if (!strcmp(var, "core.autocrlf")) {
  695. if (value && !strcasecmp(value, "input")) {
  696. if (core_eol == EOL_CRLF)
  697. return error("core.autocrlf=input conflicts with core.eol=crlf");
  698. auto_crlf = AUTO_CRLF_INPUT;
  699. return 0;
  700. }
  701. auto_crlf = git_config_bool(var, value);
  702. return 0;
  703. }
  704. if (!strcmp(var, "core.safecrlf")) {
  705. if (value && !strcasecmp(value, "warn")) {
  706. safe_crlf = SAFE_CRLF_WARN;
  707. return 0;
  708. }
  709. safe_crlf = git_config_bool(var, value);
  710. return 0;
  711. }
  712. if (!strcmp(var, "core.eol")) {
  713. if (value && !strcasecmp(value, "lf"))
  714. core_eol = EOL_LF;
  715. else if (value && !strcasecmp(value, "crlf"))
  716. core_eol = EOL_CRLF;
  717. else if (value && !strcasecmp(value, "native"))
  718. core_eol = EOL_NATIVE;
  719. else
  720. core_eol = EOL_UNSET;
  721. if (core_eol == EOL_CRLF && auto_crlf == AUTO_CRLF_INPUT)
  722. return error("core.autocrlf=input conflicts with core.eol=crlf");
  723. return 0;
  724. }
  725. if (!strcmp(var, "core.notesref")) {
  726. notes_ref_name = xstrdup(value);
  727. return 0;
  728. }
  729. if (!strcmp(var, "core.pager"))
  730. return git_config_string(&pager_program, var, value);
  731. if (!strcmp(var, "core.editor"))
  732. return git_config_string(&editor_program, var, value);
  733. if (!strcmp(var, "core.commentchar")) {
  734. const char *comment;
  735. int ret = git_config_string(&comment, var, value);
  736. if (!ret)
  737. comment_line_char = comment[0];
  738. return ret;
  739. }
  740. if (!strcmp(var, "core.askpass"))
  741. return git_config_string(&askpass_program, var, value);
  742. if (!strcmp(var, "core.excludesfile"))
  743. return git_config_pathname(&excludes_file, var, value);
  744. if (!strcmp(var, "core.whitespace")) {
  745. if (!value)
  746. return config_error_nonbool(var);
  747. whitespace_rule_cfg = parse_whitespace_rule(value);
  748. return 0;
  749. }
  750. if (!strcmp(var, "core.fsyncobjectfiles")) {
  751. fsync_object_files = git_config_bool(var, value);
  752. return 0;
  753. }
  754. if (!strcmp(var, "core.preloadindex")) {
  755. core_preload_index = git_config_bool(var, value);
  756. return 0;
  757. }
  758. if (!strcmp(var, "core.createobject")) {
  759. if (!strcmp(value, "rename"))
  760. object_creation_mode = OBJECT_CREATION_USES_RENAMES;
  761. else if (!strcmp(value, "link"))
  762. object_creation_mode = OBJECT_CREATION_USES_HARDLINKS;
  763. else
  764. die("Invalid mode for object creation: %s", value);
  765. return 0;
  766. }
  767. if (!strcmp(var, "core.sparsecheckout")) {
  768. core_apply_sparse_checkout = git_config_bool(var, value);
  769. return 0;
  770. }
  771. if (!strcmp(var, "core.precomposeunicode")) {
  772. precomposed_unicode = git_config_bool(var, value);
  773. return 0;
  774. }
  775. /* Add other config variables here and to Documentation/config.txt. */
  776. return 0;
  777. }
  778. static int git_default_i18n_config(const char *var, const char *value)
  779. {
  780. if (!strcmp(var, "i18n.commitencoding"))
  781. return git_config_string(&git_commit_encoding, var, value);
  782. if (!strcmp(var, "i18n.logoutputencoding"))
  783. return git_config_string(&git_log_output_encoding, var, value);
  784. /* Add other config variables here and to Documentation/config.txt. */
  785. return 0;
  786. }
  787. static int git_default_branch_config(const char *var, const char *value)
  788. {
  789. if (!strcmp(var, "branch.autosetupmerge")) {
  790. if (value && !strcasecmp(value, "always")) {
  791. git_branch_track = BRANCH_TRACK_ALWAYS;
  792. return 0;
  793. }
  794. git_branch_track = git_config_bool(var, value);
  795. return 0;
  796. }
  797. if (!strcmp(var, "branch.autosetuprebase")) {
  798. if (!value)
  799. return config_error_nonbool(var);
  800. else if (!strcmp(value, "never"))
  801. autorebase = AUTOREBASE_NEVER;
  802. else if (!strcmp(value, "local"))
  803. autorebase = AUTOREBASE_LOCAL;
  804. else if (!strcmp(value, "remote"))
  805. autorebase = AUTOREBASE_REMOTE;
  806. else if (!strcmp(value, "always"))
  807. autorebase = AUTOREBASE_ALWAYS;
  808. else
  809. return error("Malformed value for %s", var);
  810. return 0;
  811. }
  812. /* Add other config variables here and to Documentation/config.txt. */
  813. return 0;
  814. }
  815. static int git_default_push_config(const char *var, const char *value)
  816. {
  817. if (!strcmp(var, "push.default")) {
  818. if (!value)
  819. return config_error_nonbool(var);
  820. else if (!strcmp(value, "nothing"))
  821. push_default = PUSH_DEFAULT_NOTHING;
  822. else if (!strcmp(value, "matching"))
  823. push_default = PUSH_DEFAULT_MATCHING;
  824. else if (!strcmp(value, "simple"))
  825. push_default = PUSH_DEFAULT_SIMPLE;
  826. else if (!strcmp(value, "upstream"))
  827. push_default = PUSH_DEFAULT_UPSTREAM;
  828. else if (!strcmp(value, "tracking")) /* deprecated */
  829. push_default = PUSH_DEFAULT_UPSTREAM;
  830. else if (!strcmp(value, "current"))
  831. push_default = PUSH_DEFAULT_CURRENT;
  832. else {
  833. error("Malformed value for %s: %s", var, value);
  834. return error("Must be one of nothing, matching, simple, "
  835. "upstream or current.");
  836. }
  837. return 0;
  838. }
  839. /* Add other config variables here and to Documentation/config.txt. */
  840. return 0;
  841. }
  842. static int git_default_mailmap_config(const char *var, const char *value)
  843. {
  844. if (!strcmp(var, "mailmap.file"))
  845. return git_config_string(&git_mailmap_file, var, value);
  846. if (!strcmp(var, "mailmap.blob"))
  847. return git_config_string(&git_mailmap_blob, var, value);
  848. /* Add other config variables here and to Documentation/config.txt. */
  849. return 0;
  850. }
  851. int git_default_config(const char *var, const char *value, void *dummy)
  852. {
  853. if (starts_with(var, "core."))
  854. return git_default_core_config(var, value);
  855. if (starts_with(var, "user."))
  856. return git_ident_config(var, value, dummy);
  857. if (starts_with(var, "i18n."))
  858. return git_default_i18n_config(var, value);
  859. if (starts_with(var, "branch."))
  860. return git_default_branch_config(var, value);
  861. if (starts_with(var, "push."))
  862. return git_default_push_config(var, value);
  863. if (starts_with(var, "mailmap."))
  864. return git_default_mailmap_config(var, value);
  865. if (starts_with(var, "advice."))
  866. return git_default_advice_config(var, value);
  867. if (!strcmp(var, "pager.color") || !strcmp(var, "color.pager")) {
  868. pager_use_color = git_config_bool(var,value);
  869. return 0;
  870. }
  871. if (!strcmp(var, "pack.packsizelimit")) {
  872. pack_size_limit_cfg = git_config_ulong(var, value);
  873. return 0;
  874. }
  875. /* Add other config variables here and to Documentation/config.txt. */
  876. return 0;
  877. }
  878. /*
  879. * All source specific fields in the union, die_on_error, name and the callbacks
  880. * fgetc, ungetc, ftell of top need to be initialized before calling
  881. * this function.
  882. */
  883. static int do_config_from(struct config_source *top, config_fn_t fn, void *data)
  884. {
  885. int ret;
  886. /* push config-file parsing state stack */
  887. top->prev = cf;
  888. top->linenr = 1;
  889. top->eof = 0;
  890. strbuf_init(&top->value, 1024);
  891. strbuf_init(&top->var, 1024);
  892. cf = top;
  893. ret = git_parse_source(fn, data);
  894. /* pop config-file parsing state stack */
  895. strbuf_release(&top->value);
  896. strbuf_release(&top->var);
  897. cf = top->prev;
  898. return ret;
  899. }
  900. static int do_config_from_file(config_fn_t fn,
  901. const char *name, const char *path, FILE *f, void *data)
  902. {
  903. struct config_source top;
  904. top.u.file = f;
  905. top.name = name;
  906. top.path = path;
  907. top.die_on_error = 1;
  908. top.do_fgetc = config_file_fgetc;
  909. top.do_ungetc = config_file_ungetc;
  910. top.do_ftell = config_file_ftell;
  911. return do_config_from(&top, fn, data);
  912. }
  913. static int git_config_from_stdin(config_fn_t fn, void *data)
  914. {
  915. return do_config_from_file(fn, "<stdin>", NULL, stdin, data);
  916. }
  917. int git_config_from_file(config_fn_t fn, const char *filename, void *data)
  918. {
  919. int ret = -1;
  920. FILE *f;
  921. f = fopen(filename, "r");
  922. if (f) {
  923. ret = do_config_from_file(fn, filename, filename, f, data);
  924. fclose(f);
  925. }
  926. return ret;
  927. }
  928. int git_config_from_buf(config_fn_t fn, const char *name, const char *buf,
  929. size_t len, void *data)
  930. {
  931. struct config_source top;
  932. top.u.buf.buf = buf;
  933. top.u.buf.len = len;
  934. top.u.buf.pos = 0;
  935. top.name = name;
  936. top.path = NULL;
  937. top.die_on_error = 0;
  938. top.do_fgetc = config_buf_fgetc;
  939. top.do_ungetc = config_buf_ungetc;
  940. top.do_ftell = config_buf_ftell;
  941. return do_config_from(&top, fn, data);
  942. }
  943. static int git_config_from_blob_sha1(config_fn_t fn,
  944. const char *name,
  945. const unsigned char *sha1,
  946. void *data)
  947. {
  948. enum object_type type;
  949. char *buf;
  950. unsigned long size;
  951. int ret;
  952. buf = read_sha1_file(sha1, &type, &size);
  953. if (!buf)
  954. return error("unable to load config blob object '%s'", name);
  955. if (type != OBJ_BLOB) {
  956. free(buf);
  957. return error("reference '%s' does not point to a blob", name);
  958. }
  959. ret = git_config_from_buf(fn, name, buf, size, data);
  960. free(buf);
  961. return ret;
  962. }
  963. static int git_config_from_blob_ref(config_fn_t fn,
  964. const char *name,
  965. void *data)
  966. {
  967. unsigned char sha1[20];
  968. if (get_sha1(name, sha1) < 0)
  969. return error("unable to resolve config blob '%s'", name);
  970. return git_config_from_blob_sha1(fn, name, sha1, data);
  971. }
  972. const char *git_etc_gitconfig(void)
  973. {
  974. static const char *system_wide;
  975. if (!system_wide)
  976. system_wide = system_path(ETC_GITCONFIG);
  977. return system_wide;
  978. }
  979. int git_env_bool(const char *k, int def)
  980. {
  981. const char *v = getenv(k);
  982. return v ? git_config_bool(k, v) : def;
  983. }
  984. int git_config_system(void)
  985. {
  986. return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
  987. }
  988. int git_config_early(config_fn_t fn, void *data, const char *repo_config)
  989. {
  990. int ret = 0, found = 0;
  991. char *xdg_config = NULL;
  992. char *user_config = NULL;
  993. home_config_paths(&user_config, &xdg_config, "config");
  994. if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
  995. ret += git_config_from_file(fn, git_etc_gitconfig(),
  996. data);
  997. found += 1;
  998. }
  999. if (xdg_config && !access_or_die(xdg_config, R_OK, ACCESS_EACCES_OK)) {
  1000. ret += git_config_from_file(fn, xdg_config, data);
  1001. found += 1;
  1002. }
  1003. if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK)) {
  1004. ret += git_config_from_file(fn, user_config, data);
  1005. found += 1;
  1006. }
  1007. if (repo_config && !access_or_die(repo_config, R_OK, 0)) {
  1008. ret += git_config_from_file(fn, repo_config, data);
  1009. found += 1;
  1010. }
  1011. switch (git_config_from_parameters(fn, data)) {
  1012. case -1: /* error */
  1013. die("unable to parse command-line config");
  1014. break;
  1015. case 0: /* found nothing */
  1016. break;
  1017. default: /* found at least one item */
  1018. found++;
  1019. break;
  1020. }
  1021. free(xdg_config);
  1022. free(user_config);
  1023. return ret == 0 ? found : ret;
  1024. }
  1025. int git_config_with_options(config_fn_t fn, void *data,
  1026. struct git_config_source *config_source,
  1027. int respect_includes)
  1028. {
  1029. char *repo_config = NULL;
  1030. int ret;
  1031. struct config_include_data inc = CONFIG_INCLUDE_INIT;
  1032. if (respect_includes) {
  1033. inc.fn = fn;
  1034. inc.data = data;
  1035. fn = git_config_include;
  1036. data = &inc;
  1037. }
  1038. /*
  1039. * If we have a specific filename, use it. Otherwise, follow the
  1040. * regular lookup sequence.
  1041. */
  1042. if (config_source && config_source->use_stdin)
  1043. return git_config_from_stdin(fn, data);
  1044. else if (config_source && config_source->file)
  1045. return git_config_from_file(fn, config_source->file, data);
  1046. else if (config_source && config_source->blob)
  1047. return git_config_from_blob_ref(fn, config_source->blob, data);
  1048. repo_config = git_pathdup("config");
  1049. ret = git_config_early(fn, data, repo_config);
  1050. if (repo_config)
  1051. free(repo_config);
  1052. return ret;
  1053. }
  1054. int git_config(config_fn_t fn, void *data)
  1055. {
  1056. return git_config_with_options(fn, data, NULL, 1);
  1057. }
  1058. /*
  1059. * Find all the stuff for git_config_set() below.
  1060. */
  1061. static struct {
  1062. int baselen;
  1063. char *key;
  1064. int do_not_match;
  1065. regex_t *value_regex;
  1066. int multi_replace;
  1067. size_t *offset;
  1068. unsigned int offset_alloc;
  1069. enum { START, SECTION_SEEN, SECTION_END_SEEN, KEY_SEEN } state;
  1070. int seen;
  1071. } store;
  1072. static int matches(const char *key, const char *value)
  1073. {
  1074. return !strcmp(key, store.key) &&
  1075. (store.value_regex == NULL ||
  1076. (store.do_not_match ^
  1077. !regexec(store.value_regex, value, 0, NULL, 0)));
  1078. }
  1079. static int store_aux(const char *key, const char *value, void *cb)
  1080. {
  1081. const char *ep;
  1082. size_t section_len;
  1083. switch (store.state) {
  1084. case KEY_SEEN:
  1085. if (matches(key, value)) {
  1086. if (store.seen == 1 && store.multi_replace == 0) {
  1087. warning("%s has multiple values", key);
  1088. }
  1089. ALLOC_GROW(store.offset, store.seen + 1,
  1090. store.offset_alloc);
  1091. store.offset[store.seen] = cf->do_ftell(cf);
  1092. store.seen++;
  1093. }
  1094. break;
  1095. case SECTION_SEEN:
  1096. /*
  1097. * What we are looking for is in store.key (both
  1098. * section and var), and its section part is baselen
  1099. * long. We found key (again, both section and var).
  1100. * We would want to know if this key is in the same
  1101. * section as what we are looking for. We already
  1102. * know we are in the same section as what should
  1103. * hold store.key.
  1104. */
  1105. ep = strrchr(key, '.');
  1106. section_len = ep - key;
  1107. if ((section_len != store.baselen) ||
  1108. memcmp(key, store.key, section_len+1)) {
  1109. store.state = SECTION_END_SEEN;
  1110. break;
  1111. }
  1112. /*
  1113. * Do not increment matches: this is no match, but we
  1114. * just made sure we are in the desired section.
  1115. */
  1116. ALLOC_GROW(store.offset, store.seen + 1,
  1117. store.offset_alloc);
  1118. store.offset[store.seen] = cf->do_ftell(cf);
  1119. /* fallthru */
  1120. case SECTION_END_SEEN:
  1121. case START:
  1122. if (matches(key, value)) {
  1123. ALLOC_GROW(store.offset, store.seen + 1,
  1124. store.offset_alloc);
  1125. store.offset[store.seen] = cf->do_ftell(cf);
  1126. store.state = KEY_SEEN;
  1127. store.seen++;
  1128. } else {
  1129. if (strrchr(key, '.') - key == store.baselen &&
  1130. !strncmp(key, store.key, store.baselen)) {
  1131. store.state = SECTION_SEEN;
  1132. ALLOC_GROW(store.offset,
  1133. store.seen + 1,
  1134. store.offset_alloc);
  1135. store.offset[store.seen] = cf->do_ftell(cf);
  1136. }
  1137. }
  1138. }
  1139. return 0;
  1140. }
  1141. static int write_error(const char *filename)
  1142. {
  1143. error("failed to write new configuration file %s", filename);
  1144. /* Same error code as "failed to rename". */
  1145. return 4;
  1146. }
  1147. static int store_write_section(int fd, const char *key)
  1148. {
  1149. const char *dot;
  1150. int i, success;
  1151. struct strbuf sb = STRBUF_INIT;
  1152. dot = memchr(key, '.', store.baselen);
  1153. if (dot) {
  1154. strbuf_addf(&sb, "[%.*s \"", (int)(dot - key), key);
  1155. for (i = dot - key + 1; i < store.baselen; i++) {
  1156. if (key[i] == '"' || key[i] == '\\')
  1157. strbuf_addch(&sb, '\\');
  1158. strbuf_addch(&sb, key[i]);
  1159. }
  1160. strbuf_addstr(&sb, "\"]\n");
  1161. } else {
  1162. strbuf_addf(&sb, "[%.*s]\n", store.baselen, key);
  1163. }
  1164. success = write_in_full(fd, sb.buf, sb.len) == sb.len;
  1165. strbuf_release(&sb);
  1166. return success;
  1167. }
  1168. static int store_write_pair(int fd, const char *key, const char *value)
  1169. {
  1170. int i, success;
  1171. int length = strlen(key + store.baselen + 1);
  1172. const char *quote = "";
  1173. struct strbuf sb = STRBUF_INIT;
  1174. /*
  1175. * Check to see if the value needs to be surrounded with a dq pair.
  1176. * Note that problematic characters are always backslash-quoted; this
  1177. * check is about not losing leading or trailing SP and strings that
  1178. * follow beginning-of-comment characters (i.e. ';' and '#') by the
  1179. * configuration parser.
  1180. */
  1181. if (value[0] == ' ')
  1182. quote = "\"";
  1183. for (i = 0; value[i]; i++)
  1184. if (value[i] == ';' || value[i] == '#')
  1185. quote = "\"";
  1186. if (i && value[i - 1] == ' ')
  1187. quote = "\"";
  1188. strbuf_addf(&sb, "\t%.*s = %s",
  1189. length, key + store.baselen + 1, quote);
  1190. for (i = 0; value[i]; i++)
  1191. switch (value[i]) {
  1192. case '\n':
  1193. strbuf_addstr(&sb, "\\n");
  1194. break;
  1195. case '\t':
  1196. strbuf_addstr(&sb, "\\t");
  1197. break;
  1198. case '"':
  1199. case '\\':
  1200. strbuf_addch(&sb, '\\');
  1201. default:
  1202. strbuf_addch(&sb, value[i]);
  1203. break;
  1204. }
  1205. strbuf_addf(&sb, "%s\n", quote);
  1206. success = write_in_full(fd, sb.buf, sb.len) == sb.len;
  1207. strbuf_release(&sb);
  1208. return success;
  1209. }
  1210. static ssize_t find_beginning_of_line(const char *contents, size_t size,
  1211. size_t offset_, int *found_bracket)
  1212. {
  1213. size_t equal_offset = size, bracket_offset = size;
  1214. ssize_t offset;
  1215. contline:
  1216. for (offset = offset_-2; offset > 0
  1217. && contents[offset] != '\n'; offset--)
  1218. switch (contents[offset]) {
  1219. case '=': equal_offset = offset; break;
  1220. case ']': bracket_offset = offset; break;
  1221. }
  1222. if (offset > 0 && contents[offset-1] == '\\') {
  1223. offset_ = offset;
  1224. goto contline;
  1225. }
  1226. if (bracket_offset < equal_offset) {
  1227. *found_bracket = 1;
  1228. offset = bracket_offset+1;
  1229. } else
  1230. offset++;
  1231. return offset;
  1232. }
  1233. int git_config_set_in_file(const char *config_filename,
  1234. const char *key, const char *value)
  1235. {
  1236. return git_config_set_multivar_in_file(config_filename, key, value, NULL, 0);
  1237. }
  1238. int git_config_set(const char *key, const char *value)
  1239. {
  1240. return git_config_set_multivar(key, value, NULL, 0);
  1241. }
  1242. /*
  1243. * Auxiliary function to sanity-check and split the key into the section
  1244. * identifier and variable name.
  1245. *
  1246. * Returns 0 on success, -1 when there is an invalid character in the key and
  1247. * -2 if there is no section name in the key.
  1248. *
  1249. * store_key - pointer to char* which will hold a copy of the key with
  1250. * lowercase section and variable name
  1251. * baselen - pointer to int which will hold the length of the
  1252. * section + subsection part, can be NULL
  1253. */
  1254. int git_config_parse_key(const char *key, char **store_key, int *baselen_)
  1255. {
  1256. int i, dot, baselen;
  1257. const char *last_dot = strrchr(key, '.');
  1258. /*
  1259. * Since "key" actually contains the section name and the real
  1260. * key name separated by a dot, we have to know where the dot is.
  1261. */
  1262. if (last_dot == NULL || last_dot == key) {
  1263. error("key does not contain a section: %s", key);
  1264. return -CONFIG_NO_SECTION_OR_NAME;
  1265. }
  1266. if (!last_dot[1]) {
  1267. error("key does not contain variable name: %s", key);
  1268. return -CONFIG_NO_SECTION_OR_NAME;
  1269. }
  1270. baselen = last_dot - key;
  1271. if (baselen_)
  1272. *baselen_ = baselen;
  1273. /*
  1274. * Validate the key and while at it, lower case it for matching.
  1275. */
  1276. *store_key = xmalloc(strlen(key) + 1);
  1277. dot = 0;
  1278. for (i = 0; key[i]; i++) {
  1279. unsigned char c = key[i];
  1280. if (c == '.')
  1281. dot = 1;
  1282. /* Leave the extended basename untouched.. */
  1283. if (!dot || i > baselen) {
  1284. if (!iskeychar(c) ||
  1285. (i == baselen + 1 && !isalpha(c))) {
  1286. error("invalid key: %s", key);
  1287. goto out_free_ret_1;
  1288. }
  1289. c = tolower(c);
  1290. } else if (c == '\n') {
  1291. error("invalid key (newline): %s", key);
  1292. goto out_free_ret_1;
  1293. }
  1294. (*store_key)[i] = c;
  1295. }
  1296. (*store_key)[i] = 0;
  1297. return 0;
  1298. out_free_ret_1:
  1299. free(*store_key);
  1300. *store_key = NULL;
  1301. return -CONFIG_INVALID_KEY;
  1302. }
  1303. /*
  1304. * If value==NULL, unset in (remove from) config,
  1305. * if value_regex!=NULL, disregard key/value pairs where value does not match.
  1306. * if multi_replace==0, nothing, or only one matching key/value is replaced,
  1307. * else all matching key/values (regardless how many) are removed,
  1308. * before the new pair is written.
  1309. *
  1310. * Returns 0 on success.
  1311. *
  1312. * This function does this:
  1313. *
  1314. * - it locks the config file by creating ".git/config.lock"
  1315. *
  1316. * - it then parses the config using store_aux() as validator to find
  1317. * the position on the key/value pair to replace. If it is to be unset,
  1318. * it must be found exactly once.
  1319. *
  1320. * - the config file is mmap()ed and the part before the match (if any) is
  1321. * written to the lock file, then the changed part and the rest.
  1322. *
  1323. * - the config file is removed and the lock file rename()d to it.
  1324. *
  1325. */
  1326. int git_config_set_multivar_in_file(const char *config_filename,
  1327. const char *key, const char *value,
  1328. const char *value_regex, int multi_replace)
  1329. {
  1330. int fd = -1, in_fd;
  1331. int ret;
  1332. struct lock_file *lock = NULL;
  1333. char *filename_buf = NULL;
  1334. /* parse-key returns negative; flip the sign to feed exit(3) */
  1335. ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);
  1336. if (ret)
  1337. goto out_free;
  1338. store.multi_replace = multi_replace;
  1339. if (!config_filename)
  1340. config_filename = filename_buf = git_pathdup("config");
  1341. /*
  1342. * The lock serves a purpose in addition to locking: the new
  1343. * contents of .git/config will be written into it.
  1344. */
  1345. lock = xcalloc(sizeof(struct lock_file), 1);
  1346. fd = hold_lock_file_for_update(lock, config_filename, 0);
  1347. if (fd < 0) {
  1348. error("could not lock config file %s: %s", config_filename, strerror(errno));
  1349. free(store.key);
  1350. ret = CONFIG_NO_LOCK;
  1351. goto out_free;
  1352. }
  1353. /*
  1354. * If .git/config does not exist yet, write a minimal version.
  1355. */
  1356. in_fd = open(config_filename, O_RDONLY);
  1357. if ( in_fd < 0 ) {
  1358. free(store.key);
  1359. if ( ENOENT != errno ) {
  1360. error("opening %s: %s", config_filename,
  1361. strerror(errno));
  1362. ret = CONFIG_INVALID_FILE; /* same as "invalid config file" */
  1363. goto out_free;
  1364. }
  1365. /* if nothing to unset, error out */
  1366. if (value == NULL) {
  1367. ret = CONFIG_NOTHING_SET;
  1368. goto out_free;
  1369. }
  1370. store.key = (char *)key;
  1371. if (!store_write_section(fd, key) ||
  1372. !store_write_pair(fd, key, value))
  1373. goto write_err_out;
  1374. } else {
  1375. struct stat st;
  1376. char *contents;
  1377. size_t contents_sz, copy_begin, copy_end;
  1378. int i, new_line = 0;
  1379. if (value_regex == NULL)
  1380. store.value_regex = NULL;
  1381. else {
  1382. if (value_regex[0] == '!') {
  1383. store.do_not_match = 1;
  1384. value_regex++;
  1385. } else
  1386. store.do_not_match = 0;
  1387. store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
  1388. if (regcomp(store.value_regex, value_regex,
  1389. REG_EXTENDED)) {
  1390. error("invalid pattern: %s", value_regex);
  1391. free(store.value_regex);
  1392. ret = CONFIG_INVALID_PATTERN;
  1393. goto out_free;
  1394. }
  1395. }
  1396. ALLOC_GROW(store.offset, 1, store.offset_alloc);
  1397. store.offset[0] = 0;
  1398. store.state = START;
  1399. store.seen = 0;
  1400. /*
  1401. * After this, store.offset will contain the *end* offset
  1402. * of the last match, or remain at 0 if no match was found.
  1403. * As a side effect, we make sure to transform only a valid
  1404. * existing config file.
  1405. */
  1406. if (git_config_from_file(store_aux, config_filename, NULL)) {
  1407. error("invalid config file %s", config_filename);
  1408. free(store.key);
  1409. if (store.value_regex != NULL) {
  1410. regfree(store.value_regex);
  1411. free(store.value_regex);
  1412. }
  1413. ret = CONFIG_INVALID_FILE;
  1414. goto out_free;
  1415. }
  1416. free(store.key);
  1417. if (store.value_regex != NULL) {
  1418. regfree(store.value_regex);
  1419. free(store.value_regex);
  1420. }
  1421. /* if nothing to unset, or too many matches, error out */
  1422. if ((store.seen == 0 && value == NULL) ||
  1423. (store.seen > 1 && multi_replace == 0)) {
  1424. ret = CONFIG_NOTHING_SET;
  1425. goto out_free;
  1426. }
  1427. fstat(in_fd, &st);
  1428. contents_sz = xsize_t(st.st_size);
  1429. contents = xmmap(NULL, contents_sz, PROT_READ,
  1430. MAP_PRIVATE, in_fd, 0);
  1431. close(in_fd);
  1432. if (store.seen == 0)
  1433. store.seen = 1;
  1434. for (i = 0, copy_begin = 0; i < store.seen; i++) {
  1435. if (store.offset[i] == 0) {
  1436. store.offset[i] = copy_end = contents_sz;
  1437. } else if (store.state != KEY_SEEN) {
  1438. copy_end = store.offset[i];
  1439. } else
  1440. copy_end = find_beginning_of_line(
  1441. contents, contents_sz,
  1442. store.offset[i]-2, &new_line);
  1443. if (copy_end > 0 && contents[copy_end-1] != '\n')
  1444. new_line = 1;
  1445. /* write the first part of the config */
  1446. if (copy_end > copy_begin) {
  1447. if (write_in_full(fd, contents + copy_begin,
  1448. copy_end - copy_begin) <
  1449. copy_end - copy_begin)
  1450. goto write_err_out;
  1451. if (new_line &&
  1452. write_str_in_full(fd, "\n") != 1)
  1453. goto write_err_out;
  1454. }
  1455. copy_begin = store.offset[i];
  1456. }
  1457. /* write the pair (value == NULL means unset) */
  1458. if (value != NULL) {
  1459. if (store.state == START) {
  1460. if (!store_write_section(fd, key))
  1461. goto write_err_out;
  1462. }
  1463. if (!store_write_pair(fd, key, value))
  1464. goto write_err_out;
  1465. }
  1466. /* write the rest of the config */
  1467. if (copy_begin < contents_sz)
  1468. if (write_in_full(fd, contents + copy_begin,
  1469. contents_sz - copy_begin) <
  1470. contents_sz - copy_begin)
  1471. goto write_err_out;
  1472. munmap(contents, contents_sz);
  1473. }
  1474. if (commit_lock_file(lock) < 0) {
  1475. error("could not commit config file %s", config_filename);
  1476. ret = CONFIG_NO_WRITE;
  1477. goto out_free;
  1478. }
  1479. /*
  1480. * lock is committed, so don't try to roll it back below.
  1481. * NOTE: Since lockfile.c keeps a linked list of all created
  1482. * lock_file structures, it isn't safe to free(lock). It's
  1483. * better to just leave it hanging around.
  1484. */
  1485. lock = NULL;
  1486. ret = 0;
  1487. out_free:
  1488. if (lock)
  1489. rollback_lock_file(lock);
  1490. free(filename_buf);
  1491. return ret;
  1492. write_err_out:
  1493. ret = write_error(lock->filename);
  1494. goto out_free;
  1495. }
  1496. int git_config_set_multivar(const char *key, const char *value,
  1497. const char *value_regex, int multi_replace)
  1498. {
  1499. return git_config_set_multivar_in_file(NULL, key, value, value_regex,
  1500. multi_replace);
  1501. }
  1502. static int section_name_match (const char *buf, const char *name)
  1503. {
  1504. int i = 0, j = 0, dot = 0;
  1505. if (buf[i] != '[')
  1506. return 0;
  1507. for (i = 1; buf[i] && buf[i] != ']'; i++) {
  1508. if (!dot && isspace(buf[i])) {
  1509. dot = 1;
  1510. if (name[j++] != '.')
  1511. break;
  1512. for (i++; isspace(buf[i]); i++)
  1513. ; /* do nothing */
  1514. if (buf[i] != '"')
  1515. break;
  1516. continue;
  1517. }
  1518. if (buf[i] == '\\' && dot)
  1519. i++;
  1520. else if (buf[i] == '"' && dot) {
  1521. for (i++; isspace(buf[i]); i++)
  1522. ; /* do_nothing */
  1523. break;
  1524. }
  1525. if (buf[i] != name[j++])
  1526. break;
  1527. }
  1528. if (buf[i] == ']' && name[j] == 0) {
  1529. /*
  1530. * We match, now just find the right length offset by
  1531. * gobbling up any whitespace after it, as well
  1532. */
  1533. i++;
  1534. for (; buf[i] && isspace(buf[i]); i++)
  1535. ; /* do nothing */
  1536. return i;
  1537. }
  1538. return 0;
  1539. }
  1540. static int section_name_is_ok(const char *name)
  1541. {
  1542. /* Empty section names are bogus. */
  1543. if (!*name)
  1544. return 0;
  1545. /*
  1546. * Before a dot, we must be alphanumeric or dash. After the first dot,
  1547. * anything goes, so we can stop checking.
  1548. */
  1549. for (; *name && *name != '.'; name++)
  1550. if (*name != '-' && !isalnum(*name))
  1551. return 0;
  1552. return 1;
  1553. }
  1554. /* if new_name == NULL, the section is removed instead */
  1555. int git_config_rename_section_in_file(const char *config_filename,
  1556. const char *old_name, const char *new_name)
  1557. {
  1558. int ret = 0, remove = 0;
  1559. char *filename_buf = NULL;
  1560. struct lock_file *lock;
  1561. int out_fd;
  1562. char buf[1024];
  1563. FILE *config_file;
  1564. if (new_name && !section_name_is_ok(new_name)) {
  1565. ret = error("invalid section name: %s", new_name);
  1566. goto out;
  1567. }
  1568. if (!config_filename)
  1569. config_filename = filename_buf = git_pathdup("config");
  1570. lock = xcalloc(sizeof(struct lock_file), 1);
  1571. out_fd = hold_lock_file_for_update(lock, config_filename, 0);
  1572. if (out_fd < 0) {
  1573. ret = error("could not lock config file %s", config_filename);
  1574. goto out;
  1575. }
  1576. if (!(config_file = fopen(config_filename, "rb"))) {
  1577. /* no config file means nothing to rename, no error */
  1578. goto unlock_and_out;
  1579. }
  1580. while (fgets(buf, sizeof(buf), config_file)) {
  1581. int i;
  1582. int length;
  1583. char *output = buf;
  1584. for (i = 0; buf[i] && isspace(buf[i]); i++)
  1585. ; /* do nothing */
  1586. if (buf[i] == '[') {
  1587. /* it's a section */
  1588. int offset = section_name_match(&buf[i], old_name);
  1589. if (offset > 0) {
  1590. ret++;
  1591. if (new_name == NULL) {
  1592. remove = 1;
  1593. continue;
  1594. }
  1595. store.baselen = strlen(new_name);
  1596. if (!store_write_section(out_fd, new_name)) {
  1597. ret = write_error(lock->filename);
  1598. goto out;
  1599. }
  1600. /*
  1601. * We wrote out the new section, with
  1602. * a newline, now skip the old
  1603. * section's length
  1604. */
  1605. output += offset + i;
  1606. if (strlen(output) > 0) {
  1607. /*
  1608. * More content means there's
  1609. * a declaration to put on the
  1610. * next line; indent with a
  1611. * tab
  1612. */
  1613. output -= 1;
  1614. output[0] = '\t';
  1615. }
  1616. }
  1617. remove = 0;
  1618. }
  1619. if (remove)
  1620. continue;
  1621. length = strlen(output);
  1622. if (write_in_full(out_fd, output, length) != length) {
  1623. ret = write_error(lock->filename);
  1624. goto out;
  1625. }
  1626. }
  1627. fclose(config_file);
  1628. unlock_and_out:
  1629. if (commit_lock_file(lock) < 0)
  1630. ret = error("could not commit config file %s", config_filename);
  1631. out:
  1632. free(filename_buf);
  1633. return ret;
  1634. }
  1635. int git_config_rename_section(const char *old_name, const char *new_name)
  1636. {
  1637. return git_config_rename_section_in_file(NULL, old_name, new_name);
  1638. }
  1639. /*
  1640. * Call this to report error for your variable that should not
  1641. * get a boolean value (i.e. "[my] var" means "true").
  1642. */
  1643. #undef config_error_nonbool
  1644. int config_error_nonbool(const char *var)
  1645. {
  1646. return error("Missing value for '%s'", var);
  1647. }
  1648. int parse_config_key(const char *var,
  1649. const char *section,
  1650. const char **subsection, int *subsection_len,
  1651. const char **key)
  1652. {
  1653. int section_len = strlen(section);
  1654. const char *dot;
  1655. /* Does it start with "section." ? */
  1656. if (!starts_with(var, section) || var[section_len] != '.')
  1657. return -1;
  1658. /*
  1659. * Find the key; we don't know yet if we have a subsection, but we must
  1660. * parse backwards from the end, since the subsection may have dots in
  1661. * it, too.
  1662. */
  1663. dot = strrchr(var, '.');
  1664. *key = dot + 1;
  1665. /* Did we have a subsection at all? */
  1666. if (dot == var + section_len) {
  1667. *subsection = NULL;
  1668. *subsection_len = 0;
  1669. }
  1670. else {
  1671. *subsection = var + section_len + 1;
  1672. *subsection_len = dot - *subsection;
  1673. }
  1674. return 0;
  1675. }