@@ -576,22 +576,50 @@ int git_config_foreach_match(
576
576
* Setters
577
577
**************/
578
578
579
- static int config_error_nofiles (const char * name )
579
+ typedef enum {
580
+ BACKEND_USE_SET ,
581
+ BACKEND_USE_DELETE
582
+ } backend_use ;
583
+
584
+ static const char * uses [] = {
585
+ "set" ,
586
+ "delete"
587
+ };
588
+
589
+ static int get_backend_for_use (git_config_backend * * out ,
590
+ git_config * cfg , const char * name , backend_use use )
580
591
{
592
+ size_t i ;
593
+ file_internal * f ;
594
+
595
+ * out = NULL ;
596
+
597
+ if (git_vector_length (& cfg -> files ) == 0 ) {
598
+ giterr_set (GITERR_CONFIG ,
599
+ "cannot %s value for '%s' when no config files exist" ,
600
+ uses [use ], name );
601
+ return GIT_ENOTFOUND ;
602
+ }
603
+
604
+ git_vector_foreach (& cfg -> files , i , f ) {
605
+ if (!f -> file -> readonly ) {
606
+ * out = f -> file ;
607
+ return 0 ;
608
+ }
609
+ }
610
+
581
611
giterr_set (GITERR_CONFIG ,
582
- "cannot set value for '%s' when no config files exist" , name );
612
+ "cannot %s value for '%s' when all config files are readonly" ,
613
+ uses [use ], name );
583
614
return GIT_ENOTFOUND ;
584
615
}
585
616
586
617
int git_config_delete_entry (git_config * cfg , const char * name )
587
618
{
588
619
git_config_backend * file ;
589
- file_internal * internal ;
590
620
591
- internal = git_vector_get (& cfg -> files , 0 );
592
- if (!internal || !internal -> file )
593
- return config_error_nofiles (name );
594
- file = internal -> file ;
621
+ if (get_backend_for_use (& file , cfg , name , BACKEND_USE_DELETE ) < 0 )
622
+ return GIT_ENOTFOUND ;
595
623
596
624
return file -> del (file , name );
597
625
}
@@ -617,17 +645,14 @@ int git_config_set_string(git_config *cfg, const char *name, const char *value)
617
645
{
618
646
int error ;
619
647
git_config_backend * file ;
620
- file_internal * internal ;
621
648
622
649
if (!value ) {
623
650
giterr_set (GITERR_CONFIG , "the value to set cannot be NULL" );
624
651
return -1 ;
625
652
}
626
653
627
- internal = git_vector_get (& cfg -> files , 0 );
628
- if (!internal || !internal -> file )
629
- return config_error_nofiles (name );
630
- file = internal -> file ;
654
+ if (get_backend_for_use (& file , cfg , name , BACKEND_USE_SET ) < 0 )
655
+ return GIT_ENOTFOUND ;
631
656
632
657
error = file -> set (file , name , value );
633
658
@@ -1032,25 +1057,19 @@ int git_config_multivar_iterator_new(git_config_iterator **out, const git_config
1032
1057
int git_config_set_multivar (git_config * cfg , const char * name , const char * regexp , const char * value )
1033
1058
{
1034
1059
git_config_backend * file ;
1035
- file_internal * internal ;
1036
1060
1037
- internal = git_vector_get (& cfg -> files , 0 );
1038
- if (!internal || !internal -> file )
1039
- return config_error_nofiles (name );
1040
- file = internal -> file ;
1061
+ if (get_backend_for_use (& file , cfg , name , BACKEND_USE_DELETE ) < 0 )
1062
+ return GIT_ENOTFOUND ;
1041
1063
1042
1064
return file -> set_multivar (file , name , regexp , value );
1043
1065
}
1044
1066
1045
1067
int git_config_delete_multivar (git_config * cfg , const char * name , const char * regexp )
1046
1068
{
1047
1069
git_config_backend * file ;
1048
- file_internal * internal ;
1049
1070
1050
- internal = git_vector_get (& cfg -> files , 0 );
1051
- if (!internal || !internal -> file )
1052
- return config_error_nofiles (name );
1053
- file = internal -> file ;
1071
+ if (get_backend_for_use (& file , cfg , name , BACKEND_USE_DELETE ) < 0 )
1072
+ return GIT_ENOTFOUND ;
1054
1073
1055
1074
return file -> del_multivar (file , name , regexp );
1056
1075
}
0 commit comments