问题描述
看看周围的例子,但找不到一个。该文档没有解释,我无法弄清楚。如何修改文件(例如,README.md),为修改的文件创建提交,然后推送到服务器?
Nodegit:
Nodegit文档:
有一个如何在repo上创建/添加和提交的例子,你修改文件。
关于提交和推送,这里是我的代码如何看起来像片段,我希望它可以帮助你一点。我花了相当多的时间来弄清楚这一点,并感谢上的家伙,我终于找到了答案。
代码如下所示:
var path = require('path');
$ b var nodegit = require('nodegit'),
repoFolder = path.resolve(__ dirname,'repos / test / .git'),
fileToStage ='README.md ;
var repo,index,oid,remote;
nodegit.Repository.open(repoFolder)
.then(function(repoResult){
repo = repoResult;
return repoResult.openIndex(); $ b $ (函数(indexResult){
index = indexResult;
//这个文件位于目录的根目录下,不需要完整的路径
index.addByPath(fileToStage);
//这会将文件写入索引
index.write();
return index.writeTree ();
})
.then(function(oidResult){
oid = oidResult;
return nodegit.Reference.nameToId(repo,'HEAD') ;
})
.then(function(head){
return repo.getCommit(head);
})
.then(function(parent){
author = nodegit.Signature.now('Author Name','[email protected]');
committer = nodegit.Signature.now('Commiter Name','[email protected]') ;
return repo.createCommit('HEAD',author,committer''Added the Readm主题构建器的e文件',oid,[parent]); (
))
.then(function(commitId){
return console.log('New Commit:',commitId);
})
(function(){
return repo.getRemote('origin');
})
.then(function(remoteResult){
console.log('remote Loaded');
remote = remoteResult;
remote.setCallbacks({
credentials:function(url,userName){
return nodegit.Cred。 sshKeyFromAgent(userName);
}
});
console.log('remote Configured');
return remote.connect(nodegit.Enums.DIRECTION。 PUSH);
})
.then(function(){
console.log('remote Connected?',remote.connected())
return remote .push(
['refs / heads / master:refs / heads / master'],
null,
repo.defaultSignature(),
'Push to master'$ b (b)
.then(function(){
console.log('remote Pushed!')
})
.ca tch(function(reason){
console.log(reason);
})
希望这有助于您。
Looked around for an example, but couldn't find one. The documentation is not explained and I could not figure it out.
How to modify a file (README.md for example), create a commit for the modified file and then push the commit to the server ?
Nodegit: http://www.nodegit.org/
Nodegit documentation: http://www.nodegit.org/nodegit
There is an example of how to create/add and commit on their repo, which can help you with the modifying the file.
https://github.com/nodegit/nodegit/blob/master/examples/add-and-commit.js
Regarding the commit and push, here is the snippet of how my code looks like and i hope it helps you a bit. It took me quite a bit to figure this out and thanks to the guys on Gitter, i finaly found an answer.
The code look like this:
var path = require('path');
var nodegit = require('nodegit'),
repoFolder = path.resolve(__dirname, 'repos/test/.git'),
fileToStage = 'README.md';
var repo, index, oid, remote;
nodegit.Repository.open(repoFolder)
.then(function(repoResult) {
repo = repoResult;
return repoResult.openIndex();
})
.then(function(indexResult) {
index = indexResult;
// this file is in the root of the directory and doesn't need a full path
index.addByPath(fileToStage);
// this will write files to the index
index.write();
return index.writeTree();
})
.then(function(oidResult) {
oid = oidResult;
return nodegit.Reference.nameToId(repo, 'HEAD');
})
.then(function(head) {
return repo.getCommit(head);
})
.then(function(parent) {
author = nodegit.Signature.now('Author Name', '[email protected]');
committer = nodegit.Signature.now('Commiter Name', '[email protected]');
return repo.createCommit('HEAD', author, committer, 'Added the Readme file for theme builder', oid, [parent]);
})
.then(function(commitId) {
return console.log('New Commit: ', commitId);
})
/// PUSH
.then(function() {
return repo.getRemote('origin');
})
.then(function(remoteResult) {
console.log('remote Loaded');
remote = remoteResult;
remote.setCallbacks({
credentials: function(url, userName) {
return nodegit.Cred.sshKeyFromAgent(userName);
}
});
console.log('remote Configured');
return remote.connect(nodegit.Enums.DIRECTION.PUSH);
})
.then(function() {
console.log('remote Connected?', remote.connected())
return remote.push(
['refs/heads/master:refs/heads/master'],
null,
repo.defaultSignature(),
'Push to master'
)
})
.then(function() {
console.log('remote Pushed!')
})
.catch(function(reason) {
console.log(reason);
})
Hope this helps.
这篇关于Nodegit:如何修改文件并推送更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!