diff --git a/routers/web/repo/branch.go b/routers/web/repo/branch.go index 51d91b7d6a0..5011cbfc773 100644 --- a/routers/web/repo/branch.go +++ b/routers/web/repo/branch.go @@ -193,11 +193,11 @@ func CreateBranch(ctx *context.Context) { if form.CreateTag { target := ctx.Repo.CommitID - if ctx.Repo.IsViewBranch { + if ctx.Repo.RefFullName.IsBranch() { target = ctx.Repo.BranchName } err = release_service.CreateNewTag(ctx, ctx.Doer, ctx.Repo.Repository, target, form.NewBranchName, "") - } else if ctx.Repo.IsViewBranch { + } else if ctx.Repo.RefFullName.IsBranch() { err = repo_service.CreateNewBranch(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.BranchName, form.NewBranchName) } else { err = repo_service.CreateNewBranchFromCommit(ctx, ctx.Doer, ctx.Repo.Repository, ctx.Repo.GitRepo, ctx.Repo.CommitID, form.NewBranchName) diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index b5498ccd2c9..bad8ca56d14 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -390,12 +390,6 @@ func Diff(ctx *context.Context) { } } - ctx.Data["BranchName"], err = commit.GetBranchName() - if err != nil { - ctx.ServerError("commit.GetBranchName", err) - return - } - ctx.HTML(http.StatusOK, tplCommitPage) } diff --git a/routers/web/repo/release.go b/routers/web/repo/release.go index 8909dedbb16..19b83915afd 100644 --- a/routers/web/repo/release.go +++ b/routers/web/repo/release.go @@ -148,8 +148,6 @@ func getReleaseInfos(ctx *context.Context, opts *repo_model.FindReleasesOptions) func Releases(ctx *context.Context) { ctx.Data["PageIsReleaseList"] = true ctx.Data["Title"] = ctx.Tr("repo.release.releases") - ctx.Data["IsViewBranch"] = false - ctx.Data["IsViewTag"] = true listOptions := db.ListOptions{ Page: ctx.FormInt("page"), @@ -194,8 +192,6 @@ func Releases(ctx *context.Context) { func TagsList(ctx *context.Context) { ctx.Data["PageIsTagList"] = true ctx.Data["Title"] = ctx.Tr("repo.release.tags") - ctx.Data["IsViewBranch"] = false - ctx.Data["IsViewTag"] = true ctx.Data["CanCreateRelease"] = ctx.Repo.CanWrite(unit.TypeReleases) && !ctx.Repo.Repository.IsArchived namePattern := ctx.FormTrim("q") diff --git a/routers/web/repo/view_file.go b/routers/web/repo/view_file.go index 0b4381edfb0..57842979184 100644 --- a/routers/web/repo/view_file.go +++ b/routers/web/repo/view_file.go @@ -232,7 +232,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) { ctx.Data["CanEditFile"] = true ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.edit_this_file") } - } else if !ctx.Repo.IsViewBranch { + } else if !ctx.Repo.RefFullName.IsBranch() { ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch") } else if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) { ctx.Data["EditFileTooltip"] = ctx.Tr("repo.editor.fork_before_edit") @@ -305,7 +305,7 @@ func prepareToRenderFile(ctx *context.Context, entry *git.TreeEntry) { ctx.Data["CanDeleteFile"] = true ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.delete_this_file") } - } else if !ctx.Repo.IsViewBranch { + } else if !ctx.Repo.RefFullName.IsBranch() { ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_be_on_a_branch") } else if !ctx.Repo.CanWriteToBranch(ctx, ctx.Doer, ctx.Repo.BranchName) { ctx.Data["DeleteFileTooltip"] = ctx.Tr("repo.editor.must_have_write_access") diff --git a/routers/web/repo/view_home.go b/routers/web/repo/view_home.go index 54a58cd8be9..ff2ee3c7ec1 100644 --- a/routers/web/repo/view_home.go +++ b/routers/web/repo/view_home.go @@ -178,7 +178,7 @@ func prepareHomeSidebarLatestRelease(ctx *context.Context) { } func prepareUpstreamDivergingInfo(ctx *context.Context) { - if !ctx.Repo.Repository.IsFork || !ctx.Repo.IsViewBranch || ctx.Repo.TreePath != "" { + if !ctx.Repo.Repository.IsFork || !ctx.Repo.RefFullName.IsBranch() || ctx.Repo.TreePath != "" { return } upstreamDivergingInfo, err := repo_service.GetUpstreamDivergingInfo(ctx, ctx.Repo.Repository, ctx.Repo.BranchName) diff --git a/services/context/repo.go b/services/context/repo.go index 33a39dced1b..6c348b99cbf 100644 --- a/services/context/repo.go +++ b/services/context/repo.go @@ -53,20 +53,15 @@ type Repository struct { RepoLink string GitRepo *git.Repository - // these fields indicate the current ref type, for example: ".../src/branch/master" means IsViewBranch=true - IsViewBranch bool - IsViewTag bool - IsViewCommit bool - + // RefFullName is the full ref name that the user is viewing RefFullName git.RefName - BranchName string - TagName string + BranchName string // it is the RefFullName's short name if its type is "branch" + TagName string // it is the RefFullName's short name if its type is "tag" TreePath string - // Commit it is always set to the commit for the branch or tag - Commit *git.Commit - CommitID string - + // Commit it is always set to the commit for the branch or tag, or just the commit that the user is viewing + Commit *git.Commit + CommitID string CommitsCount int64 PullRequest *PullRequest @@ -79,7 +74,7 @@ func (r *Repository) CanWriteToBranch(ctx context.Context, user *user_model.User // CanEnableEditor returns true if repository is editable and user has proper access level. func (r *Repository) CanEnableEditor(ctx context.Context, user *user_model.User) bool { - return r.IsViewBranch && r.CanWriteToBranch(ctx, user, r.BranchName) && r.Repository.CanEnableEditor() && !r.Repository.IsArchived + return r.RefFullName.IsBranch() && r.CanWriteToBranch(ctx, user, r.BranchName) && r.Repository.CanEnableEditor() && !r.Repository.IsArchived } // CanCreateBranch returns true if repository is editable and user has proper access level. @@ -174,15 +169,9 @@ func (r *Repository) GetCommitsCount() (int64, error) { if r.Commit == nil { return 0, nil } - var contextName string - if r.IsViewBranch { - contextName = r.BranchName - } else if r.IsViewTag { - contextName = r.TagName - } else { - contextName = r.CommitID - } - return cache.GetInt64(r.Repository.GetCommitsCountCacheKey(contextName, r.IsViewBranch || r.IsViewTag), func() (int64, error) { + contextName := r.RefFullName.ShortName() + isRef := r.RefFullName.IsBranch() || r.RefFullName.IsTag() + return cache.GetInt64(r.Repository.GetCommitsCountCacheKey(contextName, isRef), func() (int64, error) { return r.Commit.CommitsCount() }) } @@ -798,7 +787,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { // Empty repository does not have reference information. if ctx.Repo.Repository.IsEmpty { // assume the user is viewing the (non-existent) default branch - ctx.Repo.IsViewBranch = true ctx.Repo.BranchName = ctx.Repo.Repository.DefaultBranch ctx.Repo.RefFullName = git.RefNameFromBranch(ctx.Repo.BranchName) // these variables are used by the template to "add/upload" new files @@ -834,7 +822,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { ctx.ServerError("GetBranchCommit", err) return } - ctx.Repo.IsViewBranch = true } else { // there is a path in request guessLegacyPath := refType == "" if guessLegacyPath { @@ -853,7 +840,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { } if refType == git.RefTypeBranch && ctx.Repo.GitRepo.IsBranchExist(refShortName) { - ctx.Repo.IsViewBranch = true ctx.Repo.BranchName = refShortName ctx.Repo.RefFullName = git.RefNameFromBranch(refShortName) @@ -864,7 +850,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { } ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() } else if refType == git.RefTypeTag && ctx.Repo.GitRepo.IsTagExist(refShortName) { - ctx.Repo.IsViewTag = true ctx.Repo.RefFullName = git.RefNameFromTag(refShortName) ctx.Repo.TagName = refShortName @@ -879,7 +864,6 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { } ctx.Repo.CommitID = ctx.Repo.Commit.ID.String() } else if git.IsStringLikelyCommitID(ctx.Repo.GetObjectFormat(), refShortName, 7) { - ctx.Repo.IsViewCommit = true ctx.Repo.RefFullName = git.RefNameFromCommit(refShortName) ctx.Repo.CommitID = refShortName @@ -915,13 +899,10 @@ func RepoRefByType(detectRefType git.RefType) func(*Context) { ctx.Data["RefTypeNameSubURL"] = ctx.Repo.RefTypeNameSubURL() ctx.Data["TreePath"] = ctx.Repo.TreePath - ctx.Data["IsViewBranch"] = ctx.Repo.IsViewBranch ctx.Data["BranchName"] = ctx.Repo.BranchName - ctx.Data["IsViewTag"] = ctx.Repo.IsViewTag ctx.Data["TagName"] = ctx.Repo.TagName - ctx.Data["IsViewCommit"] = ctx.Repo.IsViewCommit ctx.Data["CommitID"] = ctx.Repo.CommitID ctx.Data["CanCreateBranch"] = ctx.Repo.CanCreateBranch() // only used by the branch selector dropdown: AllowCreateNewRef diff --git a/templates/repo/blame.tmpl b/templates/repo/blame.tmpl index f8bd22074f4..05f79612bdd 100644 --- a/templates/repo/blame.tmpl +++ b/templates/repo/blame.tmpl @@ -18,7 +18,7 @@