Clone pulls the repository or clones it into the local path
(ctx context.Context, options CloneOptions)
| 43 | |
| 44 | // Clone pulls the repository or clones it into the local path |
| 45 | func (gr *GitCLIRepository) Clone(ctx context.Context, options CloneOptions) error { |
| 46 | // Check if repo already exists |
| 47 | _, err := os.Stat(gr.LocalPath + "/.git") |
| 48 | if err != nil { |
| 49 | // Create local path |
| 50 | err := os.MkdirAll(gr.LocalPath, 0755) |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | |
| 55 | args := []string{"clone", options.URL, gr.LocalPath} |
| 56 | if options.Branch != "" { |
| 57 | args = append(args, "--branch", options.Branch) |
| 58 | } else if options.Tag != "" { |
| 59 | args = append(args, "--branch", options.Tag) |
| 60 | } |
| 61 | |
| 62 | // Do a shallow clone by default |
| 63 | if options.Commit == "" && !options.DisableShallow { |
| 64 | args = append(args, "--depth", "1") |
| 65 | } |
| 66 | |
| 67 | args = append(args, options.Args...) |
| 68 | // Below envvar are required to prevent git from prompting for user login or ssh |
| 69 | gitEnv := []string{ |
| 70 | "GIT_TERMINAL_PROMPT=0", |
| 71 | "GIT_SSH_COMMAND=ssh -oBatchMode=yes", |
| 72 | } |
| 73 | gitEnv = append(gitEnv, os.Environ()...) |
| 74 | out, err := command.CombinedOutput(ctx, gr.LocalPath, expand.ListEnviron(gitEnv...), "git", args...) |
| 75 | if err != nil { |
| 76 | return errors.Errorf("Error running 'git %s': %v -> %s", strings.Join(args, " "), err, string(out)) |
| 77 | } |
| 78 | |
| 79 | // Checkout the commit if necessary |
| 80 | if options.Commit != "" { |
| 81 | out, err := command.CombinedOutput(ctx, gr.LocalPath, expand.ListEnviron(os.Environ()...), "git", "-C", gr.LocalPath, "checkout", options.Commit) |
| 82 | if err != nil { |
| 83 | return errors.Errorf("Error running 'git checkout %s': %v -> %s", options.Commit, err, string(out)) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | func (gr *GitCLIRepository) Pull(ctx context.Context) error { |
| 94 | out, err := command.CombinedOutput(ctx, gr.LocalPath, expand.ListEnviron(os.Environ()...), "git", "-C", gr.LocalPath, "pull") |