Skip to content

Commit ed8c63c

Browse files
authored
Deduplicate lfs common code (#30704)
1 parent 2a3906d commit ed8c63c

File tree

3 files changed

+42
-48
lines changed

3 files changed

+42
-48
lines changed

modules/git/pipeline/lfs_common.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package pipeline
5+
6+
import (
7+
"fmt"
8+
"time"
9+
10+
"code.gitea.io/gitea/modules/git"
11+
)
12+
13+
// LFSResult represents commits found using a provided pointer file hash
14+
type LFSResult struct {
15+
Name string
16+
SHA string
17+
Summary string
18+
When time.Time
19+
ParentHashes []git.ObjectID
20+
BranchName string
21+
FullCommitName string
22+
}
23+
24+
type lfsResultSlice []*LFSResult
25+
26+
func (a lfsResultSlice) Len() int { return len(a) }
27+
func (a lfsResultSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
28+
func (a lfsResultSlice) Less(i, j int) bool { return a[j].When.After(a[i].When) }
29+
30+
func lfsError(msg string, err error) error {
31+
return fmt.Errorf("LFS error occurred, %s: err: %w", msg, err)
32+
}

modules/git/pipeline/lfs.go renamed to modules/git/pipeline/lfs_gogit.go

+3-22
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ package pipeline
77

88
import (
99
"bufio"
10-
"fmt"
1110
"io"
1211
"sort"
1312
"strings"
1413
"sync"
15-
"time"
1614

1715
"code.gitea.io/gitea/modules/git"
1816

@@ -21,23 +19,6 @@ import (
2119
"github.com/go-git/go-git/v5/plumbing/object"
2220
)
2321

24-
// LFSResult represents commits found using a provided pointer file hash
25-
type LFSResult struct {
26-
Name string
27-
SHA string
28-
Summary string
29-
When time.Time
30-
ParentHashes []git.ObjectID
31-
BranchName string
32-
FullCommitName string
33-
}
34-
35-
type lfsResultSlice []*LFSResult
36-
37-
func (a lfsResultSlice) Len() int { return len(a) }
38-
func (a lfsResultSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
39-
func (a lfsResultSlice) Less(i, j int) bool { return a[j].When.After(a[i].When) }
40-
4122
// FindLFSFile finds commits that contain a provided pointer file hash
4223
func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, error) {
4324
resultsMap := map[string]*LFSResult{}
@@ -51,7 +32,7 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
5132
All: true,
5233
})
5334
if err != nil {
54-
return nil, fmt.Errorf("Failed to get GoGit CommitsIter. Error: %w", err)
35+
return nil, lfsError("failed to get GoGit CommitsIter", err)
5536
}
5637

5738
err = commitsIter.ForEach(func(gitCommit *object.Commit) error {
@@ -85,7 +66,7 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
8566
return nil
8667
})
8768
if err != nil && err != io.EOF {
88-
return nil, fmt.Errorf("Failure in CommitIter.ForEach: %w", err)
69+
return nil, lfsError("failure in CommitIter.ForEach", err)
8970
}
9071

9172
for _, result := range resultsMap {
@@ -156,7 +137,7 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
156137
select {
157138
case err, has := <-errChan:
158139
if has {
159-
return nil, fmt.Errorf("Unable to obtain name for LFS files. Error: %w", err)
140+
return nil, lfsError("unable to obtain name for LFS files", err)
160141
}
161142
default:
162143
}

modules/git/pipeline/lfs_nogogit.go

+7-26
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,14 @@ package pipeline
88
import (
99
"bufio"
1010
"bytes"
11-
"fmt"
1211
"io"
1312
"sort"
1413
"strings"
1514
"sync"
16-
"time"
1715

1816
"code.gitea.io/gitea/modules/git"
1917
)
2018

21-
// LFSResult represents commits found using a provided pointer file hash
22-
type LFSResult struct {
23-
Name string
24-
SHA string
25-
Summary string
26-
When time.Time
27-
ParentIDs []git.ObjectID
28-
BranchName string
29-
FullCommitName string
30-
}
31-
32-
type lfsResultSlice []*LFSResult
33-
34-
func (a lfsResultSlice) Len() int { return len(a) }
35-
func (a lfsResultSlice) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
36-
func (a lfsResultSlice) Less(i, j int) bool { return a[j].When.After(a[i].When) }
37-
3819
// FindLFSFile finds commits that contain a provided pointer file hash
3920
func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, error) {
4021
resultsMap := map[string]*LFSResult{}
@@ -137,11 +118,11 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
137118
n += int64(count)
138119
if bytes.Equal(binObjectID, objectID.RawValue()) {
139120
result := LFSResult{
140-
Name: curPath + string(fname),
141-
SHA: curCommit.ID.String(),
142-
Summary: strings.Split(strings.TrimSpace(curCommit.CommitMessage), "\n")[0],
143-
When: curCommit.Author.When,
144-
ParentIDs: curCommit.Parents,
121+
Name: curPath + string(fname),
122+
SHA: curCommit.ID.String(),
123+
Summary: strings.Split(strings.TrimSpace(curCommit.CommitMessage), "\n")[0],
124+
When: curCommit.Author.When,
125+
ParentHashes: curCommit.Parents,
145126
}
146127
resultsMap[curCommit.ID.String()+":"+curPath+string(fname)] = &result
147128
} else if string(mode) == git.EntryModeTree.String() {
@@ -183,7 +164,7 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
183164

184165
for _, result := range resultsMap {
185166
hasParent := false
186-
for _, parentID := range result.ParentIDs {
167+
for _, parentID := range result.ParentHashes {
187168
if _, hasParent = resultsMap[parentID.String()+":"+result.Name]; hasParent {
188169
break
189170
}
@@ -240,7 +221,7 @@ func FindLFSFile(repo *git.Repository, objectID git.ObjectID) ([]*LFSResult, err
240221
select {
241222
case err, has := <-errChan:
242223
if has {
243-
return nil, fmt.Errorf("Unable to obtain name for LFS files. Error: %w", err)
224+
return nil, lfsError("unable to obtain name for LFS files", err)
244225
}
245226
default:
246227
}

0 commit comments

Comments
 (0)