Skip to content

Commit

Permalink
Add blobFetchDebugInfo (#16)
Browse files Browse the repository at this point in the history
Fixes #15
  • Loading branch information
draftcode authored Aug 26, 2024
1 parent 7772027 commit 586b53b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
26 changes: 14 additions & 12 deletions cmd/squash_cherry_pick.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var squashCherryPick = &cobra.Command{
r := plumbing.ReferenceName(squashCherryPickArgs.conflictRef)
conflictRef = &r
}
result, fetchDebugInfo, pushDebugInfo, pushErr := nichegit.PushSquashCherryPick(
result, fetchDebugInfo, blobFetchDebugInfo, pushDebugInfo, pushErr := nichegit.PushSquashCherryPick(
squashCherryPickArgs.repoURL,
client,
plumbing.NewHash(squashCherryPickArgs.cherryPickFrom),
Expand All @@ -74,8 +74,9 @@ var squashCherryPick = &cobra.Command{
squashCherryPickArgs.abortOnConflict,
)
output := squashCherryPickOutput{
FetchDebugInfo: fetchDebugInfo,
PushDebugInfo: pushDebugInfo,
FetchDebugInfo: fetchDebugInfo,
BlobFetchDebugInfo: blobFetchDebugInfo,
PushDebugInfo: pushDebugInfo,
}
if result != nil {
output.CommitHash = result.CommitHash.String()
Expand Down Expand Up @@ -129,15 +130,16 @@ func newSignature(name, email, timestamp string) (object.Signature, error) {
}

type squashCherryPickOutput struct {
CommitHash string `json:"commitHash"`
CherryPickedFiles []string `json:"cherryPickedFiles"`
ConflictOpenFiles []string `json:"conflictOpenFiles"`
ConflictResolvedFiles []string `json:"conflictResolvedFiles"`
BinaryConflictFiles []string `json:"binaryConflictFiles"`
NonFileConflictFiles []string `json:"nonFileConflictFiles"`
FetchDebugInfo debug.FetchDebugInfo `json:"fetchDebugInfo"`
PushDebugInfo *debug.PushDebugInfo `json:"pushDebugInfo"`
Error string `json:"error,omitempty"`
CommitHash string `json:"commitHash"`
CherryPickedFiles []string `json:"cherryPickedFiles"`
ConflictOpenFiles []string `json:"conflictOpenFiles"`
ConflictResolvedFiles []string `json:"conflictResolvedFiles"`
BinaryConflictFiles []string `json:"binaryConflictFiles"`
NonFileConflictFiles []string `json:"nonFileConflictFiles"`
FetchDebugInfo debug.FetchDebugInfo `json:"fetchDebugInfo"`
BlobFetchDebugInfo *debug.FetchDebugInfo `json:"blobFetchDebugInfo"`
PushDebugInfo *debug.PushDebugInfo `json:"pushDebugInfo"`
Error string `json:"error,omitempty"`
}

func init() {
Expand Down
40 changes: 20 additions & 20 deletions squash_cherry_pick.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,61 +41,61 @@ func PushSquashCherryPick(
conflictRef *plumbing.ReferenceName,
currentRefhash *plumbing.Hash,
abortOnConflict bool,
) (*PushSquashCherryPickResult, debug.FetchDebugInfo, *debug.PushDebugInfo, error) {
) (*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, err
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, fmt.Errorf("failed to parse packfile: %v", err)
return nil, fetchDebugInfo, nil, nil, fmt.Errorf("failed to parse packfile: %v", err)
}
if _, err := parser.Parse(); err != nil {
return nil, fetchDebugInfo, nil, fmt.Errorf("failed to parse packfile: %v", err)
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, err
return nil, fetchDebugInfo, nil, nil, err
}
treeCPBase, err := getTreeFromCommit(storage, commitHashCherryPickBase)
if err != nil {
return nil, fetchDebugInfo, nil, err
return nil, fetchDebugInfo, nil, nil, err
}
treeCPTo, err := getTreeFromCommit(storage, commitHashCherryPickTo)
if err != nil {
return nil, fetchDebugInfo, nil, err
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, fmt.Errorf("failed to merge the trees: %v", err)
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)
// TODO: return debug info
_ = fetchBlobDebugInfo
blobFetchDebugInfo = &fetchBlobDebugInfo
if err != nil {
return nil, fetchDebugInfo, nil, err
return nil, fetchDebugInfo, blobFetchDebugInfo, nil, err
}
parser, err := packfile.NewParserWithStorage(packfile.NewScanner(bytes.NewReader(packfilebs)), storage)
if err != nil {
return nil, fetchDebugInfo, nil, fmt.Errorf("failed to parse packfile: %v", err)
return nil, fetchDebugInfo, blobFetchDebugInfo, nil, fmt.Errorf("failed to parse packfile: %v", err)
}
if _, err := parser.Parse(); err != nil {
return nil, fetchDebugInfo, nil, fmt.Errorf("failed to parse packfile: %v", err)
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, nil, fmt.Errorf("failed to merge the trees: %v", err)
return nil, fetchDebugInfo, blobFetchDebugInfo, nil, fmt.Errorf("failed to merge the trees: %v", err)
}
}

Expand All @@ -108,7 +108,7 @@ func PushSquashCherryPick(
}
hasConflict := len(cpResult.ConflictOpenFiles) > 0 || len(cpResult.BinaryConflictFiles) > 0 || len(cpResult.NonFileConflictFiles) > 0
if abortOnConflict && hasConflict {
return cpResult, fetchDebugInfo, nil, errors.New("conflict detected")
return cpResult, fetchDebugInfo, blobFetchDebugInfo, nil, errors.New("conflict detected")
}
commit := &object.Commit{
Message: commitMessage,
Expand All @@ -119,11 +119,11 @@ func PushSquashCherryPick(
}
obj := storage.NewEncodedObject()
if err := commit.Encode(obj); err != nil {
return cpResult, fetchDebugInfo, nil, fmt.Errorf("failed to create a commit: %v", err)
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, nil, fmt.Errorf("failed to create a commit: %v", err)
return cpResult, fetchDebugInfo, blobFetchDebugInfo, nil, fmt.Errorf("failed to create a commit: %v", err)
}
cpResult.CommitHash = commitHash

Expand All @@ -134,7 +134,7 @@ func PushSquashCherryPick(
var buf bytes.Buffer
packEncoder := packfile.NewEncoder(&buf, storage, false)
if _, err := packEncoder.Encode(newHashes, 0); err != nil {
return cpResult, fetchDebugInfo, nil, fmt.Errorf("failed to create a packfile: %v", err)
return cpResult, fetchDebugInfo, blobFetchDebugInfo, nil, fmt.Errorf("failed to create a packfile: %v", err)
}

var destRef plumbing.ReferenceName
Expand All @@ -152,9 +152,9 @@ func PushSquashCherryPick(
},
})
if err != nil {
return cpResult, fetchDebugInfo, &pushDebugInfo, err
return cpResult, fetchDebugInfo, blobFetchDebugInfo, &pushDebugInfo, err
}
return cpResult, fetchDebugInfo, &pushDebugInfo, nil
return cpResult, fetchDebugInfo, blobFetchDebugInfo, &pushDebugInfo, nil
}

type conflictBlobCollector struct {
Expand Down

0 comments on commit 586b53b

Please sign in to comment.