2014-11-17 06:57:04 +05:30
|
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2018-11-28 16:56:14 +05:30
|
|
|
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
|
// SPDX-License-Identifier: MIT
|
2014-11-17 06:57:04 +05:30
|
|
|
|
|
2015-12-05 03:46:42 +05:30
|
|
|
|
package repo
|
2014-11-17 08:02:26 +05:30
|
|
|
|
|
|
|
|
|
import (
|
2022-06-04 18:47:53 +05:30
|
|
|
|
"bytes"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
"encoding/base64"
|
2022-08-29 15:15:20 +05:30
|
|
|
|
"errors"
|
2020-06-01 02:29:34 +05:30
|
|
|
|
"fmt"
|
2022-06-04 18:47:53 +05:30
|
|
|
|
"io"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
"net/http"
|
2022-05-09 21:24:51 +05:30
|
|
|
|
"path"
|
2023-05-29 15:11:35 +05:30
|
|
|
|
"strings"
|
2019-12-24 08:03:52 +05:30
|
|
|
|
"time"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
2016-11-10 21:54:48 +05:30
|
|
|
|
"code.gitea.io/gitea/models"
|
2022-06-12 21:21:54 +05:30
|
|
|
|
git_model "code.gitea.io/gitea/models/git"
|
2021-12-10 06:57:50 +05:30
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 01:27:58 +05:30
|
|
|
|
"code.gitea.io/gitea/models/unit"
|
2016-11-10 21:54:48 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-03-27 15:03:00 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/git"
|
2022-06-04 18:47:53 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/httpcache"
|
|
|
|
|
"code.gitea.io/gitea/modules/lfs"
|
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2022-05-09 21:24:51 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-06-04 18:47:53 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/storage"
|
2019-05-11 15:51:34 +05:30
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2021-01-26 21:06:53 +05:30
|
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-06-09 05:03:54 +05:30
|
|
|
|
"code.gitea.io/gitea/routers/common"
|
2022-08-29 15:15:20 +05:30
|
|
|
|
archiver_service "code.gitea.io/gitea/services/repository/archiver"
|
2021-11-24 13:26:24 +05:30
|
|
|
|
files_service "code.gitea.io/gitea/services/repository/files"
|
2014-11-17 08:02:26 +05:30
|
|
|
|
)
|
|
|
|
|
|
2022-07-22 00:48:41 +05:30
|
|
|
|
const giteaObjectTypeHeader = "X-Gitea-Object-Type"
|
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
|
// GetRawFile get a file by path on a repository
|
2016-03-14 04:19:16 +05:30
|
|
|
|
func GetRawFile(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// 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
|
2021-02-09 05:45:47 +05:30
|
|
|
|
// - name: ref
|
|
|
|
|
// in: query
|
|
|
|
|
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
|
|
|
|
|
// type: string
|
|
|
|
|
// required: false
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// responses:
|
2018-06-01 11:21:49 +05:30
|
|
|
|
// 200:
|
2022-04-28 20:27:56 +05:30
|
|
|
|
// description: Returns raw file content.
|
2019-12-20 22:37:12 +05:30
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
2019-01-18 05:31:04 +05:30
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
2019-03-19 07:59:43 +05:30
|
|
|
|
ctx.NotFound()
|
2017-06-11 08:27:28 +05:30
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 00:48:41 +05:30
|
|
|
|
blob, entry, lastModified := getBlobForEntry(ctx)
|
2022-05-09 21:24:51 +05:30
|
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 00:48:41 +05:30
|
|
|
|
ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry)))
|
|
|
|
|
|
2023-05-21 07:20:53 +05:30
|
|
|
|
if err := common.ServeBlob(ctx.Base, ctx.Repo.TreePath, blob, lastModified); err != nil {
|
2022-05-09 21:24:51 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-04 18:47:53 +05:30
|
|
|
|
// GetRawFileOrLFS get a file by repo's path, redirecting to LFS if necessary.
|
|
|
|
|
func GetRawFileOrLFS(ctx *context.APIContext) {
|
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/media/{filepath} repository repoGetRawFileOrLFS
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Get a file or it's LFS object from a repository
|
|
|
|
|
// 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
|
|
|
|
|
// - name: ref
|
|
|
|
|
// in: query
|
|
|
|
|
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
|
|
|
|
|
// type: string
|
|
|
|
|
// required: false
|
|
|
|
|
// responses:
|
|
|
|
|
// 200:
|
|
|
|
|
// description: Returns raw file content.
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
|
|
|
ctx.NotFound()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 00:48:41 +05:30
|
|
|
|
blob, entry, lastModified := getBlobForEntry(ctx)
|
2022-06-04 18:47:53 +05:30
|
|
|
|
if ctx.Written() {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 00:48:41 +05:30
|
|
|
|
ctx.RespHeader().Set(giteaObjectTypeHeader, string(files_service.GetObjectTypeFromTreeEntry(entry)))
|
|
|
|
|
|
2022-06-04 18:47:53 +05:30
|
|
|
|
// LFS Pointer files are at most 1024 bytes - so any blob greater than 1024 bytes cannot be an LFS file
|
|
|
|
|
if blob.Size() > 1024 {
|
|
|
|
|
// First handle caching for the blob
|
|
|
|
|
if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OK not cached - serve!
|
2023-05-21 07:20:53 +05:30
|
|
|
|
if err := common.ServeBlob(ctx.Base, ctx.Repo.TreePath, blob, lastModified); err != nil {
|
2022-06-04 18:47:53 +05:30
|
|
|
|
ctx.ServerError("ServeBlob", err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OK, now the blob is known to have at most 1024 bytes we can simply read this in in one go (This saves reading it twice)
|
|
|
|
|
dataRc, err := blob.DataAsync()
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("DataAsync", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 13:04:36 +05:30
|
|
|
|
// FIXME: code from #19689, what if the file is large ... OOM ...
|
2022-06-04 18:47:53 +05:30
|
|
|
|
buf, err := io.ReadAll(dataRc)
|
|
|
|
|
if err != nil {
|
|
|
|
|
_ = dataRc.Close()
|
|
|
|
|
ctx.ServerError("DataAsync", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := dataRc.Close(); err != nil {
|
2023-05-21 07:20:53 +05:30
|
|
|
|
log.Error("Error whilst closing blob %s reader in %-v. Error: %v", blob.ID, ctx.Repo.Repository, err)
|
2022-06-04 18:47:53 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if the blob represents a pointer
|
|
|
|
|
pointer, _ := lfs.ReadPointer(bytes.NewReader(buf))
|
|
|
|
|
|
2023-05-09 13:04:36 +05:30
|
|
|
|
// if it's not a pointer, just serve the data directly
|
2022-06-04 18:47:53 +05:30
|
|
|
|
if !pointer.IsValid() {
|
|
|
|
|
// First handle caching for the blob
|
|
|
|
|
if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OK not cached - serve!
|
2023-05-21 07:20:53 +05:30
|
|
|
|
common.ServeContentByReader(ctx.Base, ctx.Repo.TreePath, blob.Size(), bytes.NewReader(buf))
|
2022-06-04 18:47:53 +05:30
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 13:04:36 +05:30
|
|
|
|
// Now check if there is a MetaObject for this pointer
|
2023-01-09 09:20:54 +05:30
|
|
|
|
meta, err := git_model.GetLFSMetaObjectByOid(ctx, ctx.Repo.Repository.ID, pointer.Oid)
|
2022-06-04 18:47:53 +05:30
|
|
|
|
|
2023-05-09 13:04:36 +05:30
|
|
|
|
// If there isn't one, just serve the data directly
|
2022-06-12 21:21:54 +05:30
|
|
|
|
if err == git_model.ErrLFSObjectNotExist {
|
2022-06-04 18:47:53 +05:30
|
|
|
|
// Handle caching for the blob SHA (not the LFS object OID)
|
|
|
|
|
if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-21 07:20:53 +05:30
|
|
|
|
common.ServeContentByReader(ctx.Base, ctx.Repo.TreePath, blob.Size(), bytes.NewReader(buf))
|
2022-06-04 18:47:53 +05:30
|
|
|
|
return
|
|
|
|
|
} else if err != nil {
|
|
|
|
|
ctx.ServerError("GetLFSMetaObjectByOid", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle caching for the LFS object OID
|
|
|
|
|
if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+pointer.Oid+`"`) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-14 09:12:38 +05:30
|
|
|
|
if setting.LFS.Storage.MinioConfig.ServeDirect {
|
2022-06-04 18:47:53 +05:30
|
|
|
|
// If we have a signed url (S3, object storage), redirect to this directly.
|
|
|
|
|
u, err := storage.LFS.URL(pointer.RelativePath(), blob.Name())
|
|
|
|
|
if u != nil && err == nil {
|
|
|
|
|
ctx.Redirect(u.String())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lfsDataRc, err := lfs.ReadMetaObject(meta.Pointer)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("ReadMetaObject", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer lfsDataRc.Close()
|
|
|
|
|
|
2023-05-21 07:20:53 +05:30
|
|
|
|
common.ServeContentByReadSeeker(ctx.Base, ctx.Repo.TreePath, lastModified, lfsDataRc)
|
2022-06-04 18:47:53 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-07-07 11:01:56 +05:30
|
|
|
|
func getBlobForEntry(ctx *context.APIContext) (blob *git.Blob, entry *git.TreeEntry, lastModified *time.Time) {
|
2022-05-09 21:24:51 +05:30
|
|
|
|
entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx.Repo.TreePath)
|
2014-11-17 08:02:26 +05:30
|
|
|
|
if err != nil {
|
2015-12-10 07:16:05 +05:30
|
|
|
|
if git.IsErrNotExist(err) {
|
2019-03-19 07:59:43 +05:30
|
|
|
|
ctx.NotFound()
|
2014-11-17 08:02:26 +05:30
|
|
|
|
} else {
|
2022-05-09 21:24:51 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetTreeEntryByPath", err)
|
2014-11-17 08:02:26 +05:30
|
|
|
|
}
|
2023-07-07 11:01:56 +05:30
|
|
|
|
return nil, nil, nil
|
2014-11-17 08:02:26 +05:30
|
|
|
|
}
|
2022-05-09 21:24:51 +05:30
|
|
|
|
|
|
|
|
|
if entry.IsDir() || entry.IsSubModule() {
|
|
|
|
|
ctx.NotFound("getBlobForEntry", nil)
|
2023-07-07 11:01:56 +05:30
|
|
|
|
return nil, nil, nil
|
2014-11-17 08:02:26 +05:30
|
|
|
|
}
|
2022-05-09 21:24:51 +05:30
|
|
|
|
|
2022-07-25 21:09:42 +05:30
|
|
|
|
info, _, err := git.Entries([]*git.TreeEntry{entry}).GetCommitsInfo(ctx, ctx.Repo.Commit, path.Dir("/" + ctx.Repo.TreePath)[1:])
|
2022-05-09 21:24:51 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetCommitsInfo", err)
|
2023-07-07 11:01:56 +05:30
|
|
|
|
return nil, nil, nil
|
2022-05-09 21:24:51 +05:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(info) == 1 {
|
|
|
|
|
// Not Modified
|
2023-07-07 11:01:56 +05:30
|
|
|
|
lastModified = &info[0].Commit.Committer.When
|
2022-05-09 21:24:51 +05:30
|
|
|
|
}
|
|
|
|
|
blob = entry.Blob()
|
|
|
|
|
|
2022-07-22 00:48:41 +05:30
|
|
|
|
return blob, entry, lastModified
|
2014-11-17 08:02:26 +05:30
|
|
|
|
}
|
2015-09-02 19:24:35 +05:30
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
|
// GetArchive get archive of a repository
|
2016-03-14 04:19:16 +05:30
|
|
|
|
func GetArchive(ctx *context.APIContext) {
|
2018-06-12 20:29:22 +05:30
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/archive/{archive} repository repoGetArchive
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// ---
|
|
|
|
|
// 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
|
2020-09-06 21:53:47 +05:30
|
|
|
|
// description: the git reference for download with attached archive format (e.g. master.zip)
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// responses:
|
2018-06-01 11:21:49 +05:30
|
|
|
|
// 200:
|
|
|
|
|
// description: success
|
2019-12-20 22:37:12 +05:30
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
|
repoPath := repo_model.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
|
2021-09-18 06:24:15 +05:30
|
|
|
|
if ctx.Repo.GitRepo == nil {
|
2022-03-30 00:43:41 +05:30
|
|
|
|
gitRepo, err := git.OpenRepository(ctx, repoPath)
|
2021-09-18 06:24:15 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
ctx.Repo.GitRepo = gitRepo
|
|
|
|
|
defer gitRepo.Close()
|
2015-09-02 19:24:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2022-08-29 15:15:20 +05:30
|
|
|
|
archiveDownload(ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func archiveDownload(ctx *context.APIContext) {
|
|
|
|
|
uri := ctx.Params("*")
|
|
|
|
|
aReq, err := archiver_service.NewRequest(ctx.Repo.Repository.ID, ctx.Repo.GitRepo, uri)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if errors.Is(err, archiver_service.ErrUnknownArchiveFormat{}) {
|
|
|
|
|
ctx.Error(http.StatusBadRequest, "unknown archive format", err)
|
|
|
|
|
} else if errors.Is(err, archiver_service.RepoRefNotFoundError{}) {
|
|
|
|
|
ctx.Error(http.StatusNotFound, "unrecognized reference", err)
|
|
|
|
|
} else {
|
|
|
|
|
ctx.ServerError("archiver_service.NewRequest", err)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
archiver, err := aReq.Await(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("archiver.Await", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
download(ctx, aReq.GetArchiveName(), archiver)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func download(ctx *context.APIContext, archiveName string, archiver *repo_model.RepoArchiver) {
|
|
|
|
|
downloadName := ctx.Repo.Repository.Name + "-" + archiveName
|
|
|
|
|
|
|
|
|
|
rPath := archiver.RelativePath()
|
2023-06-14 09:12:38 +05:30
|
|
|
|
if setting.RepoArchive.Storage.MinioConfig.ServeDirect {
|
2022-08-29 15:15:20 +05:30
|
|
|
|
// If we have a signed url (S3, object storage), redirect to this directly.
|
|
|
|
|
u, err := storage.RepoArchives.URL(rPath, downloadName)
|
|
|
|
|
if u != nil && err == nil {
|
|
|
|
|
ctx.Redirect(u.String())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we have matched and access to release or issue
|
|
|
|
|
fr, err := storage.RepoArchives.Open(rPath)
|
|
|
|
|
if err != nil {
|
|
|
|
|
ctx.ServerError("Open", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer fr.Close()
|
2022-11-24 19:55:13 +05:30
|
|
|
|
|
|
|
|
|
ctx.ServeContent(fr, &context.ServeHeaderOptions{
|
|
|
|
|
Filename: downloadName,
|
|
|
|
|
LastModified: archiver.CreatedUnix.AsLocalTime(),
|
|
|
|
|
})
|
2015-09-02 19:24:35 +05:30
|
|
|
|
}
|
2016-08-31 04:48:40 +05:30
|
|
|
|
|
2016-11-24 12:34:31 +05:30
|
|
|
|
// GetEditorconfig get editor config of a repository
|
2016-08-31 04:48:40 +05:30
|
|
|
|
func GetEditorconfig(ctx *context.APIContext) {
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// 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
|
2022-04-21 20:47:57 +05:30
|
|
|
|
// - name: ref
|
|
|
|
|
// in: query
|
|
|
|
|
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
|
|
|
|
|
// type: string
|
|
|
|
|
// required: false
|
2017-11-13 12:32:25 +05:30
|
|
|
|
// responses:
|
2018-06-01 11:21:49 +05:30
|
|
|
|
// 200:
|
|
|
|
|
// description: success
|
2019-12-20 22:37:12 +05:30
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
|
2023-04-07 01:31:20 +05:30
|
|
|
|
ec, _, err := ctx.Repo.GetEditorconfig(ctx.Repo.Commit)
|
2016-08-31 04:48:40 +05:30
|
|
|
|
if err != nil {
|
|
|
|
|
if git.IsErrNotExist(err) {
|
2019-03-19 07:59:43 +05:30
|
|
|
|
ctx.NotFound(err)
|
2016-08-31 04:48:40 +05:30
|
|
|
|
} else {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetEditorconfig", err)
|
2016-08-31 04:48:40 +05:30
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fileName := ctx.Params("filename")
|
2019-10-17 05:45:02 +05:30
|
|
|
|
def, err := ec.GetDefinitionForFilename(fileName)
|
2016-08-31 04:48:40 +05:30
|
|
|
|
if def == nil {
|
2019-03-19 07:59:43 +05:30
|
|
|
|
ctx.NotFound(err)
|
2016-08-31 04:48:40 +05:30
|
|
|
|
return
|
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.JSON(http.StatusOK, def)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 21:50:22 +05:30
|
|
|
|
// canWriteFiles returns true if repository is editable and user has proper access level.
|
2022-04-28 21:15:33 +05:30
|
|
|
|
func canWriteFiles(ctx *context.APIContext, branch string) bool {
|
2022-05-11 15:39:36 +05:30
|
|
|
|
return ctx.Repo.CanWriteToBranch(ctx.Doer, branch) &&
|
2022-04-28 21:15:33 +05:30
|
|
|
|
!ctx.Repo.Repository.IsMirror &&
|
|
|
|
|
!ctx.Repo.Repository.IsArchived
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2020-04-24 21:50:22 +05:30
|
|
|
|
// canReadFiles returns true if repository is readable and user has proper access level.
|
|
|
|
|
func canReadFiles(r *context.Repository) bool {
|
2021-11-10 01:27:58 +05:30
|
|
|
|
return r.Permission.CanRead(unit.TypeCode)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 21:19:58 +05:30
|
|
|
|
// ChangeFiles handles API call for modifying multiple files
|
2023-05-29 15:11:35 +05:30
|
|
|
|
func ChangeFiles(ctx *context.APIContext) {
|
|
|
|
|
// swagger:operation POST /repos/{owner}/{repo}/contents repository repoChangeFiles
|
|
|
|
|
// ---
|
2023-06-07 21:19:58 +05:30
|
|
|
|
// summary: Modify multiple files in a repository
|
2023-05-29 15:11:35 +05:30
|
|
|
|
// 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: body
|
|
|
|
|
// in: body
|
|
|
|
|
// required: true
|
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/ChangeFilesOptions"
|
|
|
|
|
// responses:
|
|
|
|
|
// "201":
|
|
|
|
|
// "$ref": "#/responses/FilesResponse"
|
|
|
|
|
// "403":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
// "422":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
|
|
|
|
|
apiOpts := web.GetForm(ctx).(*api.ChangeFilesOptions)
|
|
|
|
|
|
|
|
|
|
if apiOpts.BranchName == "" {
|
|
|
|
|
apiOpts.BranchName = ctx.Repo.Repository.DefaultBranch
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
files := []*files_service.ChangeRepoFile{}
|
|
|
|
|
for _, file := range apiOpts.Files {
|
|
|
|
|
changeRepoFile := &files_service.ChangeRepoFile{
|
|
|
|
|
Operation: file.Operation,
|
|
|
|
|
TreePath: file.Path,
|
|
|
|
|
FromTreePath: file.FromPath,
|
|
|
|
|
Content: file.Content,
|
|
|
|
|
SHA: file.SHA,
|
|
|
|
|
}
|
|
|
|
|
files = append(files, changeRepoFile)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts := &files_service.ChangeRepoFilesOptions{
|
|
|
|
|
Files: files,
|
|
|
|
|
Message: apiOpts.Message,
|
|
|
|
|
OldBranch: apiOpts.BranchName,
|
|
|
|
|
NewBranch: apiOpts.NewBranchName,
|
|
|
|
|
Committer: &files_service.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Committer.Name,
|
|
|
|
|
Email: apiOpts.Committer.Email,
|
|
|
|
|
},
|
|
|
|
|
Author: &files_service.IdentityOptions{
|
|
|
|
|
Name: apiOpts.Author.Name,
|
|
|
|
|
Email: apiOpts.Author.Email,
|
|
|
|
|
},
|
|
|
|
|
Dates: &files_service.CommitDateOptions{
|
|
|
|
|
Author: apiOpts.Dates.Author,
|
|
|
|
|
Committer: apiOpts.Dates.Committer,
|
|
|
|
|
},
|
|
|
|
|
Signoff: apiOpts.Signoff,
|
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Author.IsZero() {
|
|
|
|
|
opts.Dates.Author = time.Now()
|
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Committer.IsZero() {
|
|
|
|
|
opts.Dates.Committer = time.Now()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if opts.Message == "" {
|
|
|
|
|
opts.Message = changeFilesCommitMessage(ctx, files)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if filesResponse, err := createOrUpdateFiles(ctx, opts); err != nil {
|
|
|
|
|
handleCreateOrUpdateFileError(ctx, err)
|
|
|
|
|
} else {
|
|
|
|
|
ctx.JSON(http.StatusCreated, filesResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// CreateFile handles API call for creating a file
|
2021-01-26 21:06:53 +05:30
|
|
|
|
func CreateFile(ctx *context.APIContext) {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// 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 23:27:55 +05:30
|
|
|
|
// required: true
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/CreateFileOptions"
|
|
|
|
|
// responses:
|
|
|
|
|
// "201":
|
|
|
|
|
// "$ref": "#/responses/FileResponse"
|
2020-06-01 02:29:34 +05:30
|
|
|
|
// "403":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
// "422":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
|
apiOpts := web.GetForm(ctx).(*api.CreateFileOptions)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
2020-04-20 22:17:05 +05:30
|
|
|
|
if apiOpts.BranchName == "" {
|
|
|
|
|
apiOpts.BranchName = ctx.Repo.Repository.DefaultBranch
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
opts := &files_service.ChangeRepoFilesOptions{
|
|
|
|
|
Files: []*files_service.ChangeRepoFile{
|
|
|
|
|
{
|
|
|
|
|
Operation: "create",
|
|
|
|
|
TreePath: ctx.Params("*"),
|
|
|
|
|
Content: apiOpts.Content,
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Message: apiOpts.Message,
|
|
|
|
|
OldBranch: apiOpts.BranchName,
|
|
|
|
|
NewBranch: apiOpts.NewBranchName,
|
2021-11-24 13:26:24 +05:30
|
|
|
|
Committer: &files_service.IdentityOptions{
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Name: apiOpts.Committer.Name,
|
|
|
|
|
Email: apiOpts.Committer.Email,
|
|
|
|
|
},
|
2021-11-24 13:26:24 +05:30
|
|
|
|
Author: &files_service.IdentityOptions{
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Name: apiOpts.Author.Name,
|
|
|
|
|
Email: apiOpts.Author.Email,
|
|
|
|
|
},
|
2021-11-24 13:26:24 +05:30
|
|
|
|
Dates: &files_service.CommitDateOptions{
|
2019-12-24 08:03:52 +05:30
|
|
|
|
Author: apiOpts.Dates.Author,
|
|
|
|
|
Committer: apiOpts.Dates.Committer,
|
|
|
|
|
},
|
2021-01-29 14:27:45 +05:30
|
|
|
|
Signoff: apiOpts.Signoff,
|
2019-12-24 08:03:52 +05:30
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Author.IsZero() {
|
|
|
|
|
opts.Dates.Author = time.Now()
|
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Committer.IsZero() {
|
|
|
|
|
opts.Dates.Committer = time.Now()
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2019-06-29 20:49:24 +05:30
|
|
|
|
|
|
|
|
|
if opts.Message == "" {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
opts.Message = changeFilesCommitMessage(ctx, opts.Files)
|
2019-06-29 20:49:24 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
if filesResponse, err := createOrUpdateFiles(ctx, opts); err != nil {
|
2020-06-01 02:29:34 +05:30
|
|
|
|
handleCreateOrUpdateFileError(ctx, err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
} else {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
fileResponse := files_service.GetFileResponseFromFilesResponse(filesResponse, 0)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.JSON(http.StatusCreated, fileResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateFile handles API call for updating a file
|
2021-01-26 21:06:53 +05:30
|
|
|
|
func UpdateFile(ctx *context.APIContext) {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// 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 23:27:55 +05:30
|
|
|
|
// required: true
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/UpdateFileOptions"
|
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
|
|
|
|
// "$ref": "#/responses/FileResponse"
|
2020-06-01 02:29:34 +05:30
|
|
|
|
// "403":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
|
|
|
|
// "422":
|
|
|
|
|
// "$ref": "#/responses/error"
|
2021-01-26 21:06:53 +05:30
|
|
|
|
apiOpts := web.GetForm(ctx).(*api.UpdateFileOptions)
|
2020-06-01 02:29:34 +05:30
|
|
|
|
if ctx.Repo.Repository.IsEmpty {
|
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "RepoIsEmpty", fmt.Errorf("repo is empty"))
|
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
2020-04-20 22:17:05 +05:30
|
|
|
|
if apiOpts.BranchName == "" {
|
|
|
|
|
apiOpts.BranchName = ctx.Repo.Repository.DefaultBranch
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
opts := &files_service.ChangeRepoFilesOptions{
|
|
|
|
|
Files: []*files_service.ChangeRepoFile{
|
|
|
|
|
{
|
|
|
|
|
Operation: "update",
|
|
|
|
|
Content: apiOpts.Content,
|
|
|
|
|
SHA: apiOpts.SHA,
|
|
|
|
|
FromTreePath: apiOpts.FromPath,
|
|
|
|
|
TreePath: ctx.Params("*"),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
Message: apiOpts.Message,
|
|
|
|
|
OldBranch: apiOpts.BranchName,
|
|
|
|
|
NewBranch: apiOpts.NewBranchName,
|
2021-11-24 13:26:24 +05:30
|
|
|
|
Committer: &files_service.IdentityOptions{
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Name: apiOpts.Committer.Name,
|
|
|
|
|
Email: apiOpts.Committer.Email,
|
|
|
|
|
},
|
2021-11-24 13:26:24 +05:30
|
|
|
|
Author: &files_service.IdentityOptions{
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Name: apiOpts.Author.Name,
|
|
|
|
|
Email: apiOpts.Author.Email,
|
|
|
|
|
},
|
2021-11-24 13:26:24 +05:30
|
|
|
|
Dates: &files_service.CommitDateOptions{
|
2019-12-24 08:03:52 +05:30
|
|
|
|
Author: apiOpts.Dates.Author,
|
|
|
|
|
Committer: apiOpts.Dates.Committer,
|
|
|
|
|
},
|
2021-01-29 14:27:45 +05:30
|
|
|
|
Signoff: apiOpts.Signoff,
|
2019-12-24 08:03:52 +05:30
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Author.IsZero() {
|
|
|
|
|
opts.Dates.Author = time.Now()
|
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Committer.IsZero() {
|
|
|
|
|
opts.Dates.Committer = time.Now()
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:49:24 +05:30
|
|
|
|
if opts.Message == "" {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
opts.Message = changeFilesCommitMessage(ctx, opts.Files)
|
2019-06-29 20:49:24 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
if filesResponse, err := createOrUpdateFiles(ctx, opts); err != nil {
|
2020-06-01 02:29:34 +05:30
|
|
|
|
handleCreateOrUpdateFileError(ctx, err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
} else {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
fileResponse := files_service.GetFileResponseFromFilesResponse(filesResponse, 0)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.JSON(http.StatusOK, fileResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 02:29:34 +05:30
|
|
|
|
func handleCreateOrUpdateFileError(ctx *context.APIContext, err error) {
|
|
|
|
|
if models.IsErrUserCannotCommit(err) || models.IsErrFilePathProtected(err) {
|
|
|
|
|
ctx.Error(http.StatusForbidden, "Access", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-06-29 15:33:20 +05:30
|
|
|
|
if git_model.IsErrBranchAlreadyExists(err) || models.IsErrFilenameInvalid(err) || models.IsErrSHADoesNotMatch(err) ||
|
2020-06-01 02:29:34 +05:30
|
|
|
|
models.IsErrFilePathInvalid(err) || models.IsErrRepoFileAlreadyExists(err) {
|
|
|
|
|
ctx.Error(http.StatusUnprocessableEntity, "Invalid", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-06-29 15:33:20 +05:30
|
|
|
|
if git_model.IsErrBranchNotExist(err) || git.IsErrBranchNotExist(err) {
|
2020-06-07 23:00:58 +05:30
|
|
|
|
ctx.Error(http.StatusNotFound, "BranchDoesNotExist", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-06-01 02:29:34 +05:30
|
|
|
|
|
|
|
|
|
ctx.Error(http.StatusInternalServerError, "UpdateFile", err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// Called from both CreateFile or UpdateFile to handle both
|
2023-05-29 15:11:35 +05:30
|
|
|
|
func createOrUpdateFiles(ctx *context.APIContext, opts *files_service.ChangeRepoFilesOptions) (*api.FilesResponse, error) {
|
2022-04-28 21:15:33 +05:30
|
|
|
|
if !canWriteFiles(ctx, opts.OldBranch) {
|
2022-06-13 15:07:59 +05:30
|
|
|
|
return nil, repo_model.ErrUserDoesNotHaveAccessToRepo{
|
2022-03-22 12:33:22 +05:30
|
|
|
|
UserID: ctx.Doer.ID,
|
2019-04-17 21:36:35 +05:30
|
|
|
|
RepoName: ctx.Repo.Repository.LowerName,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
for _, file := range opts.Files {
|
|
|
|
|
content, err := base64.StdEncoding.DecodeString(file.Content)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
file.Content = string(content)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
return files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, opts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// format commit message if empty
|
|
|
|
|
func changeFilesCommitMessage(ctx *context.APIContext, files []*files_service.ChangeRepoFile) string {
|
|
|
|
|
var (
|
|
|
|
|
createFiles []string
|
|
|
|
|
updateFiles []string
|
|
|
|
|
deleteFiles []string
|
|
|
|
|
)
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
switch file.Operation {
|
|
|
|
|
case "create":
|
|
|
|
|
createFiles = append(createFiles, file.TreePath)
|
|
|
|
|
case "update":
|
|
|
|
|
updateFiles = append(updateFiles, file.TreePath)
|
|
|
|
|
case "delete":
|
|
|
|
|
deleteFiles = append(deleteFiles, file.TreePath)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
message := ""
|
|
|
|
|
if len(createFiles) != 0 {
|
|
|
|
|
message += ctx.Tr("repo.editor.add", strings.Join(createFiles, ", ")+"\n")
|
|
|
|
|
}
|
|
|
|
|
if len(updateFiles) != 0 {
|
|
|
|
|
message += ctx.Tr("repo.editor.update", strings.Join(updateFiles, ", ")+"\n")
|
|
|
|
|
}
|
|
|
|
|
if len(deleteFiles) != 0 {
|
|
|
|
|
message += ctx.Tr("repo.editor.delete", strings.Join(deleteFiles, ", "))
|
|
|
|
|
}
|
|
|
|
|
return strings.Trim(message, "\n")
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 15:02:37 +05:30
|
|
|
|
// DeleteFile Delete a file in a repository
|
2021-01-26 21:06:53 +05:30
|
|
|
|
func DeleteFile(ctx *context.APIContext) {
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// 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 23:27:55 +05:30
|
|
|
|
// required: true
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// schema:
|
|
|
|
|
// "$ref": "#/definitions/DeleteFileOptions"
|
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
|
|
|
|
// "$ref": "#/responses/FileDeleteResponse"
|
2020-04-15 10:48:51 +05:30
|
|
|
|
// "400":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
// "403":
|
|
|
|
|
// "$ref": "#/responses/error"
|
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/error"
|
2019-12-20 22:37:12 +05:30
|
|
|
|
|
2021-01-26 21:06:53 +05:30
|
|
|
|
apiOpts := web.GetForm(ctx).(*api.DeleteFileOptions)
|
2022-04-28 21:15:33 +05:30
|
|
|
|
if !canWriteFiles(ctx, apiOpts.BranchName) {
|
2022-06-13 15:07:59 +05:30
|
|
|
|
ctx.Error(http.StatusForbidden, "DeleteFile", repo_model.ErrUserDoesNotHaveAccessToRepo{
|
2022-03-22 12:33:22 +05:30
|
|
|
|
UserID: ctx.Doer.ID,
|
2019-04-17 21:36:35 +05:30
|
|
|
|
RepoName: ctx.Repo.Repository.LowerName,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 22:17:05 +05:30
|
|
|
|
if apiOpts.BranchName == "" {
|
|
|
|
|
apiOpts.BranchName = ctx.Repo.Repository.DefaultBranch
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
opts := &files_service.ChangeRepoFilesOptions{
|
|
|
|
|
Files: []*files_service.ChangeRepoFile{
|
|
|
|
|
{
|
|
|
|
|
Operation: "delete",
|
|
|
|
|
SHA: apiOpts.SHA,
|
|
|
|
|
TreePath: ctx.Params("*"),
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Message: apiOpts.Message,
|
|
|
|
|
OldBranch: apiOpts.BranchName,
|
|
|
|
|
NewBranch: apiOpts.NewBranchName,
|
2021-11-24 13:26:24 +05:30
|
|
|
|
Committer: &files_service.IdentityOptions{
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Name: apiOpts.Committer.Name,
|
|
|
|
|
Email: apiOpts.Committer.Email,
|
|
|
|
|
},
|
2021-11-24 13:26:24 +05:30
|
|
|
|
Author: &files_service.IdentityOptions{
|
2019-04-17 21:36:35 +05:30
|
|
|
|
Name: apiOpts.Author.Name,
|
|
|
|
|
Email: apiOpts.Author.Email,
|
|
|
|
|
},
|
2021-11-24 13:26:24 +05:30
|
|
|
|
Dates: &files_service.CommitDateOptions{
|
2019-12-24 08:03:52 +05:30
|
|
|
|
Author: apiOpts.Dates.Author,
|
|
|
|
|
Committer: apiOpts.Dates.Committer,
|
|
|
|
|
},
|
2021-01-29 14:27:45 +05:30
|
|
|
|
Signoff: apiOpts.Signoff,
|
2019-12-24 08:03:52 +05:30
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Author.IsZero() {
|
|
|
|
|
opts.Dates.Author = time.Now()
|
|
|
|
|
}
|
|
|
|
|
if opts.Dates.Committer.IsZero() {
|
|
|
|
|
opts.Dates.Committer = time.Now()
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
|
2019-06-29 20:49:24 +05:30
|
|
|
|
if opts.Message == "" {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
opts.Message = changeFilesCommitMessage(ctx, opts.Files)
|
2019-06-29 20:49:24 +05:30
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 15:11:35 +05:30
|
|
|
|
if filesResponse, err := files_service.ChangeRepoFiles(ctx, ctx.Repo.Repository, ctx.Doer, opts); err != nil {
|
2020-04-15 10:48:51 +05:30
|
|
|
|
if git.IsErrBranchNotExist(err) || models.IsErrRepoFileDoesNotExist(err) || git.IsErrNotExist(err) {
|
|
|
|
|
ctx.Error(http.StatusNotFound, "DeleteFile", err)
|
|
|
|
|
return
|
2023-06-29 15:33:20 +05:30
|
|
|
|
} else if git_model.IsErrBranchAlreadyExists(err) ||
|
2020-04-15 10:48:51 +05:30
|
|
|
|
models.IsErrFilenameInvalid(err) ||
|
|
|
|
|
models.IsErrSHADoesNotMatch(err) ||
|
|
|
|
|
models.IsErrCommitIDDoesNotMatch(err) ||
|
|
|
|
|
models.IsErrSHAOrCommitIDNotProvided(err) {
|
|
|
|
|
ctx.Error(http.StatusBadRequest, "DeleteFile", err)
|
|
|
|
|
return
|
|
|
|
|
} else if models.IsErrUserCannotCommit(err) {
|
|
|
|
|
ctx.Error(http.StatusForbidden, "DeleteFile", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-04-17 21:36:35 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "DeleteFile", err)
|
|
|
|
|
} else {
|
2023-05-29 15:11:35 +05:30
|
|
|
|
fileResponse := files_service.GetFileResponseFromFilesResponse(filesResponse, 0)
|
2020-04-15 10:48:51 +05:30
|
|
|
|
ctx.JSON(http.StatusOK, fileResponse) // FIXME on APIv2: return http.StatusNoContent
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-30 02:21:10 +05:30
|
|
|
|
// GetContents Get the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
|
|
|
|
|
func GetContents(ctx *context.APIContext) {
|
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/contents/{filepath} repository repoGetContents
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// ---
|
2019-06-30 02:21:10 +05:30
|
|
|
|
// summary: Gets the metadata and contents (if a file) of an entry in a repository, or a list of entries if a dir
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// 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
|
2019-06-30 02:21:10 +05:30
|
|
|
|
// description: path of the dir, file, symlink or submodule in the repo
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// type: string
|
|
|
|
|
// required: true
|
|
|
|
|
// - name: ref
|
|
|
|
|
// in: query
|
|
|
|
|
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
|
|
|
|
|
// type: string
|
2019-06-30 02:21:10 +05:30
|
|
|
|
// required: false
|
2019-04-17 21:36:35 +05:30
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
2019-06-30 02:21:10 +05:30
|
|
|
|
// "$ref": "#/responses/ContentsResponse"
|
2020-04-15 10:48:51 +05:30
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
2020-04-24 21:50:22 +05:30
|
|
|
|
if !canReadFiles(ctx.Repo) {
|
2022-06-13 15:07:59 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", repo_model.ErrUserDoesNotHaveAccessToRepo{
|
2022-03-22 12:33:22 +05:30
|
|
|
|
UserID: ctx.Doer.ID,
|
2019-04-17 21:36:35 +05:30
|
|
|
|
RepoName: ctx.Repo.Repository.LowerName,
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
treePath := ctx.Params("*")
|
2021-07-29 07:12:15 +05:30
|
|
|
|
ref := ctx.FormTrim("ref")
|
2019-04-17 21:36:35 +05:30
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
|
if fileList, err := files_service.GetContentsOrList(ctx, ctx.Repo.Repository, treePath, ref); err != nil {
|
2020-04-15 10:48:51 +05:30
|
|
|
|
if git.IsErrNotExist(err) {
|
|
|
|
|
ctx.NotFound("GetContentsOrList", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-06-30 02:21:10 +05:30
|
|
|
|
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
} else {
|
2019-06-30 02:21:10 +05:30
|
|
|
|
ctx.JSON(http.StatusOK, fileList)
|
2019-04-17 21:36:35 +05:30
|
|
|
|
}
|
2016-08-31 04:48:40 +05:30
|
|
|
|
}
|
2019-06-30 02:21:10 +05:30
|
|
|
|
|
|
|
|
|
// GetContentsList Get the metadata of all the entries of the root dir
|
|
|
|
|
func GetContentsList(ctx *context.APIContext) {
|
|
|
|
|
// swagger:operation GET /repos/{owner}/{repo}/contents repository repoGetContentsList
|
|
|
|
|
// ---
|
|
|
|
|
// summary: Gets the metadata of all the entries of the root dir
|
|
|
|
|
// 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: ref
|
|
|
|
|
// in: query
|
|
|
|
|
// description: "The name of the commit/branch/tag. Default the repository’s default branch (usually master)"
|
|
|
|
|
// type: string
|
|
|
|
|
// required: false
|
|
|
|
|
// responses:
|
|
|
|
|
// "200":
|
|
|
|
|
// "$ref": "#/responses/ContentsListResponse"
|
2020-04-15 10:48:51 +05:30
|
|
|
|
// "404":
|
|
|
|
|
// "$ref": "#/responses/notFound"
|
2019-06-30 02:21:10 +05:30
|
|
|
|
|
|
|
|
|
// same as GetContents(), this function is here because swagger fails if path is empty in GetContents() interface
|
|
|
|
|
GetContents(ctx)
|
|
|
|
|
}
|