2019-02-02 22:35:17 -05:00
|
|
|
// Copyright 2018 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2019 The Gitea 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 (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-03-27 04:33:00 -05:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-02-02 22:35:17 -05:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 05:21:34 -05:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2019-02-02 22:35:17 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetSingleCommit get a commit via
|
|
|
|
func GetSingleCommit(ctx *context.APIContext) {
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/git/commits/{sha} repository repoGetSingleCommit
|
|
|
|
// ---
|
|
|
|
// summary: Get a single commit from a repository
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: owner
|
|
|
|
// in: path
|
|
|
|
// description: owner of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: repo
|
|
|
|
// in: path
|
|
|
|
// description: name of the repo
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// - name: sha
|
|
|
|
// in: path
|
|
|
|
// description: the commit hash
|
|
|
|
// type: string
|
|
|
|
// required: true
|
|
|
|
// responses:
|
|
|
|
// "200":
|
|
|
|
// "$ref": "#/responses/Commit"
|
|
|
|
// "404":
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
gitRepo, err := git.OpenRepository(ctx.Repo.Repository.RepoPath())
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("OpenRepository", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
commit, err := gitRepo.GetCommit(ctx.Params(":sha"))
|
|
|
|
if err != nil {
|
|
|
|
ctx.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve author and committer information
|
|
|
|
var apiAuthor, apiCommitter *api.User
|
|
|
|
author, err := models.GetUserByEmail(commit.Author.Email)
|
|
|
|
if err != nil && !models.IsErrUserNotExist(err) {
|
|
|
|
ctx.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 := models.GetUserByEmail(commit.Committer.Email)
|
|
|
|
if err != nil && !models.IsErrUserNotExist(err) {
|
|
|
|
ctx.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.ParentCount())
|
|
|
|
for i := 0; i < commit.ParentCount(); i++ {
|
|
|
|
sha, _ := commit.ParentID(i)
|
|
|
|
apiParents[i] = &api.CommitMeta{
|
|
|
|
URL: ctx.Repo.Repository.APIURL() + "/git/commits/" + sha.String(),
|
|
|
|
SHA: sha.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(200, &api.Commit{
|
|
|
|
CommitMeta: &api.CommitMeta{
|
|
|
|
URL: setting.AppURL + ctx.Link[1:],
|
|
|
|
SHA: commit.ID.String(),
|
|
|
|
},
|
|
|
|
HTMLURL: ctx.Repo.Repository.HTMLURL() + "/commits/" + commit.ID.String(),
|
|
|
|
RepoCommit: &api.RepoCommit{
|
|
|
|
URL: setting.AppURL + ctx.Link[1:],
|
|
|
|
Author: &api.CommitUser{
|
2019-03-22 10:38:49 -05:00
|
|
|
Identity: api.Identity{
|
|
|
|
Name: commit.Author.Name,
|
|
|
|
Email: commit.Author.Email,
|
|
|
|
},
|
|
|
|
Date: commit.Author.When.Format(time.RFC3339),
|
2019-02-02 22:35:17 -05:00
|
|
|
},
|
|
|
|
Committer: &api.CommitUser{
|
2019-03-22 10:38:49 -05:00
|
|
|
Identity: api.Identity{
|
|
|
|
Name: commit.Committer.Name,
|
|
|
|
Email: commit.Committer.Email,
|
|
|
|
},
|
|
|
|
Date: commit.Committer.When.Format(time.RFC3339),
|
2019-02-02 22:35:17 -05:00
|
|
|
},
|
2019-04-12 11:11:09 -05:00
|
|
|
Message: commit.Message(),
|
2019-02-02 22:35:17 -05:00
|
|
|
Tree: &api.CommitMeta{
|
|
|
|
URL: ctx.Repo.Repository.APIURL() + "/trees/" + commit.ID.String(),
|
|
|
|
SHA: commit.ID.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Author: apiAuthor,
|
|
|
|
Committer: apiCommitter,
|
|
|
|
Parents: apiParents,
|
|
|
|
})
|
|
|
|
}
|