mirror of https://github.com/harness/drone.git
fix merge-base in diff (#206)
parent
6705c7234d
commit
aafec0a9dc
|
@ -15,15 +15,17 @@ import (
|
|||
|
||||
type RawDiffParams struct {
|
||||
ReadParams
|
||||
LeftCommitID string
|
||||
RightCommitID string
|
||||
BaseRef string
|
||||
HeadRef string
|
||||
MergeBase bool
|
||||
}
|
||||
|
||||
func (c *Client) RawDiff(ctx context.Context, params *RawDiffParams, out io.Writer) error {
|
||||
diff, err := c.diffService.RawDiff(ctx, &rpc.RawDiffRequest{
|
||||
Base: mapToRPCReadRequest(params.ReadParams),
|
||||
LeftCommitId: params.LeftCommitID,
|
||||
RightCommitId: params.RightCommitID,
|
||||
Base: mapToRPCReadRequest(params.ReadParams),
|
||||
BaseRef: params.BaseRef,
|
||||
HeadRef: params.HeadRef,
|
||||
MergeBase: params.MergeBase,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright 2022 Harness Inc. All rights reserved.
|
||||
// Use of this source code is governed by the Polyform Free Trial License
|
||||
// that can be found in the LICENSE.md file for this repository.
|
||||
|
||||
package gitea
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
)
|
||||
|
||||
func (g Adapter) RawDiff(ctx context.Context, repoPath, left, right string, w io.Writer, args ...string) error {
|
||||
cmd := git.NewCommand(ctx, append([]string{"diff", "--src-prefix=\\a/",
|
||||
"--dst-prefix=\\b/", "-M", left, right}, args...)...)
|
||||
cmd.SetDescription(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath))
|
||||
errbuf := bytes.Buffer{}
|
||||
if err := cmd.Run(&git.RunOpts{
|
||||
Dir: repoPath,
|
||||
Stderr: &errbuf,
|
||||
Stdout: w,
|
||||
}); err != nil {
|
||||
return fmt.Errorf("git diff [%s base:%s head:%s]: %s", repoPath, left, right, errbuf.String())
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -229,7 +229,6 @@ func (g Adapter) Merge(
|
|||
func (g Adapter) GetDiffTree(ctx context.Context, repoPath, baseBranch, headBranch string) (string, error) {
|
||||
getDiffTreeFromBranch := func(repoPath, baseBranch, headBranch string) (string, error) {
|
||||
var outbuf, errbuf strings.Builder
|
||||
// Compute the diff-tree for sparse-checkout
|
||||
if err := git.NewCommand(ctx, "diff-tree", "--no-commit-id",
|
||||
"--name-only", "-r", "-z", "--root", baseBranch, headBranch, "--").
|
||||
Run(&git.RunOpts{
|
||||
|
|
|
@ -5,16 +5,9 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/harness/gitness/gitrpc/internal/streamio"
|
||||
"github.com/harness/gitness/gitrpc/internal/types"
|
||||
"github.com/harness/gitness/gitrpc/rpc"
|
||||
|
||||
"code.gitea.io/gitea/modules/git"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
)
|
||||
|
||||
type DiffService struct {
|
||||
|
@ -45,25 +38,25 @@ func (s DiffService) RawDiff(request *rpc.RawDiffRequest, stream rpc.DiffService
|
|||
|
||||
repoPath := getFullPathForRepo(s.reposRoot, base.GetRepoUid())
|
||||
|
||||
cmd := git.NewCommand(ctx, "diff", "--full-index", request.LeftCommitId, request.RightCommitId)
|
||||
cmd.SetDescription(fmt.Sprintf("GetDiffRange [repo_path: %s]", repoPath))
|
||||
return cmd.Run(&git.RunOpts{
|
||||
Timeout: time.Duration(setting.Git.Timeout.Default) * time.Second,
|
||||
Dir: repoPath,
|
||||
Stderr: os.Stderr,
|
||||
Stdout: sw,
|
||||
})
|
||||
args := []string{}
|
||||
if request.GetMergeBase() {
|
||||
args = []string{
|
||||
"--merge-base",
|
||||
}
|
||||
}
|
||||
|
||||
return s.adapter.RawDiff(ctx, repoPath, request.GetBaseRef(), request.GetHeadRef(), sw, args...)
|
||||
}
|
||||
|
||||
func validateDiffRequest(in *rpc.RawDiffRequest) error {
|
||||
if in.GetBase() == nil {
|
||||
return types.ErrBaseCannotBeEmpty
|
||||
}
|
||||
if in.GetLeftCommitId() == "" {
|
||||
return types.ErrEmptyLeftCommitID
|
||||
if in.GetBaseRef() == "" {
|
||||
return types.ErrEmptyBaseRef
|
||||
}
|
||||
if in.GetRightCommitId() == "" {
|
||||
return types.ErrEmptyRightCommitID
|
||||
if in.GetHeadRef() == "" {
|
||||
return types.ErrEmptyHeadRef
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -44,4 +44,5 @@ type GitAdapter interface {
|
|||
Merge(ctx context.Context, pr *types.PullRequest, mergeMethod string, trackingBranch string,
|
||||
tmpBasePath string, mergeMsg string, env []string) error
|
||||
GetDiffTree(ctx context.Context, repoPath, baseBranch, headBranch string) (string, error)
|
||||
RawDiff(ctx context.Context, repoPath, base, head string, w io.Writer, args ...string) error
|
||||
}
|
||||
|
|
|
@ -21,8 +21,8 @@ var (
|
|||
ErrHeaderCannotBeEmpty = errors.New("header field cannot be empty")
|
||||
ErrBaseCannotBeEmpty = errors.New("base field cannot be empty")
|
||||
ErrSHADoesNotMatch = errors.New("sha does not match")
|
||||
ErrEmptyLeftCommitID = errors.New("empty LeftCommitId")
|
||||
ErrEmptyRightCommitID = errors.New("empty RightCommitId")
|
||||
ErrEmptyBaseRef = errors.New("empty base reference")
|
||||
ErrEmptyHeadRef = errors.New("empty head reference")
|
||||
)
|
||||
|
||||
// MergeConflictsError represents an error if merging fails with a conflict.
|
||||
|
|
|
@ -12,9 +12,14 @@ service DiffService {
|
|||
}
|
||||
|
||||
message RawDiffRequest {
|
||||
ReadRequest base = 1;
|
||||
string left_commit_id = 2;
|
||||
string right_commit_id = 3;
|
||||
ReadRequest base = 1;
|
||||
// base_ref is left side of compare and can be branch, commit and tag
|
||||
string base_ref = 2;
|
||||
// head_ref is right side of compare and can be branch, commit and tag
|
||||
string head_ref = 3;
|
||||
// merge_base used only in branch comparison, if merge_base is true
|
||||
// it will show diff from the commit where branch is created and head branch
|
||||
bool merge_base = 4;
|
||||
}
|
||||
|
||||
message RawDiffResponse {
|
||||
|
|
|
@ -25,9 +25,14 @@ type RawDiffRequest struct {
|
|||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Base *ReadRequest `protobuf:"bytes,1,opt,name=base,proto3" json:"base,omitempty"`
|
||||
LeftCommitId string `protobuf:"bytes,2,opt,name=left_commit_id,json=leftCommitId,proto3" json:"left_commit_id,omitempty"`
|
||||
RightCommitId string `protobuf:"bytes,3,opt,name=right_commit_id,json=rightCommitId,proto3" json:"right_commit_id,omitempty"`
|
||||
Base *ReadRequest `protobuf:"bytes,1,opt,name=base,proto3" json:"base,omitempty"`
|
||||
// base_ref is left side of compare and can be branch, commit and tag
|
||||
BaseRef string `protobuf:"bytes,2,opt,name=base_ref,json=baseRef,proto3" json:"base_ref,omitempty"`
|
||||
// head_ref is right side of compare and can be branch, commit and tag
|
||||
HeadRef string `protobuf:"bytes,3,opt,name=head_ref,json=headRef,proto3" json:"head_ref,omitempty"`
|
||||
// merge_base used only in branch comparison, if merge_base is true
|
||||
// it will show diff from the commit where branch is created and head branch
|
||||
MergeBase bool `protobuf:"varint,4,opt,name=merge_base,json=mergeBase,proto3" json:"merge_base,omitempty"`
|
||||
}
|
||||
|
||||
func (x *RawDiffRequest) Reset() {
|
||||
|
@ -69,20 +74,27 @@ func (x *RawDiffRequest) GetBase() *ReadRequest {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (x *RawDiffRequest) GetLeftCommitId() string {
|
||||
func (x *RawDiffRequest) GetBaseRef() string {
|
||||
if x != nil {
|
||||
return x.LeftCommitId
|
||||
return x.BaseRef
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RawDiffRequest) GetRightCommitId() string {
|
||||
func (x *RawDiffRequest) GetHeadRef() string {
|
||||
if x != nil {
|
||||
return x.RightCommitId
|
||||
return x.HeadRef
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *RawDiffRequest) GetMergeBase() bool {
|
||||
if x != nil {
|
||||
return x.MergeBase
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type RawDiffResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
|
@ -135,25 +147,25 @@ var File_diff_proto protoreflect.FileDescriptor
|
|||
var file_diff_proto_rawDesc = []byte{
|
||||
0x0a, 0x0a, 0x64, 0x69, 0x66, 0x66, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x03, 0x72, 0x70,
|
||||
0x63, 0x1a, 0x0c, 0x73, 0x68, 0x61, 0x72, 0x65, 0x64, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22,
|
||||
0x84, 0x01, 0x0a, 0x0e, 0x52, 0x61, 0x77, 0x44, 0x69, 0x66, 0x66, 0x52, 0x65, 0x71, 0x75, 0x65,
|
||||
0x8b, 0x01, 0x0a, 0x0e, 0x52, 0x61, 0x77, 0x44, 0x69, 0x66, 0x66, 0x52, 0x65, 0x71, 0x75, 0x65,
|
||||
0x73, 0x74, 0x12, 0x24, 0x0a, 0x04, 0x62, 0x61, 0x73, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b,
|
||||
0x32, 0x10, 0x2e, 0x72, 0x70, 0x63, 0x2e, 0x52, 0x65, 0x61, 0x64, 0x52, 0x65, 0x71, 0x75, 0x65,
|
||||
0x73, 0x74, 0x52, 0x04, 0x62, 0x61, 0x73, 0x65, 0x12, 0x24, 0x0a, 0x0e, 0x6c, 0x65, 0x66, 0x74,
|
||||
0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x5f, 0x69, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x0c, 0x6c, 0x65, 0x66, 0x74, 0x43, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x49, 0x64, 0x12, 0x26,
|
||||
0x0a, 0x0f, 0x72, 0x69, 0x67, 0x68, 0x74, 0x5f, 0x63, 0x6f, 0x6d, 0x6d, 0x69, 0x74, 0x5f, 0x69,
|
||||
0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0d, 0x72, 0x69, 0x67, 0x68, 0x74, 0x43, 0x6f,
|
||||
0x6d, 0x6d, 0x69, 0x74, 0x49, 0x64, 0x22, 0x25, 0x0a, 0x0f, 0x52, 0x61, 0x77, 0x44, 0x69, 0x66,
|
||||
0x66, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74,
|
||||
0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x32, 0x47, 0x0a,
|
||||
0x0b, 0x44, 0x69, 0x66, 0x66, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x38, 0x0a, 0x07,
|
||||
0x52, 0x61, 0x77, 0x44, 0x69, 0x66, 0x66, 0x12, 0x13, 0x2e, 0x72, 0x70, 0x63, 0x2e, 0x52, 0x61,
|
||||
0x77, 0x44, 0x69, 0x66, 0x66, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x72,
|
||||
0x70, 0x63, 0x2e, 0x52, 0x61, 0x77, 0x44, 0x69, 0x66, 0x66, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e,
|
||||
0x73, 0x65, 0x22, 0x00, 0x30, 0x01, 0x42, 0x27, 0x5a, 0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62,
|
||||
0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x72, 0x6e, 0x65, 0x73, 0x73, 0x2f, 0x67, 0x69, 0x74,
|
||||
0x6e, 0x65, 0x73, 0x73, 0x2f, 0x67, 0x69, 0x74, 0x72, 0x70, 0x63, 0x2f, 0x72, 0x70, 0x63, 0x62,
|
||||
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x73, 0x74, 0x52, 0x04, 0x62, 0x61, 0x73, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x62, 0x61, 0x73, 0x65,
|
||||
0x5f, 0x72, 0x65, 0x66, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x62, 0x61, 0x73, 0x65,
|
||||
0x52, 0x65, 0x66, 0x12, 0x19, 0x0a, 0x08, 0x68, 0x65, 0x61, 0x64, 0x5f, 0x72, 0x65, 0x66, 0x18,
|
||||
0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x68, 0x65, 0x61, 0x64, 0x52, 0x65, 0x66, 0x12, 0x1d,
|
||||
0x0a, 0x0a, 0x6d, 0x65, 0x72, 0x67, 0x65, 0x5f, 0x62, 0x61, 0x73, 0x65, 0x18, 0x04, 0x20, 0x01,
|
||||
0x28, 0x08, 0x52, 0x09, 0x6d, 0x65, 0x72, 0x67, 0x65, 0x42, 0x61, 0x73, 0x65, 0x22, 0x25, 0x0a,
|
||||
0x0f, 0x52, 0x61, 0x77, 0x44, 0x69, 0x66, 0x66, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65,
|
||||
0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x04,
|
||||
0x64, 0x61, 0x74, 0x61, 0x32, 0x47, 0x0a, 0x0b, 0x44, 0x69, 0x66, 0x66, 0x53, 0x65, 0x72, 0x76,
|
||||
0x69, 0x63, 0x65, 0x12, 0x38, 0x0a, 0x07, 0x52, 0x61, 0x77, 0x44, 0x69, 0x66, 0x66, 0x12, 0x13,
|
||||
0x2e, 0x72, 0x70, 0x63, 0x2e, 0x52, 0x61, 0x77, 0x44, 0x69, 0x66, 0x66, 0x52, 0x65, 0x71, 0x75,
|
||||
0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x72, 0x70, 0x63, 0x2e, 0x52, 0x61, 0x77, 0x44, 0x69, 0x66,
|
||||
0x66, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x30, 0x01, 0x42, 0x27, 0x5a,
|
||||
0x25, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x72, 0x6e,
|
||||
0x65, 0x73, 0x73, 0x2f, 0x67, 0x69, 0x74, 0x6e, 0x65, 0x73, 0x73, 0x2f, 0x67, 0x69, 0x74, 0x72,
|
||||
0x70, 0x63, 0x2f, 0x72, 0x70, 0x63, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.28.1
|
||||
// protoc v3.21.11
|
||||
// protoc v3.21.9
|
||||
// source: repo.proto
|
||||
|
||||
package rpc
|
||||
|
@ -130,6 +130,7 @@ type CreateRepositoryRequest struct {
|
|||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
// Types that are assignable to Data:
|
||||
//
|
||||
// *CreateRepositoryRequest_Header
|
||||
// *CreateRepositoryRequest_File
|
||||
Data isCreateRepositoryRequest_Data `protobuf_oneof:"data"`
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.2.0
|
||||
// - protoc v3.21.11
|
||||
// - protoc v3.21.9
|
||||
// source: repo.proto
|
||||
|
||||
package rpc
|
||||
|
|
|
@ -34,15 +34,17 @@ func (c *Controller) RawDiff(
|
|||
info := parseDiffPath(path)
|
||||
|
||||
return c.gitRPCClient.RawDiff(ctx, &gitrpc.RawDiffParams{
|
||||
ReadParams: CreateRPCReadParams(repo),
|
||||
LeftCommitID: info.Left,
|
||||
RightCommitID: info.Right,
|
||||
ReadParams: CreateRPCReadParams(repo),
|
||||
BaseRef: info.BaseRef,
|
||||
HeadRef: info.HeadRef,
|
||||
MergeBase: info.MergeBase,
|
||||
}, w)
|
||||
}
|
||||
|
||||
type CompareInfo struct {
|
||||
Left string
|
||||
Right string
|
||||
BaseRef string
|
||||
HeadRef string
|
||||
MergeBase bool
|
||||
}
|
||||
|
||||
func parseDiffPath(path string) CompareInfo {
|
||||
|
@ -51,10 +53,13 @@ func parseDiffPath(path string) CompareInfo {
|
|||
infos = strings.SplitN(path, "..", 2)
|
||||
}
|
||||
if len(infos) != 2 {
|
||||
return CompareInfo{}
|
||||
return CompareInfo{
|
||||
HeadRef: path,
|
||||
}
|
||||
}
|
||||
return CompareInfo{
|
||||
Left: infos[0],
|
||||
Right: infos[1],
|
||||
BaseRef: infos[0],
|
||||
HeadRef: infos[1],
|
||||
MergeBase: strings.Contains(path, "..."),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue