2014-11-16 20:27:04 -05:00
|
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2018-11-28 06:26:14 -05:00
|
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2014-11-16 20:27:04 -05:00
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2015-12-04 17:16:42 -05:00
|
|
|
|
package repo
|
2014-11-16 21:32:26 -05:00
|
|
|
|
|
|
|
|
|
import (
|
2019-04-17 12:06:35 -04:00
|
|
|
|
"encoding/base64"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
2016-11-10 11:24:48 -05:00
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-03-27 05:33:00 -04:00
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-04-17 12:06:35 -04:00
|
|
|
|
"code.gitea.io/gitea/modules/repofiles"
|
2019-05-11 06:21:34 -04:00
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2016-11-10 11:24:48 -05:00
|
|
|
|
"code.gitea.io/gitea/routers/repo"
|
2014-11-16 21:32:26 -05:00
|
|
|
|
)
|
|
|
|
|
|
2016-11-24 02:04:31 -05:00
|
|
|
|
// GetRawFile get a file by path on a repository
|
2016-03-13 18:49:16 -04:00
|
|
|
|
func GetRawFile(ctx *context.APIContext) {
|
2017-11-13 02:02:25 -05:00
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/raw/{filepath} repository repoGetRawFile
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Get a file 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: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: filepath of the file to get
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// responses:
|
2018-06-01 01:51:49 -04:00
|
|
|
|
// 200:
|
|
|
|
|
// description: success
|
2019-01-17 19:01:04 -05:00
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
2019-03-18 22:29:43 -04:00
|
|
|
|
ctx.NotFound()
|
2017-06-10 22:57:28 -04:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 00:35:03 -04:00
|
|
|
|
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
|
2014-11-16 21:32:26 -05:00
|
|
|
|
if err != nil {
|
2015-12-09 20:46:05 -05:00
|
|
|
|
if git.IsErrNotExist(err) {
|
2019-03-18 22:29:43 -04:00
|
|
|
|
ctx.NotFound()
|
2014-11-16 21:32:26 -05:00
|
|
|
|
} else {
|
2019-04-17 12:06:35 -04:00
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err)
|
2014-11-16 21:32:26 -05:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-03-13 18:49:16 -04:00
|
|
|
|
if err = repo.ServeBlob(ctx.Context, blob); err != nil {
|
2019-04-17 12:06:35 -04:00
|
|
|
|
ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
|
2014-11-16 21:32:26 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-09-02 09:54:35 -04:00
|
|
|
|
|
2016-11-24 02:04:31 -05:00
|
|
|
|
// GetArchive get archive of a repository
|
2016-03-13 18:49:16 -04:00
|
|
|
|
func GetArchive(ctx *context.APIContext) {
|
2018-06-12 10:59:22 -04:00
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/archive/{archive} repository repoGetArchive
|
2017-11-13 02:02:25 -05:00
|
|
|
|
// ---
|
|
|
|
|
// summary: Get an archive of 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: archive
|
|
|
|
|
// in: path
|
|
|
|
|
// description: archive to download, consisting of a git reference and archive
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// responses:
|
2018-06-01 01:51:49 -04:00
|
|
|
|
// 200:
|
|
|
|
|
// description: success
|
2015-09-02 09:54:35 -04:00
|
|
|
|
repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
|
|
|
|
|
gitRepo, err := git.OpenRepository(repoPath)
|
|
|
|
|
if err != nil {
|
2019-04-17 12:06:35 -04:00
|
|
|
|
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
|
2015-09-02 09:54:35 -04:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Repo.GitRepo = gitRepo
|
|
|
|
|
|
2016-03-13 18:49:16 -04:00
|
|
|
|
repo.Download(ctx.Context)
|
2015-09-02 09:54:35 -04:00
|
|
|
|
}
|
2016-08-30 19:18:40 -04:00
|
|
|
|
|
2016-11-24 02:04:31 -05:00
|
|
|
|
// GetEditorconfig get editor config of a repository
|
2016-08-30 19:18:40 -04:00
|
|
|
|
func GetEditorconfig(ctx *context.APIContext) {
|
2017-11-13 02:02:25 -05:00
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/editorconfig/{filepath} repository repoGetEditorConfig
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Get the EditorConfig definitions of a file in 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: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: filepath of file to get
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// responses:
|
2018-06-01 01:51:49 -04:00
|
|
|
|
// 200:
|
|
|
|
|
// description: success
|
2016-08-30 19:18:40 -04:00
|
|
|
|
ec, err := ctx.Repo.GetEditorconfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
if git.IsErrNotExist(err) {
|
2019-03-18 22:29:43 -04:00
|
|
|
|
ctx.NotFound(err)
|
2016-08-30 19:18:40 -04:00
|
|
|
|
} else {
|
2019-04-17 12:06:35 -04:00
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetEditorconfig", err)
|
2016-08-30 19:18:40 -04:00
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileName := ctx.Params("filename")
|
|
|
|
|
def := ec.GetDefinitionForFilename(fileName)
|
|
|
|
|
if def == nil {
|
2019-03-18 22:29:43 -04:00
|
|
|
|
ctx.NotFound(err)
|
2016-08-30 19:18:40 -04:00
|
|
|
|
return
|
|
|
|
|
}
|
2019-04-17 12:06:35 -04:00
|
|
|
|
ctx.JSON(http.StatusOK, def)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CanWriteFiles returns true if repository is editable and user has proper access level.
|
|
|
|
|
func CanWriteFiles(r *context.Repository) bool {
|
|
|
|
|
return r.Permission.CanWrite(models.UnitTypeCode) && !r.Repository.IsMirror && !r.Repository.IsArchived
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CanReadFiles returns true if repository is readable and user has proper access level.
|
|
|
|
|
func CanReadFiles(r *context.Repository) bool {
|
|
|
|
|
return r.Permission.CanRead(models.UnitTypeCode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CreateFile handles API call for creating a file
|
|
|
|
|
func CreateFile(ctx *context.APIContext, apiOpts api.CreateFileOptions) {
|
|
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/contents/{filepath} repository repoCreateFile
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Create a file in a repository
|
|
|
|
|
// consumes:
|
|
|
|
|
// - application/json
|
|
|
|
|
// 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: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: path of the file to create
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: body
|
|
|
|
|
// in: body
|
2019-05-30 13:57:55 -04:00
|
|
|
|
// required: true
|
2019-04-17 12:06:35 -04:00
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/CreateFileOptions"
|
|
|
|
|
// responses:
|
|
|
|
|
// "201":
|
|
|
|
|
// "$ref": "#/responses/FileResponse"
|
|
|
|
|
|
|
|
|
|
opts := &repofiles.UpdateRepoFileOptions{
|
|
|
|
|
Content: apiOpts.Content,
|
|
|
|
|
IsNewFile: true,
|
|
|
|
|
Message: apiOpts.Message,
|
|
|
|
|
TreePath: ctx.Params("*"),
|
|
|
|
|
OldBranch: apiOpts.BranchName,
|
|
|
|
|
NewBranch: apiOpts.NewBranchName,
|
|
|
|
|
Committer: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Committer.Name,
|
|
|
|
|
Email: apiOpts.Committer.Email,
|
|
|
|
|
},
|
|
|
|
|
Author: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Author.Name,
|
|
|
|
|
Email: apiOpts.Author.Email,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "CreateFile", err)
|
|
|
|
|
} else {
|
|
|
|
|
ctx.JSON(http.StatusCreated, fileResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateFile handles API call for updating a file
|
|
|
|
|
func UpdateFile(ctx *context.APIContext, apiOpts api.UpdateFileOptions) {
|
|
|
|
|
// swagger:operation PUT /repos/{owner}/{repo}/contents/{filepath} repository repoUpdateFile
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Update a file in a repository
|
|
|
|
|
// consumes:
|
|
|
|
|
// - application/json
|
|
|
|
|
// 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: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: path of the file to update
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: body
|
|
|
|
|
// in: body
|
2019-05-30 13:57:55 -04:00
|
|
|
|
// required: true
|
2019-04-17 12:06:35 -04:00
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/UpdateFileOptions"
|
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
|
|
|
|
// "$ref": "#/responses/FileResponse"
|
|
|
|
|
|
|
|
|
|
opts := &repofiles.UpdateRepoFileOptions{
|
|
|
|
|
Content: apiOpts.Content,
|
|
|
|
|
SHA: apiOpts.SHA,
|
|
|
|
|
IsNewFile: false,
|
|
|
|
|
Message: apiOpts.Message,
|
|
|
|
|
FromTreePath: apiOpts.FromPath,
|
|
|
|
|
TreePath: ctx.Params("*"),
|
|
|
|
|
OldBranch: apiOpts.BranchName,
|
|
|
|
|
NewBranch: apiOpts.NewBranchName,
|
|
|
|
|
Committer: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Committer.Name,
|
|
|
|
|
Email: apiOpts.Committer.Email,
|
|
|
|
|
},
|
|
|
|
|
Author: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Author.Name,
|
|
|
|
|
Email: apiOpts.Author.Email,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateFile", err)
|
|
|
|
|
} else {
|
|
|
|
|
ctx.JSON(http.StatusOK, fileResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called from both CreateFile or UpdateFile to handle both
|
|
|
|
|
func createOrUpdateFile(ctx *context.APIContext, opts *repofiles.UpdateRepoFileOptions) (*api.FileResponse, error) {
|
|
|
|
|
if !CanWriteFiles(ctx.Repo) {
|
|
|
|
|
return nil, models.ErrUserDoesNotHaveAccessToRepo{
|
|
|
|
|
UserID: ctx.User.ID,
|
|
|
|
|
RepoName: ctx.Repo.Repository.LowerName,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
content, err := base64.StdEncoding.DecodeString(opts.Content)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
opts.Content = string(content)
|
|
|
|
|
|
|
|
|
|
return repofiles.CreateOrUpdateRepoFile(ctx.Repo.Repository, ctx.User, opts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DeleteFile Delete a fle in a repository
|
|
|
|
|
func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
|
|
|
|
|
// swagger:operation DELETE /repos/{owner}/{repo}/contents/{filepath} repository repoDeleteFile
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Delete a file in a repository
|
|
|
|
|
// consumes:
|
|
|
|
|
// - application/json
|
|
|
|
|
// 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: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: path of the file to delete
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: body
|
|
|
|
|
// in: body
|
2019-05-30 13:57:55 -04:00
|
|
|
|
// required: true
|
2019-04-17 12:06:35 -04:00
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/DeleteFileOptions"
|
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
|
|
|
|
// "$ref": "#/responses/FileDeleteResponse"
|
|
|
|
|
if !CanWriteFiles(ctx.Repo) {
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{
|
|
|
|
|
UserID: ctx.User.ID,
|
|
|
|
|
RepoName: ctx.Repo.Repository.LowerName,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts := &repofiles.DeleteRepoFileOptions{
|
|
|
|
|
Message: apiOpts.Message,
|
|
|
|
|
OldBranch: apiOpts.BranchName,
|
|
|
|
|
NewBranch: apiOpts.NewBranchName,
|
|
|
|
|
SHA: apiOpts.SHA,
|
|
|
|
|
TreePath: ctx.Params("*"),
|
|
|
|
|
Committer: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Committer.Name,
|
|
|
|
|
Email: apiOpts.Committer.Email,
|
|
|
|
|
},
|
|
|
|
|
Author: &repofiles.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Author.Name,
|
|
|
|
|
Email: apiOpts.Author.Email,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if fileResponse, err := repofiles.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil {
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteFile", err)
|
|
|
|
|
} else {
|
|
|
|
|
ctx.JSON(http.StatusOK, fileResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetFileContents Get the contents of a fle in a repository
|
|
|
|
|
func GetFileContents(ctx *context.APIContext) {
|
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetFileContents
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Gets the contents of a file or directory in 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: filepath
|
|
|
|
|
// in: path
|
|
|
|
|
// description: path of the file to delete
|
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: ref
|
|
|
|
|
// in: query
|
|
|
|
|
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
|
|
|
|
|
// required: false
|
|
|
|
|
// type: string
|
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
|
|
|
|
// "$ref": "#/responses/FileContentResponse"
|
|
|
|
|
|
|
|
|
|
if !CanReadFiles(ctx.Repo) {
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetFileContents", models.ErrUserDoesNotHaveAccessToRepo{
|
|
|
|
|
UserID: ctx.User.ID,
|
|
|
|
|
RepoName: ctx.Repo.Repository.LowerName,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
treePath := ctx.Params("*")
|
|
|
|
|
ref := ctx.QueryTrim("ref")
|
|
|
|
|
|
|
|
|
|
if fileContents, err := repofiles.GetFileContents(ctx.Repo.Repository, treePath, ref); err != nil {
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetFileContents", err)
|
|
|
|
|
} else {
|
|
|
|
|
ctx.JSON(http.StatusOK, fileContents)
|
|
|
|
|
}
|
2016-08-30 19:18:40 -04:00
|
|
|
|
}
|