Skip to content

Commit 232340f

Browse files
davidsvantessonlunny
authored andcommitted
Prevent upload (overwrite) of lfs locked file (#8769)
* Check if file is locked on upload file commit. * Better user message if file is locked. * Check lfs lock before creating temporary repository. fix some errors. * move lines * Add comment that enabled setting is checked.
1 parent 9d663df commit 232340f

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

models/error.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,23 @@ func (err ErrLFSLockAlreadyExist) Error() string {
647647
return fmt.Sprintf("lfs lock already exists [rid: %d, path: %s]", err.RepoID, err.Path)
648648
}
649649

650+
// ErrLFSFileLocked represents a "LFSFileLocked" kind of error.
651+
type ErrLFSFileLocked struct {
652+
RepoID int64
653+
Path string
654+
UserName string
655+
}
656+
657+
// IsErrLFSFileLocked checks if an error is a ErrLFSFileLocked.
658+
func IsErrLFSFileLocked(err error) bool {
659+
_, ok := err.(ErrLFSFileLocked)
660+
return ok
661+
}
662+
663+
func (err ErrLFSFileLocked) Error() string {
664+
return fmt.Sprintf("File is lfs locked [repo: %d, locked by: %s, path: %s]", err.RepoID, err.UserName, err.Path)
665+
}
666+
650667
// __________ .__ __
651668
// \______ \ ____ ______ ____ _____|__|/ |_ ___________ ___.__.
652669
// | _// __ \\____ \ / _ \/ ___/ \ __\/ _ \_ __ < | |

modules/repofiles/upload.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ func UploadRepoFiles(repo *models.Repository, doer *models.User, opts *UploadRep
5555
return fmt.Errorf("GetUploadsByUUIDs [uuids: %v]: %v", opts.Files, err)
5656
}
5757

58+
names := make([]string, len(uploads))
59+
infos := make([]uploadInfo, len(uploads))
60+
for i, upload := range uploads {
61+
// Check file is not lfs locked, will return nil if lock setting not enabled
62+
filepath := path.Join(opts.TreePath, upload.Name)
63+
lfsLock, err := repo.GetTreePathLock(filepath)
64+
if err != nil {
65+
return err
66+
}
67+
if lfsLock != nil && lfsLock.OwnerID != doer.ID {
68+
return models.ErrLFSFileLocked{RepoID: repo.ID, Path: filepath, UserName: lfsLock.Owner.Name}
69+
}
70+
71+
names[i] = upload.Name
72+
infos[i] = uploadInfo{upload: upload}
73+
}
74+
5875
t, err := NewTemporaryUploadRepository(repo)
5976
if err != nil {
6077
return err
@@ -67,13 +84,6 @@ func UploadRepoFiles(repo *models.Repository, doer *models.User, opts *UploadRep
6784
return err
6885
}
6986

70-
names := make([]string, len(uploads))
71-
infos := make([]uploadInfo, len(uploads))
72-
for i, upload := range uploads {
73-
names[i] = upload.Name
74-
infos[i] = uploadInfo{upload: upload}
75-
}
76-
7787
var filename2attribute2info map[string]map[string]string
7888
if setting.LFS.StartServer {
7989
filename2attribute2info, err = t.CheckAttribute("filter", names...)

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ editor.no_changes_to_show = There are no changes to show.
742742
editor.fail_to_update_file = Failed to update/create file '%s' with error: %v
743743
editor.add_subdir = Add a directory…
744744
editor.unable_to_upload_files = Failed to upload files to '%s' with error: %v
745+
editor.upload_file_is_locked = File '%s' is locked by %s.
745746
editor.upload_files_to_dir = Upload files to '%s'
746747
editor.cannot_commit_to_protected_branch = Cannot commit to protected branch '%s'.
747748

routers/repo/editor.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,11 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
585585
Files: form.Files,
586586
}); err != nil {
587587
ctx.Data["Err_TreePath"] = true
588-
ctx.RenderWithErr(ctx.Tr("repo.editor.unable_to_upload_files", form.TreePath, err), tplUploadFile, &form)
588+
if models.IsErrLFSFileLocked(err) {
589+
ctx.RenderWithErr(ctx.Tr("repo.editor.upload_file_is_locked", err.(models.ErrLFSFileLocked).Path, err.(models.ErrLFSFileLocked).UserName), tplUploadFile, &form)
590+
} else {
591+
ctx.RenderWithErr(ctx.Tr("repo.editor.unable_to_upload_files", form.TreePath, err), tplUploadFile, &form)
592+
}
589593
return
590594
}
591595

0 commit comments

Comments
 (0)