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
;
127 char opt_env_lines
[64] = "";
128 char opt_env_columns
[64] = "";
129 char *opt_env
[] = { opt_env_lines
, opt_env_columns
, NULL
};
132 * Mapping between options and command argument mapping.
138 static char opt_diff_context_arg
[9] = "";
140 if (opt_diff_context
< 0 ||
141 !string_format(opt_diff_context_arg
, "-U%u", opt_diff_context
))
144 return opt_diff_context_arg
;
148 #define ENUM_ARG(enum_name, arg_string) ENUM_MAP_ENTRY(arg_string, enum_name)
150 static const struct enum_map_entry ignore_space_arg_map
[] = {
151 ENUM_ARG(IGNORE_SPACE_NO
, ""),
152 ENUM_ARG(IGNORE_SPACE_ALL
, "--ignore-all-space"),
153 ENUM_ARG(IGNORE_SPACE_SOME
, "--ignore-space-change"),
154 ENUM_ARG(IGNORE_SPACE_AT_EOL
, "--ignore-space-at-eol"),
160 return ignore_space_arg_map
[opt_ignore_space
].name
;
163 static const struct enum_map_entry commit_order_arg_map
[] = {
164 ENUM_ARG(COMMIT_ORDER_AUTO
, ""),
165 ENUM_ARG(COMMIT_ORDER_DEFAULT
, ""),
166 ENUM_ARG(COMMIT_ORDER_TOPO
, "--topo-order"),
167 ENUM_ARG(COMMIT_ORDER_DATE
, "--date-order"),
168 ENUM_ARG(COMMIT_ORDER_AUTHOR_DATE
, "--author-date-order"),
169 ENUM_ARG(COMMIT_ORDER_REVERSE
, "--reverse"),
175 return commit_order_arg_map
[opt_commit_order
].name
;
179 commit_order_arg_with_graph(enum graph_display graph_display
)
181 enum commit_order commit_order
= opt_commit_order
;
183 if (commit_order
== COMMIT_ORDER_AUTO
&&
184 graph_display
!= GRAPH_DISPLAY_NO
)
185 commit_order
= COMMIT_ORDER_TOPO
;
187 return commit_order_arg_map
[commit_order
].name
;
190 /* Use --show-notes to support Git >= 1.7.6 */
191 #define NOTES_ARG "--show-notes"
192 #define NOTES_EQ_ARG NOTES_ARG "="
194 static char opt_notes_arg
[SIZEOF_STR
] = NOTES_ARG
;
200 return opt_notes_arg
;
201 /* Notes are disabled by default when passing --pretty args. */
206 update_options_from_argv(const char *argv
[])
210 for (next
= flags_pos
= 0; argv
[next
]; next
++) {
211 const char *flag
= argv
[next
];
214 if (map_enum(&value
, commit_order_arg_map
, flag
)) {
215 opt_commit_order
= value
;
216 mark_option_seen(&opt_commit_order
);
220 if (map_enum(&value
, ignore_space_arg_map
, flag
)) {
221 opt_ignore_space
= value
;
222 mark_option_seen(&opt_ignore_space
);
226 if (!strcmp(flag
, "--no-notes")) {
227 opt_show_notes
= FALSE
;
228 mark_option_seen(&opt_show_notes
);
232 if (!prefixcmp(flag
, "--show-notes") ||
233 !prefixcmp(flag
, "--notes")) {
234 opt_show_notes
= TRUE
;
235 string_ncopy(opt_notes_arg
, flag
, strlen(flag
));
236 mark_option_seen(&opt_show_notes
);
240 if (!prefixcmp(flag
, "-U")
241 && parse_int(&value
, flag
+ 2, 0, 999999) == SUCCESS
) {
242 opt_diff_context
= value
;
243 mark_option_seen(&opt_diff_context
);
247 argv
[flags_pos
++] = flag
;
250 argv
[flags_pos
] = NULL
;
254 * User config file handling.
257 static const struct enum_map_entry color_map
[] = {
258 #define COLOR_MAP(name) ENUM_MAP_ENTRY(#name, COLOR_##name)
270 static const struct enum_map_entry attr_map
[] = {
271 #define ATTR_MAP(name) ENUM_MAP_ENTRY(#name, A_##name)
281 #define set_attribute(attr, name) map_enum(attr, attr_map, name)
284 parse_step(double *opt
, const char *arg
)
286 int value
= atoi(arg
);
288 if (!value
&& !isdigit(*arg
))
289 return error("Invalid double or percentage");
292 if (!strchr(arg
, '%'))
295 /* "Shift down" so 100% and 1 does not conflict. */
299 return error("Percentage is larger than 100%%");
303 return error("Percentage is less than 0%%");
309 parse_int(int *opt
, const char *arg
, int min
, int max
)
311 int value
= atoi(arg
);
313 if (min
<= value
&& value
<= max
) {
318 return error("Value must be between %d and %d", min
, max
);
322 set_color(int *color
, const char *name
)
324 if (map_enum(color
, color_map
, name
))
326 /* Git expects a plain int w/o prefix, however, color<int> is
327 * the preferred Tig color notation. */
328 if (!prefixcmp(name
, "color"))
330 return string_isnumber(name
) &&
331 parse_int(color
, name
, 0, 255) == SUCCESS
;
334 #define is_quoted(c) ((c) == '"' || (c) == '\'')
336 static enum status_code
337 parse_color_name(const char *color
, struct line_rule
*rule
, const char **prefix_ptr
)
339 const char *prefixend
= is_quoted(*color
) ? NULL
: strchr(color
, '.');
342 struct keymap
*keymap
= get_keymap(color
, prefixend
- color
);
345 return error("Unknown key map: %.*s", (int) (prefixend
- color
), color
);
347 *prefix_ptr
= keymap
->name
;
348 color
= prefixend
+ 1;
351 memset(rule
, 0, sizeof(*rule
));
352 if (is_quoted(*color
)) {
353 rule
->line
= color
+ 1;
354 rule
->linelen
= strlen(color
) - 2;
357 rule
->namelen
= strlen(color
);
364 find_remapped(const char *remapped
[][2], size_t remapped_size
, const char *arg
)
366 size_t arglen
= strlen(arg
);
369 for (i
= 0; i
< remapped_size
; i
++) {
370 const char *name
= remapped
[i
][0];
371 size_t namelen
= strlen(name
);
373 if (arglen
== namelen
&&
374 !string_enum_compare(arg
, name
, namelen
))
381 /* Wants: object fgcolor bgcolor [attribute] */
382 static enum status_code
383 option_color_command(int argc
, const char *argv
[])
385 struct line_rule rule
= {0};
386 const char *prefix
= NULL
;
387 struct line_info
*info
;
388 enum status_code code
;
391 return error("Invalid color mapping: color area fgcolor bgcolor [attrs]");
393 code
= parse_color_name(argv
[0], &rule
, &prefix
);
397 info
= add_line_rule(prefix
, &rule
);
399 static const char *obsolete
[][2] = {
400 { "acked", "' Acked-by'" },
401 { "diff-copy-from", "'copy from '" },
402 { "diff-copy-to", "'copy to '" },
403 { "diff-deleted-file-mode", "'deleted file mode '" },
404 { "diff-dissimilarity", "'dissimilarity '" },
405 { "diff-rename-from", "'rename from '" },
406 { "diff-rename-to", "'rename to '" },
407 { "diff-tree", "'diff-tree '" },
408 { "filename", "file" },
409 { "help-keymap", "help.section" },
410 { "main-revgraph", "" },
411 { "pp-adate", "'AuthorDate: '" },
412 { "pp-author", "'Author: '" },
413 { "pp-cdate", "'CommitDate: '" },
414 { "pp-commit", "'Commit: '" },
415 { "pp-date", "'Date: '" },
416 { "reviewed", "' Reviewed-by'" },
417 { "signoff", "' Signed-off-by'" },
418 { "stat-head", "status.header" },
419 { "stat-section", "status.section" },
420 { "tested", "' Tested-by'" },
421 { "tree-dir", "tree.directory" },
422 { "tree-file", "tree.file" },
423 { "tree-head", "tree.header" },
427 index
= find_remapped(obsolete
, ARRAY_SIZE(obsolete
), rule
.name
);
429 if (!*obsolete
[index
][1])
430 return error("%s is obsolete", argv
[0]);
431 /* Keep the initial prefix if defined. */
432 code
= parse_color_name(obsolete
[index
][1], &rule
, prefix
? NULL
: &prefix
);
435 info
= add_line_rule(prefix
, &rule
);
439 return error("Unknown color name: %s", argv
[0]);
441 code
= error("%s has been replaced by %s",
442 obsolete
[index
][0], obsolete
[index
][1]);
445 if (!set_color(&info
->fg
, argv
[1]))
446 return error("Unknown color: %s", argv
[1]);
448 if (!set_color(&info
->bg
, argv
[2]))
449 return error("Unknown color: %s", argv
[2]);
455 if (!set_attribute(&attr
, argv
[argc
]))
456 return error("Unknown color attribute: %s", argv
[argc
]);
463 static enum status_code
464 parse_bool(bool *opt
, const char *arg
)
466 *opt
= (!strcmp(arg
, "1") || !strcmp(arg
, "true") || !strcmp(arg
, "yes"))
468 if (*opt
|| !strcmp(arg
, "0") || !strcmp(arg
, "false") || !strcmp(arg
, "no"))
470 return error("Non-boolean value treated as false: %s", arg
);
473 static enum status_code
474 parse_enum(const char *name
, unsigned int *opt
, const char *arg
,
475 const struct enum_map
*map
)
478 enum status_code code
;
480 assert(map
->size
> 1);
482 if (map_enum_do(map
->entries
, map
->size
, (int *) opt
, arg
))
485 code
= parse_bool(&is_true
, arg
);
486 *opt
= is_true
? map
->entries
[1].value
: map
->entries
[0].value
;
489 return error("'%s' is not a valid value for %s; using %s",
490 arg
, name
, enum_name(map
->entries
[*opt
].name
));
493 static enum status_code
494 parse_string(char *opt
, const char *arg
, size_t optsize
)
496 int arglen
= strlen(arg
);
501 if (arglen
== 1 || arg
[arglen
- 1] != arg
[0])
502 return ERROR_UNMATCHED_QUOTATION
;
503 arg
+= 1; arglen
-= 2;
505 string_ncopy_do(opt
, optsize
, arg
, arglen
);
510 static enum status_code
511 parse_encoding(struct encoding
**encoding_ref
, const char *arg
, bool priority
)
513 char buf
[SIZEOF_STR
];
514 enum status_code code
= parse_string(buf
, arg
, sizeof(buf
));
516 if (code
== SUCCESS
) {
517 struct encoding
*encoding
= *encoding_ref
;
519 if (encoding
&& !priority
)
521 encoding
= encoding_open(buf
);
523 *encoding_ref
= encoding
;
529 static enum status_code
530 parse_args(const char ***args
, const char *argv
[])
532 if (!argv_copy(args
, argv
))
533 return ERROR_OUT_OF_MEMORY
;
538 parse_option(struct option_info
*option
, const char *prefix
, const char *arg
)
540 char name
[SIZEOF_STR
];
542 if (!enum_name_prefixed(name
, sizeof(name
), prefix
, option
->name
))
543 return error("Failed to parse option");
545 if (!strcmp("show-notes", name
)) {
546 bool *value
= option
->value
;
547 enum status_code res
;
549 if (parse_bool(option
->value
, arg
) == SUCCESS
)
553 string_copy(opt_notes_arg
, NOTES_EQ_ARG
);
554 res
= parse_string(opt_notes_arg
+ STRING_SIZE(NOTES_EQ_ARG
), arg
,
555 sizeof(opt_notes_arg
) - STRING_SIZE(NOTES_EQ_ARG
));
556 if (res
== SUCCESS
&& !opt_notes_arg
[STRING_SIZE(NOTES_EQ_ARG
)])
557 opt_notes_arg
[STRING_SIZE(NOTES_ARG
)] = 0;
561 if (!strcmp(option
->type
, "bool"))
562 return parse_bool(option
->value
, arg
);
564 if (!strcmp(option
->type
, "double"))
565 return parse_step(option
->value
, arg
);
567 if (!strncmp(option
->type
, "enum", 4)) {
568 const char *type
= option
->type
+ STRING_SIZE("enum ");
569 const struct enum_map
*map
= find_enum_map(type
);
571 return parse_enum(name
, option
->value
, arg
, map
);
574 if (!strcmp(option
->type
, "int")) {
575 if (strstr(name
, "title-overflow")) {
576 bool enabled
= FALSE
;
577 int *value
= option
->value
;
579 /* We try to parse it as a boolean (and set the
580 * value to 0 if fale), otherwise we parse it as
581 * an integer and use the given value. */
582 if (parse_bool(&enabled
, arg
) == SUCCESS
) {
591 if (!strcmp(name
, "line-number-interval") ||
592 !strcmp(name
, "tab-size"))
593 return parse_int(option
->value
, arg
, 1, 1024);
594 else if (!strcmp(name
, "id-width"))
595 return parse_int(option
->value
, arg
, 0, SIZEOF_REV
- 1);
597 return parse_int(option
->value
, arg
, 0, 1024);
600 return error("Unhandled option: %s", name
);
603 static enum status_code
604 parse_view_settings(struct view_column
**view_column
, const char *name_
, const char *argv
[])
606 char buf
[SIZEOF_STR
];
607 const char *name
= enum_name_copy(buf
, sizeof(buf
), name_
) ? buf
: name_
;
608 const char *prefixed
;
610 if ((prefixed
= strstr(name
, "-view-"))) {
611 const char *column_name
= prefixed
+ STRING_SIZE("-view-");
612 size_t column_namelen
= strlen(column_name
);
613 enum view_column_type type
;
615 for (type
= 0; type
< view_column_type_map
->size
; type
++) {
616 const struct enum_map_entry
*column
= &view_column_type_map
->entries
[type
];
618 if (enum_equals(*column
, column_name
, column_namelen
))
619 return parse_view_column_config(name
, type
, NULL
, argv
);
621 if (enum_equals_prefix(*column
, column_name
, column_namelen
))
622 return parse_view_column_config(name
, type
,
623 column_name
+ column
->namelen
+ 1,
628 return parse_view_config(view_column
, name
, argv
);
631 /* Wants: name = value */
632 static enum status_code
633 option_set_command(int argc
, const char *argv
[])
635 struct option_info
*option
;
636 enum status_code code
;
639 return error("Invalid set command: set option = value");
641 if (strcmp(argv
[1], "="))
642 return error("No value assigned to %s", argv
[0]);
644 option
= find_option_info(option_info
, ARRAY_SIZE(option_info
), "", argv
[0]);
649 if (!strcmp(option
->type
, "const char **"))
650 return parse_args(option
->value
, argv
+ 2);
653 return error("Invalid set command: set option = value");
655 if (!strcmp(option
->type
, "view_settings"))
656 return parse_view_settings(option
->value
, argv
[0], argv
+ 2);
658 if (!strcmp(option
->type
, "struct ref_format **"))
659 return parse_ref_formats(option
->value
, argv
+ 2);
661 code
= parse_option(option
, "", argv
[2]);
662 if (code
== SUCCESS
&& argc
!= 3)
663 return error("Option %s only takes one value", argv
[0]);
670 const char *obsolete
[][2] = {
671 { "author-width", "author" },
672 { "filename-width", "file-name" },
673 { "line-number-interval", "line-number" },
674 { "show-author", "author" },
675 { "show-date", "date" },
676 { "show-file-size", "file-size" },
677 { "show-filename", "file-name" },
679 { "show-line-numbers", "line-number" },
680 { "show-refs", "commit-title" },
681 { "show-rev-graph", "commit-title" },
682 { "title-overflow", "commit-title and text" },
684 int index
= find_remapped(obsolete
, ARRAY_SIZE(obsolete
), argv
[0]);
687 return error("%s is obsolete; see tigrc(5) for how to set the %s column option",
688 obsolete
[index
][0], obsolete
[index
][1]);
690 if (!strcmp(argv
[0], "read-git-colors"))
691 return error("read-git-colors has been obsoleted by the git-colors option");
694 return error("Unknown option name: %s", argv
[0]);
697 /* Wants: mode request key */
698 static enum status_code
699 option_bind_command(int argc
, const char *argv
[])
703 enum request request
;
704 struct keymap
*keymap
;
708 return error("Invalid key binding: bind keymap key action");
710 if (!(keymap
= get_keymap(argv
[0], strlen(argv
[0])))) {
711 if (!strcmp(argv
[0], "branch"))
712 keymap
= get_keymap("refs", strlen("refs"));
714 return error("Unknown key map: %s", argv
[0]);
717 for (keys
= 0, key_arg
= argv
[1]; *key_arg
&& keys
< ARRAY_SIZE(key
); keys
++) {
718 enum status_code code
= get_key_value(&key_arg
, &key
[keys
]);
724 if (*key_arg
&& keys
== ARRAY_SIZE(key
))
725 return error("Except for <Esc> combos only one key is allowed "
726 "in key combos: %s", argv
[1]);
728 request
= get_request(argv
[2]);
729 if (request
== REQ_UNKNOWN
) {
730 static const char *obsolete
[][2] = {
731 { "view-branch", "view-refs" },
733 static const char *toggles
[][2] = {
734 { "diff-context-down", "diff-context" },
735 { "diff-context-up", "diff-context" },
736 { "stage-next", ":/^@@" },
737 { "toggle-author", "author" },
738 { "toggle-changes", "show-changes" },
739 { "toggle-commit-order", "show-commit-order" },
740 { "toggle-date", "date" },
741 { "toggle-files", "file-filter" },
742 { "toggle-file-filter", "file-filter" },
743 { "toggle-file-size", "file-size" },
744 { "toggle-filename", "filename" },
745 { "toggle-graphic", "show-graphic" },
746 { "toggle-id", "id" },
747 { "toggle-ignore-space", "show-ignore-space" },
748 { "toggle-lineno", "line-number" },
749 { "toggle-refs", "commit-title-refs" },
750 { "toggle-rev-graph", "commit-title-graph" },
751 { "toggle-show-changes", "show-changes" },
752 { "toggle-sort-field", "sort-field" },
753 { "toggle-sort-order", "sort-order" },
754 { "toggle-title-overflow", "commit-title-overflow" },
755 { "toggle-untracked-dirs", "status-untracked-dirs" },
756 { "toggle-vertical-split", "show-vertical-split" },
760 alias
= find_remapped(obsolete
, ARRAY_SIZE(obsolete
), argv
[2]);
762 const char *action
= obsolete
[alias
][1];
764 add_keybinding(keymap
, get_request(action
), key
, keys
);
765 return error("%s has been renamed to %s",
766 obsolete
[alias
][0], action
);
769 alias
= find_remapped(toggles
, ARRAY_SIZE(toggles
), argv
[2]);
771 const char *action
= toggles
[alias
][0];
772 const char *arg
= prefixcmp(action
, "diff-context-")
773 ? NULL
: (strstr(action
, "-down") ? "-1" : "+1");
774 const char *mapped
= toggles
[alias
][1];
775 const char *toggle
[] = { ":toggle", mapped
, arg
, NULL
};
776 const char *other
[] = { mapped
, NULL
};
777 const char **prompt
= *mapped
== ':' ? other
: toggle
;
778 enum status_code code
= add_run_request(keymap
, key
, keys
, prompt
);
781 code
= error("%s has been replaced by `%s%s%s%s'",
782 action
, prompt
== other
? mapped
: ":toggle ",
783 prompt
== other
? "" : mapped
,
784 arg
? " " : "", arg
? arg
: "");
789 if (request
== REQ_UNKNOWN
)
790 return add_run_request(keymap
, key
, keys
, argv
+ 2);
792 return add_keybinding(keymap
, request
, key
, keys
);
796 static enum status_code
load_option_file(const char *path
);
798 static enum status_code
799 option_source_command(int argc
, const char *argv
[])
801 enum status_code code
;
804 return error("Invalid source command: source path");
806 code
= load_option_file(argv
[0]);
808 return code
== ERROR_FILE_DOES_NOT_EXIST
809 ? error("File does not exist: %s", argv
[0]) : code
;
813 set_option(const char *opt
, int argc
, const char *argv
[])
815 if (!strcmp(opt
, "color"))
816 return option_color_command(argc
, argv
);
818 if (!strcmp(opt
, "set"))
819 return option_set_command(argc
, argv
);
821 if (!strcmp(opt
, "bind"))
822 return option_bind_command(argc
, argv
);
824 if (!strcmp(opt
, "source"))
825 return option_source_command(argc
, argv
);
827 return error("Unknown option command: %s", opt
);
830 struct config_state
{
837 read_option(char *opt
, size_t optlen
, char *value
, size_t valuelen
, void *data
)
839 struct config_state
*config
= data
;
840 enum status_code status
= ERROR_NO_OPTION_VALUE
;
842 /* Check for comment markers, since read_properties() will
843 * only ensure opt and value are split at first " \t". */
844 optlen
= strcspn(opt
, "#");
848 if (opt
[optlen
] == 0) {
849 /* Look for comment endings in the value. */
850 size_t len
= strcspn(value
, "#");
851 const char *argv
[SIZEOF_ARG
];
854 if (len
< valuelen
) {
859 if (!argv_from_string(argv
, &argc
, value
))
860 status
= error("Too many option arguments for %s", opt
);
862 status
= set_option(opt
, argc
, argv
);
865 if (status
!= SUCCESS
) {
866 warn("%s:%zu: %s", config
->path
, config
->lineno
,
867 get_status_message(status
));
868 config
->errors
= TRUE
;
871 /* Always keep going if errors are encountered. */
875 static enum status_code
876 load_option_file(const char *path
)
878 struct config_state config
= { path
, 0, FALSE
};
880 char buf
[SIZEOF_STR
];
882 /* Do not read configuration from stdin if set to "" */
883 if (!path
|| !strlen(path
))
886 if (!prefixcmp(path
, "~/")) {
887 const char *home
= getenv("HOME");
889 if (!home
|| !string_format(buf
, "%s/%s", home
, path
+ 2))
890 return error("Failed to expand ~ to user home directory");
894 /* It's OK that the file doesn't exist. */
895 if (!io_open(&io
, "%s", path
)) {
896 /* XXX: Must return ERROR_FILE_DOES_NOT_EXIST so missing
897 * system tigrc is detected properly. */
898 if (io_error(&io
) == ENOENT
)
899 return ERROR_FILE_DOES_NOT_EXIST
;
900 return error("Error loading file %s: %s", path
, strerror(io_error(&io
)));
903 if (io_load_span(&io
, " \t", &config
.lineno
, read_option
, &config
) == ERR
||
904 config
.errors
== TRUE
)
905 warn("Errors while loading %s.", path
);
909 extern const char *builtin_config
;
914 const char *tigrc_user
= getenv("TIGRC_USER");
915 const char *tigrc_system
= getenv("TIGRC_SYSTEM");
916 const char *tig_diff_opts
= getenv("TIG_DIFF_OPTS");
917 const bool diff_opts_from_args
= !!opt_diff_options
;
918 bool custom_tigrc_system
= !!tigrc_system
;
920 opt_file_filter
= TRUE
;
921 if (!find_option_info_by_value(&opt_diff_context
)->seen
)
922 opt_diff_context
= -3;
924 if (!custom_tigrc_system
)
925 tigrc_system
= SYSCONFDIR
"/tigrc";
927 if (!*tigrc_system
||
928 (load_option_file(tigrc_system
) == ERROR_FILE_DOES_NOT_EXIST
&& !custom_tigrc_system
)) {
929 struct config_state config
= { "<built-in>", 0, FALSE
};
932 if (!io_from_string(&io
, builtin_config
))
933 die("Failed to get built-in config");
934 if (io_load_span(&io
, " \t", &config
.lineno
, read_option
, &config
) == ERR
|| config
.errors
== TRUE
)
935 die("Error in built-in config");
939 tigrc_user
= "~/.tigrc";
940 load_option_file(tigrc_user
);
942 if (!diff_opts_from_args
&& tig_diff_opts
&& *tig_diff_opts
) {
943 static const char *diff_opts
[SIZEOF_ARG
] = { NULL
};
944 char buf
[SIZEOF_STR
];
947 if (!string_format(buf
, "%s", tig_diff_opts
) ||
948 !argv_from_string(diff_opts
, &argc
, buf
))
949 die("TIG_DIFF_OPTS contains too many arguments");
950 else if (!argv_copy(&opt_diff_options
, diff_opts
))
951 die("Failed to format TIG_DIFF_OPTS arguments");
958 format_option_value(const struct option_info
*option
, char buf
[], size_t bufsize
)
962 if (!strcmp(option
->type
, "bool")) {
963 bool *opt
= option
->value
;
965 if (string_nformat(buf
, bufsize
, NULL
, "%s", *opt
? "yes" : "no"))
968 } else if (!strncmp(option
->type
, "enum", 4)) {
969 const char *type
= option
->type
+ STRING_SIZE("enum ");
970 enum author
*opt
= option
->value
;
971 const struct enum_map
*map
= find_enum_map(type
);
973 if (enum_name_copy(buf
, bufsize
, map
->entries
[*opt
].name
))
976 } else if (!strcmp(option
->type
, "int")) {
977 int *opt
= option
->value
;
979 if (opt
== &opt_diff_context
&& *opt
< 0)
982 if (string_nformat(buf
, bufsize
, NULL
, "%d", *opt
))
985 } else if (!strcmp(option
->type
, "double")) {
986 double *opt
= option
->value
;
989 if (string_nformat(buf
, bufsize
, NULL
, "%d", (int) *opt
))
992 } else if (string_nformat(buf
, bufsize
, NULL
, "%.0f%%", (*opt
) * 100)) {
996 } else if (!strcmp(option
->type
, "const char **")) {
997 const char *sep
= "";
998 const char ***opt
= option
->value
;
1002 for (i
= 0; (*opt
) && (*opt
)[i
]; i
++) {
1003 const char *arg
= (*opt
)[i
];
1005 if (!string_nformat(buf
, bufsize
, &bufpos
, "%s%s", sep
, arg
))
1013 } else if (!strcmp(option
->type
, "struct ref_format **")) {
1014 struct ref_format
***opt
= option
->value
;
1016 if (format_ref_formats(*opt
, buf
, bufsize
) == SUCCESS
)
1019 } else if (!strcmp(option
->type
, "view_settings")) {
1020 struct view_column
**opt
= option
->value
;
1022 if (format_view_config(*opt
, buf
, bufsize
) == SUCCESS
)
1026 if (string_nformat(buf
, bufsize
, NULL
, "<%s>", option
->type
))
1034 save_option_settings(FILE *file
)
1036 char buf
[SIZEOF_STR
];
1039 if (!io_fprintf(file
, "%s", "\n## Settings\n"))
1042 for (i
= 0; i
< ARRAY_SIZE(option_info
); i
++) {
1043 struct option_info
*option
= &option_info
[i
];
1044 const char *name
= enum_name(option
->name
);
1045 const char *value
= format_option_value(option
, buf
, sizeof(buf
));
1047 if (!suffixcmp(name
, strlen(name
), "-args"))
1050 if (!io_fprintf(file
, "\nset %-25s = %s", name
, value
))
1058 save_option_keybinding(void *data
, const char *group
, struct keymap
*keymap
,
1059 enum request request
, const char *key
,
1060 const struct request_info
*req_info
,
1061 const struct run_request
*run_req
)
1065 if (group
&& !io_fprintf(file
, "\n# %s", group
))
1068 if (!io_fprintf(file
, "\nbind %-10s %-15s ", enum_name(keymap
->name
), key
))
1072 return io_fprintf(file
, "%s", enum_name(req_info
->name
));
1075 const char *sep
= format_run_request_flags(run_req
);
1078 for (i
= 0; run_req
->argv
[i
]; i
++) {
1079 if (!io_fprintf(file
, "%s%s", sep
, run_req
->argv
[i
]))
1089 save_option_keybindings(FILE *file
)
1091 if (!io_fprintf(file
, "%s", "\n\n## Keybindings\n"))
1094 return foreach_key(save_option_keybinding
, file
, FALSE
);
1098 save_option_color_name(FILE *file
, int color
)
1102 for (i
= 0; i
< ARRAY_SIZE(color_map
); i
++)
1103 if (color_map
[i
].value
== color
)
1104 return io_fprintf(file
, " %-8s", enum_name(color_map
[i
].name
));
1106 return io_fprintf(file
, " color%d", color
);
1110 save_option_color_attr(FILE *file
, int attr
)
1114 for (i
= 0; i
< ARRAY_SIZE(attr_map
); i
++)
1115 if ((attr
& attr_map
[i
].value
) &&
1116 !io_fprintf(file
, " %s", enum_name(attr_map
[i
].name
)))
1123 save_option_color(void *data
, const struct line_rule
*rule
)
1126 const struct line_info
*info
;
1128 for (info
= &rule
->info
; info
; info
= info
->next
) {
1129 const char *prefix
= info
->prefix
? info
->prefix
: "";
1130 const char *prefix_sep
= info
->prefix
? "." : "";
1131 const char *quote
= *rule
->line
? "\"" : "";
1132 const char *name
= *rule
->line
? rule
->line
: enum_name(rule
->name
);
1133 int name_width
= strlen(prefix
) + strlen(prefix_sep
) + 2 * strlen(quote
) + strlen(name
);
1134 int padding
= name_width
> 30 ? 0 : 30 - name_width
;
1136 if (!io_fprintf(file
, "\ncolor %s%s%s%s%s%-*s",
1137 prefix
, prefix_sep
, quote
, name
, quote
, padding
, "")
1138 || !save_option_color_name(file
, info
->fg
)
1139 || !save_option_color_name(file
, info
->bg
)
1140 || !save_option_color_attr(file
, info
->attr
))
1148 save_option_colors(FILE *file
)
1150 if (!io_fprintf(file
, "%s", "\n\n## Colors\n"))
1153 return foreach_line_rule(save_option_color
, file
);
1157 save_options(const char *path
)
1159 int fd
= open(path
, O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1160 FILE *file
= fd
!= -1 ? fdopen(fd
, "w") : NULL
;
1161 enum status_code code
= SUCCESS
;
1164 return error("%s", strerror(errno
));
1166 if (!io_fprintf(file
, "%s", "# Saved by Tig\n")
1167 || !save_option_settings(file
)
1168 || !save_option_keybindings(file
)
1169 || !save_option_colors(file
))
1170 code
= error("Write returned an error");
1177 * Repository properties
1181 set_remote_branch(const char *name
, const char *value
, size_t valuelen
)
1183 if (!strcmp(name
, ".remote")) {
1184 string_ncopy(repo
.remote
, value
, valuelen
);
1186 } else if (*repo
.remote
&& !strcmp(name
, ".merge")) {
1187 size_t from
= strlen(repo
.remote
);
1189 if (!prefixcmp(value
, "refs/heads/"))
1190 value
+= STRING_SIZE("refs/heads/");
1192 if (!string_format_from(repo
.remote
, &from
, "/%s", value
))
1198 set_repo_config_option(char *name
, char *value
, enum status_code (*cmd
)(int, const char **))
1200 const char *argv
[SIZEOF_ARG
] = { name
, "=" };
1201 int argc
= 1 + (cmd
== option_set_command
);
1202 enum status_code code
;
1204 if (!argv_from_string(argv
, &argc
, value
))
1205 code
= error("Too many arguments");
1207 code
= cmd(argc
, argv
);
1209 if (code
!= SUCCESS
)
1210 warn("Option 'tig.%s': %s", name
, get_status_message(code
));
1214 set_work_tree(const char *value
)
1216 char cwd
[SIZEOF_STR
];
1218 if (!getcwd(cwd
, sizeof(cwd
)))
1219 die("Failed to get cwd path: %s", strerror(errno
));
1221 die("Failed to chdir(%s): %s", cwd
, strerror(errno
));
1222 if (chdir(repo
.git_dir
) < 0)
1223 die("Failed to chdir(%s): %s", repo
.git_dir
, strerror(errno
));
1224 if (!getcwd(repo
.git_dir
, sizeof(repo
.git_dir
)))
1225 die("Failed to get git path: %s", strerror(errno
));
1226 if (chdir(value
) < 0)
1227 die("Failed to chdir(%s): %s", value
, strerror(errno
));
1228 if (!getcwd(cwd
, sizeof(cwd
)))
1229 die("Failed to get cwd path: %s", strerror(errno
));
1230 if (setenv("GIT_WORK_TREE", cwd
, TRUE
))
1231 die("Failed to set GIT_WORK_TREE to '%s'", cwd
);
1232 if (setenv("GIT_DIR", repo
.git_dir
, TRUE
))
1233 die("Failed to set GIT_DIR to '%s'", repo
.git_dir
);
1234 repo
.is_inside_work_tree
= TRUE
;
1237 static struct line_info
*
1238 parse_git_color_option(struct line_info
*info
, char *value
)
1240 const char *argv
[SIZEOF_ARG
];
1242 bool first_color
= TRUE
;
1245 if (!argv_from_string(argv
, &argc
, value
))
1248 info
->fg
= COLOR_DEFAULT
;
1249 info
->bg
= COLOR_DEFAULT
;
1252 for (i
= 0; i
< argc
; i
++) {
1255 if (set_attribute(&attr
, argv
[i
])) {
1258 } else if (set_color(&attr
, argv
[i
])) {
1263 first_color
= FALSE
;
1270 set_git_color_option(const char *name
, char *value
)
1272 struct line_info parsed
= {0};
1273 struct line_info
*color
= NULL
;
1274 size_t namelen
= strlen(name
);
1277 if (!opt_git_colors
)
1280 for (i
= 0; opt_git_colors
[i
]; i
++) {
1281 struct line_rule rule
= {0};
1282 const char *prefix
= NULL
;
1283 struct line_info
*info
;
1284 const char *alias
= opt_git_colors
[i
];
1285 const char *sep
= strchr(alias
, '=');
1287 if (!sep
|| namelen
!= sep
- alias
||
1288 string_enum_compare(name
, alias
, namelen
))
1292 color
= parse_git_color_option(&parsed
, value
);
1297 if (parse_color_name(sep
+ 1, &rule
, &prefix
) == SUCCESS
&&
1298 (info
= add_line_rule(prefix
, &rule
))) {
1299 info
->fg
= parsed
.fg
;
1300 info
->bg
= parsed
.bg
;
1301 info
->attr
= parsed
.attr
;
1307 set_encoding(struct encoding
**encoding_ref
, const char *arg
, bool priority
)
1309 if (!strcasecmp(arg
, "utf-8") || !strcasecmp(arg
, "utf8"))
1311 if (parse_encoding(encoding_ref
, arg
, priority
) == SUCCESS
)
1312 encoding_arg
[0] = 0;
1316 read_repo_config_option(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
1318 if (!strcmp(name
, "i18n.commitencoding"))
1319 set_encoding(&default_encoding
, value
, FALSE
);
1321 else if (!strcmp(name
, "gui.encoding"))
1322 set_encoding(&default_encoding
, value
, TRUE
);
1324 else if (!strcmp(name
, "core.editor"))
1325 string_ncopy(opt_editor
, value
, valuelen
);
1327 else if (!strcmp(name
, "core.worktree"))
1328 set_work_tree(value
);
1330 else if (!strcmp(name
, "core.abbrev"))
1331 parse_int(&opt_id_width
, value
, 0, SIZEOF_REV
- 1);
1333 else if (!prefixcmp(name
, "tig.color."))
1334 set_repo_config_option(name
+ 10, value
, option_color_command
);
1336 else if (!prefixcmp(name
, "tig.bind."))
1337 set_repo_config_option(name
+ 9, value
, option_bind_command
);
1339 else if (!prefixcmp(name
, "tig."))
1340 set_repo_config_option(name
+ 4, value
, option_set_command
);
1342 else if (!prefixcmp(name
, "color."))
1343 set_git_color_option(name
+ STRING_SIZE("color."), value
);
1345 else if (*repo
.head
&& !prefixcmp(name
, "branch.") &&
1346 !strncmp(name
+ 7, repo
.head
, strlen(repo
.head
)))
1347 set_remote_branch(name
+ 7 + strlen(repo
.head
), value
, valuelen
);
1349 else if (!strcmp(name
, "diff.context")) {
1350 if (!find_option_info_by_value(&opt_diff_context
)->seen
)
1351 opt_diff_context
= -atoi(value
);
1353 } else if (!strcmp(name
, "format.pretty")) {
1354 if (!prefixcmp(value
, "format:") && strstr(value
, "%C("))
1355 argv_append(&opt_log_options
, "--pretty=medium");
1362 load_git_config(void)
1364 const char *config_list_argv
[] = { "git", "config", "--list", NULL
};
1366 return io_run_load(config_list_argv
, "=", read_repo_config_option
, NULL
);
1369 /* vim: set ts=8 sw=8 noexpandtab: */