Git2go:模拟git checkout并立即进行git push操作。

huangapple go评论102阅读模式
英文:

Git2go: Simulate git checkout and an immediate git push

问题

使用git2go如何实现以下操作:

$ git checkout -b feature_branch_name
... 编辑文件,添加和提交 ...
$ git push -u origin feature_branch_name

我在这里卡住了:

branch, err = repo.CreateBranch("test", headCommit, false, 
    signature, "Test branch that I was to push immediately")
if err != nil {
    panic(err)
}

更新

我现在有以下代码,它创建了分支并指向正确的分支,但是我无法像git checkout一样更新工作目录:

head, err := repository.Head()
if err != nil {
    return err
}

headCommit, err := repository.LookupCommit(head.Target())
if err != nil {
    return err
}

_, err = cs.repository.CreateBranch(name, headCommit, false)
if err != nil {
    return err
}

_, err = cs.repository.References.CreateSymbolic("HEAD","refs/heads/"+name, true, "headOne")
if err != nil {
    return err
}

opts := &git.CheckoutOpts{
    Strategy: git.CheckoutSafe | git.CheckoutRecreateMissing,
}
if err := repository.CheckoutHead(opts); err != nil {
    return err
}

我认为我现在在处理checkout选项方面遇到了困难。

我还在处理推送部分。

英文:

How can I achieve the following using git2go.

$ git checkout -b feature_branch_name
... edit files, add and commit ...
$ git push -u origin feature_branch_name

I'm stuck here:

branch, err = repo.CreateBranch("test", headCommit, false, 
    signature, "Test branch that I was to push immediately")
if err != nil {
    panic(err)
}

UPDATE

I have the following now, it creates the branch and point to the correct branch, but I can't get it to update the working directory just like git checkout does:

head, err := repository.Head()
if err != nil {
	return err
}
	
headCommit, err := repository.LookupCommit(head.Target())
if err != nil {
	return err
}
	
_, err = cs.repository.CreateBranch(name, headCommit, false)
if err != nil {
	return err
}

_, err = cs.repository.References.CreateSymbolic("HEAD","refs/heads/"+name, true, "headOne")
if err != nil {
    return err
}

opts := &git.CheckoutOpts{
    Strategy: git.CheckoutSafe | git.CheckoutRecreateMissing,
}
if err := repository.CheckoutHead(opts); err != nil {
	return err
}

I think I'm struggling with the checkout options now.

I'm still working on the push part.

答案1

得分: 3

我终于搞定了。如果有人对同样的问题感兴趣,这是代码:

git checkout

head, err := repository.Head()
if err != nil {
    return err
}

headCommit, err := repository.LookupCommit(head.Target())
if err != nil {
    return err
}

_, err = cs.repository.CreateBranch(name, headCommit, false)
if err != nil {
    return err
}

_, err = cs.repository.References.CreateSymbolic("HEAD","refs/heads/"+name, true, "headOne")
if err != nil {
    return err
}

opts := &git.CheckoutOpts{
    Strategy: git.CheckoutSafe | git.CheckoutRecreateMissing,
}
if err := repository.CheckoutHead(opts); err != nil {
    return err
}

git push

// 获取远程仓库
remote, err := repo.Remotes.Lookup("origin")
if err != nil {
	remote, err = repo.Remotes.Create("origin", repo.Path())
	if err != nil {
		return err
	}
}

// 获取分支
branch, err := repo.Branch()
if err != nil {
	return err
}

// 获取分支名
branchName, err := branch.Name()
if err != nil {
	return err
}

if err := remote.Push([]string{"refs/heads/"+branchName}, &git.PushOptions{}); err != nil {
	return err
}

return nil

根据你使用的git2go版本的不同,这篇文章也许会有所帮助:http://blog.gopheracademy.com/advent-2014/git2go-tutorial/(很棒的文章)

不过,自那时以来情况发生了很大变化。

谢谢。

英文:

I finally got it working. Here is the code if anyone is wondering about the same thing:

git checkout

head, err := repository.Head()
if err != nil {
	return err
}
	
headCommit, err := repository.LookupCommit(head.Target())
if err != nil {
	return err
}
	
_, err = cs.repository.CreateBranch(name, headCommit, false)
if err != nil {
	return err
}

_, err = cs.repository.References.CreateSymbolic("HEAD","refs/heads/"+name, true, "headOne")
if err != nil {
    return err
}

opts := &git.CheckoutOpts{
    Strategy: git.CheckoutSafe | git.CheckoutRecreateMissing,
}
if err := repository.CheckoutHead(opts); err != nil {
	return err
}

git push

// Get remote
remote, err := repo.Remotes.Lookup("origin")
if err != nil {
	remote, err = repo.Remotes.Create("origin", repo.Path())
	if err != nil {
		return err
	}
}

// Get the branch
branch, err := repo.Branch()
if err != nil {
	return err
}

// Get the name
branchName, err := branch.Name()
if err != nil {
	return err
}

if err := remote.Push([]string{"refs/heads/"+branchName}, &git.PushOptions{}); err != nil {
	return err
}

return nil

Depending on what version of git2go you are using, this might also be helpful: http://blog.gopheracademy.com/advent-2014/git2go-tutorial/ (great article)

Things have changed quite a bit since then though.

Thank you

huangapple
  • 本文由 发表于 2015年7月19日 06:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/31496175.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定