目錄
準備
代碼獲取方式有兩種:
1. 通過go-git api獲取
2. 通過git client獲取
git clone https://github.com/src-d/go-git.git
clone
go-git clone
/**
go-git clone all code
workspace: 需要指定workspace,明確把代碼clone到哪裏
url: 要clone的repo url
referenceName: 可以是分支名稱,也可以是tag,也可以是commit id
auth: 這個是可選的,就要看你有沒有授權校驗
*/
func gitClone(workspace, url, referenceName string, auth ssh.AuthMethod) (*git.Repository, error) {
return git.PlainClone(workspace, false,
&git.CloneOptions{
URL: url,
ReferenceName: plumbing.ReferenceName(referenceName),
Auth: auth,
Progress: console.Stdout(), // 公司自己封裝的log庫
})
}
git clone
/**
使用git client進行clone
url: 指明要clone的項目
workspace: 指定項目要clone到哪個目錄
*/
func osExecClone(workspace, url string) error {
console.Println("Try again with git client clone")
cmd := exec.Command("git", "clone", url, workspace)
out, err := cmd.CombinedOutput()
console.Verbose(string(out))
return err
}
pull
go-git pull
func gitPull(referenceName string, auth ssh.AuthMethod, wt *git.Worktree) error {
return wt.Pull(&git.PullOptions{
Auth: auth,
ReferenceName: plumbing.ReferenceName(referenceName),
SingleBranch: true,
Progress: console.Stdout(),
})
}
git pull
// git client pull
func osExecPull(workspace, url, referenceName, refType string) error {
if refType == "branch" {
console.Verbose(fmt.Sprintf("git client pull code by branch: %s", referenceName))
} else if refType == "tag" {
console.Verbose(fmt.Sprintf("git client pull code by tag: %s", referenceName))
} else if refType == "commit" {
console.Verbose(fmt.Sprintf("git client pull code by commit id: %s", referenceName))
}
cmd := exec.Command("git", "pull", url, referenceName)
cmd.Dir = workspace
out, err := cmd.CombinedOutput()
console.Verbose("git client pull code . " + string(out))
return err
}
fetch
go-git fetch
func gitFetch(repo *git.Repository, refName string, auth ssh.AuthMethod) error {
console.Verbose("go-git fetch:" + refName)
return repo.Fetch(&git.FetchOptions{
RefSpecs: []config.RefSpec{"refs/*:refs/*", "HEAD:ref/heads/HEAD"},
Auth: auth,
})
}
git fetch
// cmd fetch
func osExecFetch(workspace, refName, refType string) error {
if refType == "branch" {
console.Println(fmt.Sprintf("git client fetch code by branch: %s", refName))
} else if refType == "tag" {
console.Println(fmt.Sprintf("git client fetch code by tag: %s", refName))
} else if refType == "commit" {
console.Println(fmt.Sprintf("git client fetch code by commit id: %s", refName))
}
console.Println("git client fetch all")
// git fetch
cmd := exec.Command("git", "fetch")
cmd.Dir = workspace
out, err := cmd.CombinedOutput()
console.Verbose(string(out))
return err
}
checkout
go-git checkout
/**
這裏把commit id單獨處理的原因是,一個tag或者branch可以有多個commit id。
需求是根據給定的commit id來checkout到對應的commit id,與branch或者tag無關的commit id
*/
func gitCheckout(wt *git.Worktree, referenceName, refType string) error {
branch := ""
// if branch or tag
if refType == "branch" || refType == "tag" {
branch = referenceName
return wt.Checkout(&git.CheckoutOptions{
Branch: plumbing.ReferenceName(branch),
Force: true,
})
}
// if commit id
if refType == "commit" {
return wt.Checkout(&git.CheckoutOptions{
Hash: plumbing.NewHash(referenceName),
Force: true,
})
}
return nil
}
git fetch
func osExecCheckout(refType, referenceName, workspace string) error {
if refType == "branch" {
console.Verbose(fmt.Sprintf("git client checkout code by branch: %s", referenceName))
} else if refType == "tag" {
console.Verbose(fmt.Sprintf("git client checkout code by tag: %s", referenceName))
} else if refType == "commit" {
console.Verbose(fmt.Sprintf("git client checkout code by commit id: %s", referenceName))
}
cmd := exec.Command("git", "checkout", referenceName)
cmd.Dir = workspace
out, err := cmd.CombinedOutput()
console.Verbose(string(out))
return err
}
忽略host key檢查
windows
1. 在C:\Users\xxx\.ssh目錄下通過建立known_hosts,這個需要手動執行,也就是需要輸入yes纔會建立
2. 在C:\Users\xxx\.ssh目錄下通過建立config文件,文件內容如下:
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
linux
1. 在/root/.ssh目錄下通過建立known_hosts,這個需要手動執行,也就是需要輸入yes纔會建立
2. 在/root/.ssh目錄下通過建立config文件,文件內容如下:
StrictHostKeyChecking no
UserKnownHostsFile /dev/null