Skip to content

Commit a7c1ef3

Browse files
committed
Merge branch 'av/maint-config-reader'
* av/maint-config-reader: After renaming a section, print any trailing variable definitions Make section_name_match start on '[', and return the length on success
2 parents e34bbd3 + 9a5abfc commit a7c1ef3

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

config.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,9 @@ int git_config_set_multivar(const char *key, const char *value,
11741174
static int section_name_match (const char *buf, const char *name)
11751175
{
11761176
int i = 0, j = 0, dot = 0;
1177-
for (; buf[i] && buf[i] != ']'; i++) {
1177+
if (buf[i] != '[')
1178+
return 0;
1179+
for (i = 1; buf[i] && buf[i] != ']'; i++) {
11781180
if (!dot && isspace(buf[i])) {
11791181
dot = 1;
11801182
if (name[j++] != '.')
@@ -1195,7 +1197,17 @@ static int section_name_match (const char *buf, const char *name)
11951197
if (buf[i] != name[j++])
11961198
break;
11971199
}
1198-
return (buf[i] == ']' && name[j] == 0);
1200+
if (buf[i] == ']' && name[j] == 0) {
1201+
/*
1202+
* We match, now just find the right length offset by
1203+
* gobbling up any whitespace after it, as well
1204+
*/
1205+
i++;
1206+
for (; buf[i] && isspace(buf[i]); i++)
1207+
; /* do nothing */
1208+
return i;
1209+
}
1210+
return 0;
11991211
}
12001212

12011213
/* if new_name == NULL, the section is removed instead */
@@ -1225,11 +1237,13 @@ int git_config_rename_section(const char *old_name, const char *new_name)
12251237
while (fgets(buf, sizeof(buf), config_file)) {
12261238
int i;
12271239
int length;
1240+
char *output = buf;
12281241
for (i = 0; buf[i] && isspace(buf[i]); i++)
12291242
; /* do nothing */
12301243
if (buf[i] == '[') {
12311244
/* it's a section */
1232-
if (section_name_match (&buf[i+1], old_name)) {
1245+
int offset = section_name_match(&buf[i], old_name);
1246+
if (offset > 0) {
12331247
ret++;
12341248
if (new_name == NULL) {
12351249
remove = 1;
@@ -1240,14 +1254,29 @@ int git_config_rename_section(const char *old_name, const char *new_name)
12401254
ret = write_error(lock->filename);
12411255
goto out;
12421256
}
1243-
continue;
1257+
/*
1258+
* We wrote out the new section, with
1259+
* a newline, now skip the old
1260+
* section's length
1261+
*/
1262+
output += offset + i;
1263+
if (strlen(output) > 0) {
1264+
/*
1265+
* More content means there's
1266+
* a declaration to put on the
1267+
* next line; indent with a
1268+
* tab
1269+
*/
1270+
output -= 1;
1271+
output[0] = '\t';
1272+
}
12441273
}
12451274
remove = 0;
12461275
}
12471276
if (remove)
12481277
continue;
1249-
length = strlen(buf);
1250-
if (write_in_full(out_fd, buf, length) != length) {
1278+
length = strlen(output);
1279+
if (write_in_full(out_fd, output, length) != length) {
12511280
ret = write_error(lock->filename);
12521281
goto out;
12531282
}

t/t1300-repo-config.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,28 @@ EOF
459459

460460
test_expect_success "rename succeeded" "test_cmp expect .git/config"
461461

462+
cat >> .git/config << EOF
463+
[branch "vier"] z = 1
464+
EOF
465+
466+
test_expect_success "rename a section with a var on the same line" \
467+
'git config --rename-section branch.vier branch.zwei'
468+
469+
cat > expect << EOF
470+
# Hallo
471+
#Bello
472+
[branch "zwei"]
473+
x = 1
474+
[branch "zwei"]
475+
y = 1
476+
[branch "drei"]
477+
weird
478+
[branch "zwei"]
479+
z = 1
480+
EOF
481+
482+
test_expect_success "rename succeeded" "test_cmp expect .git/config"
483+
462484
cat >> .git/config << EOF
463485
[branch "zwei"] a = 1 [branch "vier"]
464486
EOF

0 commit comments

Comments
 (0)