du7133 2015-07-18 22:28
浏览 84
已采纳

Git2go:模拟git checkout和立即git push

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条回答 默认 最新

  • douchang8219 2015-07-30 11:22
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题