aboutsummaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
Diffstat (limited to 'routers')
-rw-r--r--routers/api/v1/repo/action.go30
-rw-r--r--routers/api/v1/repo/repo.go44
-rw-r--r--routers/web/feed/branch.go13
-rw-r--r--routers/web/repo/commit.go6
-rw-r--r--routers/web/repo/pull.go145
-rw-r--r--routers/web/repo/view_home.go42
-rw-r--r--routers/web/repo/view_home_test.go13
-rw-r--r--routers/web/web.go5
8 files changed, 144 insertions, 154 deletions
diff --git a/routers/api/v1/repo/action.go b/routers/api/v1/repo/action.go
index 99eef2f53b..25aabe6dd2 100644
--- a/routers/api/v1/repo/action.go
+++ b/routers/api/v1/repo/action.go
@@ -1132,18 +1132,23 @@ func GetWorkflowRun(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
runID := ctx.PathParamInt64("run")
- job, _, err := db.GetByID[actions_model.ActionRun](ctx, runID)
+ job, has, err := db.GetByID[actions_model.ActionRun](ctx, runID)
+ if err != nil {
+ ctx.APIErrorInternal(err)
+ return
+ }
- if err != nil || job.RepoID != ctx.Repo.Repository.ID {
- ctx.APIError(http.StatusNotFound, util.ErrNotExist)
+ if !has || job.RepoID != ctx.Repo.Repository.ID {
+ ctx.APIErrorNotFound(util.ErrNotExist)
+ return
}
- convertedArtifact, err := convert.ToActionWorkflowRun(ctx, ctx.Repo.Repository, job)
+ convertedRun, err := convert.ToActionWorkflowRun(ctx, ctx.Repo.Repository, job)
if err != nil {
ctx.APIErrorInternal(err)
return
}
- ctx.JSON(http.StatusOK, convertedArtifact)
+ ctx.JSON(http.StatusOK, convertedRun)
}
// ListWorkflowRunJobs Lists all jobs for a workflow run.
@@ -1237,10 +1242,15 @@ func GetWorkflowJob(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"
jobID := ctx.PathParamInt64("job_id")
- job, _, err := db.GetByID[actions_model.ActionRunJob](ctx, jobID)
+ job, has, err := db.GetByID[actions_model.ActionRunJob](ctx, jobID)
+ if err != nil {
+ ctx.APIErrorInternal(err)
+ return
+ }
- if err != nil || job.RepoID != ctx.Repo.Repository.ID {
- ctx.APIError(http.StatusNotFound, util.ErrNotExist)
+ if !has || job.RepoID != ctx.Repo.Repository.ID {
+ ctx.APIErrorNotFound(util.ErrNotExist)
+ return
}
convertedWorkflowJob, err := convert.ToActionWorkflowJob(ctx, ctx.Repo.Repository, nil, job)
@@ -1251,7 +1261,7 @@ func GetWorkflowJob(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, convertedWorkflowJob)
}
-// GetArtifacts Lists all artifacts for a repository.
+// GetArtifactsOfRun Lists all artifacts for a repository.
func GetArtifactsOfRun(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/actions/runs/{run}/artifacts repository getArtifactsOfRun
// ---
@@ -1354,7 +1364,7 @@ func DeleteActionRun(ctx *context.APIContext) {
runID := ctx.PathParamInt64("run")
run, err := actions_model.GetRunByRepoAndID(ctx, ctx.Repo.Repository.ID, runID)
if errors.Is(err, util.ErrNotExist) {
- ctx.APIError(http.StatusNotFound, err)
+ ctx.APIErrorNotFound(err)
return
} else if err != nil {
ctx.APIErrorInternal(err)
diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go
index 4b88ed0b78..7b3858ccb1 100644
--- a/routers/api/v1/repo/repo.go
+++ b/routers/api/v1/repo/repo.go
@@ -772,13 +772,8 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
var units []repo_model.RepoUnit
var deleteUnitTypes []unit_model.Type
- currHasIssues := repo.UnitEnabled(ctx, unit_model.TypeIssues)
- newHasIssues := currHasIssues
if opts.HasIssues != nil {
- newHasIssues = *opts.HasIssues
- }
- if currHasIssues || newHasIssues {
- if newHasIssues && opts.ExternalTracker != nil && !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
+ if *opts.HasIssues && opts.ExternalTracker != nil && !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
// Check that values are valid
if !validation.IsValidExternalURL(opts.ExternalTracker.ExternalTrackerURL) {
err := errors.New("External tracker URL not valid")
@@ -802,7 +797,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
},
})
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeIssues)
- } else if newHasIssues && opts.ExternalTracker == nil && !unit_model.TypeIssues.UnitGlobalDisabled() {
+ } else if *opts.HasIssues && opts.ExternalTracker == nil && !unit_model.TypeIssues.UnitGlobalDisabled() {
// Default to built-in tracker
var config *repo_model.IssuesConfig
@@ -829,7 +824,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
Config: config,
})
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalTracker)
- } else if !newHasIssues {
+ } else if !*opts.HasIssues {
if !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalTracker)
}
@@ -839,13 +834,8 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
}
}
- currHasWiki := repo.UnitEnabled(ctx, unit_model.TypeWiki)
- newHasWiki := currHasWiki
if opts.HasWiki != nil {
- newHasWiki = *opts.HasWiki
- }
- if currHasWiki || newHasWiki {
- if newHasWiki && opts.ExternalWiki != nil && !unit_model.TypeExternalWiki.UnitGlobalDisabled() {
+ if *opts.HasWiki && opts.ExternalWiki != nil && !unit_model.TypeExternalWiki.UnitGlobalDisabled() {
// Check that values are valid
if !validation.IsValidExternalURL(opts.ExternalWiki.ExternalWikiURL) {
err := errors.New("External wiki URL not valid")
@@ -861,7 +851,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
},
})
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeWiki)
- } else if newHasWiki && opts.ExternalWiki == nil && !unit_model.TypeWiki.UnitGlobalDisabled() {
+ } else if *opts.HasWiki && opts.ExternalWiki == nil && !unit_model.TypeWiki.UnitGlobalDisabled() {
config := &repo_model.UnitConfig{}
units = append(units, repo_model.RepoUnit{
RepoID: repo.ID,
@@ -869,7 +859,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
Config: config,
})
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalWiki)
- } else if !newHasWiki {
+ } else if !*opts.HasWiki {
if !unit_model.TypeExternalWiki.UnitGlobalDisabled() {
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeExternalWiki)
}
@@ -879,13 +869,8 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
}
}
- currHasPullRequests := repo.UnitEnabled(ctx, unit_model.TypePullRequests)
- newHasPullRequests := currHasPullRequests
- if opts.HasPullRequests != nil {
- newHasPullRequests = *opts.HasPullRequests
- }
- if currHasPullRequests || newHasPullRequests {
- if newHasPullRequests && !unit_model.TypePullRequests.UnitGlobalDisabled() {
+ if opts.HasPullRequests != nil && !unit_model.TypePullRequests.UnitGlobalDisabled() {
+ if *opts.HasPullRequests {
// We do allow setting individual PR settings through the API, so
// we get the config settings and then set them
// if those settings were provided in the opts.
@@ -953,18 +938,13 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
Type: unit_model.TypePullRequests,
Config: config,
})
- } else if !newHasPullRequests && !unit_model.TypePullRequests.UnitGlobalDisabled() {
+ } else {
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypePullRequests)
}
}
- currHasProjects := repo.UnitEnabled(ctx, unit_model.TypeProjects)
- newHasProjects := currHasProjects
- if opts.HasProjects != nil {
- newHasProjects = *opts.HasProjects
- }
- if currHasProjects || newHasProjects {
- if newHasProjects && !unit_model.TypeProjects.UnitGlobalDisabled() {
+ if opts.HasProjects != nil && !unit_model.TypeProjects.UnitGlobalDisabled() {
+ if *opts.HasProjects {
unit, err := repo.GetUnit(ctx, unit_model.TypeProjects)
var config *repo_model.ProjectsConfig
if err != nil {
@@ -984,7 +964,7 @@ func updateRepoUnits(ctx *context.APIContext, opts api.EditRepoOption) error {
Type: unit_model.TypeProjects,
Config: config,
})
- } else if !newHasProjects && !unit_model.TypeProjects.UnitGlobalDisabled() {
+ } else {
deleteUnitTypes = append(deleteUnitTypes, unit_model.TypeProjects)
}
}
diff --git a/routers/web/feed/branch.go b/routers/web/feed/branch.go
index 094fd987ac..eb7f6dc5bc 100644
--- a/routers/web/feed/branch.go
+++ b/routers/web/feed/branch.go
@@ -8,6 +8,7 @@ import (
"time"
"code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/services/context"
"github.com/gorilla/feeds"
@@ -15,10 +16,14 @@ import (
// ShowBranchFeed shows tags and/or releases on the repo as RSS / Atom feed
func ShowBranchFeed(ctx *context.Context, repo *repo.Repository, formatType string) {
- commits, err := ctx.Repo.Commit.CommitsByRange(0, 10, "", "", "")
- if err != nil {
- ctx.ServerError("ShowBranchFeed", err)
- return
+ var commits []*git.Commit
+ var err error
+ if ctx.Repo.Commit != nil {
+ commits, err = ctx.Repo.Commit.CommitsByRange(0, 10, "", "", "")
+ if err != nil {
+ ctx.ServerError("ShowBranchFeed", err)
+ return
+ }
}
title := "Latest commits for branch " + ctx.Repo.BranchName
diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go
index 9a06c9359b..0c60abcecd 100644
--- a/routers/web/repo/commit.go
+++ b/routers/web/repo/commit.go
@@ -15,6 +15,7 @@ import (
asymkey_model "code.gitea.io/gitea/models/asymkey"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
+ issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/renderhelper"
repo_model "code.gitea.io/gitea/models/repo"
unit_model "code.gitea.io/gitea/models/unit"
@@ -411,6 +412,11 @@ func Diff(ctx *context.Context) {
}
}
+ pr, _ := issues_model.GetPullRequestByMergedCommit(ctx, ctx.Repo.Repository.ID, commitID)
+ if pr != nil {
+ ctx.Data["MergedPRIssueNumber"] = pr.Index
+ }
+
ctx.HTML(http.StatusOK, tplCommitPage)
}
diff --git a/routers/web/repo/pull.go b/routers/web/repo/pull.go
index c5302dd50f..bc58efeb6f 100644
--- a/routers/web/repo/pull.go
+++ b/routers/web/repo/pull.go
@@ -581,7 +581,7 @@ func GetPullCommits(ctx *context.Context) {
}
resp := &pullCommitList{}
- commits, lastReviewCommitSha, err := pull_service.GetPullCommits(ctx, issue)
+ commits, lastReviewCommitSha, err := pull_service.GetPullCommits(ctx, ctx.Repo.GitRepo, ctx.Doer, issue)
if err != nil {
ctx.JSON(http.StatusInternalServerError, err)
return
@@ -643,8 +643,17 @@ func ViewPullCommits(ctx *context.Context) {
ctx.HTML(http.StatusOK, tplPullCommits)
}
+func indexCommit(commits []*git.Commit, commitID string) *git.Commit {
+ for i := range commits {
+ if commits[i].ID.String() == commitID {
+ return commits[i]
+ }
+ }
+ return nil
+}
+
// ViewPullFiles render pull request changed files list page
-func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommit string, willShowSpecifiedCommitRange, willShowSpecifiedCommit bool) {
+func viewPullFiles(ctx *context.Context, beforeCommitID, afterCommitID string) {
ctx.Data["PageIsPullList"] = true
ctx.Data["PageIsPullFiles"] = true
@@ -654,11 +663,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
}
pull := issue.PullRequest
- var (
- startCommitID string
- endCommitID string
- gitRepo = ctx.Repo.GitRepo
- )
+ gitRepo := ctx.Repo.GitRepo
prInfo := preparePullViewPullInfo(ctx, issue)
if ctx.Written() {
@@ -668,77 +673,68 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
return
}
- // Validate the given commit sha to show (if any passed)
- if willShowSpecifiedCommit || willShowSpecifiedCommitRange {
- foundStartCommit := len(specifiedStartCommit) == 0
- foundEndCommit := len(specifiedEndCommit) == 0
-
- if !(foundStartCommit && foundEndCommit) {
- for _, commit := range prInfo.Commits {
- if commit.ID.String() == specifiedStartCommit {
- foundStartCommit = true
- }
- if commit.ID.String() == specifiedEndCommit {
- foundEndCommit = true
- }
-
- if foundStartCommit && foundEndCommit {
- break
- }
- }
- }
-
- if !(foundStartCommit && foundEndCommit) {
- ctx.NotFound(nil)
- return
- }
- }
-
- if ctx.Written() {
- return
- }
-
headCommitID, err := gitRepo.GetRefCommitID(pull.GetGitHeadRefName())
if err != nil {
ctx.ServerError("GetRefCommitID", err)
return
}
- ctx.Data["IsShowingOnlySingleCommit"] = willShowSpecifiedCommit
+ isSingleCommit := beforeCommitID == "" && afterCommitID != ""
+ ctx.Data["IsShowingOnlySingleCommit"] = isSingleCommit
+ isShowAllCommits := (beforeCommitID == "" || beforeCommitID == prInfo.MergeBase) && (afterCommitID == "" || afterCommitID == headCommitID)
+ ctx.Data["IsShowingAllCommits"] = isShowAllCommits
- if willShowSpecifiedCommit || willShowSpecifiedCommitRange {
- if len(specifiedEndCommit) > 0 {
- endCommitID = specifiedEndCommit
- } else {
- endCommitID = headCommitID
- }
- if len(specifiedStartCommit) > 0 {
- startCommitID = specifiedStartCommit
+ if afterCommitID == "" || afterCommitID == headCommitID {
+ afterCommitID = headCommitID
+ }
+ afterCommit := indexCommit(prInfo.Commits, afterCommitID)
+ if afterCommit == nil {
+ ctx.HTTPError(http.StatusBadRequest, "after commit not found in PR commits")
+ return
+ }
+
+ var beforeCommit *git.Commit
+ if !isSingleCommit {
+ if beforeCommitID == "" || beforeCommitID == prInfo.MergeBase {
+ beforeCommitID = prInfo.MergeBase
+ // mergebase commit is not in the list of the pull request commits
+ beforeCommit, err = gitRepo.GetCommit(beforeCommitID)
+ if err != nil {
+ ctx.ServerError("GetCommit", err)
+ return
+ }
} else {
- startCommitID = prInfo.MergeBase
+ beforeCommit = indexCommit(prInfo.Commits, beforeCommitID)
+ if beforeCommit == nil {
+ ctx.HTTPError(http.StatusBadRequest, "before commit not found in PR commits")
+ return
+ }
}
- ctx.Data["IsShowingAllCommits"] = false
} else {
- endCommitID = headCommitID
- startCommitID = prInfo.MergeBase
- ctx.Data["IsShowingAllCommits"] = true
+ beforeCommit, err = afterCommit.Parent(0)
+ if err != nil {
+ ctx.ServerError("Parent", err)
+ return
+ }
+ beforeCommitID = beforeCommit.ID.String()
}
ctx.Data["Username"] = ctx.Repo.Owner.Name
ctx.Data["Reponame"] = ctx.Repo.Repository.Name
- ctx.Data["AfterCommitID"] = endCommitID
- ctx.Data["BeforeCommitID"] = startCommitID
-
- fileOnly := ctx.FormBool("file-only")
+ ctx.Data["MergeBase"] = prInfo.MergeBase
+ ctx.Data["AfterCommitID"] = afterCommitID
+ ctx.Data["BeforeCommitID"] = beforeCommitID
maxLines, maxFiles := setting.Git.MaxGitDiffLines, setting.Git.MaxGitDiffFiles
files := ctx.FormStrings("files")
+ fileOnly := ctx.FormBool("file-only")
if fileOnly && (len(files) == 2 || len(files) == 1) {
maxLines, maxFiles = -1, -1
}
diffOptions := &gitdiff.DiffOptions{
- AfterCommitID: endCommitID,
+ BeforeCommitID: beforeCommitID,
+ AfterCommitID: afterCommitID,
SkipTo: ctx.FormString("skip-to"),
MaxLines: maxLines,
MaxLineCharacters: setting.Git.MaxGitDiffLineCharacters,
@@ -746,10 +742,6 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
WhitespaceBehavior: gitdiff.GetWhitespaceFlag(ctx.Data["WhitespaceBehavior"].(string)),
}
- if !willShowSpecifiedCommit {
- diffOptions.BeforeCommitID = startCommitID
- }
-
diff, err := gitdiff.GetDiffForRender(ctx, ctx.Repo.RepoLink, gitRepo, diffOptions, files...)
if err != nil {
ctx.ServerError("GetDiff", err)
@@ -761,7 +753,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
// as the viewed information is designed to be loaded only on latest PR
// diff and if you're signed in.
var reviewState *pull_model.ReviewState
- if ctx.IsSigned && !willShowSpecifiedCommit && !willShowSpecifiedCommitRange {
+ if ctx.IsSigned && isShowAllCommits {
reviewState, err = gitdiff.SyncUserSpecificDiff(ctx, ctx.Doer.ID, pull, gitRepo, diff, diffOptions)
if err != nil {
ctx.ServerError("SyncUserSpecificDiff", err)
@@ -769,7 +761,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
}
}
- diffShortStat, err := gitdiff.GetDiffShortStat(ctx.Repo.GitRepo, startCommitID, endCommitID)
+ diffShortStat, err := gitdiff.GetDiffShortStat(ctx.Repo.GitRepo, beforeCommitID, afterCommitID)
if err != nil {
ctx.ServerError("GetDiffShortStat", err)
return
@@ -816,7 +808,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
if !fileOnly {
// note: use mergeBase is set to false because we already have the merge base from the pull request info
- diffTree, err := gitdiff.GetDiffTree(ctx, gitRepo, false, startCommitID, endCommitID)
+ diffTree, err := gitdiff.GetDiffTree(ctx, gitRepo, false, beforeCommitID, afterCommitID)
if err != nil {
ctx.ServerError("GetDiffTree", err)
return
@@ -836,17 +828,6 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
ctx.Data["Diff"] = diff
ctx.Data["DiffNotAvailable"] = diffShortStat.NumFiles == 0
- baseCommit, err := ctx.Repo.GitRepo.GetCommit(startCommitID)
- if err != nil {
- ctx.ServerError("GetCommit", err)
- return
- }
- commit, err := gitRepo.GetCommit(endCommitID)
- if err != nil {
- ctx.ServerError("GetCommit", err)
- return
- }
-
if ctx.IsSigned && ctx.Doer != nil {
if ctx.Data["CanMarkConversation"], err = issues_model.CanMarkConversation(ctx, issue, ctx.Doer); err != nil {
ctx.ServerError("CanMarkConversation", err)
@@ -854,7 +835,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
}
}
- setCompareContext(ctx, baseCommit, commit, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
+ setCompareContext(ctx, beforeCommit, afterCommit, ctx.Repo.Owner.Name, ctx.Repo.Repository.Name)
assigneeUsers, err := repo_model.GetRepoAssignees(ctx, ctx.Repo.Repository)
if err != nil {
@@ -901,7 +882,7 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
ctx.Data["CanBlockUser"] = func(blocker, blockee *user_model.User) bool {
return user_service.CanBlockUser(ctx, ctx.Doer, blocker, blockee)
}
- if !willShowSpecifiedCommit && !willShowSpecifiedCommitRange && pull.Flow == issues_model.PullRequestFlowGithub {
+ if isShowAllCommits && pull.Flow == issues_model.PullRequestFlowGithub {
if err := pull.LoadHeadRepo(ctx); err != nil {
ctx.ServerError("LoadHeadRepo", err)
return
@@ -930,19 +911,17 @@ func viewPullFiles(ctx *context.Context, specifiedStartCommit, specifiedEndCommi
}
func ViewPullFilesForSingleCommit(ctx *context.Context) {
- viewPullFiles(ctx, "", ctx.PathParam("sha"), true, true)
+ // it doesn't support showing files from mergebase to the special commit
+ // otherwise it will be ambiguous
+ viewPullFiles(ctx, "", ctx.PathParam("sha"))
}
func ViewPullFilesForRange(ctx *context.Context) {
- viewPullFiles(ctx, ctx.PathParam("shaFrom"), ctx.PathParam("shaTo"), true, false)
-}
-
-func ViewPullFilesStartingFromCommit(ctx *context.Context) {
- viewPullFiles(ctx, "", ctx.PathParam("sha"), true, false)
+ viewPullFiles(ctx, ctx.PathParam("shaFrom"), ctx.PathParam("shaTo"))
}
func ViewPullFilesForAllCommitsOfPr(ctx *context.Context) {
- viewPullFiles(ctx, "", "", false, false)
+ viewPullFiles(ctx, "", "")
}
// UpdatePullRequest merge PR's baseBranch into headBranch
diff --git a/routers/web/repo/view_home.go b/routers/web/repo/view_home.go
index fd6e746381..f475e93f60 100644
--- a/routers/web/repo/view_home.go
+++ b/routers/web/repo/view_home.go
@@ -19,8 +19,8 @@ import (
unit_model "code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
- giturl "code.gitea.io/gitea/modules/git/url"
"code.gitea.io/gitea/modules/gitrepo"
+ "code.gitea.io/gitea/modules/htmlutil"
"code.gitea.io/gitea/modules/httplib"
"code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
@@ -258,35 +258,41 @@ func handleRepoEmptyOrBroken(ctx *context.Context) {
ctx.Redirect(link)
}
-func handleRepoViewSubmodule(ctx *context.Context, submodule *git.SubModule) {
- // TODO: it needs to use git.NewCommitSubmoduleFile and SubmoduleWebLinkTree to correctly handle relative paths
- submoduleRepoURL, err := giturl.ParseRepositoryURL(ctx, submodule.URL)
- if err != nil {
- HandleGitError(ctx, "handleRepoViewSubmodule: ParseRepositoryURL", err)
+func isViewHomeOnlyContent(ctx *context.Context) bool {
+ return ctx.FormBool("only_content")
+}
+
+func handleRepoViewSubmodule(ctx *context.Context, commitSubmoduleFile *git.CommitSubmoduleFile) {
+ submoduleWebLink := commitSubmoduleFile.SubmoduleWebLinkTree(ctx)
+ if submoduleWebLink == nil {
+ ctx.Data["NotFoundPrompt"] = ctx.Repo.TreePath
+ ctx.NotFound(nil)
return
}
- submoduleURL := giturl.MakeRepositoryWebLink(submoduleRepoURL)
- if httplib.IsCurrentGiteaSiteURL(ctx, submoduleURL) {
- ctx.RedirectToCurrentSite(submoduleURL)
- } else {
+
+ redirectLink := submoduleWebLink.CommitWebLink
+ if isViewHomeOnlyContent(ctx) {
+ ctx.Resp.Header().Set("Content-Type", "text/html; charset=utf-8")
+ _, _ = ctx.Resp.Write([]byte(htmlutil.HTMLFormat(`<a href="%s">%s</a>`, redirectLink, redirectLink)))
+ } else if !httplib.IsCurrentGiteaSiteURL(ctx, redirectLink) {
// don't auto-redirect to external URL, to avoid open redirect or phishing
- ctx.Data["NotFoundPrompt"] = submoduleURL
+ ctx.Data["NotFoundPrompt"] = redirectLink
ctx.NotFound(nil)
+ } else {
+ ctx.Redirect(submoduleWebLink.CommitWebLink)
}
}
func prepareToRenderDirOrFile(entry *git.TreeEntry) func(ctx *context.Context) {
return func(ctx *context.Context) {
if entry.IsSubModule() {
- submodule, err := ctx.Repo.Commit.GetSubModule(entry.Name())
+ commitSubmoduleFile, err := git.GetCommitInfoSubmoduleFile(ctx.Repo.RepoLink, ctx.Repo.TreePath, ctx.Repo.Commit, entry.ID)
if err != nil {
- HandleGitError(ctx, "prepareToRenderDirOrFile: GetSubModule", err)
+ HandleGitError(ctx, "prepareToRenderDirOrFile: GetCommitInfoSubmoduleFile", err)
return
}
- handleRepoViewSubmodule(ctx, submodule)
- return
- }
- if entry.IsDir() {
+ handleRepoViewSubmodule(ctx, commitSubmoduleFile)
+ } else if entry.IsDir() {
prepareToRenderDirectory(ctx)
} else {
prepareFileView(ctx, entry)
@@ -441,7 +447,7 @@ func Home(ctx *context.Context) {
}
}
- if ctx.FormBool("only_content") {
+ if isViewHomeOnlyContent(ctx) {
ctx.HTML(http.StatusOK, tplRepoViewContent)
} else if len(treeNames) != 0 {
ctx.HTML(http.StatusOK, tplRepoView)
diff --git a/routers/web/repo/view_home_test.go b/routers/web/repo/view_home_test.go
index 6264dba71c..dd74ae560b 100644
--- a/routers/web/repo/view_home_test.go
+++ b/routers/web/repo/view_home_test.go
@@ -9,7 +9,6 @@ import (
"code.gitea.io/gitea/models/unittest"
git_module "code.gitea.io/gitea/modules/git"
- "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/contexttest"
"github.com/stretchr/testify/assert"
@@ -19,14 +18,20 @@ func TestViewHomeSubmoduleRedirect(t *testing.T) {
unittest.PrepareTestEnv(t)
ctx, _ := contexttest.MockContext(t, "/user2/repo1/src/branch/master/test-submodule")
- submodule := &git_module.SubModule{Path: "test-submodule", URL: setting.AppURL + "user2/repo-other.git"}
+ submodule := git_module.NewCommitSubmoduleFile("/user2/repo1", "test-submodule", "../repo-other", "any-ref-id")
handleRepoViewSubmodule(ctx, submodule)
assert.Equal(t, http.StatusSeeOther, ctx.Resp.WrittenStatus())
- assert.Equal(t, "/user2/repo-other", ctx.Resp.Header().Get("Location"))
+ assert.Equal(t, "/user2/repo-other/tree/any-ref-id", ctx.Resp.Header().Get("Location"))
ctx, _ = contexttest.MockContext(t, "/user2/repo1/src/branch/master/test-submodule")
- submodule = &git_module.SubModule{Path: "test-submodule", URL: "https://other/user2/repo-other.git"}
+ submodule = git_module.NewCommitSubmoduleFile("/user2/repo1", "test-submodule", "https://other/user2/repo-other.git", "any-ref-id")
handleRepoViewSubmodule(ctx, submodule)
// do not auto-redirect for external URLs, to avoid open redirect or phishing
assert.Equal(t, http.StatusNotFound, ctx.Resp.WrittenStatus())
+
+ ctx, respWriter := contexttest.MockContext(t, "/user2/repo1/src/branch/master/test-submodule?only_content=true")
+ submodule = git_module.NewCommitSubmoduleFile("/user2/repo1", "test-submodule", "../repo-other", "any-ref-id")
+ handleRepoViewSubmodule(ctx, submodule)
+ assert.Equal(t, http.StatusOK, ctx.Resp.WrittenStatus())
+ assert.Equal(t, `<a href="/user2/repo-other/tree/any-ref-id">/user2/repo-other/tree/any-ref-id</a>`, respWriter.Body.String())
}
diff --git a/routers/web/web.go b/routers/web/web.go
index f8612db504..09be0c3904 100644
--- a/routers/web/web.go
+++ b/routers/web/web.go
@@ -1531,7 +1531,7 @@ func registerWebRoutes(m *web.Router) {
m.Group("/commits", func() {
m.Get("", repo.SetWhitespaceBehavior, repo.GetPullDiffStats, repo.ViewPullCommits)
m.Get("/list", repo.GetPullCommits)
- m.Get("/{sha:[a-f0-9]{7,40}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFilesForSingleCommit)
+ m.Get("/{sha:[a-f0-9]{7,64}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFilesForSingleCommit)
})
m.Post("/merge", context.RepoMustNotBeArchived(), web.Bind(forms.MergePullRequestForm{}), repo.MergePullRequest)
m.Post("/cancel_auto_merge", context.RepoMustNotBeArchived(), repo.CancelAutoMergePullRequest)
@@ -1540,8 +1540,7 @@ func registerWebRoutes(m *web.Router) {
m.Post("/cleanup", context.RepoMustNotBeArchived(), repo.CleanUpPullRequest)
m.Group("/files", func() {
m.Get("", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFilesForAllCommitsOfPr)
- m.Get("/{sha:[a-f0-9]{7,40}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFilesStartingFromCommit)
- m.Get("/{shaFrom:[a-f0-9]{7,40}}..{shaTo:[a-f0-9]{7,40}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFilesForRange)
+ m.Get("/{shaFrom:[a-f0-9]{7,64}}..{shaTo:[a-f0-9]{7,64}}", repo.SetEditorconfigIfExists, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.SetShowOutdatedComments, repo.ViewPullFilesForRange)
m.Group("/reviews", func() {
m.Get("/new_comment", repo.RenderNewCodeCommentForm)
m.Post("/comments", web.Bind(forms.CodeCommentForm{}), repo.SetShowOutdatedComments, repo.CreateCodeComment)