-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsquash_cherry_pick.go
181 lines (164 loc) · 6.6 KB
/
squash_cherry_pick.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2024 Aviator Technologies, Inc.
// SPDX-License-Identifier: MIT
package nichegit
import (
"bytes"
"errors"
"fmt"
"net/http"
"github.com/aviator-co/niche-git/debug"
"github.com/aviator-co/niche-git/internal/fetch"
"github.com/aviator-co/niche-git/internal/merge"
"github.com/aviator-co/niche-git/internal/push"
"github.com/aviator-co/niche-git/internal/resolvediff3"
"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 PushSquashCherryPickResult struct {
CommitHash plumbing.Hash
CherryPickedFiles []string
ConflictOpenFiles []string
ConflictResolvedFiles []string
BinaryConflictFiles []string
NonFileConflictFiles []string
}
// PushSquashCherryPick creates a new commit with the changes between the two commits and push to
// the specified ref.
func PushSquashCherryPick(
repoURL string,
client *http.Client,
commitHashCherryPickFrom, commitHashCherryPickTo, commitHashCherryPickBase plumbing.Hash,
commitMessage string,
author, committer object.Signature,
ref plumbing.ReferenceName,
conflictRef *plumbing.ReferenceName,
currentRefhash *plumbing.Hash,
abortOnConflict bool,
) (*PushSquashCherryPickResult, debug.FetchDebugInfo, *debug.FetchDebugInfo, *debug.PushDebugInfo, error) {
packfilebs, fetchDebugInfo, err := fetch.FetchBlobNonePackfile(repoURL, client, []plumbing.Hash{commitHashCherryPickFrom, commitHashCherryPickBase, commitHashCherryPickTo})
if err != nil {
return nil, fetchDebugInfo, nil, nil, err
}
storage := memory.NewStorage()
parser, err := packfile.NewParserWithStorage(packfile.NewScanner(bytes.NewReader(packfilebs)), storage)
if err != nil {
return nil, fetchDebugInfo, nil, nil, fmt.Errorf("failed to parse packfile: %v", err)
}
if _, err := parser.Parse(); err != nil {
return nil, fetchDebugInfo, nil, nil, fmt.Errorf("failed to parse packfile: %v", err)
}
treeCPFrom, err := getTreeFromCommit(storage, commitHashCherryPickFrom)
if err != nil {
return nil, fetchDebugInfo, nil, nil, err
}
treeCPBase, err := getTreeFromCommit(storage, commitHashCherryPickBase)
if err != nil {
return nil, fetchDebugInfo, nil, nil, err
}
treeCPTo, err := getTreeFromCommit(storage, commitHashCherryPickTo)
if err != nil {
return nil, fetchDebugInfo, nil, nil, err
}
collector := &conflictBlobCollector{}
mergeResult, err := merge.MergeTree(storage, treeCPFrom, treeCPTo, treeCPBase, collector.Resolve)
if err != nil {
return nil, fetchDebugInfo, nil, nil, fmt.Errorf("failed to merge the trees: %v", err)
}
resolver := resolvediff3.NewDiff3Resolver(storage, "Cherry-pick content", "Base content", ".rej", "")
var blobFetchDebugInfo *debug.FetchDebugInfo
if len(mergeResult.FilesConflict) != 0 {
// Need to fetch blobs and resolve the conflicts.
if len(collector.blobHashes) > 0 {
packfilebs, fetchBlobDebugInfo, err := fetch.FetchBlobPackfile(repoURL, client, collector.blobHashes)
blobFetchDebugInfo = &fetchBlobDebugInfo
if err != nil {
return nil, fetchDebugInfo, blobFetchDebugInfo, nil, err
}
parser, err := packfile.NewParserWithStorage(packfile.NewScanner(bytes.NewReader(packfilebs)), storage)
if err != nil {
return nil, fetchDebugInfo, blobFetchDebugInfo, nil, fmt.Errorf("failed to parse packfile: %v", err)
}
if _, err := parser.Parse(); err != nil {
return nil, fetchDebugInfo, blobFetchDebugInfo, nil, fmt.Errorf("failed to parse packfile: %v", err)
}
}
mergeResult, err = merge.MergeTree(storage, treeCPFrom, treeCPTo, treeCPBase, resolver.Resolve)
if err != nil {
return nil, fetchDebugInfo, blobFetchDebugInfo, nil, fmt.Errorf("failed to merge the trees: %v", err)
}
}
cpResult := &PushSquashCherryPickResult{
CherryPickedFiles: mergeResult.FilesPickedEntry1,
ConflictOpenFiles: resolver.ConflictOpenFiles,
ConflictResolvedFiles: resolver.ConflictResolvedFiles,
BinaryConflictFiles: resolver.BinaryConflictFiles,
NonFileConflictFiles: resolver.NonFileConflictFiles,
}
hasConflict := len(cpResult.ConflictOpenFiles) > 0 || len(cpResult.BinaryConflictFiles) > 0 || len(cpResult.NonFileConflictFiles) > 0
if abortOnConflict && hasConflict {
return cpResult, fetchDebugInfo, blobFetchDebugInfo, nil, errors.New("conflict detected")
}
commit := &object.Commit{
Message: commitMessage,
Author: author,
Committer: committer,
TreeHash: mergeResult.TreeHash,
ParentHashes: []plumbing.Hash{commitHashCherryPickTo},
}
obj := storage.NewEncodedObject()
if err := commit.Encode(obj); err != nil {
return cpResult, fetchDebugInfo, blobFetchDebugInfo, nil, fmt.Errorf("failed to create a commit: %v", err)
}
commitHash, err := storage.SetEncodedObject(obj)
if err != nil {
return cpResult, fetchDebugInfo, blobFetchDebugInfo, nil, fmt.Errorf("failed to create a commit: %v", err)
}
cpResult.CommitHash = commitHash
newHashes := []plumbing.Hash{commitHash}
newHashes = append(newHashes, mergeResult.NewHashes...)
newHashes = append(newHashes, resolver.NewHashes...)
var buf bytes.Buffer
packEncoder := packfile.NewEncoder(&buf, storage, false)
if _, err := packEncoder.Encode(newHashes, 0); err != nil {
return cpResult, fetchDebugInfo, blobFetchDebugInfo, nil, fmt.Errorf("failed to create a packfile: %v", err)
}
var destRef plumbing.ReferenceName
if hasConflict && conflictRef != nil {
destRef = *conflictRef
} else {
destRef = ref
}
pushDebugInfo, err := push.Push(repoURL, client, &buf, []push.RefUpdate{
{
Name: destRef,
OldHash: currentRefhash,
NewHash: commitHash,
},
})
if err != nil {
return cpResult, fetchDebugInfo, blobFetchDebugInfo, &pushDebugInfo, err
}
return cpResult, fetchDebugInfo, blobFetchDebugInfo, &pushDebugInfo, nil
}
type conflictBlobCollector struct {
blobHashes []plumbing.Hash
}
func (c *conflictBlobCollector) Resolve(parentPath string, entry1, entry2, entryBase *object.TreeEntry) ([]object.TreeEntry, error) {
if entry1 != nil && entry1.Mode.IsFile() && entry2 != nil && entry2.Mode.IsFile() && entryBase != nil && entryBase.Mode.IsFile() {
c.blobHashes = append(c.blobHashes, entry1.Hash, entry2.Hash, entryBase.Hash)
}
return nil, nil
}
func getTreeFromCommit(storage *memory.Storage, commitHash plumbing.Hash) (*object.Tree, error) {
commit, err := object.GetCommit(storage, commitHash)
if err != nil {
return nil, fmt.Errorf("cannot find %q in the fetched packfile: %v", commitHash.String(), err)
}
tree, err := commit.Tree()
if err != nil {
return nil, fmt.Errorf("cannot find the tree of %q in the fetched packfile: %v", commitHash.String(), err)
}
return tree, nil
}