-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodified_files.go
More file actions
99 lines (88 loc) · 2.95 KB
/
Copy pathmodified_files.go
File metadata and controls
99 lines (88 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2024 Aviator Technologies, Inc.
// SPDX-License-Identifier: MIT
package nichegit
import (
"bytes"
"context"
"fmt"
"net/http"
"sort"
"github.com/aviator-co/niche-git/debug"
"github.com/aviator-co/niche-git/internal/diff"
"github.com/aviator-co/niche-git/internal/fetch"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/format/packfile"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/storage/memory"
)
type GetModifiedFilesArgs struct {
RepoURL string `json:"repoURL"`
CommitHash1 string `json:"commitHash1"`
CommitHash2 string `json:"commitHash2"`
}
type GetModifiedFilesOutput struct {
Files []string `json:"files"`
DebugInfo debug.FetchDebugInfo `json:"debugInfo"`
Error string `json:"error,omitempty"`
}
func GetModifiedFiles(ctx context.Context, client *http.Client, args GetModifiedFilesArgs) GetModifiedFilesOutput {
files, debugInfo, fetchErr := fetchModifiedFiles(
ctx,
args.RepoURL,
client,
plumbing.NewHash(args.CommitHash1),
plumbing.NewHash(args.CommitHash2),
)
if files == nil {
// Always create an empty slice for JSON output.
files = []string{}
}
output := GetModifiedFilesOutput{
Files: files,
DebugInfo: debugInfo,
}
sort.Strings(output.Files)
if fetchErr != nil {
output.Error = fetchErr.Error()
}
return output
}
func fetchModifiedFiles(ctx context.Context, repoURL string, client *http.Client, commitHash1, commitHash2 plumbing.Hash) ([]string, debug.FetchDebugInfo, error) {
packfilebs, debugInfo, err := fetch.FetchBlobNonePackfile(ctx, repoURL, client, []plumbing.Hash{commitHash1, commitHash2}, 1)
if err != nil {
return nil, debugInfo, err
}
storage := memory.NewStorage()
parser, err := packfile.NewParserWithStorage(packfile.NewScanner(bytes.NewReader(packfilebs)), storage)
if err != nil {
return nil, debugInfo, fmt.Errorf("failed to parse packfile: %v", err)
}
if _, err := parser.Parse(); err != nil {
return nil, debugInfo, fmt.Errorf("failed to parse packfile: %v", err)
}
commit1, err := object.GetCommit(storage, commitHash1)
if err != nil {
return nil, debugInfo, fmt.Errorf("cannot find %q in the fetched packfile: %v", commitHash1, err)
}
commit2, err := object.GetCommit(storage, commitHash2)
if err != nil {
return nil, debugInfo, fmt.Errorf("cannot find %q in the fetched packfile: %v", commitHash2, err)
}
tree1, err := commit1.Tree()
if err != nil {
return nil, debugInfo, fmt.Errorf("cannot find the tree of %q in the fetched packfile: %v", commitHash1, err)
}
tree2, err := commit2.Tree()
if err != nil {
return nil, debugInfo, fmt.Errorf("cannot find the tree of %q in the fetched packfile: %v", commitHash2, err)
}
modified, err := diff.DiffTree(storage, tree1, tree2)
if err != nil {
return nil, debugInfo, fmt.Errorf("failed to take file diffs: %v", err)
}
var ret []string
for pth := range modified {
ret = append(ret, pth)
}
return ret, debugInfo, nil
}