2017-03-07 57 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 => { 
    ... 
});