1 /* Copyright (c) 2006-2015 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 *prefix
, const char *name
)
41 size_t namelen
= strlen(name
);
42 char prefixed
[SIZEOF_STR
];
45 if (*prefix
&& namelen
== strlen(prefix
) &&
46 !string_enum_compare(prefix
, name
, namelen
)) {
48 namelen
= strlen(name
);
51 for (i
= 0; i
< options
; i
++) {
52 if (!strcmp(option
[i
].type
, "view_settings") &&
53 enum_equals_prefix(option
[i
], name
, namelen
))
56 if (enum_equals(option
[i
], name
, namelen
))
59 if (enum_name_prefixed(prefixed
, sizeof(prefixed
), prefix
, option
[i
].name
) &&
60 namelen
== strlen(prefixed
) &&
61 !string_enum_compare(prefixed
, name
, namelen
))
68 static struct option_info
*
69 find_option_info_by_value(void *value
)
73 for (i
= 0; i
< ARRAY_SIZE(option_info
); i
++)
74 if (option_info
[i
].value
== value
)
75 return &option_info
[i
];
81 mark_option_seen(void *value
)
83 struct option_info
*option
= find_option_info_by_value(value
);
90 find_column_option_info(enum view_column_type type
, union view_column_options
*opts
,
91 const char *option
, struct option_info
*column_info
,
92 const char **column_name
)
94 #define DEFINE_COLUMN_OPTION_INFO(name, type, flags) \
95 { #name, STRING_SIZE(#name), #type, &opt->name, flags },
97 #define DEFINE_COLUMN_OPTION_INFO_CHECK(name, id, options) \
98 if (type == VIEW_COLUMN_##id) { \
99 struct name##_options *opt = &opts->name; \
100 struct option_info info[] = { \
101 options(DEFINE_COLUMN_OPTION_INFO) \
103 struct option_info *match; \
104 match = find_option_info(info, ARRAY_SIZE(info), #name, option); \
106 *column_info = *match; \
107 *column_name = #name; \
108 return column_info; \
112 COLUMN_OPTIONS(DEFINE_COLUMN_OPTION_INFO_CHECK
);
122 iconv_t opt_iconv_out
= ICONV_NONE
;
123 char opt_editor
[SIZEOF_STR
] = "";
124 const char **opt_cmdline_argv
= NULL
;
125 const char **opt_rev_argv
= NULL
;
126 const char **opt_file_argv
= NULL
;
129 * Mapping between options and command argument mapping.
135 static char opt_diff_context_arg
[9] = "";
137 if (opt_diff_context
< 0 ||
138 !string_format(opt_diff_context_arg
, "-U%u", opt_diff_context
))
141 return opt_diff_context_arg
;
147 return opt_mailmap
? "--use-mailmap" : "";
151 log_custom_pretty_arg(void)
154 ? "--pretty=format:commit %m %H %P%x00%aN <%aE> %ad%x00%s"
155 : "--pretty=format:commit %m %H %P%x00%an <%ae> %ad%x00%s";
158 #define ENUM_ARG(enum_name, arg_string) ENUM_MAP_ENTRY(arg_string, enum_name)
160 static const struct enum_map_entry ignore_space_arg_map
[] = {
161 ENUM_ARG(IGNORE_SPACE_NO
, ""),
162 ENUM_ARG(IGNORE_SPACE_ALL
, "--ignore-all-space"),
163 ENUM_ARG(IGNORE_SPACE_SOME
, "--ignore-space-change"),
164 ENUM_ARG(IGNORE_SPACE_AT_EOL
, "--ignore-space-at-eol"),
170 return ignore_space_arg_map
[opt_ignore_space
].name
;
173 static const struct enum_map_entry commit_order_arg_map
[] = {
174 ENUM_ARG(COMMIT_ORDER_AUTO
, ""),
175 ENUM_ARG(COMMIT_ORDER_DEFAULT
, ""),
176 ENUM_ARG(COMMIT_ORDER_TOPO
, "--topo-order"),
177 ENUM_ARG(COMMIT_ORDER_DATE
, "--date-order"),
178 ENUM_ARG(COMMIT_ORDER_AUTHOR_DATE
, "--author-date-order"),
179 ENUM_ARG(COMMIT_ORDER_REVERSE
, "--reverse"),
185 return commit_order_arg_map
[opt_commit_order
].name
;
189 commit_order_arg_with_graph(enum graph_display graph_display
)
191 enum commit_order commit_order
= opt_commit_order
;
193 if (commit_order
== COMMIT_ORDER_AUTO
&&
194 graph_display
!= GRAPH_DISPLAY_NO
)
195 commit_order
= COMMIT_ORDER_TOPO
;
197 return commit_order_arg_map
[commit_order
].name
;
200 /* Use --show-notes to support Git >= 1.7.6 */
201 #define NOTES_ARG "--show-notes"
202 #define NOTES_EQ_ARG NOTES_ARG "="
204 static char opt_notes_arg
[SIZEOF_STR
] = NOTES_ARG
;
210 return opt_notes_arg
;
211 /* Notes are disabled by default when passing --pretty args. */
216 update_options_from_argv(const char *argv
[])
220 for (next
= flags_pos
= 0; argv
[next
]; next
++) {
221 const char *flag
= argv
[next
];
224 if (map_enum(&value
, commit_order_arg_map
, flag
)) {
225 opt_commit_order
= value
;
226 mark_option_seen(&opt_commit_order
);
230 if (map_enum(&value
, ignore_space_arg_map
, flag
)) {
231 opt_ignore_space
= value
;
232 mark_option_seen(&opt_ignore_space
);
236 if (!strcmp(flag
, "--no-notes")) {
237 opt_show_notes
= false;
238 mark_option_seen(&opt_show_notes
);
242 if (!prefixcmp(flag
, "--show-notes") ||
243 !prefixcmp(flag
, "--notes")) {
244 opt_show_notes
= true;
245 string_ncopy(opt_notes_arg
, flag
, strlen(flag
));
246 mark_option_seen(&opt_show_notes
);
250 if (!prefixcmp(flag
, "-U")
251 && parse_int(&value
, flag
+ 2, 0, 999999) == SUCCESS
) {
252 opt_diff_context
= value
;
253 mark_option_seen(&opt_diff_context
);
257 argv
[flags_pos
++] = flag
;
260 argv
[flags_pos
] = NULL
;
264 * User config file handling.
267 static const struct enum_map_entry color_map
[] = {
268 #define COLOR_MAP(name) ENUM_MAP_ENTRY(#name, COLOR_##name)
280 static const struct enum_map_entry attr_map
[] = {
281 #define ATTR_MAP(name) ENUM_MAP_ENTRY(#name, A_##name)
291 #define set_attribute(attr, name) map_enum(attr, attr_map, name)
294 parse_step(double *opt
, const char *arg
)
296 int value
= atoi(arg
);
298 if (!value
&& !isdigit(*arg
))
299 return error("Invalid double or percentage");
302 if (!strchr(arg
, '%'))
305 /* "Shift down" so 100% and 1 does not conflict. */
309 return error("Percentage is larger than 100%%");
313 return error("Percentage is less than 0%%");
319 parse_int(int *opt
, const char *arg
, int min
, int max
)
321 int value
= atoi(arg
);
323 if (min
<= value
&& value
<= max
) {
328 return error("Value must be between %d and %d", min
, max
);
332 set_color(int *color
, const char *name
)
334 if (map_enum(color
, color_map
, name
))
336 /* Git expects a plain int w/o prefix, however, color<int> is
337 * the preferred Tig color notation. */
338 if (!prefixcmp(name
, "color"))
340 return string_isnumber(name
) &&
341 parse_int(color
, name
, 0, 255) == SUCCESS
;
344 #define is_quoted(c) ((c) == '"' || (c) == '\'')
346 static enum status_code
347 parse_color_name(const char *color
, struct line_rule
*rule
, const char **prefix_ptr
)
349 const char *prefixend
= is_quoted(*color
) ? NULL
: strchr(color
, '.');
352 struct keymap
*keymap
= get_keymap(color
, prefixend
- color
);
355 return error("Unknown key map: %.*s", (int) (prefixend
- color
), color
);
357 *prefix_ptr
= keymap
->name
;
358 color
= prefixend
+ 1;
361 memset(rule
, 0, sizeof(*rule
));
362 if (is_quoted(*color
)) {
363 rule
->line
= color
+ 1;
364 rule
->linelen
= strlen(color
) - 2;
367 rule
->namelen
= strlen(color
);
374 find_remapped(const char *remapped
[][2], size_t remapped_size
, const char *arg
)
376 size_t arglen
= strlen(arg
);
379 for (i
= 0; i
< remapped_size
; i
++) {
380 const char *name
= remapped
[i
][0];
381 size_t namelen
= strlen(name
);
383 if (arglen
== namelen
&&
384 !string_enum_compare(arg
, name
, namelen
))
391 /* Wants: object fgcolor bgcolor [attribute] */
392 static enum status_code
393 option_color_command(int argc
, const char *argv
[])
395 struct line_rule rule
= {0};
396 const char *prefix
= NULL
;
397 struct line_info
*info
;
398 enum status_code code
;
401 return error("Invalid color mapping: color area fgcolor bgcolor [attrs]");
403 code
= parse_color_name(argv
[0], &rule
, &prefix
);
407 info
= add_line_rule(prefix
, &rule
);
409 static const char *obsolete
[][2] = {
410 { "acked", "' Acked-by'" },
411 { "diff-copy-from", "'copy from '" },
412 { "diff-copy-to", "'copy to '" },
413 { "diff-deleted-file-mode", "'deleted file mode '" },
414 { "diff-dissimilarity", "'dissimilarity '" },
415 { "diff-rename-from", "'rename from '" },
416 { "diff-rename-to", "'rename to '" },
417 { "diff-tree", "'diff-tree '" },
418 { "filename", "file" },
419 { "help-keymap", "help.section" },
420 { "main-revgraph", "" },
421 { "pp-adate", "'AuthorDate: '" },
422 { "pp-author", "'Author: '" },
423 { "pp-cdate", "'CommitDate: '" },
424 { "pp-commit", "'Commit: '" },
425 { "pp-date", "'Date: '" },
426 { "reviewed", "' Reviewed-by'" },
427 { "signoff", "' Signed-off-by'" },
428 { "stat-head", "status.header" },
429 { "stat-section", "status.section" },
430 { "tested", "' Tested-by'" },
431 { "tree-dir", "tree.directory" },
432 { "tree-file", "tree.file" },
433 { "tree-head", "tree.header" },
437 index
= find_remapped(obsolete
, ARRAY_SIZE(obsolete
), rule
.name
);
439 if (!*obsolete
[index
][1])
440 return error("%s is obsolete", argv
[0]);
441 /* Keep the initial prefix if defined. */
442 code
= parse_color_name(obsolete
[index
][1], &rule
, prefix
? NULL
: &prefix
);
445 info
= add_line_rule(prefix
, &rule
);
449 return error("Unknown color name: %s", argv
[0]);
451 code
= error("%s has been replaced by %s",
452 obsolete
[index
][0], obsolete
[index
][1]);
455 if (!set_color(&info
->fg
, argv
[1]))
456 return error("Unknown color: %s", argv
[1]);
458 if (!set_color(&info
->bg
, argv
[2]))
459 return error("Unknown color: %s", argv
[2]);
465 if (!set_attribute(&attr
, argv
[argc
]))
466 return error("Unknown color attribute: %s", argv
[argc
]);
473 static enum status_code
474 parse_bool(bool *opt
, const char *arg
)
476 *opt
= (!strcmp(arg
, "1") || !strcmp(arg
, "true") || !strcmp(arg
, "yes"))
478 if (*opt
|| !strcmp(arg
, "0") || !strcmp(arg
, "false") || !strcmp(arg
, "no"))
480 return error("Non-boolean value treated as false: %s", arg
);
483 static enum status_code
484 parse_enum(const char *name
, unsigned int *opt
, const char *arg
,
485 const struct enum_map
*map
)
488 enum status_code code
;
490 assert(map
->size
> 1);
492 if (map_enum_do(map
->entries
, map
->size
, (int *) opt
, arg
))
495 code
= parse_bool(&is_true
, arg
);
496 *opt
= is_true
? map
->entries
[1].value
: map
->entries
[0].value
;
499 return error("'%s' is not a valid value for %s; using %s",
500 arg
, name
, enum_name(map
->entries
[*opt
].name
));
503 static enum status_code
504 parse_string(char *opt
, const char *arg
, size_t optsize
)
506 int arglen
= strlen(arg
);
511 if (arglen
== 1 || arg
[arglen
- 1] != arg
[0])
512 return ERROR_UNMATCHED_QUOTATION
;
513 arg
+= 1; arglen
-= 2;
515 string_ncopy_do(opt
, optsize
, arg
, arglen
);
520 static enum status_code
521 parse_encoding(struct encoding
**encoding_ref
, const char *arg
, bool priority
)
523 char buf
[SIZEOF_STR
];
524 enum status_code code
= parse_string(buf
, arg
, sizeof(buf
));
526 if (code
== SUCCESS
) {
527 struct encoding
*encoding
= *encoding_ref
;
529 if (encoding
&& !priority
)
531 encoding
= encoding_open(buf
);
533 *encoding_ref
= encoding
;
539 static enum status_code
540 parse_args(const char ***args
, const char *argv
[])
542 if (!argv_copy(args
, argv
))
543 return ERROR_OUT_OF_MEMORY
;
548 parse_option(struct option_info
*option
, const char *prefix
, const char *arg
)
550 char name
[SIZEOF_STR
];
552 if (!enum_name_prefixed(name
, sizeof(name
), prefix
, option
->name
))
553 return error("Failed to parse option");
555 if (!strcmp("show-notes", name
)) {
556 bool *value
= option
->value
;
557 enum status_code res
;
559 if (parse_bool(option
->value
, arg
) == SUCCESS
)
563 string_copy(opt_notes_arg
, NOTES_EQ_ARG
);
564 res
= parse_string(opt_notes_arg
+ STRING_SIZE(NOTES_EQ_ARG
), arg
,
565 sizeof(opt_notes_arg
) - STRING_SIZE(NOTES_EQ_ARG
));
566 if (res
== SUCCESS
&& !opt_notes_arg
[STRING_SIZE(NOTES_EQ_ARG
)])
567 opt_notes_arg
[STRING_SIZE(NOTES_ARG
)] = 0;
571 if (!strcmp(option
->type
, "bool"))
572 return parse_bool(option
->value
, arg
);
574 if (!strcmp(option
->type
, "double"))
575 return parse_step(option
->value
, arg
);
577 if (!strncmp(option
->type
, "enum", 4)) {
578 const char *type
= option
->type
+ STRING_SIZE("enum ");
579 const struct enum_map
*map
= find_enum_map(type
);
581 return parse_enum(name
, option
->value
, arg
, map
);
584 if (!strcmp(option
->type
, "int")) {
585 if (strstr(name
, "title-overflow")) {
586 bool enabled
= false;
587 int *value
= option
->value
;
589 /* We try to parse it as a boolean (and set the
590 * value to 0 if fale), otherwise we parse it as
591 * an integer and use the given value. */
592 if (parse_bool(&enabled
, arg
) == SUCCESS
) {
601 if (!strcmp(name
, "line-number-interval") ||
602 !strcmp(name
, "tab-size"))
603 return parse_int(option
->value
, arg
, 1, 1024);
604 else if (!strcmp(name
, "id-width"))
605 return parse_int(option
->value
, arg
, 0, SIZEOF_REV
- 1);
607 return parse_int(option
->value
, arg
, 0, 1024);
610 return error("Unhandled option: %s", name
);
613 static enum status_code
614 parse_view_settings(struct view_column
**view_column
, const char *name_
, const char *argv
[])
616 char buf
[SIZEOF_STR
];
617 const char *name
= enum_name_copy(buf
, sizeof(buf
), name_
) ? buf
: name_
;
618 const char *prefixed
;
620 if ((prefixed
= strstr(name
, "-view-"))) {
621 const char *column_name
= prefixed
+ STRING_SIZE("-view-");
622 size_t column_namelen
= strlen(column_name
);
623 enum view_column_type type
;
625 for (type
= 0; type
< view_column_type_map
->size
; type
++) {
626 const struct enum_map_entry
*column
= &view_column_type_map
->entries
[type
];
628 if (enum_equals(*column
, column_name
, column_namelen
))
629 return parse_view_column_config(name
, type
, NULL
, argv
);
631 if (enum_equals_prefix(*column
, column_name
, column_namelen
))
632 return parse_view_column_config(name
, type
,
633 column_name
+ column
->namelen
+ 1,
638 return parse_view_config(view_column
, name
, argv
);
641 /* Wants: name = value */
642 static enum status_code
643 option_set_command(int argc
, const char *argv
[])
645 struct option_info
*option
;
646 enum status_code code
;
649 return error("Invalid set command: set option = value");
651 if (strcmp(argv
[1], "="))
652 return error("No value assigned to %s", argv
[0]);
654 option
= find_option_info(option_info
, ARRAY_SIZE(option_info
), "", argv
[0]);
659 if (!strcmp(option
->type
, "const char **"))
660 return parse_args(option
->value
, argv
+ 2);
663 return error("Invalid set command: set option = value");
665 if (!strcmp(option
->type
, "view_settings"))
666 return parse_view_settings(option
->value
, argv
[0], argv
+ 2);
668 if (!strcmp(option
->type
, "struct ref_format **"))
669 return parse_ref_formats(option
->value
, argv
+ 2);
671 code
= parse_option(option
, "", argv
[2]);
672 if (code
== SUCCESS
&& argc
!= 3)
673 return error("Option %s only takes one value", argv
[0]);
680 const char *obsolete
[][2] = {
681 { "author-width", "author" },
682 { "filename-width", "file-name" },
683 { "line-number-interval", "line-number" },
684 { "show-author", "author" },
685 { "show-date", "date" },
686 { "show-file-size", "file-size" },
687 { "show-filename", "file-name" },
689 { "show-line-numbers", "line-number" },
690 { "show-refs", "commit-title" },
691 { "show-rev-graph", "commit-title" },
692 { "title-overflow", "commit-title and text" },
694 int index
= find_remapped(obsolete
, ARRAY_SIZE(obsolete
), argv
[0]);
697 return error("%s is obsolete; see tigrc(5) for how to set the %s column option",
698 obsolete
[index
][0], obsolete
[index
][1]);
700 if (!strcmp(argv
[0], "read-git-colors"))
701 return error("read-git-colors has been obsoleted by the git-colors option");
704 return error("Unknown option name: %s", argv
[0]);
707 /* Wants: mode request key */
708 static enum status_code
709 option_bind_command(int argc
, const char *argv
[])
713 enum request request
;
714 struct keymap
*keymap
;
718 return error("Invalid key binding: bind keymap key action");
720 if (!(keymap
= get_keymap(argv
[0], strlen(argv
[0])))) {
721 if (!strcmp(argv
[0], "branch"))
722 keymap
= get_keymap("refs", strlen("refs"));
724 return error("Unknown key map: %s", argv
[0]);
727 for (keys
= 0, key_arg
= argv
[1]; *key_arg
&& keys
< ARRAY_SIZE(key
); keys
++) {
728 enum status_code code
= get_key_value(&key_arg
, &key
[keys
]);
734 if (*key_arg
&& keys
== ARRAY_SIZE(key
))
735 return error("Except for <Esc> combos only one key is allowed "
736 "in key combos: %s", argv
[1]);
738 request
= get_request(argv
[2]);
739 if (request
== REQ_UNKNOWN
) {
740 static const char *obsolete
[][2] = {
741 { "view-branch", "view-refs" },
743 static const char *toggles
[][2] = {
744 { "diff-context-down", "diff-context" },
745 { "diff-context-up", "diff-context" },
746 { "stage-next", ":/^@@" },
747 { "toggle-author", "author" },
748 { "toggle-changes", "show-changes" },
749 { "toggle-commit-order", "show-commit-order" },
750 { "toggle-date", "date" },
751 { "toggle-files", "file-filter" },
752 { "toggle-file-filter", "file-filter" },
753 { "toggle-file-size", "file-size" },
754 { "toggle-filename", "filename" },
755 { "toggle-graphic", "show-graphic" },
756 { "toggle-id", "id" },
757 { "toggle-ignore-space", "show-ignore-space" },
758 { "toggle-lineno", "line-number" },
759 { "toggle-refs", "commit-title-refs" },
760 { "toggle-rev-graph", "commit-title-graph" },
761 { "toggle-show-changes", "show-changes" },
762 { "toggle-sort-field", "sort-field" },
763 { "toggle-sort-order", "sort-order" },
764 { "toggle-title-overflow", "commit-title-overflow" },
765 { "toggle-untracked-dirs", "status-untracked-dirs" },
766 { "toggle-vertical-split", "show-vertical-split" },
770 alias
= find_remapped(obsolete
, ARRAY_SIZE(obsolete
), argv
[2]);
772 const char *action
= obsolete
[alias
][1];
774 add_keybinding(keymap
, get_request(action
), key
, keys
);
775 return error("%s has been renamed to %s",
776 obsolete
[alias
][0], action
);
779 alias
= find_remapped(toggles
, ARRAY_SIZE(toggles
), argv
[2]);
781 const char *action
= toggles
[alias
][0];
782 const char *arg
= prefixcmp(action
, "diff-context-")
783 ? NULL
: (strstr(action
, "-down") ? "-1" : "+1");
784 const char *mapped
= toggles
[alias
][1];
785 const char *toggle
[] = { ":toggle", mapped
, arg
, NULL
};
786 const char *other
[] = { mapped
, NULL
};
787 const char **prompt
= *mapped
== ':' ? other
: toggle
;
788 enum status_code code
= add_run_request(keymap
, key
, keys
, prompt
);
791 code
= error("%s has been replaced by `%s%s%s%s'",
792 action
, prompt
== other
? mapped
: ":toggle ",
793 prompt
== other
? "" : mapped
,
794 arg
? " " : "", arg
? arg
: "");
799 if (request
== REQ_UNKNOWN
)
800 return add_run_request(keymap
, key
, keys
, argv
+ 2);
802 return add_keybinding(keymap
, request
, key
, keys
);
806 static enum status_code
load_option_file(const char *path
);
808 static enum status_code
809 option_source_command(int argc
, const char *argv
[])
811 enum status_code code
;
814 return error("Invalid source command: source path");
816 code
= load_option_file(argv
[0]);
818 return code
== ERROR_FILE_DOES_NOT_EXIST
819 ? error("File does not exist: %s", argv
[0]) : code
;
823 set_option(const char *opt
, int argc
, const char *argv
[])
825 if (!strcmp(opt
, "color"))
826 return option_color_command(argc
, argv
);
828 if (!strcmp(opt
, "set"))
829 return option_set_command(argc
, argv
);
831 if (!strcmp(opt
, "bind"))
832 return option_bind_command(argc
, argv
);
834 if (!strcmp(opt
, "source"))
835 return option_source_command(argc
, argv
);
837 return error("Unknown option command: %s", opt
);
840 struct config_state
{
846 static enum status_code
847 read_option(char *opt
, size_t optlen
, char *value
, size_t valuelen
, void *data
)
849 struct config_state
*config
= data
;
850 enum status_code status
= ERROR_NO_OPTION_VALUE
;
852 /* Check for comment markers, since read_properties() will
853 * only ensure opt and value are split at first " \t". */
854 optlen
= strcspn(opt
, "#");
858 if (opt
[optlen
] == 0) {
859 /* Look for comment endings in the value. */
860 size_t len
= strcspn(value
, "#");
861 const char *argv
[SIZEOF_ARG
];
864 if (len
< valuelen
) {
869 if (!argv_from_string(argv
, &argc
, value
))
870 status
= error("Too many option arguments for %s", opt
);
872 status
= set_option(opt
, argc
, argv
);
875 if (status
!= SUCCESS
) {
876 warn("%s:%zu: %s", config
->path
, config
->lineno
,
877 get_status_message(status
));
878 config
->errors
= true;
881 /* Always keep going if errors are encountered. */
885 static enum status_code
886 load_option_file(const char *path
)
888 struct config_state config
= { path
, 0, false };
890 char buf
[SIZEOF_STR
];
892 /* Do not read configuration from stdin if set to "" */
893 if (!path
|| !strlen(path
))
896 if (!prefixcmp(path
, "~/")) {
897 const char *home
= getenv("HOME");
899 if (!home
|| !string_format(buf
, "%s/%s", home
, path
+ 2))
900 return error("Failed to expand ~ to user home directory");
904 /* It's OK that the file doesn't exist. */
905 if (!io_open(&io
, "%s", path
)) {
906 /* XXX: Must return ERROR_FILE_DOES_NOT_EXIST so missing
907 * system tigrc is detected properly. */
908 if (io_error(&io
) == ENOENT
)
909 return ERROR_FILE_DOES_NOT_EXIST
;
910 return error("Error loading file %s: %s", path
, io_strerror(&io
));
913 if (io_load_span(&io
, " \t", &config
.lineno
, read_option
, &config
) != SUCCESS
||
914 config
.errors
== true)
915 warn("Errors while loading %s.", path
);
919 extern const char *builtin_config
;
924 const char *tigrc_user
= getenv("TIGRC_USER");
925 const char *tigrc_system
= getenv("TIGRC_SYSTEM");
926 const char *tig_diff_opts
= getenv("TIG_DIFF_OPTS");
927 const bool diff_opts_from_args
= !!opt_diff_options
;
928 bool custom_tigrc_system
= !!tigrc_system
;
930 opt_file_filter
= true;
931 if (!find_option_info_by_value(&opt_diff_context
)->seen
)
932 opt_diff_context
= -3;
934 if (!custom_tigrc_system
)
935 tigrc_system
= SYSCONFDIR
"/tigrc";
937 if (!*tigrc_system
||
938 (load_option_file(tigrc_system
) == ERROR_FILE_DOES_NOT_EXIST
&& !custom_tigrc_system
)) {
939 struct config_state config
= { "<built-in>", 0, false };
942 if (!io_from_string(&io
, builtin_config
))
943 return error("Failed to get built-in config");
944 if (io_load_span(&io
, " \t", &config
.lineno
, read_option
, &config
) != SUCCESS
|| config
.errors
== true)
945 return error("Error in built-in config");
949 tigrc_user
= "~/.tigrc";
950 load_option_file(tigrc_user
);
952 if (!diff_opts_from_args
&& tig_diff_opts
&& *tig_diff_opts
) {
953 static const char *diff_opts
[SIZEOF_ARG
] = { NULL
};
954 char buf
[SIZEOF_STR
];
957 if (!string_format(buf
, "%s", tig_diff_opts
) ||
958 !argv_from_string(diff_opts
, &argc
, buf
))
959 return error("TIG_DIFF_OPTS contains too many arguments");
960 else if (!argv_copy(&opt_diff_options
, diff_opts
))
961 return error("Failed to format TIG_DIFF_OPTS arguments");
968 format_option_value(const struct option_info
*option
, char buf
[], size_t bufsize
)
972 if (!strcmp(option
->type
, "bool")) {
973 bool *opt
= option
->value
;
975 if (string_nformat(buf
, bufsize
, NULL
, "%s", *opt
? "yes" : "no"))
978 } else if (!strncmp(option
->type
, "enum", 4)) {
979 const char *type
= option
->type
+ STRING_SIZE("enum ");
980 enum author
*opt
= option
->value
;
981 const struct enum_map
*map
= find_enum_map(type
);
983 if (enum_name_copy(buf
, bufsize
, map
->entries
[*opt
].name
))
986 } else if (!strcmp(option
->type
, "int")) {
987 int *opt
= option
->value
;
989 if (opt
== &opt_diff_context
&& *opt
< 0)
992 if (string_nformat(buf
, bufsize
, NULL
, "%d", *opt
))
995 } else if (!strcmp(option
->type
, "double")) {
996 double *opt
= option
->value
;
999 if (string_nformat(buf
, bufsize
, NULL
, "%d", (int) *opt
))
1002 } else if (string_nformat(buf
, bufsize
, NULL
, "%.0f%%", (*opt
) * 100)) {
1006 } else if (!strcmp(option
->type
, "const char **")) {
1007 const char *sep
= "";
1008 const char ***opt
= option
->value
;
1012 for (i
= 0; (*opt
) && (*opt
)[i
]; i
++) {
1013 const char *arg
= (*opt
)[i
];
1015 if (!string_nformat(buf
, bufsize
, &bufpos
, "%s%s", sep
, arg
))
1023 } else if (!strcmp(option
->type
, "struct ref_format **")) {
1024 struct ref_format
***opt
= option
->value
;
1026 if (format_ref_formats(*opt
, buf
, bufsize
) == SUCCESS
)
1029 } else if (!strcmp(option
->type
, "view_settings")) {
1030 struct view_column
**opt
= option
->value
;
1032 if (format_view_config(*opt
, buf
, bufsize
) == SUCCESS
)
1036 if (string_nformat(buf
, bufsize
, NULL
, "<%s>", option
->type
))
1044 save_option_settings(FILE *file
)
1046 char buf
[SIZEOF_STR
];
1049 if (!io_fprintf(file
, "%s", "\n## Settings\n"))
1052 for (i
= 0; i
< ARRAY_SIZE(option_info
); i
++) {
1053 struct option_info
*option
= &option_info
[i
];
1054 const char *name
= enum_name(option
->name
);
1055 const char *value
= format_option_value(option
, buf
, sizeof(buf
));
1057 if (!suffixcmp(name
, strlen(name
), "-args"))
1060 if (!io_fprintf(file
, "\nset %-25s = %s", name
, value
))
1068 save_option_keybinding(void *data
, const char *group
, struct keymap
*keymap
,
1069 enum request request
, const char *key
,
1070 const struct request_info
*req_info
,
1071 const struct run_request
*run_req
)
1075 if (group
&& !io_fprintf(file
, "\n# %s", group
))
1078 if (!io_fprintf(file
, "\nbind %-10s %-15s ", enum_name(keymap
->name
), key
))
1082 return io_fprintf(file
, "%s", enum_name(req_info
->name
));
1085 const char *sep
= format_run_request_flags(run_req
);
1088 for (i
= 0; run_req
->argv
[i
]; i
++) {
1089 if (!io_fprintf(file
, "%s%s", sep
, run_req
->argv
[i
]))
1099 save_option_keybindings(FILE *file
)
1101 if (!io_fprintf(file
, "%s", "\n\n## Keybindings\n"))
1104 return foreach_key(save_option_keybinding
, file
, false);
1108 save_option_color_name(FILE *file
, int color
)
1112 for (i
= 0; i
< ARRAY_SIZE(color_map
); i
++)
1113 if (color_map
[i
].value
== color
)
1114 return io_fprintf(file
, " %-8s", enum_name(color_map
[i
].name
));
1116 return io_fprintf(file
, " color%d", color
);
1120 save_option_color_attr(FILE *file
, int attr
)
1124 for (i
= 0; i
< ARRAY_SIZE(attr_map
); i
++)
1125 if ((attr
& attr_map
[i
].value
) &&
1126 !io_fprintf(file
, " %s", enum_name(attr_map
[i
].name
)))
1133 save_option_color(void *data
, const struct line_rule
*rule
)
1136 const struct line_info
*info
;
1138 for (info
= &rule
->info
; info
; info
= info
->next
) {
1139 const char *prefix
= info
->prefix
? info
->prefix
: "";
1140 const char *prefix_sep
= info
->prefix
? "." : "";
1141 const char *quote
= *rule
->line
? "\"" : "";
1142 const char *name
= *rule
->line
? rule
->line
: enum_name(rule
->name
);
1143 int name_width
= strlen(prefix
) + strlen(prefix_sep
) + 2 * strlen(quote
) + strlen(name
);
1144 int padding
= name_width
> 30 ? 0 : 30 - name_width
;
1146 if (!io_fprintf(file
, "\ncolor %s%s%s%s%s%-*s",
1147 prefix
, prefix_sep
, quote
, name
, quote
, padding
, "")
1148 || !save_option_color_name(file
, info
->fg
)
1149 || !save_option_color_name(file
, info
->bg
)
1150 || !save_option_color_attr(file
, info
->attr
))
1158 save_option_colors(FILE *file
)
1160 if (!io_fprintf(file
, "%s", "\n\n## Colors\n"))
1163 return foreach_line_rule(save_option_color
, file
);
1167 save_options(const char *path
)
1169 int fd
= open(path
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1170 FILE *file
= fd
!= -1 ? fdopen(fd
, "w") : NULL
;
1171 enum status_code code
= SUCCESS
;
1174 return error("%s", strerror(errno
));
1176 if (!io_fprintf(file
, "%s", "# Saved by Tig\n")
1177 || !save_option_settings(file
)
1178 || !save_option_keybindings(file
)
1179 || !save_option_colors(file
))
1180 code
= error("Write returned an error");
1187 * Repository properties
1191 set_remote_branch(const char *name
, const char *value
, size_t valuelen
)
1193 if (!strcmp(name
, ".remote")) {
1194 string_ncopy(repo
.remote
, value
, valuelen
);
1196 } else if (*repo
.remote
&& !strcmp(name
, ".merge")) {
1197 size_t from
= strlen(repo
.remote
);
1199 if (!prefixcmp(value
, "refs/heads/"))
1200 value
+= STRING_SIZE("refs/heads/");
1202 if (!string_format_from(repo
.remote
, &from
, "/%s", value
))
1208 set_repo_config_option(char *name
, char *value
, enum status_code (*cmd
)(int, const char **))
1210 const char *argv
[SIZEOF_ARG
] = { name
, "=" };
1211 int argc
= 1 + (cmd
== option_set_command
);
1212 enum status_code code
;
1214 if (!argv_from_string(argv
, &argc
, value
))
1215 code
= error("Too many arguments");
1217 code
= cmd(argc
, argv
);
1219 if (code
!= SUCCESS
)
1220 warn("Option 'tig.%s': %s", name
, get_status_message(code
));
1224 set_work_tree(const char *value
)
1226 char cwd
[SIZEOF_STR
];
1228 if (!getcwd(cwd
, sizeof(cwd
)))
1229 die("Failed to get cwd path: %s", strerror(errno
));
1231 die("Failed to chdir(%s): %s", cwd
, strerror(errno
));
1232 if (chdir(repo
.git_dir
) < 0)
1233 die("Failed to chdir(%s): %s", repo
.git_dir
, strerror(errno
));
1234 if (!getcwd(repo
.git_dir
, sizeof(repo
.git_dir
)))
1235 die("Failed to get git path: %s", strerror(errno
));
1236 if (chdir(value
) < 0)
1237 die("Failed to chdir(%s): %s", value
, strerror(errno
));
1238 if (!getcwd(cwd
, sizeof(cwd
)))
1239 die("Failed to get cwd path: %s", strerror(errno
));
1240 if (setenv("GIT_WORK_TREE", cwd
, true))
1241 die("Failed to set GIT_WORK_TREE to '%s'", cwd
);
1242 if (setenv("GIT_DIR", repo
.git_dir
, true))
1243 die("Failed to set GIT_DIR to '%s'", repo
.git_dir
);
1244 repo
.is_inside_work_tree
= true;
1247 static struct line_info
*
1248 parse_git_color_option(struct line_info
*info
, char *value
)
1250 const char *argv
[SIZEOF_ARG
];
1252 bool first_color
= true;
1255 if (!argv_from_string(argv
, &argc
, value
))
1258 info
->fg
= COLOR_DEFAULT
;
1259 info
->bg
= COLOR_DEFAULT
;
1262 for (i
= 0; i
< argc
; i
++) {
1265 if (set_attribute(&attr
, argv
[i
])) {
1268 } else if (set_color(&attr
, argv
[i
])) {
1273 first_color
= false;
1280 set_git_color_option(const char *name
, char *value
)
1282 struct line_info parsed
= {0};
1283 struct line_info
*color
= NULL
;
1284 size_t namelen
= strlen(name
);
1287 if (!opt_git_colors
)
1290 for (i
= 0; opt_git_colors
[i
]; i
++) {
1291 struct line_rule rule
= {0};
1292 const char *prefix
= NULL
;
1293 struct line_info
*info
;
1294 const char *alias
= opt_git_colors
[i
];
1295 const char *sep
= strchr(alias
, '=');
1297 if (!sep
|| namelen
!= sep
- alias
||
1298 string_enum_compare(name
, alias
, namelen
))
1302 color
= parse_git_color_option(&parsed
, value
);
1307 if (parse_color_name(sep
+ 1, &rule
, &prefix
) == SUCCESS
&&
1308 (info
= add_line_rule(prefix
, &rule
))) {
1309 info
->fg
= parsed
.fg
;
1310 info
->bg
= parsed
.bg
;
1311 info
->attr
= parsed
.attr
;
1317 set_encoding(struct encoding
**encoding_ref
, const char *arg
, bool priority
)
1319 if (!strcasecmp(arg
, "utf-8") || !strcasecmp(arg
, "utf8"))
1321 if (parse_encoding(encoding_ref
, arg
, priority
) == SUCCESS
)
1322 encoding_arg
[0] = 0;
1325 static enum status_code
1326 read_repo_config_option(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
1328 if (!strcmp(name
, "i18n.commitencoding"))
1329 set_encoding(&default_encoding
, value
, false);
1331 else if (!strcmp(name
, "gui.encoding"))
1332 set_encoding(&default_encoding
, value
, true);
1334 else if (!strcmp(name
, "core.editor"))
1335 string_ncopy(opt_editor
, value
, valuelen
);
1337 else if (!strcmp(name
, "core.worktree"))
1338 set_work_tree(value
);
1340 else if (!strcmp(name
, "core.abbrev"))
1341 parse_int(&opt_id_width
, value
, 0, SIZEOF_REV
- 1);
1343 else if (!prefixcmp(name
, "tig.color."))
1344 set_repo_config_option(name
+ 10, value
, option_color_command
);
1346 else if (!prefixcmp(name
, "tig.bind."))
1347 set_repo_config_option(name
+ 9, value
, option_bind_command
);
1349 else if (!prefixcmp(name
, "tig."))
1350 set_repo_config_option(name
+ 4, value
, option_set_command
);
1352 else if (!prefixcmp(name
, "color."))
1353 set_git_color_option(name
+ STRING_SIZE("color."), value
);
1355 else if (*repo
.head
&& !prefixcmp(name
, "branch.") &&
1356 !strncmp(name
+ 7, repo
.head
, strlen(repo
.head
)))
1357 set_remote_branch(name
+ 7 + strlen(repo
.head
), value
, valuelen
);
1359 else if (!strcmp(name
, "diff.context")) {
1360 if (!find_option_info_by_value(&opt_diff_context
)->seen
)
1361 opt_diff_context
= -atoi(value
);
1363 } else if (!strcmp(name
, "format.pretty")) {
1364 if (!prefixcmp(value
, "format:") && strstr(value
, "%C("))
1365 argv_append(&opt_log_options
, "--pretty=medium");
1372 load_git_config(void)
1374 const char *config_list_argv
[] = { "git", "config", "--list", NULL
};
1376 return io_run_load(config_list_argv
, "=", read_repo_config_option
, NULL
);
1379 /* vim: set ts=8 sw=8 noexpandtab: */