@@ -1174,7 +1174,9 @@ int git_config_set_multivar(const char *key, const char *value,
11741174static 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 }
0 commit comments