Git2Go Fetch 或 Hard Pull

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

Git2Go Fetch or Hard Pull

问题

我有一个代码库,但在某些情况下,本地文件可能会发生更改。我想使用git2go执行git pull并覆盖更改。在普通的git命令行中,我可以执行以下操作:

git fetch --all
git reset --hard origin/master

因此,我尝试使用git2go执行相同的操作:

func Pull(source string, dest string) error {
    repo, err := git.OpenRepository(dest)
    remote, err := repo.LookupRemote("origin")
    log.Println(repo) //打印repo地址

    cbs := &git.RemoteCallbacks{
        CredentialsCallback:      credentialsCallback,
        CertificateCheckCallback: certificateCheckCallback,
    }

    err = remote.SetCallbacks(cbs)
    err = remote.Fetch([]string{}, "")
    log.Println("fetch error: ", err) //打印fetch错误
    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)
    log.Println("err: ", err)
    repo.StateCleanup() //打印合并错误
    return err
}

它可以正常编译,并且似乎运行正常-它将错误记录为<nil>,但被覆盖的文件不会更改为远程文件的内容。我觉得我可能漏掉了一个关键要素...。

我得到的输出是:

2015/05/19 20:40:15 GitHandler.go:44: &{0x7f46cc000a40}
2015/05/19 20:40:22 GitHandler.go:53: fetch error: <nil>
2015/05/19 20:40:22 GitHandler.go:59: err: <nil>
英文:

I have a repository, however in some situations the files locally may get changed. I want to do a git pull and over write the changes with git2go. In normal git - i.e on the terminal I could do

git fetch --all
git reset --hard origin/master

I am therefore trying to do the same using git2go

func Pull(source string, dest string) error {
    repo, err:= git.OpenRepository(dest)
    remote, err:= repo.LookupRemote(&quot;origin&quot;)
    log.Println(repo) //print repo address

    cbs := &amp;git.RemoteCallbacks{
        CredentialsCallback:      credentialsCallback,
        CertificateCheckCallback: certificateCheckCallback,
    }

    err = remote.SetCallbacks(cbs)
    err = remote.Fetch([]string{}, &quot;&quot;)
    log.Println(&quot;fetch error: &quot;, err) //print fetch error
    remote_master, err := repo.LookupReference(&quot;refs/remotes/origin/master&quot;)
    mergeRemoteHead, err := repo.AnnotatedCommitFromRef(remote_master)
    mergeHeads := make([]*git.AnnotatedCommit, 1)
    mergeHeads[0] = mergeRemoteHead
    err = repo.Merge(mergeHeads, nil, nil)
    log.Println(&quot;err: &quot;, err)
    repo.StateCleanup() //print merge error
    return err
}

It compiles fine, and seems to run - it logs the error to be <nil> however the overwritten files don't change to what they are remotely. I feel I am missing one key element...../

The output I get is:

2015/05/19 20:40:15 GitHandler.go:44: &amp;{0x7f46cc000a40}
2015/05/19 20:40:22 GitHandler.go:53: fetch error:  &lt;nil&gt;
2015/05/19 20:40:22 GitHandler.go:59: err:  &lt;nil&gt;

答案1

得分: 3

你想要替换的git命令和你正在编写的Go服务器代码有非常不同的目的。通过合并无法以任何方式模拟硬重置。

如果你确实想要合并,你应该检查结果索引是否存在冲突,并在必要时解决它们。然后你需要创建合并提交并更新当前分支。

如果你想要执行硬重置的步骤,那么你需要将这些步骤写出来,执行目标树的检出,将目标树读入索引并写出,然后将当前分支更新到目标提交。

这些是非常不同的更新方法,其中一个保留了本地提交,另一个则没有。一个创建历史记录,另一个则没有。我建议首先确定你希望执行哪种形式的更新。

英文:

The git commands you want to replace and the code you're writing in go server very different purposes. A hard reset cannot be simulated in any way by a merge.

If you do want to merge, you should check the resulting index for conflicts and resolve them if necessary. You then need to create the merge commit and update the current branch.

If what you want is to do the steps of a hard reset, then you need to write those out, perform a checkout of your target tree, read the target tree into the index and write that out, and then update the current branch to the target commit.

These are very different approaches to updates, one of them preserves local commits, the other one doesn't. One creates history, the other one doesn't. I would recomment first figuring out which form of update you wish to perform.

huangapple
  • 本文由 发表于 2015年5月20日 03:42:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/30334749.html
匿名

发表评论

匿名网友

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

确定