|
1 | 1 | package git |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "os" |
5 | 6 |
|
6 | 7 | git "github.com/go-git/go-git/v5" |
| 8 | + "github.com/go-git/go-git/v5/plumbing" |
7 | 9 | "github.com/go-git/go-git/v5/plumbing/transport" |
8 | 10 | "github.com/go-git/go-git/v5/plumbing/transport/http" |
9 | 11 | ) |
10 | 12 |
|
11 | 13 | // Config for the git repository |
12 | 14 | type Config struct { |
13 | | - Username string `env:"GIT_USERNAME" env-default:"kite-bot" env-description:"Git username"` |
14 | | - Email string `env:"GIT_EMAIL" env-default:"[email protected]" env-description:"Git user email"` |
15 | | - Token string `env:"GIT_TOKEN" env-description:"Git access token"` |
16 | | - URL string `env:"GIT_URL" env-default:"https://github.com/openware/versions.git" env-description:"Git repository url"` |
17 | | - Branch string `env:"DRONE_BRANCH" env-default:"2-6-stable" env-description:"drone target branch"` |
18 | | - Repo string `env:"DRONE_REPO_NAME" env-description:"component repo name"` |
| 15 | + Username string `env:"GIT_USERNAME" env-default:"kite-bot" env-description:"Git username"` |
| 16 | + Email string `env:"GIT_EMAIL" env-default:"[email protected]" env-description:"Git user email"` |
| 17 | + Token string `env:"GIT_TOKEN" env-description:"Git access token"` |
| 18 | + URL string `env:"GIT_URL" env-default:"https://github.com/openware/versions.git" env-description:"Git repository url"` |
| 19 | + CloneBranch string `env:"CLONE_BRANCH" env-description:"target clone branch"` |
| 20 | + Branch string `env:"DRONE_BRANCH" env-default:"2-6-stable" env-description:"drone target branch"` |
| 21 | + Repo string `env:"DRONE_REPO_NAME" env-description:"component repo name"` |
19 | 22 | } |
20 | 23 |
|
21 | 24 | // Auth to describe auth method |
@@ -45,11 +48,18 @@ func (a *AuthToken) Method() transport.AuthMethod { |
45 | 48 |
|
46 | 49 | // Clone repository with config |
47 | 50 | func Clone(cnf *Config, auth Auth, outDir string) (*git.Repository, error) { |
48 | | - repo, err := git.PlainClone(outDir, false, &git.CloneOptions{ |
| 51 | + cloneOptions := &git.CloneOptions{ |
49 | 52 | Auth: auth.Method(), |
50 | 53 | URL: cnf.URL, |
51 | 54 | Progress: os.Stdout, |
52 | | - }) |
| 55 | + } |
| 56 | + |
| 57 | + if cnf.CloneBranch != "" { |
| 58 | + cloneOptions.ReferenceName = plumbing.NewBranchReferenceName(cnf.CloneBranch) |
| 59 | + fmt.Printf("Cloning %s branch\n", cnf.CloneBranch) |
| 60 | + } |
| 61 | + |
| 62 | + repo, err := git.PlainClone(outDir, false, cloneOptions) |
53 | 63 | if err != nil { |
54 | 64 | return nil, err |
55 | 65 | } |
|
0 commit comments