Git2go:模拟git checkout和立即git推送

时间:2015-07-18 22:28:01

标签: git go libgit2

如何使用git2go实现以下功能。

$ git checkout -b feature_branch_name
... edit files, add and commit ...
$ 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
}

我认为我现在正在努力解决结帐选项

我还在推动推动部件。

1 个答案:

答案 0 :(得分: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

// 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

根据您使用的git2go版本,这可能也会有所帮助:http://blog.gopheracademy.com/advent-2014/git2go-tutorial/(精彩文章)

但事情发生了很大变化。

谢谢