<commit_before>package git

import (
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/url"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"syscall"
	"time"

	"path/filepath"

	"github.com/ovh/cds/sdk/vcs"
)

var (
	verbose bool
	//LogFunc can be overrided
	LogFunc = log.Printf
)

func init() {
	if os.Getenv("CDS_VERBOSE") == "true" {
		verbose = true
	}
}

// AuthOpts is a optional structs for git command
type AuthOpts struct {
	Username   string
	Password   string
	PrivateKey vcs.SSHKey
}

// CloneOpts is a optional structs for git clone command
type CloneOpts struct {
	Depth                   int
	SingleBranch            bool
	Branch                  string
	Recursive               bool
	Verbose                 bool
	Quiet                   bool
	CheckoutCommit          string
	NoStrictHostKeyChecking bool
}

// OutputOpts is a optional structs for git clone command
type OutputOpts struct {
	Stdout io.Writer
	Stderr io.Writer
}

// Clone make a git clone
func Clone(repo string, path string, auth *AuthOpts, opts *CloneOpts, output *OutputOpts) error {
	if verbose {
		t1 := time.Now()
		defer LogFunc("Git clone %s (%v s)\n", path, int(time.Since(t1).Seconds()))
	}

	if strings.HasPrefix(repo, "http://") || strings.HasPrefix(repo, "ftp://") || strings.HasPrefix(repo, "ftps://") {
		return fmt.Errorf("Git protocol not supported")
	}

	if strings.HasPrefix(repo, "https://") {
		return gitCloneOverHTTPS(repo, path, auth, opts, output)
	}
	return gitCloneOverSSH(repo, path, auth, opts, output)
}

func gitCloneOverHTTPS(repo string, path string, auth *AuthOpts, opts *CloneOpts, output *OutputOpts) error {
	if auth == nil {
		cmd := gitCommand(repo, path, opts)
		return runCommand(cmd, output)<commit_msg>feat (worker): add log on git clone (#916)<commit_after>package git

import (
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/url"
	"os"
	"os/exec"
	"runtime"
	"strings"
	"syscall"
	"time"

	"path/filepath"

	"github.com/ovh/cds/sdk/vcs"
)

var (
	verbose bool
	//LogFunc can be overrided
	LogFunc = log.Printf
)

func init() {
	if os.Getenv("CDS_VERBOSE") == "true" {
		verbose = true
	}
}

// AuthOpts is a optional structs for git command
type AuthOpts struct {
	Username   string
	Password   string
	PrivateKey vcs.SSHKey
}

// CloneOpts is a optional structs for git clone command
type CloneOpts struct {
	Depth                   int
	SingleBranch            bool
	Branch                  string
	Recursive               bool
	Verbose                 bool
	Quiet                   bool
	CheckoutCommit          string
	NoStrictHostKeyChecking bool
}

// OutputOpts is a optional structs for git clone command
type OutputOpts struct {
	Stdout io.Writer
	Stderr io.Writer
}

// Clone make a git clone
func Clone(repo string, path string, auth *AuthOpts, opts *CloneOpts, output *OutputOpts) error {
	if verbose {
		t1 := time.Now()
		if opts != nil && opts.CheckoutCommit != "" {
			defer LogFunc("Checkout commit %s\n", opts.CheckoutCommit)
		}
		defer LogFunc("Git clone %s (%v s)\n", path, int(time.Since(t1).Seconds()))
	}

	if strings.HasPrefix(repo, "http://") || strings.HasPrefix(repo, "ftp://") || strings.HasPrefix(repo, "ftps://") {
		return fmt.Errorf("Git protocol not supported")
	}

	if strings.HasPrefix(repo, "https://") {
		return gitCloneOverHTTPS(repo, path, auth, opts, output)
	}
	return gitCloneOverSSH(repo, path, auth, opts, output)
}

func gitCloneOverHTTPS(repo string, path string, auth *AuthOpts, opts *CloneOpts, output *OutputOpts) error {
	if auth == nil {
		cmd := gitCommand(repo, path, opts)
		return runCommand(cmd, output)
{'fraction_non_alphanumeric': 0.11872017962391243, 'fraction_numerical': 0.0019646365422396855, 'mean_word_length': 3.0918484500574053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3903992', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1252, 'n_words': 357}