使用nodegit切换分支/标记 [英] Switch Branch/Tag with nodegit

查看:348
本文介绍了使用nodegit切换分支/标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试着打开一个现有的repo并使用nodegit更改分支或标记。文件很广泛,但似乎已过时。任何关于我在做什么的想法都是错误的?

  var NodeGit = require(nodegit); 
var open = NodeGit.Repository.open;
var Tag = NodeGit.Tag;
var Checkout = NodeGit.Checkout;

open(location).then(function(repo){
Tag.list(repo).then(function(array){
// array [[v1。 0.0']
var ref = array [0]
Checkout.tree(repo,ref).then(function(){
//想要以分离状态检出标签。
});
});
});


解决方案

你的代码。首先是你没有终止承诺链,这样错误就被吞噬了。您需要使用 .catch .done 来结束它。



第二,我认为你不太清楚结帐的功能。低级git中的一个令人困惑的事情,以及它与git CLI的不同之处在于Checkout only 会更新您的工作目录以反映第二个参数指向的树。



第三,你传递一个字符串给一个期待别的东西的方法。 文档显示它正在寻找Oid,Tree,Commit或Reference。让我们来看看这些代码。

  var NodeGit = require(nodegit); 
var open = NodeGit.Repository.open;
var Tag = NodeGit.Tag;
var Checkout = NodeGit.Checkout;

open(location).then(function(repo){
return Tag.list(repo)
.then(function(array){
// array是['v1.0.0','v2.0.0']
返回Tag.lookup(repo,array [0]);
})
.then(function(tag){
return Checkout.tree(repo,tag.targetId(),{checkoutStrategy:Checkout.STRATEGY.SAFE_CREATE})
.then(function(){
repo.setHeadDetached(tag.targetId() ,repo.defaultSignature,Checkout:HEAD+ tag.targetId());
});
});
})
.catch(function(error){
//记录错误
});

这应该指向正确的方向。如果您需要更多帮助,我建议在我们非常活跃的我们的 gitter频道停止。


I have been trying all morning to open an existing repo and change branch or tag using nodegit. The documentation is extensive but seems to be out of date. Any ideas on what I'm doing wrong?

var NodeGit = require("nodegit");
var open = NodeGit.Repository.open;
var Tag = NodeGit.Tag;
var Checkout = NodeGit.Checkout;

open(location).then(function (repo) {
    Tag.list(repo).then(function(array) {
        // array is ['v1.0.0']
        var ref = array[0]
        Checkout.tree(repo, ref).then(function() {
            // Want tag to be checked out out in detached state.
        });
    });
});

解决方案

So there are a few things you're missing with your code. First one is that you aren't terminating the promise chain so errors are being swallowed. You'll want to end it with either a .catch or a .done.

Second, I think you're not quite sure what a checkout does. One of the confusing things with low-level git and how it differs from git CLI is that Checkout only updates your working directory to reflect the tree pointed to by the second parameter.

Third, you're passing in a string to a method that is expecting something else. The docs are showing that it's looking for an Oid, Tree, Commit, or Reference. Let's spruce up that code a bit.

var NodeGit = require("nodegit");
var open = NodeGit.Repository.open;
var Tag = NodeGit.Tag;
var Checkout = NodeGit.Checkout;

open(location).then(function (repo) {
  return Tag.list(repo)
    .then(function(array) {
      // array is ['v1.0.0','v2.0.0']
      return Tag.lookup(repo,array[0]);
    })
    .then(function(tag) {
      return Checkout.tree(repo, tag.targetId(), { checkoutStrategy: Checkout.STRATEGY.SAFE_CREATE})
        .then(function() {
          repo.setHeadDetached(tag.targetId(), repo.defaultSignature, "Checkout: HEAD " + tag.targetId());
        });
     });
})
.catch(function(error) {
  // log error
});

That should point you in the right direction. If you need more help I would recommend stopping by our gitter channel where we are pretty active.

这篇关于使用nodegit切换分支/标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆