轨道 · 写下来的字
go-git
clone
func Clone(ctx *gin.Context) {
// Clones the repository into the given dir, just as a normal git clone does
url := "https://example.com/team/repo.git"
dir := "/tmp/git-demo"
username := "git-user"
password := os.Getenv("GIT_TOKEN") // 记得 import "os",不要把凭据写进代码
buf := bytes.NewBufferString("")
r, err := git.PlainClone(dir, false, &git.CloneOptions{
URL: url,
Auth: &githttp.BasicAuth{
Username: username,
Password: password,
},
Progress: buf,
})
// fmt.Println(buf)
if err != nil {
log.Println(err)
ctx.String(http.StatusInternalServerError, err.Error())
return
}
// Print the latest commit that was just pulled
ref, err := r.Head()
if err != nil {
log.Println(err)
ctx.String(http.StatusInternalServerError, err.Error())
ctx.AbortWithError(http.StatusInternalServerError, err)
}
commit, err := r.CommitObject(ref.Hash())
if err != nil {
log.Println(err)
ctx.String(http.StatusInternalServerError, err.Error())
return
}
fmt.Println("last commit", commit)
ctx.String(http.StatusOK, buf.String())
}← 回到文章