Files
Gogs/routers/repo/commit.go

240 lines
6.5 KiB
Go
Raw Normal View History

2014-03-24 18:25:15 +08:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package repo
import (
"container/list"
"path"
2015-12-15 17:25:45 -05:00
"github.com/gogits/git-module"
"github.com/gogits/gogs/models"
"github.com/gogits/gogs/pkg/context"
"github.com/gogits/gogs/pkg/setting"
2017-04-05 09:17:21 -04:00
"github.com/gogits/gogs/pkg/tool"
)
const (
2017-04-05 09:17:21 -04:00
COMMITS = "repo/commits"
DIFF = "repo/diff/page"
)
2017-06-03 07:26:09 -04:00
func RefCommits(c *context.Context) {
c.Data["PageIsViewFiles"] = true
2014-11-06 22:06:41 -05:00
switch {
2017-06-03 07:26:09 -04:00
case len(c.Repo.TreePath) == 0:
Commits(c)
case c.Repo.TreePath == "search":
SearchCommits(c)
2014-11-06 22:06:41 -05:00
default:
2017-06-03 07:26:09 -04:00
FileHistory(c)
2014-11-06 22:06:41 -05:00
}
}
2014-12-09 02:18:25 -05:00
func RenderIssueLinks(oldCommits *list.List, repoLink string) *list.List {
newCommits := list.New()
for e := oldCommits.Front(); e != nil; e = e.Next() {
c := e.Value.(*git.Commit)
newCommits.PushBack(c)
}
return newCommits
}
2017-06-03 07:26:09 -04:00
func renderCommits(c *context.Context, filename string) {
c.Data["Title"] = c.Tr("repo.commits.commit_history") + " · " + c.Repo.Repository.FullName()
c.Data["PageIsCommits"] = true
2017-06-03 07:26:09 -04:00
page := c.QueryInt("page")
if page < 1 {
page = 1
}
2017-06-03 07:26:09 -04:00
pageSize := c.QueryInt("pageSize")
if pageSize < 1 {
pageSize = git.DefaultCommitsPageSize
}
// Both 'git log branchName' and 'git log commitID' work.
var err error
var commits *list.List
if len(filename) == 0 {
2017-06-03 07:26:09 -04:00
commits, err = c.Repo.Commit.CommitsByRangeSize(page, pageSize)
} else {
2017-06-03 07:26:09 -04:00
commits, err = c.Repo.GitRepo.CommitsByFileAndRangeSize(c.Repo.BranchName, filename, page, pageSize)
}
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "CommitsByRangeSize/CommitsByFileAndRangeSize", err)
return
}
2017-06-03 07:26:09 -04:00
commits = RenderIssueLinks(commits, c.Repo.RepoLink)
2014-09-26 08:55:13 -04:00
commits = models.ValidateCommitsWithEmails(commits)
2017-06-03 07:26:09 -04:00
c.Data["Commits"] = commits
if page > 1 {
2017-06-03 07:26:09 -04:00
c.Data["HasPrevious"] = true
c.Data["PreviousPage"] = page - 1
}
if commits.Len() == pageSize {
2017-06-03 07:26:09 -04:00
c.Data["HasNext"] = true
c.Data["NextPage"] = page + 1
}
2017-06-03 07:26:09 -04:00
c.Data["PageSize"] = pageSize
2017-06-03 07:26:09 -04:00
c.Data["Username"] = c.Repo.Owner.Name
c.Data["Reponame"] = c.Repo.Repository.Name
c.HTML(200, COMMITS)
}
2017-06-03 07:26:09 -04:00
func Commits(c *context.Context) {
renderCommits(c, "")
}
2017-06-03 07:26:09 -04:00
func SearchCommits(c *context.Context) {
c.Data["PageIsCommits"] = true
2017-06-03 07:26:09 -04:00
keyword := c.Query("q")
if len(keyword) == 0 {
2017-06-03 07:26:09 -04:00
c.Redirect(c.Repo.RepoLink + "/commits/" + c.Repo.BranchName)
return
}
2017-06-03 07:26:09 -04:00
commits, err := c.Repo.Commit.SearchCommits(keyword)
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "SearchCommits", err)
return
}
2017-06-03 07:26:09 -04:00
commits = RenderIssueLinks(commits, c.Repo.RepoLink)
2014-09-26 08:55:13 -04:00
commits = models.ValidateCommitsWithEmails(commits)
2017-06-03 07:26:09 -04:00
c.Data["Commits"] = commits
2017-06-03 07:26:09 -04:00
c.Data["Keyword"] = keyword
c.Data["Username"] = c.Repo.Owner.Name
c.Data["Reponame"] = c.Repo.Repository.Name
c.Data["Branch"] = c.Repo.BranchName
c.HTML(200, COMMITS)
}
2017-06-03 07:26:09 -04:00
func FileHistory(c *context.Context) {
renderCommits(c, c.Repo.TreePath)
2014-11-06 22:06:41 -05:00
}
2017-06-03 07:26:09 -04:00
func Diff(c *context.Context) {
c.Data["PageIsDiff"] = true
c.Data["RequireHighlightJS"] = true
2017-06-03 07:26:09 -04:00
userName := c.Repo.Owner.Name
repoName := c.Repo.Repository.Name
commitID := c.Params(":sha")
2016-03-21 10:49:46 -04:00
2017-06-03 07:26:09 -04:00
commit, err := c.Repo.GitRepo.GetCommit(commitID)
2016-03-21 10:49:46 -04:00
if err != nil {
if git.IsErrNotExist(err) {
2017-06-03 07:26:09 -04:00
c.Handle(404, "Repo.GitRepo.GetCommit", err)
} else {
2017-06-03 07:26:09 -04:00
c.Handle(500, "Repo.GitRepo.GetCommit", err)
}
2016-03-21 10:49:46 -04:00
return
}
diff, err := models.GetDiffCommit(models.RepoPath(userName, repoName),
commitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
if err != nil {
2017-06-03 07:26:09 -04:00
c.NotFoundOrServerError("GetDiffCommit", git.IsErrNotExist, err)
return
}
parents := make([]string, commit.ParentCount())
for i := 0; i < commit.ParentCount(); i++ {
sha, err := commit.ParentID(i)
parents[i] = sha.String()
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(404, "repo.Diff", err)
return
}
}
2017-06-03 07:26:09 -04:00
setEditorconfigIfExists(c)
if c.Written() {
return
}
2017-06-03 07:26:09 -04:00
c.Data["CommitID"] = commitID
c.Data["IsSplitStyle"] = c.Query("style") == "split"
c.Data["Username"] = userName
c.Data["Reponame"] = repoName
c.Data["IsImageFile"] = commit.IsImageFile
c.Data["Title"] = commit.Summary() + " · " + tool.ShortSHA1(commitID)
c.Data["Commit"] = commit
c.Data["Author"] = models.ValidateCommitWithEmail(commit)
c.Data["Diff"] = diff
c.Data["Parents"] = parents
c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", commitID)
2015-08-20 20:18:49 +08:00
if commit.ParentCount() > 0 {
2017-06-03 07:26:09 -04:00
c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", parents[0])
}
2017-06-03 07:26:09 -04:00
c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", commitID)
c.HTML(200, DIFF)
}
func RawDiff(c *context.Context) {
if err := git.GetRawDiff(
models.RepoPath(c.Repo.Owner.Name, c.Repo.Repository.Name),
c.Params(":sha"),
git.RawDiffType(c.Params(":ext")),
c.Resp,
); err != nil {
c.NotFoundOrServerError("GetRawDiff", git.IsErrNotExist, err)
return
}
2016-03-21 10:49:46 -04:00
}
2017-06-03 07:26:09 -04:00
func CompareDiff(c *context.Context) {
c.Data["IsDiffCompare"] = true
userName := c.Repo.Owner.Name
repoName := c.Repo.Repository.Name
beforeCommitID := c.Params(":before")
afterCommitID := c.Params(":after")
2014-08-26 08:20:18 -04:00
2017-06-03 07:26:09 -04:00
commit, err := c.Repo.GitRepo.GetCommit(afterCommitID)
2014-08-26 08:20:18 -04:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(404, "GetCommit", err)
2014-08-26 08:20:18 -04:00
return
}
2015-08-31 16:24:28 +09:00
diff, err := models.GetDiffRange(models.RepoPath(userName, repoName), beforeCommitID,
afterCommitID, setting.Git.MaxGitDiffLines,
setting.Git.MaxGitDiffLineCharacters, setting.Git.MaxGitDiffFiles)
2014-08-26 08:20:18 -04:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(404, "GetDiffRange", err)
2014-08-26 08:20:18 -04:00
return
}
2015-08-31 16:24:28 +09:00
commits, err := commit.CommitsBeforeUntil(beforeCommitID)
2014-08-26 08:20:18 -04:00
if err != nil {
2017-06-03 07:26:09 -04:00
c.Handle(500, "CommitsBeforeUntil", err)
2014-08-26 08:20:18 -04:00
return
}
2014-10-10 21:40:51 -04:00
commits = models.ValidateCommitsWithEmails(commits)
2014-08-26 08:20:18 -04:00
2017-06-03 07:26:09 -04:00
c.Data["IsSplitStyle"] = c.Query("style") == "split"
c.Data["CommitRepoLink"] = c.Repo.RepoLink
c.Data["Commits"] = commits
c.Data["CommitsCount"] = commits.Len()
c.Data["BeforeCommitID"] = beforeCommitID
c.Data["AfterCommitID"] = afterCommitID
c.Data["Username"] = userName
c.Data["Reponame"] = repoName
c.Data["IsImageFile"] = commit.IsImageFile
c.Data["Title"] = "Comparing " + tool.ShortSHA1(beforeCommitID) + "..." + tool.ShortSHA1(afterCommitID) + " · " + userName + "/" + repoName
c.Data["Commit"] = commit
c.Data["Diff"] = diff
c.Data["DiffNotAvailable"] = diff.NumFiles() == 0
c.Data["SourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", afterCommitID)
c.Data["BeforeSourcePath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "src", beforeCommitID)
c.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(userName, repoName, "raw", afterCommitID)
c.HTML(200, DIFF)
2014-08-26 08:20:18 -04:00
}