Skip to content

Commit 088e994

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. move strbuf init and release out of loop to reuse same buffer. dedent sort -u and sed in tests and replace grep with sed, to avoid piping grep's output to sed. Suggested-by: Siddharth Shrimali <r.siddharth.shrimali@gmail.com> Signed-off-by: Amisha Chhajed <amishhhaaaa@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7c02d39 commit 088e994

File tree

2 files changed

+78
-52
lines changed

2 files changed

+78
-52
lines changed

builtin/help.c

Lines changed: 56 additions & 35 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[] = {
@@ -131,13 +174,12 @@ static void list_config_help(enum show_config_type type)
131174
struct string_list keys = STRING_LIST_INIT_DUP;
132175
struct string_list keys_uniq = STRING_LIST_INIT_DUP;
133176
struct string_list_item *item;
177+
struct strbuf sb = STRBUF_INIT;
134178

135179
for (p = config_name_list; *p; p++) {
136180
const char *var = *p;
137-
struct strbuf sb = STRBUF_INIT;
138181

139182
for (e = slot_expansions; e->prefix; e++) {
140-
141183
strbuf_reset(&sb);
142184
strbuf_addf(&sb, "%s.%s", e->prefix, e->placeholder);
143185
if (!strcasecmp(var, sb.buf)) {
@@ -146,60 +188,39 @@ static void list_config_help(enum show_config_type type)
146188
break;
147189
}
148190
}
149-
strbuf_release(&sb);
191+
150192
if (!e->prefix)
151193
string_list_append(&keys, var);
152194
}
153195

196+
strbuf_release(&sb);
197+
154198
for (e = slot_expansions; e->prefix; e++)
155199
if (!e->found)
156200
BUG("slot_expansion %s.%s is not used",
157201
e->prefix, e->placeholder);
158202

159-
string_list_sort(&keys);
160203
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-
166204
switch (type) {
167205
case SHOW_CONFIG_HUMAN:
168-
puts(var);
169-
continue;
206+
string_list_append(&keys_uniq, keys.items[i].string);
207+
break;
170208
case SHOW_CONFIG_SECTIONS:
171-
dot = strchr(var, '.');
209+
set_config_sections(&keys_uniq, &keys.items[i]);
172210
break;
173211
case SHOW_CONFIG_VARS:
212+
set_config_vars(&keys_uniq, &keys.items[i]);
174213
break;
214+
default:
215+
BUG("%d: unexpected type", type);
175216
}
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-
197217
}
198-
string_list_clear(&keys, 0);
199-
string_list_remove_duplicates(&keys_uniq, 0);
218+
219+
string_list_sort_u(&keys_uniq, 0);
200220
for_each_string_list_item(item, &keys_uniq)
201221
puts(item->string);
202222
string_list_clear(&keys_uniq, 0);
223+
string_list_clear(&keys, 0);
203224
}
204225

205226
static enum help_format parse_help_format(const char *format)

t/t0012-help.sh

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -141,35 +141,40 @@ test_expect_success 'git help -c' '
141141
142142
'\''git help config'\'' for more information
143143
EOF
144-
grep -v -E \
145-
-e "^[^.]+\.[^.]+$" \
146-
-e "^[^.]+\.[^.]+\.[^.]+$" \
147-
help.output >actual &&
144+
sed -E -e "
145+
/^[^.]+\.[^.]+$/d
146+
/^[^.]+\.[^.]+\.[^.]+$/d
147+
" 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 -E -e "
154+
/^[^.]+\.[^.]+$/b out
155+
/^[^.]+\.[^.]+\.[^.]+$/b out
156+
d
157+
: out
158+
s/\*.*//
159+
s/<.*//
160+
" human | sort -u >human.munged &&
158161
159162
git help --config-for-completion >vars &&
160163
test_cmp human.munged vars
161164
'
162165

163166
test_expect_success 'git help --config-sections-for-completion' '
164167
git help -c >human &&
165-
grep -E \
166-
-e "^[^.]+\.[^.]+$" \
167-
-e "^[^.]+\.[^.]+\.[^.]+$" human |
168-
sed -e "s/\..*//" |
169-
sort -u >human.munged &&
170-
171-
git help --config-sections-for-completion >sections &&
172-
test_cmp human.munged sections
168+
sed -E -e "
169+
/^[^.]+\.[^.]+$/b out
170+
/^[^.]+\.[^.]+\.[^.]+$/b out
171+
d
172+
: out
173+
s/\..*//
174+
" human | sort -u >expect &&
175+
176+
git help --config-sections-for-completion >actual &&
177+
test_cmp expect actual
173178
'
174179

175180
test_section_spacing () {

0 commit comments

Comments
 (0)