-
Notifications
You must be signed in to change notification settings - Fork 1
/
commits.go
94 lines (79 loc) · 2.64 KB
/
commits.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
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
// Copyright 2024 Aviator Technologies, Inc.
// SPDX-License-Identifier: MIT
package nichegit
import (
"bytes"
"fmt"
"net/http"
"time"
"github.com/aviator-co/niche-git/debug"
"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 CommitSignature struct {
Name string `json:"name"`
Email string `json:"email"`
Timestamp time.Time `json:"timestamp"`
}
type CommitInfo struct {
// Hash is the commit hash.
Hash string `json:"hash"`
// Author is the author of the commit.
Author CommitSignature `json:"author"`
// Committer is the committer of the commit.
Committer CommitSignature `json:"committer"`
// Message is the commit message.
Message string `json:"message"`
// TreeHash is the hash of the tree object of the commit.
TreeHash string `json:"treeHash"`
// ParentHashes are the hashes of the parent commits.
ParentHashes []string `json:"parentHashes"`
}
func FetchCommits(repoURL string, client *http.Client, wantCommitHashes, haveCommitHashes []plumbing.Hash) ([]*CommitInfo, debug.FetchDebugInfo, error) {
packfilebs, debugInfo, err := fetch.FetchCommitOnlyPackfile(repoURL, client, wantCommitHashes, haveCommitHashes)
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)
}
var ret []*CommitInfo
for hash := range storage.Commits {
commit, err := object.GetCommit(storage, hash)
if err != nil {
return nil, debugInfo, fmt.Errorf("cannot parse %q in the fetched packfile: %v", hash, err)
}
ret = append(ret, convertCommitInfo(commit))
}
return ret, debugInfo, nil
}
func convertCommitInfo(commit *object.Commit) *CommitInfo {
var parentHashes []string
for _, parent := range commit.ParentHashes {
parentHashes = append(parentHashes, parent.String())
}
return &CommitInfo{
Hash: commit.Hash.String(),
Author: CommitSignature{
Name: commit.Author.Name,
Email: commit.Author.Email,
Timestamp: commit.Author.When,
},
Committer: CommitSignature{
Name: commit.Committer.Name,
Email: commit.Committer.Email,
Timestamp: commit.Committer.When,
},
Message: commit.Message,
TreeHash: commit.TreeHash.String(),
ParentHashes: parentHashes,
}
}