1 /* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include "tig/types.h"
19 #include "tig/refdb.h"
20 #include "tig/options.h"
21 #include "tig/request.h"
30 #define DEFINE_OPTION_VARIABLES(name, type, flags) type opt_##name;
31 OPTION_INFO(DEFINE_OPTION_VARIABLES
);
33 static struct option_info option_info
[] = {
34 #define DEFINE_OPTION_INFO(name, type, flags) { #name, STRING_SIZE(#name), #type, &opt_##name },
35 OPTION_INFO(DEFINE_OPTION_INFO
)
39 find_option_info(struct option_info
*option
, size_t options
, const char *name
)
41 size_t namelen
= strlen(name
);
44 for (i
= 0; i
< options
; i
++)
45 if (enum_equals(option
[i
], name
, namelen
))
51 static struct option_info
*
52 find_option_info_by_value(void *value
)
56 for (i
= 0; i
< ARRAY_SIZE(option_info
); i
++)
57 if (option_info
[i
].value
== value
)
58 return &option_info
[i
];
64 mark_option_seen(void *value
)
66 struct option_info
*option
= find_option_info_by_value(value
);
76 iconv_t opt_iconv_out
= ICONV_NONE
;
77 char opt_editor
[SIZEOF_STR
] = "";
78 const char **opt_cmdline_argv
= NULL
;
79 const char **opt_rev_argv
= NULL
;
80 const char **opt_file_argv
= NULL
;
81 char opt_env_lines
[64] = "";
82 char opt_env_columns
[64] = "";
83 char *opt_env
[] = { opt_env_lines
, opt_env_columns
, NULL
};
86 * Mapping between options and command argument mapping.
92 static char opt_diff_context_arg
[9] = "";
94 if (opt_diff_context
< 0 ||
95 !string_format(opt_diff_context_arg
, "-U%u", opt_diff_context
))
98 return opt_diff_context_arg
;
102 #define ENUM_ARG(enum_name, arg_string) ENUM_MAP_ENTRY(arg_string, enum_name)
104 static const struct enum_map_entry ignore_space_arg_map
[] = {
105 ENUM_ARG(IGNORE_SPACE_NO
, ""),
106 ENUM_ARG(IGNORE_SPACE_ALL
, "--ignore-all-space"),
107 ENUM_ARG(IGNORE_SPACE_SOME
, "--ignore-space-change"),
108 ENUM_ARG(IGNORE_SPACE_AT_EOL
, "--ignore-space-at-eol"),
114 return ignore_space_arg_map
[opt_ignore_space
].name
;
117 static const struct enum_map_entry commit_order_arg_map
[] = {
118 ENUM_ARG(COMMIT_ORDER_DEFAULT
, ""),
119 ENUM_ARG(COMMIT_ORDER_TOPO
, "--topo-order"),
120 ENUM_ARG(COMMIT_ORDER_DATE
, "--date-order"),
121 ENUM_ARG(COMMIT_ORDER_AUTHOR_DATE
, "--author-date-order"),
122 ENUM_ARG(COMMIT_ORDER_REVERSE
, "--reverse"),
128 return commit_order_arg_map
[opt_commit_order
].name
;
131 /* Use --show-notes to support Git >= 1.7.6 */
132 #define NOTES_ARG "--show-notes"
133 #define NOTES_EQ_ARG NOTES_ARG "="
135 static char opt_notes_arg
[SIZEOF_STR
] = NOTES_ARG
;
141 return opt_notes_arg
;
142 /* Notes are disabled by default when passing --pretty args. */
147 update_options_from_argv(const char *argv
[])
151 for (next
= flags_pos
= 0; argv
[next
]; next
++) {
152 const char *flag
= argv
[next
];
155 if (map_enum(&value
, commit_order_arg_map
, flag
)) {
156 opt_commit_order
= value
;
157 mark_option_seen(&opt_commit_order
);
161 if (map_enum(&value
, ignore_space_arg_map
, flag
)) {
162 opt_ignore_space
= value
;
163 mark_option_seen(&opt_ignore_space
);
167 if (!strcmp(flag
, "--no-notes")) {
168 opt_show_notes
= FALSE
;
169 mark_option_seen(&opt_show_notes
);
173 if (!prefixcmp(flag
, "--show-notes") ||
174 !prefixcmp(flag
, "--notes")) {
175 opt_show_notes
= TRUE
;
176 string_ncopy(opt_notes_arg
, flag
, strlen(flag
));
177 mark_option_seen(&opt_show_notes
);
181 if (!prefixcmp(flag
, "-U")
182 && parse_int(&value
, flag
+ 2, 0, 999999) == SUCCESS
) {
183 opt_diff_context
= value
;
184 mark_option_seen(&opt_diff_context
);
188 argv
[flags_pos
++] = flag
;
191 argv
[flags_pos
] = NULL
;
195 * User config file handling.
198 static const struct enum_map_entry color_map
[] = {
199 #define COLOR_MAP(name) ENUM_MAP_ENTRY(#name, COLOR_##name)
211 static const struct enum_map_entry attr_map
[] = {
212 #define ATTR_MAP(name) ENUM_MAP_ENTRY(#name, A_##name)
222 #define set_attribute(attr, name) map_enum(attr, attr_map, name)
225 parse_step(double *opt
, const char *arg
)
228 if (!strchr(arg
, '%'))
231 /* "Shift down" so 100% and 1 does not conflict. */
232 *opt
= (*opt
- 1) / 100;
235 return error("Percentage is larger than 100%%");
239 return error("Percentage is less than 0%%");
245 parse_int(int *opt
, const char *arg
, int min
, int max
)
247 int value
= atoi(arg
);
249 if (min
<= value
&& value
<= max
) {
254 return error("Value must be between %d and %d", min
, max
);
258 set_color(int *color
, const char *name
)
260 if (map_enum(color
, color_map
, name
))
262 if (!prefixcmp(name
, "color"))
263 return parse_int(color
, name
+ 5, 0, 255) == SUCCESS
;
264 /* Used when reading git colors. Git expects a plain int w/o prefix. */
265 return parse_int(color
, name
, 0, 255) == SUCCESS
;
268 #define is_quoted(c) ((c) == '"' || (c) == '\'')
270 static enum status_code
271 parse_color_name(const char *color
, struct line_rule
*rule
, const char **prefix_ptr
)
273 const char *prefixend
= is_quoted(*color
) ? NULL
: strchr(color
, '.');
276 struct keymap
*keymap
= get_keymap(color
, prefixend
- color
);
279 return error("Unknown key map: %.*s", (int) (prefixend
- color
), color
);
281 *prefix_ptr
= keymap
->name
;
282 color
= prefixend
+ 1;
285 memset(rule
, 0, sizeof(*rule
));
286 if (is_quoted(*color
)) {
287 rule
->line
= color
+ 1;
288 rule
->linelen
= strlen(color
) - 2;
291 rule
->namelen
= strlen(color
);
298 find_remapped(const char *remapped
[][2], size_t remapped_size
, const char *arg
)
300 size_t arglen
= strlen(arg
);
303 for (i
= 0; i
< remapped_size
; i
++) {
304 const char *name
= remapped
[i
][0];
305 size_t namelen
= strlen(name
);
307 if (arglen
== namelen
&&
308 !string_enum_compare(arg
, name
, namelen
))
315 /* Wants: object fgcolor bgcolor [attribute] */
316 static enum status_code
317 option_color_command(int argc
, const char *argv
[])
319 struct line_rule rule
= {};
320 const char *prefix
= NULL
;
321 struct line_info
*info
;
322 enum status_code code
;
325 return error("Invalid color mapping: color area fgcolor bgcolor [attrs]");
327 code
= parse_color_name(argv
[0], &rule
, &prefix
);
331 info
= add_line_rule(prefix
, &rule
);
333 static const char *obsolete
[][2] = {
334 { "acked", "' Acked-by'" },
335 { "diff-copy-from", "'copy from '" },
336 { "diff-copy-to", "'copy to '" },
337 { "diff-deleted-file-mode", "'deleted file mode '" },
338 { "diff-dissimilarity", "'dissimilarity '" },
339 { "diff-rename-from", "'rename from '" },
340 { "diff-rename-to", "'rename to '" },
341 { "diff-tree", "'diff-tree '" },
342 { "filename", "file" },
343 { "help-keymap", "help.section" },
344 { "pp-adate", "'AuthorDate: '" },
345 { "pp-author", "'Author: '" },
346 { "pp-cdate", "'CommitDate: '" },
347 { "pp-commit", "'Commit: '" },
348 { "pp-date", "'Date: '" },
349 { "reviewed", "' Reviewed-by'" },
350 { "signoff", "' Signed-off-by'" },
351 { "stat-head", "status.header" },
352 { "stat-section", "status.section" },
353 { "tested", "' Tested-by'" },
354 { "tree-dir", "tree.directory" },
355 { "tree-file", "tree.file" },
356 { "tree-head", "tree.header" },
360 index
= find_remapped(obsolete
, ARRAY_SIZE(obsolete
), rule
.name
);
362 /* Keep the initial prefix if defined. */
363 code
= parse_color_name(obsolete
[index
][1], &rule
, prefix
? NULL
: &prefix
);
366 info
= add_line_rule(prefix
, &rule
);
370 return error("Unknown color name: %s", argv
[0]);
372 code
= error("%s has been replaced by %s",
373 obsolete
[index
][0], obsolete
[index
][1]);
376 if (!set_color(&info
->fg
, argv
[1]))
377 return error("Unknown color: %s", argv
[1]);
379 if (!set_color(&info
->bg
, argv
[2]))
380 return error("Unknown color: %s", argv
[2]);
386 if (!set_attribute(&attr
, argv
[argc
]))
387 return error("Unknown color attribute: %s", argv
[argc
]);
394 static enum status_code
395 parse_bool(bool *opt
, const char *arg
)
397 *opt
= (!strcmp(arg
, "1") || !strcmp(arg
, "true") || !strcmp(arg
, "yes"))
399 if (*opt
|| !strcmp(arg
, "0") || !strcmp(arg
, "false") || !strcmp(arg
, "no"))
401 return error("Non-boolean value treated as false: %s", arg
);
404 static enum status_code
405 parse_enum(unsigned int *opt
, const char *arg
, const struct enum_map
*map
)
409 assert(map
->size
> 1);
411 if (map_enum_do(map
->entries
, map
->size
, (int *) opt
, arg
))
414 parse_bool(&is_true
, arg
);
415 *opt
= is_true
? map
->entries
[1].value
: map
->entries
[0].value
;
419 static enum status_code
420 parse_string(char *opt
, const char *arg
, size_t optsize
)
422 int arglen
= strlen(arg
);
427 if (arglen
== 1 || arg
[arglen
- 1] != arg
[0])
428 return ERROR_UNMATCHED_QUOTATION
;
429 arg
+= 1; arglen
-= 2;
431 string_ncopy_do(opt
, optsize
, arg
, arglen
);
436 static enum status_code
437 parse_encoding(struct encoding
**encoding_ref
, const char *arg
, bool priority
)
439 char buf
[SIZEOF_STR
];
440 enum status_code code
= parse_string(buf
, arg
, sizeof(buf
));
442 if (code
== SUCCESS
) {
443 struct encoding
*encoding
= *encoding_ref
;
445 if (encoding
&& !priority
)
447 encoding
= encoding_open(buf
);
449 *encoding_ref
= encoding
;
455 static enum status_code
456 parse_args(const char ***args
, const char *argv
[])
458 if (!argv_copy(args
, argv
))
459 return ERROR_OUT_OF_MEMORY
;
464 parse_option(struct option_info
*option
, const char *prefix
, const char *arg
)
466 char name
[SIZEOF_STR
];
468 if (!enum_name_prefixed(name
, sizeof(name
), prefix
, option
->name
))
469 return error("Failed to parse option");
471 if (!strcmp("show-notes", name
)) {
472 bool *value
= option
->value
;
473 enum status_code res
;
475 if (parse_bool(option
->value
, arg
) == SUCCESS
)
479 string_copy(opt_notes_arg
, NOTES_EQ_ARG
);
480 res
= parse_string(opt_notes_arg
+ STRING_SIZE(NOTES_EQ_ARG
), arg
,
481 sizeof(opt_notes_arg
) - STRING_SIZE(NOTES_EQ_ARG
));
482 if (res
== SUCCESS
&& !opt_notes_arg
[STRING_SIZE(NOTES_EQ_ARG
)])
483 opt_notes_arg
[STRING_SIZE(NOTES_ARG
)] = 0;
487 if (!strcmp(option
->type
, "bool"))
488 return parse_bool(option
->value
, arg
);
490 if (!strcmp(option
->type
, "double"))
491 return parse_step(option
->value
, arg
);
493 if (!strncmp(option
->type
, "enum", 4)) {
494 const char *type
= option
->type
+ STRING_SIZE("enum ");
495 const struct enum_map
*map
= find_enum_map(type
);
497 return parse_enum(option
->value
, arg
, map
);
500 if (!strcmp(option
->type
, "int")) {
501 if (strstr(name
, "title-overflow")) {
502 bool enabled
= FALSE
;
503 int *value
= option
->value
;
505 /* We try to parse it as a boolean (and set the
506 * value to 0 if fale), otherwise we parse it as
507 * an integer and use the given value. */
508 if (parse_bool(&enabled
, arg
) == SUCCESS
) {
517 if (!strcmp(name
, "line-number-interval") ||
518 !strcmp(name
, "tab-size"))
519 return parse_int(option
->value
, arg
, 1, 1024);
520 else if (!strcmp(name
, "id-width"))
521 return parse_int(option
->value
, arg
, 0, SIZEOF_REV
- 1);
523 return parse_int(option
->value
, arg
, 0, 1024);
526 return error("Unhandled option: %s", name
);
534 static struct view_config view_configs
[] = {
535 { "blame-view", &opt_blame_view
},
536 { "blob-view", &opt_blob_view
},
537 { "diff-view", &opt_diff_view
},
538 { "grep-view", &opt_grep_view
},
539 { "log-view", &opt_log_view
},
540 { "main-view", &opt_main_view
},
541 { "pager-view", &opt_pager_view
},
542 { "refs-view", &opt_refs_view
},
543 { "stage-view", &opt_stage_view
},
544 { "stash-view", &opt_stash_view
},
545 { "status-view", &opt_status_view
},
546 { "tree-view", &opt_tree_view
},
549 static enum status_code
550 check_view_config(struct option_info
*option
, const char *argv
[])
552 const char *name
= enum_name(option
->name
);
555 for (i
= 0; i
< ARRAY_SIZE(view_configs
); i
++)
556 if (!strcmp(name
, view_configs
[i
].name
))
557 return parse_view_config(name
, argv
);
562 /* Wants: name = value */
563 static enum status_code
564 option_set_command(int argc
, const char *argv
[])
566 struct option_info
*option
;
569 return error("Invalid set command: set option = value");
571 if (strcmp(argv
[1], "="))
572 return error("No value assigned to %s", argv
[0]);
574 if (!strcmp(argv
[0], "reference-format"))
575 return parse_ref_formats(argv
+ 2);
577 option
= find_option_info(option_info
, ARRAY_SIZE(option_info
), argv
[0]);
579 enum status_code code
;
584 if (!strcmp(option
->type
, "const char **")) {
585 code
= check_view_config(option
, argv
+ 2);
588 return parse_args(option
->value
, argv
+ 2);
591 code
= parse_option(option
, "", argv
[2]);
592 if (code
== SUCCESS
&& argc
!= 3)
593 return error("Option %s only takes one value", argv
[0]);
600 const char *obsolete
[][2] = {
601 { "author-width", "author" },
602 { "filename-width", "file-name" },
603 { "line-number-interval", "line-number" },
604 { "show-author", "author" },
605 { "show-date", "date" },
606 { "show-file-size", "file-size" },
607 { "show-filename", "file-name" },
609 { "show-line-numbers", "line-number" },
610 { "show-refs", "commit-title" },
611 { "show-rev-graph", "commit-title" },
612 { "title-overflow", "commit-title and text" },
614 int index
= find_remapped(obsolete
, ARRAY_SIZE(obsolete
), argv
[0]);
617 return error("%s is obsolete; use the %s view column options instead",
618 obsolete
[index
][0], obsolete
[index
][1]);
621 return error("Unknown option name: %s", argv
[0]);
624 /* Wants: mode request key */
625 static enum status_code
626 option_bind_command(int argc
, const char *argv
[])
630 enum request request
;
631 struct keymap
*keymap
;
635 return error("Invalid key binding: bind keymap key action");
637 if (!(keymap
= get_keymap(argv
[0], strlen(argv
[0])))) {
638 if (!strcmp(argv
[0], "branch"))
639 keymap
= get_keymap("refs", strlen("refs"));
641 return error("Unknown key map: %s", argv
[0]);
644 for (keys
= 0, key_arg
= argv
[1]; *key_arg
&& keys
< ARRAY_SIZE(key
); keys
++) {
645 if (get_key_value(&key_arg
, &key
[keys
]) == ERR
)
646 return error("Unknown key combo: %s", argv
[1]);
649 if (*key_arg
&& keys
== ARRAY_SIZE(key
))
650 return error("Max %zu keys are allowed in key combos: %s", ARRAY_SIZE(key
), argv
[1]);
652 request
= get_request(argv
[2]);
653 if (request
== REQ_UNKNOWN
) {
654 static const char *obsolete
[][2] = {
655 { "view-branch", "view-refs" },
657 static const char *toggles
[][2] = {
658 { "diff-context-down", "diff-context" },
659 { "diff-context-up", "diff-context" },
660 { "toggle-author", "author" },
661 { "toggle-changes", "show-changes" },
662 { "toggle-commit-order", "show-commit-order" },
663 { "toggle-date", "date" },
664 { "toggle-file-filter", "file-filter" },
665 { "toggle-file-size", "file-size" },
666 { "toggle-filename", "filename" },
667 { "toggle-graphic", "show-graphic" },
668 { "toggle-id", "id" },
669 { "toggle-ignore-space", "show-ignore-space" },
670 { "toggle-lineno", "line-number" },
671 { "toggle-refs", "commit-title-refs" },
672 { "toggle-rev-graph", "commit-title-graph" },
673 { "toggle-sort-field", "sort-field" },
674 { "toggle-sort-order", "sort-order" },
675 { "toggle-title-overflow", "commit-title-overflow" },
676 { "toggle-untracked-dirs", "status-untracked-dirs" },
677 { "toggle-vertical-split", "show-vertical-split" },
681 alias
= find_remapped(obsolete
, ARRAY_SIZE(obsolete
), argv
[2]);
683 const char *action
= obsolete
[alias
][1];
685 add_keybinding(keymap
, get_request(action
), key
, keys
);
686 return error("%s has been renamed to %s",
687 obsolete
[alias
][0], action
);
690 alias
= find_remapped(toggles
, ARRAY_SIZE(toggles
), argv
[2]);
692 const char *action
= toggles
[alias
][0];
693 const char *arg
= prefixcmp(action
, "diff-context-")
694 ? NULL
: (strstr(action
, "-down") ? "-1" : "+1");
695 const char *toggle
[] = { ":toggle", toggles
[alias
][1], arg
, NULL
};
696 enum status_code code
= add_run_request(keymap
, key
, keys
, toggle
);
699 code
= error("%s has been replaced by `:toggle %s%s%s'",
700 action
, toggles
[alias
][1],
701 arg
? " " : "", arg
? arg
: "");
706 if (request
== REQ_UNKNOWN
)
707 return add_run_request(keymap
, key
, keys
, argv
+ 2);
709 return add_keybinding(keymap
, request
, key
, keys
);
713 static enum status_code
load_option_file(const char *path
);
715 static enum status_code
716 option_source_command(int argc
, const char *argv
[])
718 enum status_code code
;
721 return error("Invalid source command: source path");
723 code
= load_option_file(argv
[0]);
725 return code
== ERROR_FILE_DOES_NOT_EXIST
726 ? error("File does not exist: %s", argv
[0]) : code
;
730 set_option(const char *opt
, int argc
, const char *argv
[])
732 if (!strcmp(opt
, "color"))
733 return option_color_command(argc
, argv
);
735 if (!strcmp(opt
, "set"))
736 return option_set_command(argc
, argv
);
738 if (!strcmp(opt
, "bind"))
739 return option_bind_command(argc
, argv
);
741 if (!strcmp(opt
, "source"))
742 return option_source_command(argc
, argv
);
744 return error("Unknown option command: %s", opt
);
747 struct config_state
{
754 read_option(char *opt
, size_t optlen
, char *value
, size_t valuelen
, void *data
)
756 struct config_state
*config
= data
;
757 enum status_code status
= ERROR_NO_OPTION_VALUE
;
761 /* Check for comment markers, since read_properties() will
762 * only ensure opt and value are split at first " \t". */
763 optlen
= strcspn(opt
, "#");
767 if (opt
[optlen
] == 0) {
768 /* Look for comment endings in the value. */
769 size_t len
= strcspn(value
, "#");
770 const char *argv
[SIZEOF_ARG
];
773 if (len
< valuelen
) {
778 if (!argv_from_string(argv
, &argc
, value
))
779 status
= error("Too many option arguments for %s", opt
);
781 status
= set_option(opt
, argc
, argv
);
784 if (status
!= SUCCESS
) {
785 warn("%s:%d: %s", config
->path
, config
->lineno
,
786 get_status_message(status
));
787 config
->errors
= TRUE
;
790 /* Always keep going if errors are encountered. */
794 static enum status_code
795 load_option_file(const char *path
)
797 struct config_state config
= { path
, 0, FALSE
};
799 char buf
[SIZEOF_STR
];
801 /* Do not read configuration from stdin if set to "" */
802 if (!path
|| !strlen(path
))
805 if (!prefixcmp(path
, "~/")) {
806 const char *home
= getenv("HOME");
808 if (!home
|| !string_format(buf
, "%s/%s", home
, path
+ 2))
809 return error("Failed to expand ~ to user home directory");
813 /* It's OK that the file doesn't exist. */
814 if (!io_open(&io
, "%s", path
)) {
815 /* XXX: Must return ERROR_FILE_DOES_NOT_EXIST so missing
816 * system tigrc is detected properly. */
817 if (io_error(&io
) == ENOENT
)
818 return ERROR_FILE_DOES_NOT_EXIST
;
819 return error("Error loading file %s: %s", path
, strerror(io_error(&io
)));
822 if (io_load(&io
, " \t", read_option
, &config
) == ERR
||
823 config
.errors
== TRUE
)
824 warn("Errors while loading %s.", path
);
828 extern const char *builtin_config
;
833 const char *tigrc_user
= getenv("TIGRC_USER");
834 const char *tigrc_system
= getenv("TIGRC_SYSTEM");
835 const char *tig_diff_opts
= getenv("TIG_DIFF_OPTS");
836 const bool diff_opts_from_args
= !!opt_diff_options
;
837 bool custom_tigrc_system
= !!tigrc_system
;
839 opt_file_filter
= TRUE
;
840 if (!find_option_info_by_value(&opt_diff_context
)->seen
)
841 opt_diff_context
= -3;
843 if (!custom_tigrc_system
)
844 tigrc_system
= SYSCONFDIR
"/tigrc";
846 if (load_option_file(tigrc_system
) == ERROR_FILE_DOES_NOT_EXIST
&& !custom_tigrc_system
) {
847 struct config_state config
= { "<built-in>", 0, FALSE
};
850 if (!io_from_string(&io
, builtin_config
) ||
851 !io_load(&io
, " \t", read_option
, &config
) == ERR
||
852 config
.errors
== TRUE
)
853 die("Error in built-in config");
857 tigrc_user
= "~/.tigrc";
858 load_option_file(tigrc_user
);
860 if (!diff_opts_from_args
&& tig_diff_opts
&& *tig_diff_opts
) {
861 static const char *diff_opts
[SIZEOF_ARG
] = { NULL
};
862 char buf
[SIZEOF_STR
];
865 if (!string_format(buf
, "%s", tig_diff_opts
) ||
866 !argv_from_string(diff_opts
, &argc
, buf
))
867 die("TIG_DIFF_OPTS contains too many arguments");
868 else if (!argv_copy(&opt_diff_options
, diff_opts
))
869 die("Failed to format TIG_DIFF_OPTS arguments");
876 * Repository properties
880 set_remote_branch(const char *name
, const char *value
, size_t valuelen
)
882 if (!strcmp(name
, ".remote")) {
883 string_ncopy(repo
.remote
, value
, valuelen
);
885 } else if (*repo
.remote
&& !strcmp(name
, ".merge")) {
886 size_t from
= strlen(repo
.remote
);
888 if (!prefixcmp(value
, "refs/heads/"))
889 value
+= STRING_SIZE("refs/heads/");
891 if (!string_format_from(repo
.remote
, &from
, "/%s", value
))
897 set_repo_config_option(char *name
, char *value
, enum status_code (*cmd
)(int, const char **))
899 const char *argv
[SIZEOF_ARG
] = { name
, "=" };
900 int argc
= 1 + (cmd
== option_set_command
);
901 enum status_code code
;
903 if (!argv_from_string(argv
, &argc
, value
))
904 code
= error("Too many arguments");
906 code
= cmd(argc
, argv
);
909 warn("Option 'tig.%s': %s", name
, get_status_message(code
));
913 set_work_tree(const char *value
)
915 char cwd
[SIZEOF_STR
];
917 if (!getcwd(cwd
, sizeof(cwd
)))
918 die("Failed to get cwd path: %s", strerror(errno
));
920 die("Failed to chdir(%s): %s", cwd
, strerror(errno
));
921 if (chdir(repo
.git_dir
) < 0)
922 die("Failed to chdir(%s): %s", repo
.git_dir
, strerror(errno
));
923 if (!getcwd(repo
.git_dir
, sizeof(repo
.git_dir
)))
924 die("Failed to get git path: %s", strerror(errno
));
925 if (chdir(value
) < 0)
926 die("Failed to chdir(%s): %s", value
, strerror(errno
));
927 if (!getcwd(cwd
, sizeof(cwd
)))
928 die("Failed to get cwd path: %s", strerror(errno
));
929 if (setenv("GIT_WORK_TREE", cwd
, TRUE
))
930 die("Failed to set GIT_WORK_TREE to '%s'", cwd
);
931 if (setenv("GIT_DIR", repo
.git_dir
, TRUE
))
932 die("Failed to set GIT_DIR to '%s'", repo
.git_dir
);
933 repo
.is_inside_work_tree
= TRUE
;
936 static struct line_info
*
937 parse_git_color_option(struct line_info
*info
, char *value
)
939 const char *argv
[SIZEOF_ARG
];
941 bool first_color
= TRUE
;
944 if (!argv_from_string(argv
, &argc
, value
))
947 info
->fg
= COLOR_DEFAULT
;
948 info
->bg
= COLOR_DEFAULT
;
951 for (i
= 0; i
< argc
; i
++) {
954 if (set_attribute(&attr
, argv
[i
])) {
957 } else if (set_color(&attr
, argv
[i
])) {
969 set_git_color_option(const char *name
, char *value
)
971 static const char *git_colors
[][2] = {
972 { "branch.current", "main-head" },
973 { "branch.local", "main-ref" },
974 { "branch.plain", "main-ref" },
975 { "branch.remote", "main-remote" },
977 { "diff.meta", "diff-header" },
978 { "diff.meta", "diff-index" },
979 { "diff.meta", "diff-oldmode" },
980 { "diff.meta", "diff-newmode" },
981 { "diff.frag", "diff-chunk" },
982 { "diff.old", "diff-del" },
983 { "diff.new", "diff-add" },
985 { "grep.filename", "grep.file" },
986 { "grep.linenumber", "grep.line-number" },
987 { "grep.separator", "grep.delimiter" },
989 { "status.branch", "status.header" },
990 { "status.added", "stat-staged" },
991 { "status.updated", "stat-staged" },
992 { "status.changed", "stat-unstaged" },
993 { "status.untracked", "stat-untracked" },
995 struct line_info parsed
= {};
998 if (!opt_read_git_colors
)
1001 i
= find_remapped(git_colors
, ARRAY_SIZE(git_colors
), name
);
1002 if (i
< 0 || !parse_git_color_option(&parsed
, value
))
1005 for (; i
< ARRAY_SIZE(git_colors
) && !strcasecmp(git_colors
[i
][0], name
); i
++) {
1006 struct line_rule rule
= {};
1007 const char *prefix
= NULL
;
1008 struct line_info
*info
;
1010 if (parse_color_name(git_colors
[i
][1], &rule
, &prefix
) == SUCCESS
&&
1011 (info
= add_line_rule(prefix
, &rule
))) {
1012 info
->fg
= parsed
.fg
;
1013 info
->bg
= parsed
.bg
;
1014 info
->attr
= parsed
.attr
;
1020 set_encoding(struct encoding
**encoding_ref
, const char *arg
, bool priority
)
1022 if (parse_encoding(encoding_ref
, arg
, priority
) == SUCCESS
)
1023 encoding_arg
[0] = 0;
1027 read_repo_config_option(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
1029 if (!strcmp(name
, "i18n.commitencoding"))
1030 set_encoding(&default_encoding
, value
, FALSE
);
1032 else if (!strcmp(name
, "gui.encoding"))
1033 set_encoding(&default_encoding
, value
, TRUE
);
1035 else if (!strcmp(name
, "core.editor"))
1036 string_ncopy(opt_editor
, value
, valuelen
);
1038 else if (!strcmp(name
, "core.worktree"))
1039 set_work_tree(value
);
1041 else if (!strcmp(name
, "core.abbrev"))
1042 parse_int(&opt_id_width
, value
, 0, SIZEOF_REV
- 1);
1044 else if (!prefixcmp(name
, "tig.color."))
1045 set_repo_config_option(name
+ 10, value
, option_color_command
);
1047 else if (!prefixcmp(name
, "tig.bind."))
1048 set_repo_config_option(name
+ 9, value
, option_bind_command
);
1050 else if (!prefixcmp(name
, "tig."))
1051 set_repo_config_option(name
+ 4, value
, option_set_command
);
1053 else if (!prefixcmp(name
, "color."))
1054 set_git_color_option(name
+ STRING_SIZE("color."), value
);
1056 else if (*repo
.head
&& !prefixcmp(name
, "branch.") &&
1057 !strncmp(name
+ 7, repo
.head
, strlen(repo
.head
)))
1058 set_remote_branch(name
+ 7 + strlen(repo
.head
), value
, valuelen
);
1060 else if (!strcmp(name
, "diff.context")) {
1061 if (!find_option_info_by_value(&opt_diff_context
)->seen
)
1062 opt_diff_context
= -atoi(value
);
1069 load_git_config(void)
1071 const char *config_list_argv
[] = { "git", "config", "--list", NULL
};
1073 return io_run_load(config_list_argv
, "=", read_repo_config_option
, NULL
);
1076 /* vim: set ts=8 sw=8 noexpandtab: */