2017-11-29 04:58:37 +08:00
|
|
|
// Copyright 2017 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.
|
|
|
|
|
2022-06-12 23:51:54 +08:00
|
|
|
package git
|
2017-11-29 04:58:37 +08:00
|
|
|
|
|
|
|
import (
|
2022-04-28 19:48:48 +08:00
|
|
|
"context"
|
2017-11-29 04:58:37 +08:00
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-28 19:58:28 +08:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 18:09:36 +08:00
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 09:27:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 03:57:58 +08:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 17:49:20 +08:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-12-10 09:27:50 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2017-11-29 04:58:37 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// LFSLock represents a git lfs lock of repository.
|
|
|
|
type LFSLock struct {
|
2021-12-10 09:27:50 +08:00
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
OwnerID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
Path string `xorm:"TEXT"`
|
|
|
|
Created time.Time `xorm:"created"`
|
2017-11-29 04:58:37 +08:00
|
|
|
}
|
|
|
|
|
2021-09-19 19:49:59 +08:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(LFSLock))
|
|
|
|
}
|
|
|
|
|
2017-11-29 04:58:37 +08:00
|
|
|
// BeforeInsert is invoked from XORM before inserting an object of this type.
|
|
|
|
func (l *LFSLock) BeforeInsert() {
|
|
|
|
l.Path = cleanPath(l.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanPath(p string) string {
|
2019-12-12 21:18:07 +08:00
|
|
|
return path.Clean("/" + p)[1:]
|
2017-11-29 04:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateLFSLock creates a new lock.
|
2021-12-10 09:27:50 +08:00
|
|
|
func CreateLFSLock(repo *repo_model.Repository, lock *LFSLock) (*LFSLock, error) {
|
2022-04-28 19:48:48 +08:00
|
|
|
dbCtx, committer, err := db.TxContext()
|
2017-11-29 04:58:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-28 19:48:48 +08:00
|
|
|
defer committer.Close()
|
|
|
|
|
|
|
|
if err := CheckLFSAccessForRepo(dbCtx, lock.OwnerID, repo, perm.AccessModeWrite); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-29 04:58:37 +08:00
|
|
|
|
2019-12-12 21:18:07 +08:00
|
|
|
lock.Path = cleanPath(lock.Path)
|
2021-12-10 09:27:50 +08:00
|
|
|
lock.RepoID = repo.ID
|
2019-12-12 21:18:07 +08:00
|
|
|
|
2022-04-28 19:48:48 +08:00
|
|
|
l, err := GetLFSLock(dbCtx, repo, lock.Path)
|
2017-11-29 04:58:37 +08:00
|
|
|
if err == nil {
|
|
|
|
return l, ErrLFSLockAlreadyExist{lock.RepoID, lock.Path}
|
|
|
|
}
|
|
|
|
if !IsErrLFSLockNotExist(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:48:48 +08:00
|
|
|
if err := db.Insert(dbCtx, lock); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lock, committer.Commit()
|
2017-11-29 04:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLFSLock returns release by given path.
|
2022-04-28 19:48:48 +08:00
|
|
|
func GetLFSLock(ctx context.Context, repo *repo_model.Repository, path string) (*LFSLock, error) {
|
2017-11-29 04:58:37 +08:00
|
|
|
path = cleanPath(path)
|
2018-01-28 00:48:15 +08:00
|
|
|
rel := &LFSLock{RepoID: repo.ID}
|
2022-04-28 19:48:48 +08:00
|
|
|
has, err := db.GetEngine(ctx).Where("lower(path) = ?", strings.ToLower(path)).Get(rel)
|
2017-11-29 04:58:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
2018-01-28 00:48:15 +08:00
|
|
|
return nil, ErrLFSLockNotExist{0, repo.ID, path}
|
2017-11-29 04:58:37 +08:00
|
|
|
}
|
|
|
|
return rel, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLFSLockByID returns release by given id.
|
2022-04-28 19:48:48 +08:00
|
|
|
func GetLFSLockByID(ctx context.Context, id int64) (*LFSLock, error) {
|
2017-11-29 04:58:37 +08:00
|
|
|
lock := new(LFSLock)
|
2022-04-28 19:48:48 +08:00
|
|
|
has, err := db.GetEngine(ctx).ID(id).Get(lock)
|
2017-11-29 04:58:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, ErrLFSLockNotExist{id, 0, ""}
|
|
|
|
}
|
|
|
|
return lock, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLFSLockByRepoID returns a list of locks of repository.
|
2019-12-12 21:18:07 +08:00
|
|
|
func GetLFSLockByRepoID(repoID int64, page, pageSize int) ([]*LFSLock, error) {
|
2021-11-21 23:41:00 +08:00
|
|
|
e := db.GetEngine(db.DefaultContext)
|
2019-12-12 21:18:07 +08:00
|
|
|
if page >= 0 && pageSize > 0 {
|
|
|
|
start := 0
|
|
|
|
if page > 0 {
|
|
|
|
start = (page - 1) * pageSize
|
|
|
|
}
|
2021-11-21 23:41:00 +08:00
|
|
|
e.Limit(pageSize, start)
|
2019-12-12 21:18:07 +08:00
|
|
|
}
|
|
|
|
lfsLocks := make([]*LFSLock, 0, pageSize)
|
2021-11-21 23:41:00 +08:00
|
|
|
return lfsLocks, e.Find(&lfsLocks, &LFSLock{RepoID: repoID})
|
2019-12-12 21:18:07 +08:00
|
|
|
}
|
|
|
|
|
2021-12-10 09:27:50 +08:00
|
|
|
// GetTreePathLock returns LSF lock for the treePath
|
|
|
|
func GetTreePathLock(repoID int64, treePath string) (*LFSLock, error) {
|
|
|
|
if !setting.LFS.StartServer {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
locks, err := GetLFSLockByRepoID(repoID, 0, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, lock := range locks {
|
|
|
|
if lock.Path == treePath {
|
|
|
|
return lock, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-12-12 21:18:07 +08:00
|
|
|
// CountLFSLockByRepoID returns a count of all LFSLocks associated with a repository.
|
|
|
|
func CountLFSLockByRepoID(repoID int64) (int64, error) {
|
2021-09-23 23:45:36 +08:00
|
|
|
return db.GetEngine(db.DefaultContext).Count(&LFSLock{RepoID: repoID})
|
2017-11-29 04:58:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLFSLockByID deletes a lock by given ID.
|
2021-12-10 09:27:50 +08:00
|
|
|
func DeleteLFSLockByID(id int64, repo *repo_model.Repository, u *user_model.User, force bool) (*LFSLock, error) {
|
2022-04-28 19:48:48 +08:00
|
|
|
dbCtx, committer, err := db.TxContext()
|
2017-11-29 04:58:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-28 19:48:48 +08:00
|
|
|
defer committer.Close()
|
2017-11-29 04:58:37 +08:00
|
|
|
|
2022-04-28 19:48:48 +08:00
|
|
|
lock, err := GetLFSLockByID(dbCtx, id)
|
2017-11-29 04:58:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:48:48 +08:00
|
|
|
if err := CheckLFSAccessForRepo(dbCtx, u.ID, repo, perm.AccessModeWrite); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-29 04:58:37 +08:00
|
|
|
if !force && u.ID != lock.OwnerID {
|
|
|
|
return nil, fmt.Errorf("user doesn't own lock and force flag is not set")
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:48:48 +08:00
|
|
|
if _, err := db.GetEngine(dbCtx).ID(id).Delete(new(LFSLock)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lock, committer.Commit()
|
2017-11-29 04:58:37 +08:00
|
|
|
}
|
|
|
|
|
2021-03-15 02:52:12 +08:00
|
|
|
// CheckLFSAccessForRepo check needed access mode base on action
|
2022-04-28 19:48:48 +08:00
|
|
|
func CheckLFSAccessForRepo(ctx context.Context, ownerID int64, repo *repo_model.Repository, mode perm.AccessMode) error {
|
2021-11-24 17:49:20 +08:00
|
|
|
if ownerID == 0 {
|
2018-01-28 00:48:15 +08:00
|
|
|
return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode}
|
2017-11-29 04:58:37 +08:00
|
|
|
}
|
2022-04-28 19:48:48 +08:00
|
|
|
u, err := user_model.GetUserByIDCtx(ctx, ownerID)
|
2021-11-24 17:49:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-11 18:09:36 +08:00
|
|
|
perm, err := access_model.GetUserRepoPermission(ctx, repo, u)
|
2017-11-29 04:58:37 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-11-28 19:26:14 +08:00
|
|
|
}
|
2021-11-10 03:57:58 +08:00
|
|
|
if !perm.CanAccess(mode, unit.TypeCode) {
|
2018-01-28 00:48:15 +08:00
|
|
|
return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode}
|
2017-11-29 04:58:37 +08:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|