@matheusbsbs / matheus-bagliano / commits / 9a5abfc737
Fork 0

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>

Alex Vandiver committed Jul 24, 2009 at 17:21 UTC 9a5abfc737b18c23d060ff3be1ee8df560b81fa5
2 files changed +41 -3
config.c
+19 -3
@@ -1237,6 +1237,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
1237 while (fgets(buf, sizeof(buf), config_file)) {
1238 int i;
1239 int length;
1240 + char *output = buf;
1241 for (i = 0; buf[i] && isspace(buf[i]); i++)
1242 ; /* do nothing */
1243 if (buf[i] == '[') {
@@ -1253,14 +1254,29 @@ int git_config_rename_section(const char *old_name, const char *new_name)
1254 ret = write_error(lock->filename);
1255 goto out;
1256 }
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 + }
1273 }
1274 remove = 0;
1275 }
1276 if (remove)
1277 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) {
1280 ret = write_error(lock->filename);
1281 goto out;
1282 }
t/t1300-repo-config.sh
+22
@@ -459,6 +459,28 @@ EOF
459
460 test_expect_success "rename succeeded" "test_cmp expect .git/config"
461
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 +
484 cat >> .git/config << EOF
485 [branch "zwei"] a = 1 [branch "vier"]
486 EOF