-
Notifications
You must be signed in to change notification settings - Fork 1
/
modified_files.go
63 lines (55 loc) · 2.1 KB
/
modified_files.go
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
// Copyright 2024 Aviator Technologies, Inc.
// SPDX-License-Identifier: MIT
package nichegit
import (
"bytes"
"fmt"
"net/http"
"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"
)
// FetchModifiedFiles returns the list of files that were modified between two commits.
func FetchModifiedFiles(repoURL string, client *http.Client, commitHash1, commitHash2 plumbing.Hash) ([]string, debug.FetchDebugInfo, error) {
packfilebs, debugInfo, err := fetch.FetchBlobNonePackfile(repoURL, client, []plumbing.Hash{commitHash1, commitHash2})
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
}