config: restructure format_config() for better control flow
Commit 578625fa91 (config: add '--name-only' option to list only variable names, 2015-08-10) modified format_config() such that it returned from the middle of the function when showing only keys, resulting in ugly code structure. Reorganize the if statements and dealing with the key-value delimiter to make the function easier to read. Signed-off-by: SZEDER Gábor <szeder@ira.uka.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
SZEDER Gábor committed
Aug 20, 2015 at 16:14 UTC
ebca2d49577665db0318a9c91c0bcca7e4eed963
1 file changed
+37
-41
builtin/config.c
+37
-41
@@ -108,52 +108,48 @@ struct strbuf_list {
108
109
static int format_config(struct strbuf *buf, const char *key_, const char *value_)
110
{
111
- int must_free_vptr = 0;
112
- int must_print_delim = 0;
113
- char value[256];
114
- const char *vptr = value;
115
-
111
strbuf_init(buf, 0);
112
118
- if (show_keys) {
113
+ if (show_keys)
114
strbuf_addstr(buf, key_);
120
- must_print_delim = 1;
121
- }
122
- if (omit_values) {
123
- strbuf_addch(buf, term);
124
- return 0;
125
- }
126
- if (types == TYPE_INT)
127
- sprintf(value, "%"PRId64,
128
- git_config_int64(key_, value_ ? value_ : ""));
129
- else if (types == TYPE_BOOL)
130
- vptr = git_config_bool(key_, value_) ? "true" : "false";
131
- else if (types == TYPE_BOOL_OR_INT) {
132
- int is_bool, v;
133
- v = git_config_bool_or_int(key_, value_, &is_bool);
134
- if (is_bool)
135
- vptr = v ? "true" : "false";
136
- else
137
- sprintf(value, "%d", v);
138
- } else if (types == TYPE_PATH) {
139
- if (git_config_pathname(&vptr, key_, value_) < 0)
140
- return -1;
141
- must_free_vptr = 1;
142
- } else if (value_) {
143
- vptr = value_;
144
- } else {
145
- /* Just show the key name */
146
- vptr = "";
147
- must_print_delim = 0;
148
- }
115
+ if (!omit_values) {
116
+ int must_free_vptr = 0;
117
+ int must_add_delim = show_keys;
118
+ char value[256];
119
+ const char *vptr = value;
120
+
121
+ if (types == TYPE_INT)
122
+ sprintf(value, "%"PRId64,
123
+ git_config_int64(key_, value_ ? value_ : ""));
124
+ else if (types == TYPE_BOOL)
125
+ vptr = git_config_bool(key_, value_) ? "true" : "false";
126
+ else if (types == TYPE_BOOL_OR_INT) {
127
+ int is_bool, v;
128
+ v = git_config_bool_or_int(key_, value_, &is_bool);
129
+ if (is_bool)
130
+ vptr = v ? "true" : "false";
131
+ else
132
+ sprintf(value, "%d", v);
133
+ } else if (types == TYPE_PATH) {
134
+ if (git_config_pathname(&vptr, key_, value_) < 0)
135
+ return -1;
136
+ must_free_vptr = 1;
137
+ } else if (value_) {
138
+ vptr = value_;
139
+ } else {
140
+ /* Just show the key name */
141
+ vptr = "";
142
+ must_add_delim = 0;
143
+ }
144
150
- if (must_print_delim)
151
- strbuf_addch(buf, key_delim);
152
- strbuf_addstr(buf, vptr);
153
- strbuf_addch(buf, term);
145
+ if (must_add_delim)
146
+ strbuf_addch(buf, key_delim);
147
+ strbuf_addstr(buf, vptr);
148
155
- if (must_free_vptr)
156
- free((char *)vptr);
149
+ if (must_free_vptr)
150
+ free((char *)vptr);
151
+ }
152
+ strbuf_addch(buf, term);
153
return 0;
154
}
155