@@ -1174,7 +1174,9 @@ int git_config_set_multivar(const char *key, const char *value,
1174
1174
static int section_name_match (const char * buf , const char * name )
1175
1175
{
1176
1176
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 ++ ) {
1178
1180
if (!dot && isspace (buf [i ])) {
1179
1181
dot = 1 ;
1180
1182
if (name [j ++ ] != '.' )
@@ -1195,7 +1197,17 @@ static int section_name_match (const char *buf, const char *name)
1195
1197
if (buf [i ] != name [j ++ ])
1196
1198
break ;
1197
1199
}
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 ;
1199
1211
}
1200
1212
1201
1213
/* 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)
1225
1237
while (fgets (buf , sizeof (buf ), config_file )) {
1226
1238
int i ;
1227
1239
int length ;
1240
+ char * output = buf ;
1228
1241
for (i = 0 ; buf [i ] && isspace (buf [i ]); i ++ )
1229
1242
; /* do nothing */
1230
1243
if (buf [i ] == '[' ) {
1231
1244
/* 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 ) {
1233
1247
ret ++ ;
1234
1248
if (new_name == NULL ) {
1235
1249
remove = 1 ;
@@ -1240,14 +1254,29 @@ int git_config_rename_section(const char *old_name, const char *new_name)
1240
1254
ret = write_error (lock -> filename );
1241
1255
goto out ;
1242
1256
}
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
+ }
1244
1273
}
1245
1274
remove = 0 ;
1246
1275
}
1247
1276
if (remove )
1248
1277
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 ) {
1251
1280
ret = write_error (lock -> filename );
1252
1281
goto out ;
1253
1282
}
0 commit comments