Skip to content

Commit e38b675

Browse files
committed
rework commit change list
add finding base for unknown starting point add bypass for pushing
1 parent bd6ae40 commit e38b675

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

routers/private/hook_pre_receive.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) {
122122
newCommitID := opts.NewCommitIDs[i]
123123
refFullName := opts.RefFullNames[i]
124124

125-
preReceiveSecrets(ourCtx, oldCommitID, newCommitID)
125+
preReceiveSecrets(ourCtx, oldCommitID, newCommitID, refFullName)
126126
switch {
127127
case refFullName.IsBranch():
128128
preReceiveBranch(ourCtx, oldCommitID, newCommitID, refFullName)
@@ -543,17 +543,36 @@ func (ctx *preReceiveContext) loadPusherAndPermission() bool {
543543
}
544544

545545
// checks commits for secrets
546-
func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string) {
546+
func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string, _ git.RefName) {
547+
if ctx.opts.GitPushOptions.Bool("skip.secret-detection").Has() {
548+
return
549+
}
550+
repo := ctx.Repo.Repository
551+
552+
// New commit is empty so there's nothing to check for
553+
if newCommitID == ctx.Repo.GetObjectFormat().EmptyObjectID().String() {
554+
return
555+
}
556+
547557
detector, err := gitleaks.NewDetectorDefaultConfig()
548558
if err != nil {
549559
ctx.Status(http.StatusTeapot)
550560
return
551561
}
562+
if oldCommitID == ctx.Repo.GetObjectFormat().EmptyObjectID().String() {
563+
564+
base, _, err := git.NewCommand("merge-base").AddDynamicArguments(newCommitID).RunStdString(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
565+
if err != nil {
566+
ctx.Status(http.StatusTeapot)
567+
return
568+
}
569+
oldCommitID = base
570+
}
571+
// out, _, err = git.NewCommand("format-patch", "--stdout", "-U0").AddDynamicArguments(oldCommitID, newCommitID).RunStdBytes(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
572+
out, _, err := git.NewCommand("show", "-U0").AddDynamicArguments(oldCommitID+".."+newCommitID).RunStdBytes(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
552573

553-
repo := ctx.Repo.Repository
554-
out, _, err := git.NewCommand("diff", "-U0").AddDynamicArguments(oldCommitID, newCommitID).RunStdBytes(ctx, &git.RunOpts{Dir: repo.RepoPath(), Env: ctx.env})
555574
if err != nil {
556-
ctx.Status(http.StatusTeapot)
575+
ctx.JSON(http.StatusTeapot, private.Response{Err: err.Error(), UserMsg: err.Error()})
557576
return
558577
}
559578
giteaCmd, err := newPreReceiveDiff(bytes.NewReader(out))
@@ -568,12 +587,12 @@ func preReceiveSecrets(ctx *preReceiveContext, oldCommitID, newCommitID string)
568587
}
569588
if len(findings) != 0 {
570589
msg := strings.Builder{}
571-
msg.WriteString("This repository has secret detection enabled! Following secrets were detected:\n\n")
590+
msg.WriteString("This repository has secret detection enabled! Following secrets were detected:\n")
572591

573592
for _, finding := range findings {
574-
msg.WriteString(fmt.Sprintf("Commit %s contains a secret in %v:%v\n", newCommitID, finding.File, finding.StartLine))
593+
msg.WriteString(fmt.Sprintf("\n-- Commit %s contains a secret in %v:%v\n", finding.Commit, finding.File, finding.StartLine))
575594
msg.WriteString(fmt.Sprintf("RuleID: %v", finding.RuleID))
576-
msg.WriteString("\n---------------\n")
595+
577596
}
578597

579598
ctx.JSON(http.StatusForbidden, private.Response{UserMsg: msg.String()})
@@ -596,7 +615,6 @@ func newPreReceiveDiff(r io.Reader) (*giteacmd, error) {
596615

597616
// DiffFilesCh implements sources.Git.
598617
func (g *giteacmd) DiffFilesCh() <-chan *gitdiff.File {
599-
log.Info("asking for channel for files")
600618
return g.diffCh
601619
}
602620

@@ -607,6 +625,5 @@ func (g *giteacmd) ErrCh() <-chan error {
607625

608626
// Wait implements sources.Git.
609627
func (g *giteacmd) Wait() (err error) {
610-
log.Info("asking for wait")
611628
return nil
612629
}

0 commit comments

Comments
 (0)