unwind tryBranch into own func

This commit is contained in:
6543 2022-11-12 02:54:56 +01:00
parent 3d9ffcf8d7
commit f13feec8bf
No known key found for this signature in database
GPG key ID: B8BE6D610E61C862
5 changed files with 78 additions and 119 deletions

View file

@ -2,9 +2,7 @@ package server
import (
"net/http"
"path"
"strings"
"time"
"codeberg.org/codeberg/pages/html"
"codeberg.org/codeberg/pages/server/cache"
@ -51,13 +49,14 @@ func tryUpstream(ctx *context.Context, giteaClient *gitea.Client,
}
}
func tryBranch2(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Client,
repoOwner, repoName, branch string, _path []string, canonicalLink string) (
targetRepo, targetPath, targetBranch string, branchTimestamp *time.Time,
works bool) {
// tryBranch checks if a branch exists and populates the target variables. If canonicalLink is non-empty,
// it will also disallow search indexing and add a Link header to the canonical URL.
func tryBranch(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Client,
repoOwner, repoName, branch, path string, canonicalLink bool,
) (*gitea.BranchTimestamp, bool) {
if repoName == "" {
log.Debug().Msg("tryBranch: repo == ''")
return "", "", "", nil, false
log.Debug().Msg("tryBranch: repo is empty")
return nil, false
}
// Replace "~" to "/" so we can access branch that contains slash character
@ -68,25 +67,17 @@ func tryBranch2(log zerolog.Logger, ctx *context.Context, giteaClient *gitea.Cli
branchTimestampResult := upstream.GetBranchTimestamp(giteaClient, repoOwner, repoName, branch)
if branchTimestampResult == nil {
log.Debug().Msg("tryBranch: branch doesn't exist")
return "", "", "", nil, false
return nil, false
}
// Branch exists, use it
targetRepo = repoName
targetPath = path.Join(_path...)
targetBranch = branchTimestampResult.Branch
branchTimestamp = &branchTimestampResult.Timestamp
if canonicalLink != "" {
if canonicalLink {
// Hide from search machines & add canonical link
ctx.RespWriter.Header().Set("X-Robots-Tag", "noarchive, noindex")
ctx.RespWriter.Header().Set("Link",
strings.NewReplacer("%b", targetBranch, "%p", targetPath).Replace(canonicalLink)+
"; rel=\"canonical\"",
)
giteaClient.ContentWebLink(repoOwner, repoName, branchTimestampResult.Branch, path)+
"; rel=\"canonical\"")
}
log.Debug().Msg("tryBranch: true")
return
return branchTimestampResult, true
}