当前位置: 首页 > 工具软件 > go-github > 使用案例 >

使用go拉取github代码

凌修伟
2023-12-01

目录

准备

clone 

go-git clone

git clone

pull

go-git pull

git pull

fetch

go-git fetch

git fetch

checkout

go-git checkout

git fetch

忽略host key检查


准备

代码获取方式有两种:

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

 

 类似资料: