-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathclone.go
More file actions
71 lines (59 loc) · 1.51 KB
/
clone.go
File metadata and controls
71 lines (59 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"fmt"
"path"
"strings"
"github.com/go-git/go-git/v6"
"github.com/go-git/go-git/v6/plumbing/transport"
"github.com/spf13/cobra"
)
var (
cloneBare bool
cloneProgress bool
cloneDepth int
cloneTags bool
)
func init() {
cloneCmd.Flags().BoolVarP(&cloneBare, "bare", "", false, "Create a bare repository")
cloneCmd.Flags().BoolVarP(&cloneProgress, "progress", "", true, "Show clone progress")
cloneCmd.Flags().IntVarP(&cloneDepth, "depth", "", 0, "Create a shallow clone of that depth")
cloneCmd.Flags().BoolVarP(&cloneTags, "tags", "", false, "Clone tags")
rootCmd.AddCommand(cloneCmd)
rootCmd.CompletionOptions.HiddenDefaultCmd = true
}
var cloneCmd = &cobra.Command{
Use: "clone [<options>] [--] <repo> [<dir>]",
Short: "Clone a repository into a new directory",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
dir := path.Base(args[0])
if len(args) > 1 {
dir = args[1]
} else {
dir = strings.TrimSuffix(dir, ".git")
if cloneBare {
dir = dir + ".git"
}
}
ep, err := transport.NewEndpoint(args[0])
if err != nil {
return err
}
opts := git.CloneOptions{
URL: args[0],
Depth: cloneDepth,
Auth: defaultAuth(ep),
Bare: cloneBare,
}
if cloneTags {
opts.Tags = git.TagFollowing
}
if cloneProgress {
opts.Progress = cmd.OutOrStdout()
}
fmt.Fprintf(cmd.ErrOrStderr(), "Cloning into '%s'...\n", dir)
_, err = git.PlainClone(dir, &opts)
return err
},
DisableFlagsInUseLine: true,
}