/
clone.go
81 lines (76 loc) · 1.97 KB
/
clone.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
76
77
78
79
80
81
//Clone a repository into a different directory that does not yet exist.
package main
import (
"errors"
"flag"
"fmt"
"github.com/tomheng/gogit/git"
"github.com/tomheng/gogit/internal/file"
)
var cloneFlag = flag.NewFlagSet("clone", flag.ExitOnError)
func newCloneCmd() *Command {
return &Command{
Run: runClone,
UsageLine: "clone specific git repo",
Flag: *cloneFlag,
}
}
//git clone on addr
func runClone(cmd *Command, args []string) (err error) {
if len(args) < 1 {
return errors.New("args not enogh")
}
dir := ""
if len(args) > 1 {
dir = args[1]
}
repo, err := git.NewRepo(args[0], dir)
if err != nil {
return
}
defer repo.Distruct()
if file.IsExist(repo.ClonePath) {
err = errors.New("fatal: destination path '" + repo.ClonePath + "' already exists and is not an empty directory.")
return
}
file.MakeDir(repo.ClonePath)
//Todo:may be we should Chdir
repoFile, err := repo.GetTmpPackFile()
if err != nil {
return
}
defer func() {
repoFile.Close()
//os.Remove(tmpPackFilePath)
}()
fmt.Printf("Cloning into '%s'...\n", repo.Name)
err = repo.FetchPack(func(dataType byte, data []byte) {
/*
1 the remainder of the packet line is a chunk of the pack file - this is the payload channel
2 this is progress information that the server sends - the client prints that on STDERR prefixed with remote: "
3 this is error infomration that will cause the client to print out the message on STDERR and exit with an error code (not implemented in our example)
*/
switch dataType {
case git.DATA_FRAME:
_, err := repoFile.Write(data)
if err != nil {
panic(err)
//returnData <- err
}
case git.PROGRESS_FRAME:
progress := string(data)
fmt.Print("\r", "remote: "+progress)
case git.ERROR_FRAME: //had convert to error value in receiveWithSideband
fmt.Println(string(data))
default:
}
}, (*repo).CreateLocalRefs)
if err != nil {
return
}
err = repo.SaveLooseObjects(repoFile)
if err != nil {
return
}
return
}