Skip to content

Commit 571ce8c

Browse files
amishhaagitster
authored andcommitted
help: cleanup the contruction of keys_uniq
construction of keys_uniq depends on sort operation executed on keys before processing, which does not gurantee that keys_uniq will be sorted. refactor the code to shift the sort operation after the processing to remove dependency on key's sort operation and strictly maintain the sorted order of keys_uniq. dedent sort -u and sed in tests and replace grep with sed. Signed-off-by: Amisha Chhajed <136238836+amishhaa@users.noreply.github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7c02d39 commit 571ce8c

2 files changed

Lines changed: 65 additions & 45 deletions

File tree

builtin/help.c

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,49 @@ struct slot_expansion {
111111
int found;
112112
};
113113

114+
static void set_config_vars(struct string_list *keys_uniq, struct string_list_item *var)
115+
{
116+
struct strbuf sb = STRBUF_INIT;
117+
const char *str = var->string;
118+
const char *wildcard = strchr(str, '*');
119+
const char *tag = strchr(str, '<');
120+
const char *cut;
121+
122+
if (wildcard && tag)
123+
cut = wildcard < tag ? wildcard : tag;
124+
else if (wildcard)
125+
cut = wildcard;
126+
else if (tag)
127+
cut = tag;
128+
else {
129+
string_list_append(keys_uniq, str);
130+
return;
131+
}
132+
133+
strbuf_add(&sb, str, cut - str);
134+
string_list_append(keys_uniq, sb.buf);
135+
strbuf_release(&sb);
136+
}
137+
138+
static void set_config_sections(struct string_list *keys_uniq, struct string_list_item *var)
139+
{
140+
struct strbuf sb = STRBUF_INIT;
141+
const char *str = var->string;
142+
const char *dot = strchr(str, '.');
143+
const char *cut;
144+
145+
if (dot)
146+
cut = dot;
147+
else {
148+
set_config_vars(keys_uniq, var);
149+
return;
150+
}
151+
152+
strbuf_add(&sb, str, cut - str);
153+
string_list_append(keys_uniq, sb.buf);
154+
strbuf_release(&sb);
155+
}
156+
114157
static void list_config_help(enum show_config_type type)
115158
{
116159
struct slot_expansion slot_expansions[] = {
@@ -156,50 +199,27 @@ static void list_config_help(enum show_config_type type)
156199
BUG("slot_expansion %s.%s is not used",
157200
e->prefix, e->placeholder);
158201

159-
string_list_sort(&keys);
160202
for (size_t i = 0; i < keys.nr; i++) {
161-
const char *var = keys.items[i].string;
162-
const char *wildcard, *tag, *cut;
163-
const char *dot = NULL;
164-
struct strbuf sb = STRBUF_INIT;
165-
166203
switch (type) {
167204
case SHOW_CONFIG_HUMAN:
168-
puts(var);
169-
continue;
205+
string_list_append(&keys_uniq, keys.items[i].string);
206+
break;
170207
case SHOW_CONFIG_SECTIONS:
171-
dot = strchr(var, '.');
208+
set_config_sections(&keys_uniq, &keys.items[i]);
172209
break;
173210
case SHOW_CONFIG_VARS:
211+
set_config_vars(&keys_uniq, &keys.items[i]);
174212
break;
213+
default:
214+
BUG("%d: unexpected type", type);
175215
}
176-
wildcard = strchr(var, '*');
177-
tag = strchr(var, '<');
178-
179-
if (!dot && !wildcard && !tag) {
180-
string_list_append(&keys_uniq, var);
181-
continue;
182-
}
183-
184-
if (dot)
185-
cut = dot;
186-
else if (wildcard && !tag)
187-
cut = wildcard;
188-
else if (!wildcard && tag)
189-
cut = tag;
190-
else
191-
cut = wildcard < tag ? wildcard : tag;
192-
193-
strbuf_add(&sb, var, cut - var);
194-
string_list_append(&keys_uniq, sb.buf);
195-
strbuf_release(&sb);
196-
197216
}
198-
string_list_clear(&keys, 0);
199-
string_list_remove_duplicates(&keys_uniq, 0);
217+
218+
string_list_sort_u(&keys_uniq, 0);
200219
for_each_string_list_item(item, &keys_uniq)
201220
puts(item->string);
202221
string_list_clear(&keys_uniq, 0);
222+
string_list_clear(&keys, 0);
203223
}
204224

205225
static enum help_format parse_help_format(const char *format)

t/t0012-help.sh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,32 +141,32 @@ test_expect_success 'git help -c' '
141141
142142
'\''git help config'\'' for more information
143143
EOF
144-
grep -v -E \
145-
-e "^[^.]+\.[^.]+$" \
146-
-e "^[^.]+\.[^.]+\.[^.]+$" \
144+
sed \
145+
-e "/^[^.]*\.[^.]*$/d" \
146+
-e "/^[^.]*\.[^.]*\.[^.]*$/d" \
147147
help.output >actual &&
148148
test_cmp expect actual
149149
'
150150

151151
test_expect_success 'git help --config-for-completion' '
152152
git help -c >human &&
153-
grep -E \
154-
-e "^[^.]+\.[^.]+$" \
155-
-e "^[^.]+\.[^.]+\.[^.]+$" human |
156-
sed -e "s/\*.*//" -e "s/<.*//" |
157-
sort -u >human.munged &&
153+
sed -n \
154+
-e "/^[^.]*\.[^.]*$/p" \
155+
-e "/^[^.]*\.[^.]*\.[^.]*$/p" human |
156+
sed -e "s/\*.*//" -e "s/<.*//" |
157+
sort -u >human.munged &&
158158
159159
git help --config-for-completion >vars &&
160160
test_cmp human.munged vars
161161
'
162162

163163
test_expect_success 'git help --config-sections-for-completion' '
164164
git help -c >human &&
165-
grep -E \
166-
-e "^[^.]+\.[^.]+$" \
167-
-e "^[^.]+\.[^.]+\.[^.]+$" human |
168-
sed -e "s/\..*//" |
169-
sort -u >human.munged &&
165+
sed -n \
166+
-e "/^[^.]*\.[^.]*$/p" \
167+
-e "/^[^.]*\.[^.]*\.[^.]*$/p" human |
168+
sed -e "s/\..*//" |
169+
sort -u >human.munged &&
170170
171171
git help --config-sections-for-completion >sections &&
172172
test_cmp human.munged sections

0 commit comments

Comments
 (0)