2017-03-07 39 views
0

我想克隆到本地文件系统的回购,然后签出特定的提交。
这是我有:如何使用提交版本更新本地路径?

Git.Clone(GIT_REPO_URL, localPath, CLONE_OPTIONS).then((repo) => { 
    return repo.getCommit(version).then((commit) => { 
     // use the local tree 
    }); 
}).catch((error) => { 
    // handler clone failure 
}); 

这个克隆的回购只是罚款,但当地版本我最终是主的现任掌门人,而不是我签出(version)提交。

如何更新本地树以匹配此提交?
谢谢。

+0

你不需要检查提交吗?那是什么'getCommit'呢?虽然它会让你处于独立的状态...... – evolutionxbox

+0

@evolutionxbox是的,这就是[getCommit](http://www.nodegit.org/api/repository/#getCommit)的用处,但它似乎不适用于改变本地树只是将它作为参数传递给函数。我的问题是如何更新与本地树? –

+0

只是一个有用的提示:为了正确的错误处理,您希望返回由'repo.getCommit(...)'链给出的承诺。就像现在一样,例如getCommit错误处理程序本身会发生的错误将不会处理,并且可能会导致程序崩溃。 – Frxstrem

回答

3

getCommit不修改本地树;它仅检索脚本中内存中的提交,以便您可以在不修改本地树的情况下使用它。 (例如,如果您想要浏览git历史记录并进行一些分析,但不需要实际更新本地树以自行访问文件)。

要实际上请检查一个特定的分支或提交,你想要使用checkoutBranchcheckoutRef函数,它们实际上会更新本地工作树。 (请注意他们的描述是如何明确地这样说的,与getCommit不同,它并没有说它修改了工作三,因为它并没有这样做)。

+0

你知道如何使用'checkoutBranch'和'checkoutRef'吗?似乎我需要创建一个[参考](http://www.nodegit.org/api/reference/),但不清楚如何为提交版本 –

1

如果您想结算随机提交,则需要改用Checkout.tree(*)函数。

var repository; 
NodeGit.Clone(GIT_REPO_URL, localPath, CLONE_OPTIONS) 
.then(function(repo) { 
    repository = repo; 
    return repo.getCommit(version); 
}) 
.then(function(commit) { 
    return NodeGit.Checkout.tree(repository, commit, checkoutStrategy: 
     git.Checkout.STRATEGY.FORCE 
    }); 
}).catch((error) => { 
    // handler clone failure 
}); 

您可能会也可能不希望分离您的HEAD

编辑: 这将结帐并将HEAD设置为有问题的提交。

var repository; 
NodeGit.Clone(GIT_REPO_URL, localPath, CLONE_OPTIONS) 
.then(function(repo) { 
    repository = repo; 
    return repo.getCommit(version); 
}) 
.then(function(commit) { 
    repository.setHeadDetached(commit.id()); 
}).catch((error) => { 
    // handler clone failure 
}); 
+0

这样做这似乎很有前途,但本地树仍然设置为'master'的 –

0

我没有设法使其工作,因为它原来是太复杂获得微不足道的工作什么的,我已经决定只是这样做:

import * as process from "child_process"; 

const command = `git clone ${ GIT_REPO_URL } ${ repoPath } && cd ${ repoPath } && git checkout ${ version }`; 
process.exec(command, error => { 
    ... 
}); 
相关问题