/
add.go
57 lines (49 loc) · 1.48 KB
/
add.go
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
package cmd
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/go-git/go-git/v5"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var addCmd = &cobra.Command{
Use: "add",
Short: "Add a new repository with a local clone",
Long: `This command adds a repository by making a new shallow clone of the repository.`,
Args: cobra.RangeArgs(1, 2),
Run: runAdd,
}
func runAdd(cmd *cobra.Command, args []string) {
repoDir := viper.GetString("repo_dir")
url := args[0]
var targetDir string
if len(args) == 2 {
targetDir = args[1]
} else {
targetDir = extractHumanishRepoName(url)
}
path := filepath.Join(repoDir, targetDir)
_, err := git.PlainClone(path, false, &git.CloneOptions{
URL: url,
Depth: 1,
NoCheckout: true,
SingleBranch: false,
})
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: failed to clone repository from %s as %s: %v\n", url, targetDir, err)
os.Exit(1)
}
fmt.Printf("Added repository for monitoring from %s as %s.\n", url, targetDir)
}
func extractHumanishRepoName(url string) string {
// based on https://github.com/git/git/blob/90bbd502d54fe920356fa9278055dc9c9bfe9a56/contrib/examples/git-clone.sh#L231-L232
targetDir := url // just to get started
targetDir = strings.TrimSpace(targetDir)
targetDir = strings.TrimSuffix(targetDir, "/")
targetDir = regexp.MustCompile(`:*/*\.git`).ReplaceAllString(targetDir, "")
targetDir = regexp.MustCompile(`.*[/:]`).ReplaceAllString(targetDir, "")
return targetDir
}