Skip to content

Commit 1310649

Browse files
authored
render plain text file if the LFS object doesn't exist (#31812)
We had an issue where a repo was using LFS to store a file, but the user did not push the file. When trying to view the file, Gitea returned a 500 HTTP status code referencing `ErrLFSObjectNotExist`. It appears the intent was the render this file as plain text, but the conditional was flipped. I've also added a test to verify that the file is rendered as plain text.
1 parent 7569a47 commit 1310649

File tree

7 files changed

+18
-5
lines changed

7 files changed

+18
-5
lines changed

routers/web/repo/view.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,12 @@ func getFileReader(ctx gocontext.Context, repoID int64, blob *git.Blob) ([]byte,
234234
}
235235

236236
meta, err := git_model.GetLFSMetaObjectByOid(ctx, repoID, pointer.Oid)
237-
if err != nil && err != git_model.ErrLFSObjectNotExist { // fallback to plain file
237+
if err != nil { // fallback to plain file
238+
log.Warn("Unable to access LFS pointer %s in repo %d: %v", pointer.Oid, repoID, err)
238239
return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil
239240
}
240241

241242
dataRc.Close()
242-
if err != nil {
243-
return nil, nil, nil, err
244-
}
245243

246244
dataRc, err = lfs.ReadMetaObject(pointer)
247245
if err != nil {

tests/gitea-repositories-meta/user2/lfs.git/objects/30/77e1c4c8964613df72c37d14275c1eda5228a9

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
xK��OR0�0`p�� �t
2+
��s��MQH��)I-��I+VH�LK3rS��S�,ݒԊ.-���t"U&e��23�,1'�8���A�
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
73cf03db6ece34e12bf91e8853dc58f678f2f82d
1+
e9c32647bab825977942598c0efa415de300304b

tests/integration/lfs_view_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ func TestLFSRender(t *testing.T) {
8989
content := doc.Find("div.file-view").Text()
9090
assert.Contains(t, content, "Testing READMEs in LFS")
9191
})
92+
93+
// check that an invalid lfs entry defaults to plaintext
94+
t.Run("Invalid", func(t *testing.T) {
95+
defer tests.PrintCurrentTest(t)()
96+
97+
req := NewRequest(t, "GET", "/user2/lfs/src/branch/master/invalid")
98+
resp := session.MakeRequest(t, req, http.StatusOK)
99+
100+
doc := NewHTMLParser(t, resp.Body).doc
101+
102+
content := doc.Find("div.file-view").Text()
103+
assert.Contains(t, content, "oid sha256:9d178b5f15046343fd32f451df93acc2bdd9e6373be478b968e4cad6b6647351")
104+
})
92105
}
93106

94107
// TestLFSLockView tests the LFS lock view on settings page of repositories

0 commit comments

Comments
 (0)