gogs/internal/route/api/v1/repo/commits.go
ᴜɴᴋɴᴡᴏɴ 6437d0180b
git: migrate to github.com/gogs/git-module@v1.0.0 (#5958)
* WIP

* Finish `internal/db/git_diff.go`

* FInish internal/db/mirror.go

* Finish internal/db/pull.go

* Finish internal/db/release.go

* Finish internal/db/repo.go

* Finish internal/db/repo_branch.go

* Finish internal/db/repo_editor.go

* Finish internal/db/update.go

* Save my work

* Add license header

* Compile!

* Merge master

* Finish internal/cmd/hook.go

* Finish internal/conf/static.go

* Finish internal/context/repo.go

* Finish internal/db/action.go

* Finish internal/db/git_diff.go

* Fix submodule URL inferring

* Finish internal/db/mirror.go

* Updat to beta.4

* css: update fonts

* Finish internal/db/pull.go

* Finish internal/db/release.go

* Finish internal/db/repo_branch.go

* Finish internal/db/wiki.go

* gitutil: enhance infer submodule UR

* Finish internal/route/api/v1/repo/commits.go

* mirror: only collect branch commits after sync

* mirror: fix tag support

* Finish internal/db/repo.go

* Finish internal/db/repo_editor.go

* Finish internal/db/update.go

* Finish internal/gitutil/pull_request.go

* Make it compile

* Finish internal/route/repo/setting.go

* Finish internal/route/repo/branch.go

* Finish internal/route/api/v1/repo/file.go

* Finish internal/route/repo/download.go

* Finish internal/route/repo/editor.go

* Use helper

* Finish internal/route/repo/issue.go

* Finish internal/route/repo/pull.go

* Finish internal/route/repo/release.go

* Finish internal/route/repo/repo.go

* Finish internal/route/repo/wiki.go

* Finish internal/route/repo/commit.go

* Finish internal/route/repo/view.go

* Finish internal/gitutil/tag.go

* go.sum
2020-03-08 19:09:31 +08:00

140 lines
3.6 KiB
Go

// Copyright 2018 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 (
"net/http"
"strings"
"time"
"github.com/gogs/git-module"
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/context"
"gogs.io/gogs/internal/db"
"gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/gitutil"
)
func GetSingleCommit(c *context.APIContext) {
if strings.Contains(c.Req.Header.Get("Accept"), api.MediaApplicationSHA) {
c.SetParams("*", c.Params(":sha"))
GetReferenceSHA(c)
return
}
gitRepo, err := git.Open(c.Repo.Repository.RepoPath())
if err != nil {
c.ServerError("open repository", err)
return
}
commit, err := gitRepo.CatFileCommit(c.Params(":sha"))
if err != nil {
c.NotFoundOrServerError("get commit", gitutil.IsErrRevisionNotExist, err)
return
}
// Retrieve author and committer information
var apiAuthor, apiCommitter *api.User
author, err := db.GetUserByEmail(commit.Author.Email)
if err != nil && !errors.IsUserNotExist(err) {
c.ServerError("Get user by author email", err)
return
} else if err == nil {
apiAuthor = author.APIFormat()
}
// Save one query if the author is also the committer
if commit.Committer.Email == commit.Author.Email {
apiCommitter = apiAuthor
} else {
committer, err := db.GetUserByEmail(commit.Committer.Email)
if err != nil && !errors.IsUserNotExist(err) {
c.ServerError("Get user by committer email", err)
return
} else if err == nil {
apiCommitter = committer.APIFormat()
}
}
// Retrieve parent(s) of the commit
apiParents := make([]*api.CommitMeta, commit.ParentsCount())
for i := 0; i < commit.ParentsCount(); i++ {
sha, _ := commit.ParentID(i)
apiParents[i] = &api.CommitMeta{
URL: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/commits/" + sha.String(),
SHA: sha.String(),
}
}
c.JSONSuccess(&api.Commit{
CommitMeta: &api.CommitMeta{
URL: conf.Server.ExternalURL + c.Link[1:],
SHA: commit.ID.String(),
},
HTMLURL: c.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(),
RepoCommit: &api.RepoCommit{
URL: conf.Server.ExternalURL + c.Link[1:],
Author: &api.CommitUser{
Name: commit.Author.Name,
Email: commit.Author.Email,
Date: commit.Author.When.Format(time.RFC3339),
},
Committer: &api.CommitUser{
Name: commit.Committer.Name,
Email: commit.Committer.Email,
Date: commit.Committer.When.Format(time.RFC3339),
},
Message: commit.Summary(),
Tree: &api.CommitMeta{
URL: c.BaseURL + "/repos/" + c.Repo.Repository.FullName() + "/tree/" + commit.ID.String(),
SHA: commit.ID.String(),
},
},
Author: apiAuthor,
Committer: apiCommitter,
Parents: apiParents,
})
}
func GetReferenceSHA(c *context.APIContext) {
gitRepo, err := git.Open(c.Repo.Repository.RepoPath())
if err != nil {
c.ServerError("open repository", err)
return
}
ref := c.Params("*")
refType := 0 // 0-unknown, 1-branch, 2-tag
if strings.HasPrefix(ref, git.RefsHeads) {
ref = strings.TrimPrefix(ref, git.RefsHeads)
refType = 1
} else if strings.HasPrefix(ref, git.RefsTags) {
ref = strings.TrimPrefix(ref, git.RefsTags)
refType = 2
} else {
if gitRepo.HasBranch(ref) {
refType = 1
} else if gitRepo.HasTag(ref) {
refType = 2
} else {
c.NotFound()
return
}
}
var sha string
if refType == 1 {
sha, err = gitRepo.BranchCommitID(ref)
} else if refType == 2 {
sha, err = gitRepo.TagCommitID(ref)
}
if err != nil {
c.NotFoundOrServerError("get reference commit ID", gitutil.IsErrRevisionNotExist, err)
return
}
c.PlainText(http.StatusOK, []byte(sha))
}