1
1
mirror of https://github.com/go-gitea/gitea synced 2025-01-10 17:54:27 +00:00

make the code simpler

This commit is contained in:
Lunny Xiao 2024-12-11 00:28:29 -08:00
parent 07abd8d1a9
commit 743fa68010
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A

View File

@ -29,17 +29,6 @@ import (
"github.com/gobwas/glob" "github.com/gobwas/glob"
) )
// getDiffOrPatch generates either diff or formatted patch data between given revisions
func getDiffOrPatch(repo *git.Repository, compareString string, w io.Writer, patch, binary bool) error {
if patch {
return repo.GetPatch(compareString, w)
}
if binary {
return repo.GetDiffBinary(compareString, w)
}
return repo.GetDiff(compareString, w)
}
// DownloadDiffOrPatch will write the patch for the pr to the writer // DownloadDiffOrPatch will write the patch for the pr to the writer
func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io.Writer, patch, binary bool) error { func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io.Writer, patch, binary bool) error {
if err := pr.LoadBaseRepo(ctx); err != nil { if err := pr.LoadBaseRepo(ctx); err != nil {
@ -53,7 +42,17 @@ func DownloadDiffOrPatch(ctx context.Context, pr *issues_model.PullRequest, w io
} }
defer closer.Close() defer closer.Close()
if err := getDiffOrPatch(gitRepo, pr.MergeBase+"..."+pr.GetGitRefName(), w, patch, binary); err != nil { compareString := pr.MergeBase + "..." + pr.GetGitRefName()
switch {
case patch:
err = gitRepo.GetPatch(compareString, w)
case binary:
err = gitRepo.GetDiffBinary(compareString, w)
default:
err = gitRepo.GetDiff(compareString, w)
}
if err != nil {
log.Error("unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err) log.Error("unable to get patch file from %s to %s in %s Error: %v", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
return fmt.Errorf("unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err) return fmt.Errorf("unable to get patch file from %s to %s in %s Error: %w", pr.MergeBase, pr.HeadBranch, pr.BaseRepo.FullName(), err)
} }