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