简体   繁体   中英

Unable to git clone using go language

I'm trying to clone the git/bitbucket repository using the below go-lang code snippet, but it's not working, I can't see any errors either.

dir, err := ioutil.TempDir("", "clone-example")
if err != nil {
    log.Fatal(err)
}

defer os.RemoveAll(dir) // clean up

// Clones the repository into the given dir, just as a normal git clone does
_, err = git.PlainClone(dir, false, &git.CloneOptions{
    URL: "<https://git repository url***>",
    Auth: &http.BasicAuth{
        Username: "*****",
        Password: "***",
    },
})
fmt.Println(err)

if err != nil {
    log.Fatal(err)
}

The code works, It just deletes the folder right after the function ends (Also beware that the cloned project goes to /tmp/<project-name> )

comment this line to prevent it.

 //defer os.RemoveAll(dir) // clean up

If you don't want to deal with disk cleanup (which gives the illusion the clone has failed, since the cloned folder is deleted), you can do an in-memory clone, as in this go-git example :

// Clones the given repository in memory, creating the remote, the local
// branches and fetching the objects, exactly as:
Info("git clone https://github.com/src-d/go-siva")

r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
    URL: "https://github.com/src-d/go-siva",
})

CheckIfError(err)

// Gets the HEAD history from HEAD, just like does:
Info("git log")

// ... retrieves the branch pointed by HEAD
ref, err := r.Head()
CheckIfError(err)


// ... retrieves the commit history
cIter, err := r.Log(&git.LogOptions{From: ref.Hash()})
CheckIfError(err)

// ... just iterates over the commits, printing it
err = cIter.ForEach(func(c *object.Commit) error {
    fmt.Println(c)
    return nil
})
CheckIfError(err)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM