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

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

755 lines
22KB

  1. #include "builtin.h"
  2. #include "cache.h"
  3. #include "config.h"
  4. #include "color.h"
  5. #include "parse-options.h"
  6. #include "urlmatch.h"
  7. #include "quote.h"
  8. static const char *const builtin_config_usage[] = {
  9. N_("git config [<options>]"),
  10. NULL
  11. };
  12. static char *key;
  13. static regex_t *key_regexp;
  14. static regex_t *regexp;
  15. static int show_keys;
  16. static int omit_values;
  17. static int use_key_regexp;
  18. static int do_all;
  19. static int do_not_match;
  20. static char delim = '=';
  21. static char key_delim = ' ';
  22. static char term = '\n';
  23. static int use_global_config, use_system_config, use_local_config;
  24. static struct git_config_source given_config_source;
  25. static int actions, types;
  26. static int end_null;
  27. static int respect_includes_opt = -1;
  28. static struct config_options config_options;
  29. static int show_origin;
  30. #define ACTION_GET (1<<0)
  31. #define ACTION_GET_ALL (1<<1)
  32. #define ACTION_GET_REGEXP (1<<2)
  33. #define ACTION_REPLACE_ALL (1<<3)
  34. #define ACTION_ADD (1<<4)
  35. #define ACTION_UNSET (1<<5)
  36. #define ACTION_UNSET_ALL (1<<6)
  37. #define ACTION_RENAME_SECTION (1<<7)
  38. #define ACTION_REMOVE_SECTION (1<<8)
  39. #define ACTION_LIST (1<<9)
  40. #define ACTION_EDIT (1<<10)
  41. #define ACTION_SET (1<<11)
  42. #define ACTION_SET_ALL (1<<12)
  43. #define ACTION_GET_COLOR (1<<13)
  44. #define ACTION_GET_COLORBOOL (1<<14)
  45. #define ACTION_GET_URLMATCH (1<<15)
  46. /*
  47. * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than
  48. * one line of output and which should therefore be paged.
  49. */
  50. #define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \
  51. ACTION_GET_REGEXP | ACTION_GET_URLMATCH)
  52. #define TYPE_BOOL (1<<0)
  53. #define TYPE_INT (1<<1)
  54. #define TYPE_BOOL_OR_INT (1<<2)
  55. #define TYPE_PATH (1<<3)
  56. #define TYPE_EXPIRY_DATE (1<<4)
  57. static struct option builtin_config_options[] = {
  58. OPT_GROUP(N_("Config file location")),
  59. OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
  60. OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
  61. OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
  62. OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
  63. OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
  64. OPT_GROUP(N_("Action")),
  65. OPT_BIT(0, "get", &actions, N_("get value: name [value-regex]"), ACTION_GET),
  66. OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-regex]"), ACTION_GET_ALL),
  67. OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-regex]"), ACTION_GET_REGEXP),
  68. OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
  69. OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value_regex]"), ACTION_REPLACE_ALL),
  70. OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
  71. OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-regex]"), ACTION_UNSET),
  72. OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-regex]"), ACTION_UNSET_ALL),
  73. OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
  74. OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
  75. OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
  76. OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
  77. OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
  78. OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
  79. OPT_GROUP(N_("Type")),
  80. OPT_BIT(0, "bool", &types, N_("value is \"true\" or \"false\""), TYPE_BOOL),
  81. OPT_BIT(0, "int", &types, N_("value is decimal number"), TYPE_INT),
  82. OPT_BIT(0, "bool-or-int", &types, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
  83. OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
  84. OPT_BIT(0, "expiry-date", &types, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
  85. OPT_GROUP(N_("Other")),
  86. OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
  87. OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
  88. OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
  89. OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
  90. OPT_END(),
  91. };
  92. static void check_argc(int argc, int min, int max) {
  93. if (argc >= min && argc <= max)
  94. return;
  95. error("wrong number of arguments");
  96. usage_with_options(builtin_config_usage, builtin_config_options);
  97. }
  98. static void show_config_origin(struct strbuf *buf)
  99. {
  100. const char term = end_null ? '\0' : '\t';
  101. strbuf_addstr(buf, current_config_origin_type());
  102. strbuf_addch(buf, ':');
  103. if (end_null)
  104. strbuf_addstr(buf, current_config_name());
  105. else
  106. quote_c_style(current_config_name(), buf, NULL, 0);
  107. strbuf_addch(buf, term);
  108. }
  109. static int show_all_config(const char *key_, const char *value_, void *cb)
  110. {
  111. if (show_origin) {
  112. struct strbuf buf = STRBUF_INIT;
  113. show_config_origin(&buf);
  114. /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
  115. fwrite(buf.buf, 1, buf.len, stdout);
  116. strbuf_release(&buf);
  117. }
  118. if (!omit_values && value_)
  119. printf("%s%c%s%c", key_, delim, value_, term);
  120. else
  121. printf("%s%c", key_, term);
  122. return 0;
  123. }
  124. struct strbuf_list {
  125. struct strbuf *items;
  126. int nr;
  127. int alloc;
  128. };
  129. static int format_config(struct strbuf *buf, const char *key_, const char *value_)
  130. {
  131. if (show_origin)
  132. show_config_origin(buf);
  133. if (show_keys)
  134. strbuf_addstr(buf, key_);
  135. if (!omit_values) {
  136. if (show_keys)
  137. strbuf_addch(buf, key_delim);
  138. if (types == TYPE_INT)
  139. strbuf_addf(buf, "%"PRId64,
  140. git_config_int64(key_, value_ ? value_ : ""));
  141. else if (types == TYPE_BOOL)
  142. strbuf_addstr(buf, git_config_bool(key_, value_) ?
  143. "true" : "false");
  144. else if (types == TYPE_BOOL_OR_INT) {
  145. int is_bool, v;
  146. v = git_config_bool_or_int(key_, value_, &is_bool);
  147. if (is_bool)
  148. strbuf_addstr(buf, v ? "true" : "false");
  149. else
  150. strbuf_addf(buf, "%d", v);
  151. } else if (types == TYPE_PATH) {
  152. const char *v;
  153. if (git_config_pathname(&v, key_, value_) < 0)
  154. return -1;
  155. strbuf_addstr(buf, v);
  156. free((char *)v);
  157. } else if (types == TYPE_EXPIRY_DATE) {
  158. timestamp_t t;
  159. if (git_config_expiry_date(&t, key_, value_) < 0)
  160. return -1;
  161. strbuf_addf(buf, "%"PRItime, t);
  162. } else if (value_) {
  163. strbuf_addstr(buf, value_);
  164. } else {
  165. /* Just show the key name; back out delimiter */
  166. if (show_keys)
  167. strbuf_setlen(buf, buf->len - 1);
  168. }
  169. }
  170. strbuf_addch(buf, term);
  171. return 0;
  172. }
  173. static int collect_config(const char *key_, const char *value_, void *cb)
  174. {
  175. struct strbuf_list *values = cb;
  176. if (!use_key_regexp && strcmp(key_, key))
  177. return 0;
  178. if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
  179. return 0;
  180. if (regexp != NULL &&
  181. (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
  182. return 0;
  183. ALLOC_GROW(values->items, values->nr + 1, values->alloc);
  184. strbuf_init(&values->items[values->nr], 0);
  185. return format_config(&values->items[values->nr++], key_, value_);
  186. }
  187. static int get_value(const char *key_, const char *regex_)
  188. {
  189. int ret = CONFIG_GENERIC_ERROR;
  190. struct strbuf_list values = {NULL};
  191. int i;
  192. if (use_key_regexp) {
  193. char *tl;
  194. /*
  195. * NEEDSWORK: this naive pattern lowercasing obviously does not
  196. * work for more complex patterns like "^[^.]*Foo.*bar".
  197. * Perhaps we should deprecate this altogether someday.
  198. */
  199. key = xstrdup(key_);
  200. for (tl = key + strlen(key) - 1;
  201. tl >= key && *tl != '.';
  202. tl--)
  203. *tl = tolower(*tl);
  204. for (tl = key; *tl && *tl != '.'; tl++)
  205. *tl = tolower(*tl);
  206. key_regexp = (regex_t*)xmalloc(sizeof(regex_t));
  207. if (regcomp(key_regexp, key, REG_EXTENDED)) {
  208. error("invalid key pattern: %s", key_);
  209. FREE_AND_NULL(key_regexp);
  210. ret = CONFIG_INVALID_PATTERN;
  211. goto free_strings;
  212. }
  213. } else {
  214. if (git_config_parse_key(key_, &key, NULL)) {
  215. ret = CONFIG_INVALID_KEY;
  216. goto free_strings;
  217. }
  218. }
  219. if (regex_) {
  220. if (regex_[0] == '!') {
  221. do_not_match = 1;
  222. regex_++;
  223. }
  224. regexp = (regex_t*)xmalloc(sizeof(regex_t));
  225. if (regcomp(regexp, regex_, REG_EXTENDED)) {
  226. error("invalid pattern: %s", regex_);
  227. FREE_AND_NULL(regexp);
  228. ret = CONFIG_INVALID_PATTERN;
  229. goto free_strings;
  230. }
  231. }
  232. config_with_options(collect_config, &values,
  233. &given_config_source, &config_options);
  234. ret = !values.nr;
  235. for (i = 0; i < values.nr; i++) {
  236. struct strbuf *buf = values.items + i;
  237. if (do_all || i == values.nr - 1)
  238. fwrite(buf->buf, 1, buf->len, stdout);
  239. strbuf_release(buf);
  240. }
  241. free(values.items);
  242. free_strings:
  243. free(key);
  244. if (key_regexp) {
  245. regfree(key_regexp);
  246. free(key_regexp);
  247. }
  248. if (regexp) {
  249. regfree(regexp);
  250. free(regexp);
  251. }
  252. return ret;
  253. }
  254. static char *normalize_value(const char *key, const char *value)
  255. {
  256. if (!value)
  257. return NULL;
  258. if (types == 0 || types == TYPE_PATH || types == TYPE_EXPIRY_DATE)
  259. /*
  260. * We don't do normalization for TYPE_PATH here: If
  261. * the path is like ~/foobar/, we prefer to store
  262. * "~/foobar/" in the config file, and to expand the ~
  263. * when retrieving the value.
  264. * Also don't do normalization for expiry dates.
  265. */
  266. return xstrdup(value);
  267. if (types == TYPE_INT)
  268. return xstrfmt("%"PRId64, git_config_int64(key, value));
  269. if (types == TYPE_BOOL)
  270. return xstrdup(git_config_bool(key, value) ? "true" : "false");
  271. if (types == TYPE_BOOL_OR_INT) {
  272. int is_bool, v;
  273. v = git_config_bool_or_int(key, value, &is_bool);
  274. if (!is_bool)
  275. return xstrfmt("%d", v);
  276. else
  277. return xstrdup(v ? "true" : "false");
  278. }
  279. BUG("cannot normalize type %d", types);
  280. }
  281. static int get_color_found;
  282. static const char *get_color_slot;
  283. static const char *get_colorbool_slot;
  284. static char parsed_color[COLOR_MAXLEN];
  285. static int git_get_color_config(const char *var, const char *value, void *cb)
  286. {
  287. if (!strcmp(var, get_color_slot)) {
  288. if (!value)
  289. config_error_nonbool(var);
  290. if (color_parse(value, parsed_color) < 0)
  291. return -1;
  292. get_color_found = 1;
  293. }
  294. return 0;
  295. }
  296. static void get_color(const char *var, const char *def_color)
  297. {
  298. get_color_slot = var;
  299. get_color_found = 0;
  300. parsed_color[0] = '\0';
  301. config_with_options(git_get_color_config, NULL,
  302. &given_config_source, &config_options);
  303. if (!get_color_found && def_color) {
  304. if (color_parse(def_color, parsed_color) < 0)
  305. die(_("unable to parse default color value"));
  306. }
  307. fputs(parsed_color, stdout);
  308. }
  309. static int get_colorbool_found;
  310. static int get_diff_color_found;
  311. static int get_color_ui_found;
  312. static int git_get_colorbool_config(const char *var, const char *value,
  313. void *cb)
  314. {
  315. if (!strcmp(var, get_colorbool_slot))
  316. get_colorbool_found = git_config_colorbool(var, value);
  317. else if (!strcmp(var, "diff.color"))
  318. get_diff_color_found = git_config_colorbool(var, value);
  319. else if (!strcmp(var, "color.ui"))
  320. get_color_ui_found = git_config_colorbool(var, value);
  321. return 0;
  322. }
  323. static int get_colorbool(const char *var, int print)
  324. {
  325. get_colorbool_slot = var;
  326. get_colorbool_found = -1;
  327. get_diff_color_found = -1;
  328. get_color_ui_found = -1;
  329. config_with_options(git_get_colorbool_config, NULL,
  330. &given_config_source, &config_options);
  331. if (get_colorbool_found < 0) {
  332. if (!strcmp(get_colorbool_slot, "color.diff"))
  333. get_colorbool_found = get_diff_color_found;
  334. if (get_colorbool_found < 0)
  335. get_colorbool_found = get_color_ui_found;
  336. }
  337. if (get_colorbool_found < 0)
  338. /* default value if none found in config */
  339. get_colorbool_found = GIT_COLOR_AUTO;
  340. get_colorbool_found = want_color(get_colorbool_found);
  341. if (print) {
  342. printf("%s\n", get_colorbool_found ? "true" : "false");
  343. return 0;
  344. } else
  345. return get_colorbool_found ? 0 : 1;
  346. }
  347. static void check_write(void)
  348. {
  349. if (!given_config_source.file && !startup_info->have_repository)
  350. die("not in a git directory");
  351. if (given_config_source.use_stdin)
  352. die("writing to stdin is not supported");
  353. if (given_config_source.blob)
  354. die("writing config blobs is not supported");
  355. }
  356. struct urlmatch_current_candidate_value {
  357. char value_is_null;
  358. struct strbuf value;
  359. };
  360. static int urlmatch_collect_fn(const char *var, const char *value, void *cb)
  361. {
  362. struct string_list *values = cb;
  363. struct string_list_item *item = string_list_insert(values, var);
  364. struct urlmatch_current_candidate_value *matched = item->util;
  365. if (!matched) {
  366. matched = xmalloc(sizeof(*matched));
  367. strbuf_init(&matched->value, 0);
  368. item->util = matched;
  369. } else {
  370. strbuf_reset(&matched->value);
  371. }
  372. if (value) {
  373. strbuf_addstr(&matched->value, value);
  374. matched->value_is_null = 0;
  375. } else {
  376. matched->value_is_null = 1;
  377. }
  378. return 0;
  379. }
  380. static int get_urlmatch(const char *var, const char *url)
  381. {
  382. int ret;
  383. char *section_tail;
  384. struct string_list_item *item;
  385. struct urlmatch_config config = { STRING_LIST_INIT_DUP };
  386. struct string_list values = STRING_LIST_INIT_DUP;
  387. config.collect_fn = urlmatch_collect_fn;
  388. config.cascade_fn = NULL;
  389. config.cb = &values;
  390. if (!url_normalize(url, &config.url))
  391. die("%s", config.url.err);
  392. config.section = xstrdup_tolower(var);
  393. section_tail = strchr(config.section, '.');
  394. if (section_tail) {
  395. *section_tail = '\0';
  396. config.key = section_tail + 1;
  397. show_keys = 0;
  398. } else {
  399. config.key = NULL;
  400. show_keys = 1;
  401. }
  402. config_with_options(urlmatch_config_entry, &config,
  403. &given_config_source, &config_options);
  404. ret = !values.nr;
  405. for_each_string_list_item(item, &values) {
  406. struct urlmatch_current_candidate_value *matched = item->util;
  407. struct strbuf buf = STRBUF_INIT;
  408. format_config(&buf, item->string,
  409. matched->value_is_null ? NULL : matched->value.buf);
  410. fwrite(buf.buf, 1, buf.len, stdout);
  411. strbuf_release(&buf);
  412. strbuf_release(&matched->value);
  413. }
  414. string_list_clear(&config.vars, 1);
  415. string_list_clear(&values, 1);
  416. free(config.url.url);
  417. free((void *)config.section);
  418. return ret;
  419. }
  420. static char *default_user_config(void)
  421. {
  422. struct strbuf buf = STRBUF_INIT;
  423. strbuf_addf(&buf,
  424. _("# This is Git's per-user configuration file.\n"
  425. "[user]\n"
  426. "# Please adapt and uncomment the following lines:\n"
  427. "# name = %s\n"
  428. "# email = %s\n"),
  429. ident_default_name(),
  430. ident_default_email());
  431. return strbuf_detach(&buf, NULL);
  432. }
  433. int cmd_config(int argc, const char **argv, const char *prefix)
  434. {
  435. int nongit = !startup_info->have_repository;
  436. char *value;
  437. given_config_source.file = getenv(CONFIG_ENVIRONMENT);
  438. argc = parse_options(argc, argv, prefix, builtin_config_options,
  439. builtin_config_usage,
  440. PARSE_OPT_STOP_AT_NON_OPTION);
  441. if (use_global_config + use_system_config + use_local_config +
  442. !!given_config_source.file + !!given_config_source.blob > 1) {
  443. error("only one config file at a time.");
  444. usage_with_options(builtin_config_usage, builtin_config_options);
  445. }
  446. if (use_local_config && nongit)
  447. die(_("--local can only be used inside a git repository"));
  448. if (given_config_source.file &&
  449. !strcmp(given_config_source.file, "-")) {
  450. given_config_source.file = NULL;
  451. given_config_source.use_stdin = 1;
  452. }
  453. if (use_global_config) {
  454. char *user_config = expand_user_path("~/.gitconfig", 0);
  455. char *xdg_config = xdg_config_home("config");
  456. if (!user_config)
  457. /*
  458. * It is unknown if HOME/.gitconfig exists, so
  459. * we do not know if we should write to XDG
  460. * location; error out even if XDG_CONFIG_HOME
  461. * is set and points at a sane location.
  462. */
  463. die("$HOME not set");
  464. if (access_or_warn(user_config, R_OK, 0) &&
  465. xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
  466. given_config_source.file = xdg_config;
  467. free(user_config);
  468. } else {
  469. given_config_source.file = user_config;
  470. free(xdg_config);
  471. }
  472. }
  473. else if (use_system_config)
  474. given_config_source.file = git_etc_gitconfig();
  475. else if (use_local_config)
  476. given_config_source.file = git_pathdup("config");
  477. else if (given_config_source.file) {
  478. if (!is_absolute_path(given_config_source.file) && prefix)
  479. given_config_source.file =
  480. prefix_filename(prefix, given_config_source.file);
  481. }
  482. if (respect_includes_opt == -1)
  483. config_options.respect_includes = !given_config_source.file;
  484. else
  485. config_options.respect_includes = respect_includes_opt;
  486. if (!nongit) {
  487. config_options.commondir = get_git_common_dir();
  488. config_options.git_dir = get_git_dir();
  489. }
  490. if (end_null) {
  491. term = '\0';
  492. delim = '\n';
  493. key_delim = '\n';
  494. }
  495. if (HAS_MULTI_BITS(types)) {
  496. error("only one type at a time.");
  497. usage_with_options(builtin_config_usage, builtin_config_options);
  498. }
  499. if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && types) {
  500. error("--get-color and variable type are incoherent");
  501. usage_with_options(builtin_config_usage, builtin_config_options);
  502. }
  503. if (HAS_MULTI_BITS(actions)) {
  504. error("only one action at a time.");
  505. usage_with_options(builtin_config_usage, builtin_config_options);
  506. }
  507. if (actions == 0)
  508. switch (argc) {
  509. case 1: actions = ACTION_GET; break;
  510. case 2: actions = ACTION_SET; break;
  511. case 3: actions = ACTION_SET_ALL; break;
  512. default:
  513. usage_with_options(builtin_config_usage, builtin_config_options);
  514. }
  515. if (omit_values &&
  516. !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
  517. error("--name-only is only applicable to --list or --get-regexp");
  518. usage_with_options(builtin_config_usage, builtin_config_options);
  519. }
  520. if (show_origin && !(actions &
  521. (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
  522. error("--show-origin is only applicable to --get, --get-all, "
  523. "--get-regexp, and --list.");
  524. usage_with_options(builtin_config_usage, builtin_config_options);
  525. }
  526. if (actions & PAGING_ACTIONS)
  527. setup_auto_pager("config", 1);
  528. if (actions == ACTION_LIST) {
  529. check_argc(argc, 0, 0);
  530. if (config_with_options(show_all_config, NULL,
  531. &given_config_source,
  532. &config_options) < 0) {
  533. if (given_config_source.file)
  534. die_errno("unable to read config file '%s'",
  535. given_config_source.file);
  536. else
  537. die("error processing config file(s)");
  538. }
  539. }
  540. else if (actions == ACTION_EDIT) {
  541. char *config_file;
  542. check_argc(argc, 0, 0);
  543. if (!given_config_source.file && nongit)
  544. die("not in a git directory");
  545. if (given_config_source.use_stdin)
  546. die("editing stdin is not supported");
  547. if (given_config_source.blob)
  548. die("editing blobs is not supported");
  549. git_config(git_default_config, NULL);
  550. config_file = given_config_source.file ?
  551. xstrdup(given_config_source.file) :
  552. git_pathdup("config");
  553. if (use_global_config) {
  554. int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
  555. if (fd >= 0) {
  556. char *content = default_user_config();
  557. write_str_in_full(fd, content);
  558. free(content);
  559. close(fd);
  560. }
  561. else if (errno != EEXIST)
  562. die_errno(_("cannot create configuration file %s"), config_file);
  563. }
  564. launch_editor(config_file, NULL, NULL);
  565. free(config_file);
  566. }
  567. else if (actions == ACTION_SET) {
  568. int ret;
  569. check_write();
  570. check_argc(argc, 2, 2);
  571. value = normalize_value(argv[0], argv[1]);
  572. UNLEAK(value);
  573. ret = git_config_set_in_file_gently(given_config_source.file, argv[0], value);
  574. if (ret == CONFIG_NOTHING_SET)
  575. error(_("cannot overwrite multiple values with a single value\n"
  576. " Use a regexp, --add or --replace-all to change %s."), argv[0]);
  577. return ret;
  578. }
  579. else if (actions == ACTION_SET_ALL) {
  580. check_write();
  581. check_argc(argc, 2, 3);
  582. value = normalize_value(argv[0], argv[1]);
  583. UNLEAK(value);
  584. return git_config_set_multivar_in_file_gently(given_config_source.file,
  585. argv[0], value, argv[2], 0);
  586. }
  587. else if (actions == ACTION_ADD) {
  588. check_write();
  589. check_argc(argc, 2, 2);
  590. value = normalize_value(argv[0], argv[1]);
  591. UNLEAK(value);
  592. return git_config_set_multivar_in_file_gently(given_config_source.file,
  593. argv[0], value,
  594. CONFIG_REGEX_NONE, 0);
  595. }
  596. else if (actions == ACTION_REPLACE_ALL) {
  597. check_write();
  598. check_argc(argc, 2, 3);
  599. value = normalize_value(argv[0], argv[1]);
  600. UNLEAK(value);
  601. return git_config_set_multivar_in_file_gently(given_config_source.file,
  602. argv[0], value, argv[2], 1);
  603. }
  604. else if (actions == ACTION_GET) {
  605. check_argc(argc, 1, 2);
  606. return get_value(argv[0], argv[1]);
  607. }
  608. else if (actions == ACTION_GET_ALL) {
  609. do_all = 1;
  610. check_argc(argc, 1, 2);
  611. return get_value(argv[0], argv[1]);
  612. }
  613. else if (actions == ACTION_GET_REGEXP) {
  614. show_keys = 1;
  615. use_key_regexp = 1;
  616. do_all = 1;
  617. check_argc(argc, 1, 2);
  618. return get_value(argv[0], argv[1]);
  619. }
  620. else if (actions == ACTION_GET_URLMATCH) {
  621. check_argc(argc, 2, 2);
  622. return get_urlmatch(argv[0], argv[1]);
  623. }
  624. else if (actions == ACTION_UNSET) {
  625. check_write();
  626. check_argc(argc, 1, 2);
  627. if (argc == 2)
  628. return git_config_set_multivar_in_file_gently(given_config_source.file,
  629. argv[0], NULL, argv[1], 0);
  630. else
  631. return git_config_set_in_file_gently(given_config_source.file,
  632. argv[0], NULL);
  633. }
  634. else if (actions == ACTION_UNSET_ALL) {
  635. check_write();
  636. check_argc(argc, 1, 2);
  637. return git_config_set_multivar_in_file_gently(given_config_source.file,
  638. argv[0], NULL, argv[1], 1);
  639. }
  640. else if (actions == ACTION_RENAME_SECTION) {
  641. int ret;
  642. check_write();
  643. check_argc(argc, 2, 2);
  644. ret = git_config_rename_section_in_file(given_config_source.file,
  645. argv[0], argv[1]);
  646. if (ret < 0)
  647. return ret;
  648. if (ret == 0)
  649. die("No such section!");
  650. }
  651. else if (actions == ACTION_REMOVE_SECTION) {
  652. int ret;
  653. check_write();
  654. check_argc(argc, 1, 1);
  655. ret = git_config_rename_section_in_file(given_config_source.file,
  656. argv[0], NULL);
  657. if (ret < 0)
  658. return ret;
  659. if (ret == 0)
  660. die("No such section!");
  661. }
  662. else if (actions == ACTION_GET_COLOR) {
  663. check_argc(argc, 1, 2);
  664. get_color(argv[0], argv[1]);
  665. }
  666. else if (actions == ACTION_GET_COLORBOOL) {
  667. check_argc(argc, 1, 2);
  668. if (argc == 2)
  669. color_stdout_is_tty = git_config_bool("command line", argv[1]);
  670. return get_colorbool(argv[0], argc == 2);
  671. }
  672. return 0;
  673. }