3535 GlobalConfigFile string
3636
3737 // DefaultContext is the default context to run git commands in
38- // will be overwritten by Init with HammerContext
38+ // will be overwritten by InitWithConfigSync with HammerContext
3939 DefaultContext = context .Background ()
4040
4141 // SupportProcReceive version >= 2.29.0
@@ -129,8 +129,9 @@ func VersionInfo() string {
129129 return fmt .Sprintf (format , args ... )
130130}
131131
132- // Init initializes git module
133- func Init (ctx context.Context ) error {
132+ // InitSimple initializes git module with a very simple step, no config changes, no global command arguments.
133+ // This method doesn't change anything to filesystem
134+ func InitSimple (ctx context.Context ) error {
134135 DefaultContext = ctx
135136
136137 if setting .Git .Timeout .Default > 0 {
@@ -141,9 +142,6 @@ func Init(ctx context.Context) error {
141142 return errors .New ("RepoRootPath is empty, git module needs that setting before initialization" )
142143 }
143144
144- if err := os .MkdirAll (setting .RepoRootPath , os .ModePerm ); err != nil {
145- return fmt .Errorf ("unable to create directory %s, err:%w" , setting .RepoRootPath , err )
146- }
147145 GlobalConfigFile = setting .RepoRootPath + "/gitconfig"
148146
149147 if err := SetExecutablePath (setting .Git .Path ); err != nil {
@@ -153,6 +151,20 @@ func Init(ctx context.Context) error {
153151 // force cleanup args
154152 globalCommandArgs = []string {}
155153
154+ return nil
155+ }
156+
157+ // InitWithConfigSync initializes git module. This method may create directories or write files into filesystem
158+ func InitWithConfigSync (ctx context.Context ) error {
159+ err := InitSimple (ctx )
160+ if err != nil {
161+ return err
162+ }
163+
164+ if err = os .MkdirAll (setting .RepoRootPath , os .ModePerm ); err != nil {
165+ return fmt .Errorf ("unable to create directory %s, err:%w" , setting .RepoRootPath , err )
166+ }
167+
156168 if CheckGitVersionAtLeast ("2.9" ) == nil {
157169 // Explicitly disable credential helper, otherwise Git credentials might leak
158170 globalCommandArgs = append (globalCommandArgs , "-c" , "credential.helper=" )
@@ -223,13 +235,11 @@ func Init(ctx context.Context) error {
223235 if err := configSet ("core.longpaths" , "true" ); err != nil {
224236 return err
225237 }
226- }
227- if setting .Git .DisableCoreProtectNTFS {
228- if err := configSet ("core.protectntfs" , "false" ); err != nil {
229- return err
238+ if setting .Git .DisableCoreProtectNTFS {
239+ globalCommandArgs = append (globalCommandArgs , "-c" , "core.protectntfs=false" )
230240 }
231- globalCommandArgs = append (globalCommandArgs , "-c" , "core.protectntfs=false" )
232241 }
242+
233243 return nil
234244}
235245
0 commit comments