Skip to content

Commit 9a5abfc

Browse files
alexmvgitster
authored andcommitted
After renaming a section, print any trailing variable definitions
Signed-off-by: Alex Vandiver <alex@chmrr.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a4c0d46 commit 9a5abfc

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

config.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
12371237
while (fgets(buf, sizeof(buf), config_file)) {
12381238
int i;
12391239
int length;
1240+
char *output = buf;
12401241
for (i = 0; buf[i] && isspace(buf[i]); i++)
12411242
; /* do nothing */
12421243
if (buf[i] == '[') {
@@ -1253,14 +1254,29 @@ int git_config_rename_section(const char *old_name, const char *new_name)
12531254
ret = write_error(lock->filename);
12541255
goto out;
12551256
}
1256-
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+
}
12571273
}
12581274
remove = 0;
12591275
}
12601276
if (remove)
12611277
continue;
1262-
length = strlen(buf);
1263-
if (write_in_full(out_fd, buf, length) != length) {
1278+
length = strlen(output);
1279+
if (write_in_full(out_fd, output, length) != length) {
12641280
ret = write_error(lock->filename);
12651281
goto out;
12661282
}

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)