diff options
author | ᴜɴᴋɴᴡᴏɴ <u@gogs.io> | 2020-03-08 19:09:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-08 19:09:31 +0800 |
commit | 6437d0180b97a26319b50c2e22927dac7c94fcdd (patch) | |
tree | 3d0d097e7f498e4b970065096e7500876d365a8b /internal/route/api/v1/repo | |
parent | c65b5b9f84dee21dc362311b299694e8e00f6ac6 (diff) |
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
Diffstat (limited to 'internal/route/api/v1/repo')
-rw-r--r-- | internal/route/api/v1/repo/commits.go | 37 | ||||
-rw-r--r-- | internal/route/api/v1/repo/contents.go | 75 | ||||
-rw-r--r-- | internal/route/api/v1/repo/file.go | 15 | ||||
-rw-r--r-- | internal/route/api/v1/repo/tree.go | 21 |
4 files changed, 70 insertions, 78 deletions
diff --git a/internal/route/api/v1/repo/commits.go b/internal/route/api/v1/repo/commits.go index ddcd09b7..b3903053 100644 --- a/internal/route/api/v1/repo/commits.go +++ b/internal/route/api/v1/repo/commits.go @@ -16,6 +16,7 @@ import ( "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) { @@ -25,14 +26,14 @@ func GetSingleCommit(c *context.APIContext) { return } - gitRepo, err := git.OpenRepository(c.Repo.Repository.RepoPath()) + gitRepo, err := git.Open(c.Repo.Repository.RepoPath()) if err != nil { - c.ServerError("OpenRepository", err) + c.ServerError("open repository", err) return } - commit, err := gitRepo.GetCommit(c.Params(":sha")) + commit, err := gitRepo.CatFileCommit(c.Params(":sha")) if err != nil { - c.NotFoundOrServerError("GetCommit", git.IsErrNotExist, err) + c.NotFoundOrServerError("get commit", gitutil.IsErrRevisionNotExist, err) return } @@ -59,8 +60,8 @@ func GetSingleCommit(c *context.APIContext) { } // Retrieve parent(s) of the commit - apiParents := make([]*api.CommitMeta, commit.ParentCount()) - for i := 0; i < commit.ParentCount(); i++ { + 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(), @@ -99,24 +100,24 @@ func GetSingleCommit(c *context.APIContext) { } func GetReferenceSHA(c *context.APIContext) { - gitRepo, err := git.OpenRepository(c.Repo.Repository.RepoPath()) + gitRepo, err := git.Open(c.Repo.Repository.RepoPath()) if err != nil { - c.ServerError("OpenRepository", err) + c.ServerError("open repository", err) return } ref := c.Params("*") - refType := 0 // 0-undetermined, 1-branch, 2-tag - if strings.HasPrefix(ref, git.BRANCH_PREFIX) { - ref = strings.TrimPrefix(ref, git.BRANCH_PREFIX) + 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.TAG_PREFIX) { - ref = strings.TrimPrefix(ref, git.TAG_PREFIX) + } else if strings.HasPrefix(ref, git.RefsTags) { + ref = strings.TrimPrefix(ref, git.RefsTags) refType = 2 } else { - if gitRepo.IsBranchExist(ref) { + if gitRepo.HasBranch(ref) { refType = 1 - } else if gitRepo.IsTagExist(ref) { + } else if gitRepo.HasTag(ref) { refType = 2 } else { c.NotFound() @@ -126,12 +127,12 @@ func GetReferenceSHA(c *context.APIContext) { var sha string if refType == 1 { - sha, err = gitRepo.GetBranchCommitID(ref) + sha, err = gitRepo.BranchCommitID(ref) } else if refType == 2 { - sha, err = gitRepo.GetTagCommitID(ref) + sha, err = gitRepo.TagCommitID(ref) } if err != nil { - c.NotFoundOrServerError("get reference commit ID", git.IsErrNotExist, err) + c.NotFoundOrServerError("get reference commit ID", gitutil.IsErrRevisionNotExist, err) return } c.PlainText(http.StatusOK, []byte(sha)) diff --git a/internal/route/api/v1/repo/contents.go b/internal/route/api/v1/repo/contents.go index 7f7ec18e..40303921 100644 --- a/internal/route/api/v1/repo/contents.go +++ b/internal/route/api/v1/repo/contents.go @@ -7,11 +7,9 @@ package repo import ( "encoding/base64" "fmt" - "io/ioutil" - - "github.com/gogs/git-module" "gogs.io/gogs/internal/context" + "gogs.io/gogs/internal/gitutil" ) type repoContent struct { @@ -38,9 +36,9 @@ type Links struct { } func GetContents(c *context.APIContext) { - treeEntry, err := c.Repo.Commit.GetTreeEntryByPath(c.Repo.TreePath) + treeEntry, err := c.Repo.Commit.TreeEntry(c.Repo.TreePath) if err != nil { - c.NotFoundOrServerError("GetTreeEntryByPath", git.IsErrNotExist, err) + c.NotFoundOrServerError("get tree entry", gitutil.IsErrRevisionNotExist, err) return } username := c.Params(":username") @@ -56,8 +54,8 @@ func GetContents(c *context.APIContext) { // :baseurl/repos/:username/:project/tree/:sha templateHTMLLLink := "%s/repos/%s/%s/tree/%s" - gitURL := fmt.Sprintf(templateGitURLLink, c.BaseURL, username, reponame, treeEntry.ID.String()) - htmlURL := fmt.Sprintf(templateHTMLLLink, c.BaseURL, username, reponame, treeEntry.ID.String()) + gitURL := fmt.Sprintf(templateGitURLLink, c.BaseURL, username, reponame, treeEntry.ID().String()) + htmlURL := fmt.Sprintf(templateHTMLLLink, c.BaseURL, username, reponame, treeEntry.ID().String()) selfURL := fmt.Sprintf(templateSelfLink, c.BaseURL, username, reponame, c.Repo.TreePath) // TODO(unknwon): Make a treeEntryToRepoContent helper. @@ -65,7 +63,7 @@ func GetContents(c *context.APIContext) { Size: treeEntry.Size(), Name: treeEntry.Name(), Path: c.Repo.TreePath, - Sha: treeEntry.ID.String(), + Sha: treeEntry.ID().String(), URL: selfURL, GitURL: gitURL, HTMLURL: htmlURL, @@ -82,65 +80,57 @@ func GetContents(c *context.APIContext) { // 2. SubModule // 3. SymLink // 4. Blob (file) - if treeEntry.IsSubModule() { + if treeEntry.IsCommit() { // TODO(unknwon): submoduleURL is not set as current git-module doesn't handle it properly contents.Type = "submodule" c.JSONSuccess(contents) return - } else if treeEntry.IsLink() { + } else if treeEntry.IsSymlink() { contents.Type = "symlink" - blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath) + blob, err := c.Repo.Commit.Blob(c.Repo.TreePath) if err != nil { c.ServerError("GetBlobByPath", err) return } - b, err := blob.Data() + p, err := blob.Bytes() if err != nil { c.ServerError("Data", err) return } - buf, err := ioutil.ReadAll(b) - if err != nil { - c.ServerError("ReadAll", err) - return - } - contents.Target = string(buf) + contents.Target = string(p) c.JSONSuccess(contents) return - } else if treeEntry.Type == "blob" { - blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath) + } else if treeEntry.IsBlob() { + blob, err := c.Repo.Commit.Blob(c.Repo.TreePath) if err != nil { c.ServerError("GetBlobByPath", err) return } - b, err := blob.Data() + p, err := blob.Bytes() if err != nil { c.ServerError("Data", err) return } - buf, err := ioutil.ReadAll(b) - if err != nil { - c.ServerError("ReadAll", err) - return - } - contents.Content = base64.StdEncoding.EncodeToString(buf) + contents.Content = base64.StdEncoding.EncodeToString(p) contents.Type = "file" c.JSONSuccess(contents) return } + // TODO: treeEntry.IsExec() + // treeEntry is a directory - dirTree, err := c.Repo.GitRepo.GetTree(treeEntry.ID.String()) + dirTree, err := c.Repo.GitRepo.LsTree(treeEntry.ID().String()) if err != nil { - c.NotFoundOrServerError("GetTree", git.IsErrNotExist, err) + c.NotFoundOrServerError("get tree", gitutil.IsErrRevisionNotExist, err) return } - entries, err := dirTree.ListEntries() + entries, err := dirTree.Entries() if err != nil { - c.NotFoundOrServerError("ListEntries", git.IsErrNotExist, err) + c.NotFoundOrServerError("list entries", gitutil.IsErrRevisionNotExist, err) return } @@ -151,33 +141,28 @@ func GetContents(c *context.APIContext) { var results = make([]*repoContent, 0, len(entries)) for _, entry := range entries { - gitURL := fmt.Sprintf(templateGitURLLink, c.BaseURL, username, reponame, entry.ID.String()) - htmlURL := fmt.Sprintf(templateHTMLLLink, c.BaseURL, username, reponame, entry.ID.String()) + gitURL := fmt.Sprintf(templateGitURLLink, c.BaseURL, username, reponame, entry.ID().String()) + htmlURL := fmt.Sprintf(templateHTMLLLink, c.BaseURL, username, reponame, entry.ID().String()) selfURL := fmt.Sprintf(templateSelfLink, c.BaseURL, username, reponame, c.Repo.TreePath) var contentType string - if entry.IsDir() { + if entry.IsTree() { contentType = "dir" - } else if entry.IsSubModule() { + } else if entry.IsCommit() { // TODO(unknwon): submoduleURL is not set as current git-module doesn't handle it properly contentType = "submodule" - } else if entry.IsLink() { + } else if entry.IsSymlink() { contentType = "symlink" - blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath) + blob, err := c.Repo.Commit.Blob(c.Repo.TreePath) if err != nil { c.ServerError("GetBlobByPath", err) return } - b, err := blob.Data() + p, err := blob.Bytes() if err != nil { c.ServerError("Data", err) return } - buf, err := ioutil.ReadAll(b) - if err != nil { - c.ServerError("ReadAll", err) - return - } - contents.Target = string(buf) + contents.Target = string(p) } else { contentType = "file" } @@ -187,7 +172,7 @@ func GetContents(c *context.APIContext) { Size: entry.Size(), Name: entry.Name(), Path: c.Repo.TreePath, - Sha: entry.ID.String(), + Sha: entry.ID().String(), URL: selfURL, GitURL: gitURL, HTMLURL: htmlURL, diff --git a/internal/route/api/v1/repo/file.go b/internal/route/api/v1/repo/file.go index 6b1cf470..414d0285 100644 --- a/internal/route/api/v1/repo/file.go +++ b/internal/route/api/v1/repo/file.go @@ -9,6 +9,7 @@ import ( "gogs.io/gogs/internal/context" "gogs.io/gogs/internal/db" + "gogs.io/gogs/internal/gitutil" "gogs.io/gogs/internal/route/repo" ) @@ -23,9 +24,9 @@ func GetRawFile(c *context.APIContext) { return } - blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath) + blob, err := c.Repo.Commit.Blob(c.Repo.TreePath) if err != nil { - c.NotFoundOrServerError("GetBlobByPath", git.IsErrNotExist, err) + c.NotFoundOrServerError("get blob", gitutil.IsErrRevisionNotExist, err) return } if err = repo.ServeBlob(c.Context, blob); err != nil { @@ -35,9 +36,9 @@ func GetRawFile(c *context.APIContext) { func GetArchive(c *context.APIContext) { repoPath := db.RepoPath(c.Params(":username"), c.Params(":reponame")) - gitRepo, err := git.OpenRepository(repoPath) + gitRepo, err := git.Open(repoPath) if err != nil { - c.ServerError("OpenRepository", err) + c.ServerError("open repository", err) return } c.Repo.GitRepo = gitRepo @@ -46,16 +47,16 @@ func GetArchive(c *context.APIContext) { } func GetEditorconfig(c *context.APIContext) { - ec, err := c.Repo.GetEditorconfig() + ec, err := c.Repo.Editorconfig() if err != nil { - c.NotFoundOrServerError("GetEditorconfig", git.IsErrNotExist, err) + c.NotFoundOrServerError("get .editorconfig", gitutil.IsErrRevisionNotExist, err) return } fileName := c.Params("filename") def, err := ec.GetDefinitionForFilename(fileName) if err != nil { - c.ServerError("GetDefinitionForFilename", err) + c.ServerError("get definition for filename", err) return } if def == nil { diff --git a/internal/route/api/v1/repo/tree.go b/internal/route/api/v1/repo/tree.go index c39f3acd..b3485e7a 100644 --- a/internal/route/api/v1/repo/tree.go +++ b/internal/route/api/v1/repo/tree.go @@ -1,3 +1,7 @@ +// Copyright 2020 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 ( @@ -6,6 +10,7 @@ import ( "github.com/gogs/git-module" "gogs.io/gogs/internal/context" + "gogs.io/gogs/internal/gitutil" ) type repoGitTree struct { @@ -24,14 +29,14 @@ type repoGitTreeEntry struct { } func GetRepoGitTree(c *context.APIContext) { - gitTree, err := c.Repo.GitRepo.GetTree(c.Params(":sha")) + gitTree, err := c.Repo.GitRepo.LsTree(c.Params(":sha")) if err != nil { - c.NotFoundOrServerError("GetRepoGitTree", git.IsErrNotExist, err) + c.NotFoundOrServerError("get tree", gitutil.IsErrRevisionNotExist, err) return } - entries, err := gitTree.ListEntries() + entries, err := gitTree.Entries() if err != nil { - c.ServerError("GetRepoGitTree", err) + c.ServerError("list entries", err) return } @@ -48,7 +53,7 @@ func GetRepoGitTree(c *context.APIContext) { children := make([]*repoGitTreeEntry, 0, len(entries)) for _, entry := range entries { var mode string - switch entry.Type { + switch entry.Type() { case git.ObjectCommit: mode = "160000" case git.ObjectTree: @@ -63,10 +68,10 @@ func GetRepoGitTree(c *context.APIContext) { children = append(children, &repoGitTreeEntry{ Path: entry.Name(), Mode: mode, - Type: string(entry.Type), + Type: string(entry.Type()), Size: entry.Size(), - Sha: entry.ID.String(), - URL: fmt.Sprintf(templateURL+"/%s", entry.ID.String()), + Sha: entry.ID().String(), + URL: fmt.Sprintf(templateURL+"/%s", entry.ID().String()), }) } c.JSONSuccess(&repoGitTree{ |