@@ -554,6 +554,11 @@ func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string,
554554 return
555555 }
556556
557+ // Skip check if disabled in repository
558+ if ctx .Repo .Repository .IsPushSecretDetectionEnabled () {
559+ return
560+ }
561+
557562 // Bypass allowed only if user is repository admin
558563 if ctx .opts .GitPushOptions .Bool ("skip.secret-detection" ).Value () && ctx .Repo .IsAdmin () {
559564 return
@@ -564,36 +569,38 @@ func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string,
564569 if newCommitID == ctx .Repo .GetObjectFormat ().EmptyObjectID ().String () {
565570 return
566571 }
567- config , _ , _ := git .NewCommand ("show" ).AddDynamicArguments (repo .DefaultBranch + ":.gitleaks.toml" ).RunStdString (ctx , & git.RunOpts {Dir : repo .RepoPath (), Env : ctx .env })
568- detector , err := newDetector (config )
572+
573+ var err error
574+ var detector * gitleaks.Detector
575+
576+ config , _ , err := git .NewCommand ("show" ).AddDynamicArguments (repo .DefaultBranch + ":.gitleaks.toml" ).RunStdString (ctx , & git.RunOpts {Dir : repo .RepoPath (), Env : ctx .env })
577+ if err != nil { //File has to exist to be taken into consideration
578+ detector , err = newDetector (config )
579+ } else {
580+ detector , err = gitleaks .NewDetectorDefaultConfig ()
581+ }
569582 if err != nil {
570583 ctx .JSON (http .StatusTeapot , private.Response {Err : err .Error (), UserMsg : err .Error ()})
571584 return
572585 }
573586
574587 // if this reference is new we need a base to compare to
575588 if oldCommitID == ctx .Repo .GetObjectFormat ().EmptyObjectID ().String () {
576- base , _ , err := git .NewCommand ("merge-base" ).AddDynamicArguments (newCommitID ).RunStdString (ctx , & git.RunOpts {Dir : repo .RepoPath (), Env : ctx .env })
577- if err != nil {
578- ctx .Status (http .StatusTeapot )
579- return
580- }
581- oldCommitID = base
589+ oldCommitID = repo .DefaultBranch
582590 }
583- // out, _, err = git.NewCommand("format-patch", "--stdout", "-U0").AddDynamicArguments(oldCommitID, newCommitID).RunStdBytes(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
584591 out , _ , err := git .NewCommand ("show" , "-U0" ).AddDynamicArguments (oldCommitID + ".." + newCommitID ).RunStdBytes (ctx , & git.RunOpts {Dir : repo .RepoPath (), Env : ctx .env })
585592 if err != nil {
586593 ctx .JSON (http .StatusTeapot , private.Response {Err : err .Error (), UserMsg : err .Error ()})
587594 return
588595 }
589596 giteaCmd , err := newPreReceiveDiff (bytes .NewReader (out ))
590597 if err != nil {
591- ctx .Status (http .StatusTeapot )
598+ ctx .JSON (http .StatusTeapot , private. Response { Err : err . Error (), UserMsg : err . Error ()} )
592599 return
593600 }
594601 findings , err := detector .DetectGit (giteaCmd , gitleaks .NewRemoteInfo (scm .GitHubPlatform , repo .Website ))
595602 if err != nil {
596- ctx .Status (http .StatusTeapot )
603+ ctx .JSON (http .StatusTeapot , private. Response { Err : err . Error (), UserMsg : err . Error ()} )
597604 return
598605 }
599606
@@ -645,12 +652,9 @@ func init() {
645652
646653func newDetector (config string ) (* gitleaks.Detector , error ) {
647654 viper .SetConfigType ("toml" )
648- var err error
649- if len (config ) > 0 {
650- err = viper .ReadConfig (strings .NewReader (config ))
651- } else {
652- err = viper .ReadConfig (strings .NewReader (gitleaks_config .DefaultConfig ))
653- }
655+
656+ err := viper .ReadConfig (strings .NewReader (config ))
657+
654658 if err != nil {
655659 return nil , err
656660 }
0 commit comments