mirror of
https://codeberg.org/Codeberg/pages-server.git
synced 2025-04-25 14:26:58 +00:00
unwind tryBranch into own func
This commit is contained in:
parent
3d9ffcf8d7
commit
f13feec8bf
5 changed files with 78 additions and 119 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue