Skip to content

Commit b57a036

Browse files
bytedreamwxiaoguang
authored andcommitted
Fix LFS file not stored in LFS when uploaded/edited via API or web UI (go-gitea#34367)
Files that should be stored in LFS and are uploaded/edited from the API or web UI aren't stored in LFS. This may be a regression from go-gitea#34154. --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent a3a95a0 commit b57a036

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

modules/git/attribute/checker.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ func checkAttrCommand(gitRepo *git.Repository, treeish string, filenames, attrib
3939
)
4040
cancel = deleteTemporaryFile
4141
}
42-
} // else: no treeish, assume it is a not a bare repo, read from working directory
42+
} else {
43+
// Read from existing index, in cases where the repo is bare and has an index,
44+
// or the work tree contains unstaged changes that shouldn't affect the attribute check.
45+
// It is caller's responsibility to add changed ".gitattributes" into the index if they want to respect the new changes.
46+
cmd.AddArguments("--cached")
47+
}
4348

4449
cmd.AddDynamicArguments(attributes...)
4550
if len(filenames) > 0 {

modules/git/attribute/checker_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,18 @@ func Test_Checker(t *testing.T) {
5757
assert.Equal(t, expectedAttrs(), attrs["i-am-a-python.p"])
5858
})
5959

60+
t.Run("Run git check-attr in bare repository using index", func(t *testing.T) {
61+
attrs, err := CheckAttributes(t.Context(), gitRepo, "", CheckAttributeOpts{
62+
Filenames: []string{"i-am-a-python.p"},
63+
Attributes: LinguistAttributes,
64+
})
65+
assert.NoError(t, err)
66+
assert.Len(t, attrs, 1)
67+
assert.Equal(t, expectedAttrs(), attrs["i-am-a-python.p"])
68+
})
69+
6070
if !git.DefaultFeatures().SupportCheckAttrOnBare {
61-
t.Skip("git version 2.40 is required to support run check-attr on bare repo")
71+
t.Skip("git version 2.40 is required to support run check-attr on bare repo without using index")
6272
return
6373
}
6474

services/repository/files/upload.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
107107
}
108108

109109
var attributesMap map[string]*attribute.Attributes
110+
// when uploading to an empty repo, the old branch doesn't exist, but some "global gitattributes" or "info/attributes" may exist
110111
if setting.LFS.StartServer {
111112
attributesMap, err = attribute.CheckAttributes(ctx, t.gitRepo, "" /* use temp repo's working dir */, attribute.CheckAttributeOpts{
112113
Attributes: []string{attribute.Filter},
@@ -118,6 +119,12 @@ func UploadRepoFiles(ctx context.Context, repo *repo_model.Repository, doer *use
118119
}
119120

120121
// Copy uploaded files into repository.
122+
// TODO: there is a small problem: when uploading LFS files with ".gitattributes", the "check-attr" runs before this loop,
123+
// so LFS files are not able to be added as LFS objects. Ideally we need to do in 3 steps in the future:
124+
// 1. Add ".gitattributes" to git index
125+
// 2. Run "check-attr" (the previous attribute.CheckAttributes call)
126+
// 3. Add files to git index (this loop)
127+
// This problem is trivial so maybe no need to spend too much time on it at the moment.
121128
for i := range infos {
122129
if err := copyUploadedLFSFileIntoRepository(ctx, &infos[i], attributesMap, t, opts.TreePath); err != nil {
123130
return err

0 commit comments

Comments
 (0)