@@ -1174,37 +1174,30 @@ static int write_on_eof(
11741174 */
11751175static int config_write (diskfile_backend * cfg , const char * orig_key , const char * key , const p_regex_t * preg , const char * value )
11761176{
1177- int result ;
1178- char * orig_section , * section , * orig_name , * name , * ldot ;
1179- git_filebuf file = GIT_FILEBUF_INIT ;
1177+ char * orig_section = NULL , * section = NULL , * orig_name , * name , * ldot ;
11801178 git_buf buf = GIT_BUF_INIT , contents = GIT_BUF_INIT ;
1181- git_config_parser reader ;
1179+ git_filebuf file = GIT_FILEBUF_INIT ;
11821180 struct write_data write_data ;
1181+ git_config_parser reader ;
1182+ int error ;
11831183
1184- memset (& reader , 0 , sizeof (reader ));
1185- reader .path = cfg -> file .path ;
1184+ memset (& write_data , 0 , sizeof (write_data ));
11861185
11871186 if (cfg -> locked ) {
1188- result = git_buf_puts (& contents , git_buf_cstr (& cfg -> locked_content ) == NULL ? "" : git_buf_cstr (& cfg -> locked_content ));
1187+ error = git_buf_puts (& contents , git_buf_cstr (& cfg -> locked_content ) == NULL ? "" : git_buf_cstr (& cfg -> locked_content ));
11891188 } else {
1190- /* Lock the file */
1191- if ((result = git_filebuf_open (
1192- & file , cfg -> file .path , GIT_FILEBUF_HASH_CONTENTS , GIT_CONFIG_FILE_MODE )) < 0 ) {
1193- git_buf_dispose (& contents );
1194- return result ;
1195- }
1189+ if ((error = git_filebuf_open (& file , cfg -> file .path , GIT_FILEBUF_HASH_CONTENTS ,
1190+ GIT_CONFIG_FILE_MODE )) < 0 )
1191+ goto done ;
11961192
11971193 /* We need to read in our own config file */
1198- result = git_futils_readbuffer (& contents , cfg -> file .path );
1194+ error = git_futils_readbuffer (& contents , cfg -> file .path );
11991195 }
1196+ if (error < 0 && error != GIT_ENOTFOUND )
1197+ goto done ;
12001198
1201- /* Initialise the reading position */
1202- if (result == 0 || result == GIT_ENOTFOUND ) {
1203- git_parse_ctx_init (& reader .ctx , contents .ptr , contents .size );
1204- } else {
1205- git_filebuf_cleanup (& file );
1206- return -1 ; /* OS error when reading the file */
1207- }
1199+ reader .path = cfg -> file .path ;
1200+ git_parse_ctx_init (& reader .ctx , contents .ptr , contents .size );
12081201
12091202 ldot = strrchr (key , '.' );
12101203 name = ldot + 1 ;
@@ -1217,30 +1210,16 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
12171210 GIT_ERROR_CHECK_ALLOC (orig_section );
12181211
12191212 write_data .buf = & buf ;
1220- git_buf_init (& write_data .buffered_comment , 0 );
12211213 write_data .orig_section = orig_section ;
12221214 write_data .section = section ;
1223- write_data .in_section = 0 ;
1224- write_data .preg_replaced = 0 ;
12251215 write_data .orig_name = orig_name ;
12261216 write_data .name = name ;
12271217 write_data .preg = preg ;
12281218 write_data .value = value ;
12291219
1230- result = git_config_parse (& reader ,
1231- write_on_section ,
1232- write_on_variable ,
1233- write_on_comment ,
1234- write_on_eof ,
1235- & write_data );
1236- git__free (section );
1237- git__free (orig_section );
1238- git_buf_dispose (& write_data .buffered_comment );
1239-
1240- if (result < 0 ) {
1241- git_filebuf_cleanup (& file );
1220+ if ((error = git_config_parse (& reader , write_on_section , write_on_variable ,
1221+ write_on_comment , write_on_eof , & write_data )) < 0 )
12421222 goto done ;
1243- }
12441223
12451224 if (cfg -> locked ) {
12461225 size_t len = buf .asize ;
@@ -1250,16 +1229,21 @@ static int config_write(diskfile_backend *cfg, const char *orig_key, const char
12501229 } else {
12511230 git_filebuf_write (& file , git_buf_cstr (& buf ), git_buf_len (& buf ));
12521231
1253- if ((result = git_filebuf_commit (& file )) < 0 )
1232+ if ((error = git_filebuf_commit (& file )) < 0 )
12541233 goto done ;
12551234
1256- if ((result = config_refresh_from_buffer (& cfg -> header .parent , buf .ptr , buf .size )) < 0 )
1235+ if ((error = config_refresh_from_buffer (& cfg -> header .parent , buf .ptr , buf .size )) < 0 )
12571236 goto done ;
12581237 }
12591238
12601239done :
1240+ git__free (section );
1241+ git__free (orig_section );
1242+ git_buf_dispose (& write_data .buffered_comment );
12611243 git_buf_dispose (& buf );
12621244 git_buf_dispose (& contents );
1245+ git_filebuf_cleanup (& file );
12631246 git_parse_ctx_clear (& reader .ctx );
1264- return result ;
1247+
1248+ return error ;
12651249}
0 commit comments