Skip to content

Commit dd97082

Browse files
authored
Merge pull request #4 from git-pkgs/fix-lint-issues
Fix lint issues from golangci-lint quality checks
2 parents a9164a1 + 721aaa9 commit dd97082

6 files changed

Lines changed: 76 additions & 59 deletions

File tree

archives.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Reader interface {
4848
// Open creates an archive reader for the given content.
4949
// The filename is used to detect the archive format.
5050
// The content reader will be read entirely into memory.
51+
//nolint:ireturn // factory function returning interface by design
5152
func Open(filename string, content io.Reader) (Reader, error) {
5253
format := detectFormat(filename)
5354
if format == "" {
@@ -79,6 +80,7 @@ func Open(filename string, content io.Reader) (Reader, error) {
7980

8081
// OpenWithPrefix opens an archive and strips the given prefix from all paths.
8182
// This is useful for npm packages which wrap content in a "package/" directory.
83+
//nolint:ireturn // factory function returning interface by design
8284
func OpenWithPrefix(filename string, content io.Reader, stripPrefix string) (Reader, error) {
8385
reader, err := Open(filename, content)
8486
if err != nil {

diff/diff.go

Lines changed: 67 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ import (
1212
"github.com/git-pkgs/archives"
1313
)
1414

15+
const (
16+
// binaryCheckSize is the number of bytes to check for null bytes when
17+
// determining if content is binary.
18+
binaryCheckSize = 8192
19+
20+
// Diff type constants for FileDiff.Type.
21+
TypeModified = "modified"
22+
TypeAdded = "added"
23+
TypeDeleted = "deleted"
24+
)
25+
1526
// FileDiff represents the diff for a single file.
1627
type FileDiff struct {
1728
Path string `json:"path"`
@@ -87,67 +98,71 @@ func Compare(oldReader, newReader archives.Reader) (*CompareResult, error) {
8798
oldExists := oldMap[path]
8899
newExists := newMap[path]
89100

90-
var fileDiff FileDiff
101+
fileDiff, ok := compareFile(path, oldExists, newExists, oldReader, newReader)
102+
if !ok {
103+
continue
104+
}
91105

92-
if oldExists.Path != "" && newExists.Path == "" {
93-
// File was deleted
94-
fileDiff = FileDiff{
95-
Path: path,
96-
Type: "deleted",
97-
}
106+
switch fileDiff.Type {
107+
case TypeDeleted:
98108
result.FilesDeleted++
99-
} else if oldExists.Path == "" && newExists.Path != "" {
100-
// File was added
101-
fileDiff = FileDiff{
102-
Path: path,
103-
Type: "added",
104-
}
109+
case TypeAdded:
105110
result.FilesAdded++
111+
case TypeModified:
112+
result.FilesChanged++
113+
result.TotalAdded += fileDiff.LinesAdded
114+
result.TotalDeleted += fileDiff.LinesDeleted
115+
}
106116

107-
// Try to get content for added files
108-
if content, err := readFileContent(newReader, path); err == nil {
109-
if isBinary(content) {
110-
fileDiff.IsBinary = true
111-
} else {
112-
fileDiff.Diff = generateAddedDiff(path, content)
113-
fileDiff.LinesAdded = countLines(content)
114-
}
115-
}
116-
} else {
117-
// File exists in both - check if modified
118-
oldContent, err1 := readFileContent(oldReader, path)
119-
newContent, err2 := readFileContent(newReader, path)
120-
121-
if err1 != nil || err2 != nil {
122-
continue // Skip files we can't read
123-
}
124-
125-
if bytes.Equal(oldContent, newContent) {
126-
continue // No change
127-
}
117+
result.Files = append(result.Files, fileDiff)
118+
}
128119

129-
fileDiff = FileDiff{
130-
Path: path,
131-
Type: "modified",
132-
}
133-
result.FilesChanged++
120+
return result, nil
121+
}
134122

135-
if isBinary(oldContent) || isBinary(newContent) {
136-
fileDiff.IsBinary = true
123+
// compareFile compares a single file between old and new archives.
124+
// Returns the diff and false if the file should be skipped.
125+
func compareFile(path string, oldInfo, newInfo archives.FileInfo, oldReader, newReader archives.Reader) (FileDiff, bool) {
126+
inOld := oldInfo.Path != ""
127+
inNew := newInfo.Path != ""
128+
129+
switch {
130+
case inOld && !inNew:
131+
return FileDiff{Path: path, Type: TypeDeleted}, true
132+
133+
case !inOld && inNew:
134+
fd := FileDiff{Path: path, Type: TypeAdded}
135+
if content, err := readFileContent(newReader, path); err == nil {
136+
if isBinary(content) {
137+
fd.IsBinary = true
137138
} else {
138-
diffText, added, deleted := generateUnifiedDiff(path, oldContent, newContent)
139-
fileDiff.Diff = diffText
140-
fileDiff.LinesAdded = added
141-
fileDiff.LinesDeleted = deleted
142-
result.TotalAdded += added
143-
result.TotalDeleted += deleted
139+
fd.Diff = generateAddedDiff(path, content)
140+
fd.LinesAdded = countLines(content)
144141
}
145142
}
143+
return fd, true
146144

147-
result.Files = append(result.Files, fileDiff)
148-
}
145+
default:
146+
oldContent, err1 := readFileContent(oldReader, path)
147+
newContent, err2 := readFileContent(newReader, path)
148+
if err1 != nil || err2 != nil {
149+
return FileDiff{}, false
150+
}
151+
if bytes.Equal(oldContent, newContent) {
152+
return FileDiff{}, false
153+
}
149154

150-
return result, nil
155+
fd := FileDiff{Path: path, Type: TypeModified}
156+
if isBinary(oldContent) || isBinary(newContent) {
157+
fd.IsBinary = true
158+
} else {
159+
diffText, added, deleted := generateUnifiedDiff(path, oldContent, newContent)
160+
fd.Diff = diffText
161+
fd.LinesAdded = added
162+
fd.LinesDeleted = deleted
163+
}
164+
return fd, true
165+
}
151166
}
152167

153168
// readFileContent reads a file's content from an archive reader.
@@ -169,8 +184,8 @@ func isBinary(content []byte) bool {
169184

170185
// Check first 8KB for null bytes
171186
checkLen := len(content)
172-
if checkLen > 8192 {
173-
checkLen = 8192
187+
if checkLen > binaryCheckSize {
188+
checkLen = binaryCheckSize
174189
}
175190

176191
for i := 0; i < checkLen; i++ {

diff/diff_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,21 @@ func TestCompare(t *testing.T) {
8282
}
8383

8484
// Check deleted file
85-
if f, ok := fileMap["deleted.txt"]; !ok || f.Type != "deleted" {
85+
if f, ok := fileMap["deleted.txt"]; !ok || f.Type != TypeDeleted {
8686
t.Error("deleted.txt should be marked as deleted")
8787
}
8888

8989
// Check added file
90-
if f, ok := fileMap["added.txt"]; !ok || f.Type != "added" {
90+
if f, ok := fileMap["added.txt"]; !ok || f.Type != TypeAdded {
9191
t.Error("added.txt should be marked as added")
9292
}
9393

9494
// Check modified files
95-
if f, ok := fileMap["README.md"]; !ok || f.Type != "modified" {
95+
if f, ok := fileMap["README.md"]; !ok || f.Type != TypeModified {
9696
t.Error("README.md should be marked as modified")
9797
}
9898

99-
if f, ok := fileMap["src/main.go"]; !ok || f.Type != "modified" {
99+
if f, ok := fileMap["src/main.go"]; !ok || f.Type != TypeModified {
100100
t.Error("src/main.go should be marked as modified")
101101
}
102102
}

gem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type gemReader struct {
1313
dataReader Reader // The inner data.tar.gz reader
1414
}
1515

16-
func openGem(content io.Reader) (Reader, error) {
16+
func openGem(content io.Reader) (*gemReader, error) {
1717
// Read the gem file as a tar archive
1818
tr := tar.NewReader(content)
1919

tar.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type tarFileEntry struct {
2121
data []byte
2222
}
2323

24-
func openTar(content io.Reader, compression string) (Reader, error) {
24+
func openTar(content io.Reader, compression string) (*tarReader, error) {
2525
// Wrap with decompressor if needed
2626
r := io.Reader(content)
2727

zip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type zipReader struct {
1313
reader *zip.Reader
1414
}
1515

16-
func openZip(content io.Reader) (Reader, error) {
16+
func openZip(content io.Reader) (*zipReader, error) {
1717
// Read entire content into memory
1818
data, err := io.ReadAll(content)
1919
if err != nil {

0 commit comments

Comments
 (0)