This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (65 loc) · 1.91 KB
/
main.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"flag"
"fmt"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/zapatacomputing/git-import/ssh"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
)
func main() {
url := flag.String("url", "", "the url of the git repository to clone")
dir := flag.String("dir", "", "the location to put the cloned repository")
branch := flag.String("branch", "", "the repository branch to clone")
tag := flag.String("tag", "", "the tag to clone")
flag.Parse()
err := Clone(*url, *dir, *branch, *tag)
if err != nil {
log.WithFields(log.Fields{
"url": *url,
"dir": *dir,
"branch": *branch,
"tag": *tag,
"error": err,
}).Error("Error cloning git repository")
os.Exit(1)
}
}
// Clone clones the git repository at the specified url to the given location
// Using a 1-commit clone of the given branch
func Clone(url string, dir string, branch string, tag string) error {
err1 := ssh.Check(url)
if err1 != nil {
return fmt.Errorf("git-import: unable to import from [%s] due to error : %w", url, err1)
}
ctx, cancel := context.WithTimeout(context.TODO(), 300*time.Second)
defer cancel()
if tag == "" && branch == "" {
return fmt.Errorf("Please specify either a branch or a tag.")
}
if tag != "" && branch != "" {
return fmt.Errorf("Please specify only the branch or only the tag.")
}
var referenceName plumbing.ReferenceName
if branch != "" {
referenceName = plumbing.NewBranchReferenceName(branch)
}
// Tags take precedence over branches so even if a branch was previously specified, we override the reference name with a tag
if tag != "" {
referenceName = plumbing.NewTagReferenceName(tag)
}
_, err := git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{
URL: url,
Depth: 1,
Progress: os.Stdout,
SingleBranch: true,
ReferenceName: referenceName,
})
if err != nil {
return err
}
return nil
}