Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions sqle/api/controller/v1/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,19 +477,12 @@ func CloneGitRepository(ctx context.Context, url, username, password, branch str
return os.RemoveAll(directory)
}

cloneOpts := &git.CloneOptions{
URL: url,
InsecureSkipTLS: true, // 跳过 SSL 证书验证,支持自签名证书
}
if branch != "" {
cloneOpts.ReferenceName = plumbing.ReferenceName(branch)
}

auth, err := getGitAuthMethod(url, username, password)
if err != nil {
return nil, "", cleanup, err
}
cloneOpts.Auth = auth

cloneOpts := newCloneOptions(url, branch, auth)

repository, err = git.PlainCloneContext(ctx, directory, false, cloneOpts)
if err != nil {
Expand All @@ -499,6 +492,23 @@ func CloneGitRepository(ctx context.Context, url, username, password, branch str
return repository, directory, cleanup, nil
}

const shallowCloneDepth = 1

func newCloneOptions(url, branch string, auth transport.AuthMethod) *git.CloneOptions {
cloneOpts := &git.CloneOptions{
URL: url,
Auth: auth,
Depth: shallowCloneDepth,
InsecureSkipTLS: true, // 跳过 SSL 证书验证,支持自签名证书
}
if branch != "" {
cloneOpts.ReferenceName = plumbing.ReferenceName(branch)
cloneOpts.SingleBranch = true
}

return cloneOpts
}

type TestGitConnectionReqV1 struct {
GitHttpUrl string `json:"git_http_url" form:"git_http_url" valid:"required"`
GitUserName string `json:"git_user_name" form:"git_user_name"`
Expand Down
45 changes: 45 additions & 0 deletions sqle/api/controller/v1/configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package v1

import (
"testing"
)

func TestNewCloneOptionsWithoutBranch(t *testing.T) {
cloneOpts := newCloneOptions("https://example.com/test.git", "", nil)

if cloneOpts.URL != "https://example.com/test.git" {
t.Fatalf("unexpected clone url: %s", cloneOpts.URL)
}
if cloneOpts.Depth != shallowCloneDepth {
t.Fatalf("unexpected clone depth: %d", cloneOpts.Depth)
}
if !cloneOpts.InsecureSkipTLS {
t.Fatal("expected InsecureSkipTLS to be true")
}
if cloneOpts.SingleBranch {
t.Fatal("expected SingleBranch to be false when branch is empty")
}
if cloneOpts.ReferenceName.String() != "" {
t.Fatalf("unexpected reference name: %s", cloneOpts.ReferenceName.String())
}
}

func TestNewCloneOptionsWithBranch(t *testing.T) {
cloneOpts := newCloneOptions("https://example.com/test.git", "refs/heads/main", nil)

if cloneOpts.URL != "https://example.com/test.git" {
t.Fatalf("unexpected clone url: %s", cloneOpts.URL)
}
if cloneOpts.Depth != shallowCloneDepth {
t.Fatalf("unexpected clone depth: %d", cloneOpts.Depth)
}
if !cloneOpts.InsecureSkipTLS {
t.Fatal("expected InsecureSkipTLS to be true")
}
if !cloneOpts.SingleBranch {
t.Fatal("expected SingleBranch to be true when branch is provided")
}
if cloneOpts.ReferenceName.String() != "refs/heads/main" {
t.Fatalf("unexpected reference name: %s", cloneOpts.ReferenceName.String())
}
}
Loading