@@ -11,7 +11,6 @@ import (
1111 "os"
1212 "os/exec"
1313 "path/filepath"
14- "regexp"
1514 "runtime"
1615 "strings"
1716 "time"
@@ -219,187 +218,3 @@ func InitFull(ctx context.Context) (err error) {
219218
220219 return syncGitConfig ()
221220}
222-
223- // syncGitConfig only modifies gitconfig, won't change global variables (otherwise there will be data-race problem)
224- func syncGitConfig () (err error ) {
225- if err = os .MkdirAll (HomeDir (), os .ModePerm ); err != nil {
226- return fmt .Errorf ("unable to prepare git home directory %s, err: %w" , HomeDir (), err )
227- }
228-
229- // first, write user's git config options to git config file
230- // user config options could be overwritten by builtin values later, because if a value is builtin, it must have some special purposes
231- for k , v := range setting .GitConfig .Options {
232- if err = configSet (strings .ToLower (k ), v ); err != nil {
233- return err
234- }
235- }
236-
237- // Git requires setting user.name and user.email in order to commit changes - old comment: "if they're not set just add some defaults"
238- // TODO: need to confirm whether users really need to change these values manually. It seems that these values are dummy only and not really used.
239- // If these values are not really used, then they can be set (overwritten) directly without considering about existence.
240- for configKey , defaultValue := range map [string ]string {
241- "user.name" : "Gitea" ,
242- "user.email" :
"[email protected] " ,
243- } {
244- if err := configSetNonExist (configKey , defaultValue ); err != nil {
245- return err
246- }
247- }
248-
249- // Set git some configurations - these must be set to these values for gitea to work correctly
250- if err := configSet ("core.quotePath" , "false" ); err != nil {
251- return err
252- }
253-
254- if DefaultFeatures ().CheckVersionAtLeast ("2.10" ) {
255- if err := configSet ("receive.advertisePushOptions" , "true" ); err != nil {
256- return err
257- }
258- }
259-
260- if DefaultFeatures ().CheckVersionAtLeast ("2.18" ) {
261- if err := configSet ("core.commitGraph" , "true" ); err != nil {
262- return err
263- }
264- if err := configSet ("gc.writeCommitGraph" , "true" ); err != nil {
265- return err
266- }
267- if err := configSet ("fetch.writeCommitGraph" , "true" ); err != nil {
268- return err
269- }
270- }
271-
272- if DefaultFeatures ().SupportProcReceive {
273- // set support for AGit flow
274- if err := configAddNonExist ("receive.procReceiveRefs" , "refs/for" ); err != nil {
275- return err
276- }
277- if err := configAddNonExist ("receive.procReceiveRefs" , "refs/for-review" ); err != nil {
278- return err
279- }
280- } else {
281- if err := configUnsetAll ("receive.procReceiveRefs" , "refs/for" ); err != nil {
282- return err
283- }
284- if err := configUnsetAll ("receive.procReceiveRefs" , "refs/for-review" ); err != nil {
285- return err
286- }
287- }
288-
289- // Due to CVE-2022-24765, git now denies access to git directories which are not owned by current user.
290- // However, some docker users and samba users find it difficult to configure their systems correctly,
291- // so that Gitea's git repositories are owned by the Gitea user.
292- // (Possibly Windows Service users - but ownership in this case should really be set correctly on the filesystem.)
293- // See issue: https://github.com/go-gitea/gitea/issues/19455
294- // As Gitea now always use its internal git config file, and access to the git repositories is managed through Gitea,
295- // it is now safe to set "safe.directory=*" for internal usage only.
296- // Although this setting is only supported by some new git versions, it is also tolerated by earlier versions
297- if err := configAddNonExist ("safe.directory" , "*" ); err != nil {
298- return err
299- }
300-
301- if runtime .GOOS == "windows" {
302- if err := configSet ("core.longpaths" , "true" ); err != nil {
303- return err
304- }
305- if setting .Git .DisableCoreProtectNTFS {
306- err = configSet ("core.protectNTFS" , "false" )
307- } else {
308- err = configUnsetAll ("core.protectNTFS" , "false" )
309- }
310- if err != nil {
311- return err
312- }
313- }
314-
315- // By default partial clones are disabled, enable them from git v2.22
316- if ! setting .Git .DisablePartialClone && DefaultFeatures ().CheckVersionAtLeast ("2.22" ) {
317- if err = configSet ("uploadpack.allowfilter" , "true" ); err != nil {
318- return err
319- }
320- err = configSet ("uploadpack.allowAnySHA1InWant" , "true" )
321- } else {
322- if err = configUnsetAll ("uploadpack.allowfilter" , "true" ); err != nil {
323- return err
324- }
325- err = configUnsetAll ("uploadpack.allowAnySHA1InWant" , "true" )
326- }
327-
328- return err
329- }
330-
331- func configSet (key , value string ) error {
332- stdout , _ , err := NewCommand (DefaultContext , "config" , "--global" , "--get" ).AddDynamicArguments (key ).RunStdString (nil )
333- if err != nil && ! IsErrorExitCode (err , 1 ) {
334- return fmt .Errorf ("failed to get git config %s, err: %w" , key , err )
335- }
336-
337- currValue := strings .TrimSpace (stdout )
338- if currValue == value {
339- return nil
340- }
341-
342- _ , _ , err = NewCommand (DefaultContext , "config" , "--global" ).AddDynamicArguments (key , value ).RunStdString (nil )
343- if err != nil {
344- return fmt .Errorf ("failed to set git global config %s, err: %w" , key , err )
345- }
346-
347- return nil
348- }
349-
350- func configSetNonExist (key , value string ) error {
351- _ , _ , err := NewCommand (DefaultContext , "config" , "--global" , "--get" ).AddDynamicArguments (key ).RunStdString (nil )
352- if err == nil {
353- // already exist
354- return nil
355- }
356- if IsErrorExitCode (err , 1 ) {
357- // not exist, set new config
358- _ , _ , err = NewCommand (DefaultContext , "config" , "--global" ).AddDynamicArguments (key , value ).RunStdString (nil )
359- if err != nil {
360- return fmt .Errorf ("failed to set git global config %s, err: %w" , key , err )
361- }
362- return nil
363- }
364-
365- return fmt .Errorf ("failed to get git config %s, err: %w" , key , err )
366- }
367-
368- func configAddNonExist (key , value string ) error {
369- _ , _ , err := NewCommand (DefaultContext , "config" , "--global" , "--get" ).AddDynamicArguments (key , regexp .QuoteMeta (value )).RunStdString (nil )
370- if err == nil {
371- // already exist
372- return nil
373- }
374- if IsErrorExitCode (err , 1 ) {
375- // not exist, add new config
376- _ , _ , err = NewCommand (DefaultContext , "config" , "--global" , "--add" ).AddDynamicArguments (key , value ).RunStdString (nil )
377- if err != nil {
378- return fmt .Errorf ("failed to add git global config %s, err: %w" , key , err )
379- }
380- return nil
381- }
382- return fmt .Errorf ("failed to get git config %s, err: %w" , key , err )
383- }
384-
385- func configUnsetAll (key , value string ) error {
386- _ , _ , err := NewCommand (DefaultContext , "config" , "--global" , "--get" ).AddDynamicArguments (key ).RunStdString (nil )
387- if err == nil {
388- // exist, need to remove
389- _ , _ , err = NewCommand (DefaultContext , "config" , "--global" , "--unset-all" ).AddDynamicArguments (key , regexp .QuoteMeta (value )).RunStdString (nil )
390- if err != nil {
391- return fmt .Errorf ("failed to unset git global config %s, err: %w" , key , err )
392- }
393- return nil
394- }
395- if IsErrorExitCode (err , 1 ) {
396- // not exist
397- return nil
398- }
399- return fmt .Errorf ("failed to get git config %s, err: %w" , key , err )
400- }
401-
402- // Fsck verifies the connectivity and validity of the objects in the database
403- func Fsck (ctx context.Context , repoPath string , timeout time.Duration , args TrustedCmdArgs ) error {
404- return NewCommand (ctx , "fsck" ).AddArguments (args ... ).Run (& RunOpts {Timeout : timeout , Dir : repoPath })
405- }
0 commit comments