package utils import ( "html/template" "net/http" "os" "path/filepath" "repobrowser/internal/config" "repobrowser/internal/git" "repobrowser/internal/models" "repobrowser/internal/templating" "strings" "github.com/gin-gonic/gin" ) func RenderPage(c *gin.Context, data models.PageData) { tmpl, err := template.New("base").Parse(templating.TemplateBase) if err != nil { c.String(http.StatusInternalServerError, err.Error()) return } c.Status(http.StatusOK) c.Header("Content-Type", "text/html; charset=utf-8") tmpl.Execute(c.Writer, data) } func ActiveBranch(c *gin.Context, def string) string { if b := c.Query("branch"); b != "" { return b } return def } func RepoPath(cfg config.RepositoryBrowserConfiguration, name string) string { if name == "" || strings.Contains(name, "/") || strings.Contains(name, "..") { return "" } p := filepath.Join(cfg.Root, name) // Confirm it stays inside reposRoot rel, err := filepath.Rel(cfg.Root, p) if err != nil || strings.HasPrefix(rel, "..") { return "" } return p } // ResolveRepo validates the :repo param and returns (repoName, repoPath). // Aborts with 404 on bad input. func ResolveRepo(cfg config.RepositoryBrowserConfiguration, name string) (string, string, bool) { //name := c.Param("repo") p := RepoPath(cfg, name) if p == "" { //c.String(http.StatusNotFound, "repository not found") return "", "", false } if _, err := os.Stat(p); err != nil { //c.String(http.StatusNotFound, "repository not found") return "", "", false } return name, p, true } func BasePageData(c *gin.Context, repoName, rp, tab string, crumb template.HTML) models.PageData { all, cur := git.ListBranches(rp) branch := ActiveBranch(c, cur) return models.PageData{ Repo: repoName, Branch: branch, Branches: git.BuildBranchLinks(all, branch, repoName, tab), ActiveTab: tab, Breadcrumb: crumb, } }