从远程进行git fetch && git merge的git2go操作。

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

Doing git fetch && git merge from remote with git2go

问题

我正在使用libgit2/git2go v0.22,并尝试实现类似于从远程仓库进行"git pull"的操作。在这种情况下,工作目录不会有任何更改:没有修改、没有提交、没有推送。它只会从远程仓库拉取数据。

使用git2go,我可以克隆远程仓库,加载/查找远程origin,获取远程仓库,列出远程头等等。似乎唯一不起作用的步骤是合并操作。
我的代码如下(我省略了错误处理部分):

repo, err := git.OpenRepository(sitesConfig.Sites[SiteName].Path)
remote, err := repo.LookupRemote("origin")
err = remote.SetCallbacks(&rcbs)
err = remote.Connect(git.ConnectDirectionFetch)
err = remote.ConnectFetch()

remote_master, err := repo.LookupReference("refs/remotes/origin/master")
mergeRemoteHead, err := repo.AnnotatedCommitFromRef(remote_master)
mergeHeads := make([]*git.AnnotatedCommit, 1)
mergeHeads[0] = mergeRemoteHead
err = repo.Merge(mergeHeads, nil, nil)
repo.StateCleanup()

运行此代码时没有出现错误,但工作目录没有更新。在相同的目录下使用"git pull"和"git fetch && git merge origin/master"命令可以正常工作。

是否有遗漏的部分?需要进行最终提交吗?

英文:

I'm using libgit2/git2go v0.22 and trying to implement something similar to a "git pull" from a remote repository. In this case The working directory doesn't write anything: no changes, no commits, no push. It will only pull data from the remote.

Using git2go I can clone the remote repository, load/lookup a remote origin, fetch the remote, list remote headers, etc. It seems that the only step not working is the merge.
My code looks like this (i'm ommiting the error handling):

repo, err:= git.OpenRepository(sitesConfig.Sites[SiteName].Path)
remote, err:= repo.LookupRemote("origin")
err = remote.SetCallbacks(&rcbs)
err = remote.Connect(git.ConnectDirectionFetch)
err = remote.ConnectFetch()

remote_master, err := repo.LookupReference("refs/remotes/origin/master")
mergeRemoteHead, err := repo.AnnotatedCommitFromRef(remote_master)
mergeHeads := make([]*git.AnnotatedCommit, 1)
mergeHeads[0] = mergeRemoteHead
err = repo.Merge(mergeHeads, nil, nil)
repo.StateCleanup()

No errors appear when running this code, but the working directory is not updated. Using "git pull" and "git fetch && git marge origin/master" on the same directory worked fine.

Is something missing? A final commit?

答案1

得分: 1

你正在要求两次连接到远程 (err = remote.Connect(git.ConnectDirectionFetch)err = remote.ConnectFetch()),但你并没有要求它做任何事情。不要尝试两次连接,而是使用一次性的全部获取方法

remote.Fetch(nil, nil, nil)

来连接、下载和更新远程跟踪分支。然后你可以检查是否有更新并合并或执行其他需要的操作。

英文:

You are asking to connect to the remote twice (err = remote.Connect(git.ConnectDirectionFetch) and err = remote.ConnectFetch()), but you're not asking it for anything. Instead of trying to connect twice, use the all-in-one fetch method

remote.Fetch(nil, nil, nil)

to connect, download and update the remote-tracking branches. Then you can check whether there was an update and merge or do whatever you need.

huangapple
  • 本文由 发表于 2015年4月14日 22:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/29629911.html
匿名

发表评论

匿名网友

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

确定