@@ -71,6 +71,64 @@ static int show_origin;
71
71
72
72
static NORETURN void usage_builtin_config (void );
73
73
74
+ static void set_config_source_file (void )
75
+ {
76
+ int nongit = !startup_info -> have_repository ;
77
+
78
+ if (use_global_config + use_system_config + use_local_config +
79
+ use_worktree_config +
80
+ !!given_config_source .file + !!given_config_source .blob > 1 )
81
+ die (_ ("only one config file at a time" ));
82
+
83
+ if (use_local_config && nongit )
84
+ die (_ ("--local can only be used inside a git repository" ));
85
+
86
+ if (given_config_source .blob && nongit )
87
+ die (_ ("--blob can only be used inside a git repository" ));
88
+
89
+ if (given_config_source .file &&
90
+ !strcmp (given_config_source .file , "-" )) {
91
+ given_config_source .file = NULL ;
92
+ given_config_source .use_stdin = 1 ;
93
+ }
94
+
95
+ if (use_global_config ) {
96
+ char * user_config = expand_user_path ("~/.gitconfig" , 0 );
97
+ char * xdg_config = xdg_config_home ("config" );
98
+
99
+ if (!user_config )
100
+ /*
101
+ * It is unknown if HOME/.gitconfig exists, so
102
+ * we do not know if we should write to XDG
103
+ * location; error out even if XDG_CONFIG_HOME
104
+ * is set and points at a sane location.
105
+ */
106
+ die (_ ("$HOME not set" ));
107
+
108
+ if (access_or_warn (user_config , R_OK , 0 ) &&
109
+ xdg_config && !access_or_warn (xdg_config , R_OK , 0 )) {
110
+ given_config_source .file = xdg_config ;
111
+ free (user_config );
112
+ } else {
113
+ given_config_source .file = user_config ;
114
+ free (xdg_config );
115
+ }
116
+ }
117
+ else if (use_system_config )
118
+ given_config_source .file = git_etc_gitconfig ();
119
+ else if (use_local_config )
120
+ given_config_source .file = git_pathdup ("config" );
121
+ else if (use_worktree_config ) {
122
+ given_config_source .file = get_worktree_config (the_repository );
123
+ if (!given_config_source .file )
124
+ die (_ ("--worktree cannot be used with multiple "
125
+ "working trees unless the config\n"
126
+ "extension worktreeConfig is enabled. "
127
+ "Please read \"CONFIGURATION FILE\"\n"
128
+ "section in \"git help worktree\" for details" ));
129
+ }
130
+ }
131
+
74
132
static int option_parse_type (const struct option * opt , const char * arg ,
75
133
int unset )
76
134
{
@@ -604,60 +662,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
604
662
builtin_config_usage ,
605
663
PARSE_OPT_STOP_AT_NON_OPTION );
606
664
607
- if (use_global_config + use_system_config + use_local_config +
608
- use_worktree_config +
609
- !!given_config_source .file + !!given_config_source .blob > 1 ) {
610
- error (_ ("only one config file at a time" ));
611
- usage_builtin_config ();
612
- }
613
-
614
- if (use_local_config && nongit )
615
- die (_ ("--local can only be used inside a git repository" ));
616
-
617
- if (given_config_source .blob && nongit )
618
- die (_ ("--blob can only be used inside a git repository" ));
619
-
620
- if (given_config_source .file &&
621
- !strcmp (given_config_source .file , "-" )) {
622
- given_config_source .file = NULL ;
623
- given_config_source .use_stdin = 1 ;
624
- }
625
-
626
- if (use_global_config ) {
627
- char * user_config = expand_user_path ("~/.gitconfig" , 0 );
628
- char * xdg_config = xdg_config_home ("config" );
629
-
630
- if (!user_config )
631
- /*
632
- * It is unknown if HOME/.gitconfig exists, so
633
- * we do not know if we should write to XDG
634
- * location; error out even if XDG_CONFIG_HOME
635
- * is set and points at a sane location.
636
- */
637
- die (_ ("$HOME not set" ));
638
-
639
- if (access_or_warn (user_config , R_OK , 0 ) &&
640
- xdg_config && !access_or_warn (xdg_config , R_OK , 0 )) {
641
- given_config_source .file = xdg_config ;
642
- free (user_config );
643
- } else {
644
- given_config_source .file = user_config ;
645
- free (xdg_config );
646
- }
647
- }
648
- else if (use_system_config )
649
- given_config_source .file = git_etc_gitconfig ();
650
- else if (use_local_config )
651
- given_config_source .file = git_pathdup ("config" );
652
- else if (use_worktree_config ) {
653
- given_config_source .file = get_worktree_config (the_repository );
654
- if (!given_config_source .file )
655
- die (_ ("--worktree cannot be used with multiple "
656
- "working trees unless the config\n"
657
- "extension worktreeConfig is enabled. "
658
- "Please read \"CONFIGURATION FILE\"\n"
659
- "section in \"git help worktree\" for details" ));
660
- }
665
+ set_config_source_file ();
661
666
662
667
if (respect_includes_opt == -1 )
663
668
config_options .respect_includes = !given_config_source .file ;
0 commit comments