Skip to content

Commit 54d2e4d

Browse files
committed
Workaround LFS files not being available in pull requests
Patch taken from upstream Gitea issue go-gitea#17715, associating the LFS pointer when the file is downloaded.
1 parent 146fff2 commit 54d2e4d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

models/git/lfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func CountLFSMetaObjects(ctx context.Context, repoID int64) (int64, error) {
239239

240240
// LFSObjectAccessible checks if a provided Oid is accessible to the user
241241
func LFSObjectAccessible(ctx context.Context, user *user_model.User, oid string) (bool, error) {
242-
if user.IsAdmin {
242+
if user != nil && user.IsAdmin {
243243
count, err := db.GetEngine(ctx).Count(&LFSMetaObject{Pointer: lfs.Pointer{Oid: oid}})
244244
return count > 0, err
245245
}

services/lfs/server.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,26 @@ func BatchHandler(ctx *context.Context) {
246246
responseObject = buildObjectResponse(rc, p, false, !exists, err)
247247
} else {
248248
var err *lfs_module.ObjectError
249+
250+
if exists && meta == nil {
251+
accessible, accessibleErr := git_model.LFSObjectAccessible(ctx, ctx.Doer, p.Oid)
252+
if accessibleErr != nil {
253+
log.Error("Unable to check if LFS MetaObject [%s] is accessible. Error: %v", p.Oid, err)
254+
writeStatus(ctx, http.StatusInternalServerError)
255+
return
256+
}
257+
if accessible {
258+
_, newMetaObjErr := git_model.NewLFSMetaObject(ctx, &git_model.LFSMetaObject{Pointer: p, RepositoryID: repository.ID})
259+
if newMetaObjErr != nil {
260+
log.Error("Unable to create LFS MetaObject [%s] for %s/%s. Error: %v", p.Oid, rc.User, rc.Repo, err)
261+
writeStatus(ctx, http.StatusInternalServerError)
262+
return
263+
}
264+
} else {
265+
exists = false
266+
}
267+
}
268+
249269
if !exists || meta == nil {
250270
err = &lfs_module.ObjectError{
251271
Code: http.StatusNotFound,

0 commit comments

Comments
 (0)