-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommits.go
More file actions
130 lines (113 loc) · 3.79 KB
/
Copy pathcommits.go
File metadata and controls
130 lines (113 loc) · 3.79 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2024 Aviator Technologies, Inc.
// SPDX-License-Identifier: MIT
package nichegit
import (
"bytes"
"context"
"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 GetCommitsArgs struct {
RepoURL string `json:"repoURL"`
WantCommitHashes []string `json:"wantCommitHashes"`
HaveCommitHashes []string `json:"haveCommitHashes"`
}
type GetCommitsOutput struct {
Commits []*CommitInfo `json:"commits"`
DebugInfo debug.FetchDebugInfo `json:"debugInfo"`
Error string `json:"error,omitempty"`
}
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 GetCommits(ctx context.Context, client *http.Client, args GetCommitsArgs) GetCommitsOutput {
var wantCommitHashes []plumbing.Hash
for _, s := range args.WantCommitHashes {
wantCommitHashes = append(wantCommitHashes, plumbing.NewHash(s))
}
var haveCommitHashes []plumbing.Hash
for _, s := range args.HaveCommitHashes {
haveCommitHashes = append(haveCommitHashes, plumbing.NewHash(s))
}
commits, debugInfo, fetchErr := FetchCommits(ctx, args.RepoURL, client, wantCommitHashes, haveCommitHashes)
if commits == nil {
// Always create an empty slice for JSON output.
commits = []*CommitInfo{}
}
output := GetCommitsOutput{
Commits: commits,
DebugInfo: debugInfo,
}
if fetchErr != nil {
output.Error = fetchErr.Error()
}
return output
}
func FetchCommits(ctx context.Context, repoURL string, client *http.Client, wantCommitHashes, haveCommitHashes []plumbing.Hash) ([]*CommitInfo, debug.FetchDebugInfo, error) {
packfilebs, debugInfo, err := fetch.FetchCommitOnlyPackfile(ctx, repoURL, client, wantCommitHashes, haveCommitHashes, 0)
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,
}
}