Javascript Nodegit:如何修改文件并推送更改?

Javascript Nodegit:如何修改文件并推送更改?,javascript,node.js,git,nodegit,Javascript,Node.js,Git,Nodegit,四处寻找示例,但找不到。文档没有解释,我无法理解 如何修改文件(例如README.md),为修改后的文件创建提交,然后将提交推送到服务器 Nodegit: Nodegit文档:这里有一个关于如何在他们的repo上创建/添加和提交的示例,它可以帮助您修改文件 关于提交和推送,这里是我的代码的片段,我希望它能对您有所帮助。我花了不少时间才弄明白这一点,多亏了现场的人,我终于找到了答案 代码如下所示: var path=require('path'); var nodegit=require('no

四处寻找示例,但找不到。文档没有解释,我无法理解

如何修改文件(例如README.md),为修改后的文件创建提交,然后将提交推送到服务器

Nodegit:


Nodegit文档:

这里有一个关于如何在他们的repo上创建/添加和提交的示例,它可以帮助您修改文件

关于提交和推送,这里是我的代码的片段,我希望它能对您有所帮助。我花了不少时间才弄明白这一点,多亏了现场的人,我终于找到了答案

代码如下所示:

var path=require('path');
var nodegit=require('nodegit'),
repoFolder=path.resolve(uu dirname,'repos/test/.git'),
fileToStage='README.md';
var回购、指数、oid、远程;
nodegit.Repository.open(repoFolder)
.then(函数(结果){
回购=回购结果;
返回reposult.openIndex();
})
.then(函数(indexResult){
指数=指数结果;
//此文件位于目录的根目录中,不需要完整路径
index.addByPath(fileToStage);
//这将把文件写入索引
index.write();
返回index.writeTree();
})
.then(函数(结果){
oid=oid结果;
返回nodegit.Reference.nameToId(repo,“HEAD”);
})
.然后(功能(头部){
返回回购getCommit(head);
})
.然后(函数(父函数){
author=nodegit.Signature.now('author Name','author@email.com');
committer=nodegit.Signature.now('committer Name','commiter@email.com');
return repo.createCommit('HEAD',author,committer',添加了主题生成器的自述文件',oid,[parent]);
})
.then(功能(委员会){
返回console.log('newcommit:',commitId);
})
///推
.然后(函数(){
返回repo.getRemote(“来源”);
})
.then(函数(remoteResult){
log(“远程加载”);
远程=远程结果;
remote.setCallbacks({
凭据:函数(url、用户名){
返回nodegit.Cred.sshKeyFromAgent(用户名);
}
});
log(“远程配置”);
返回remote.connect(nodegit.Enums.DIRECTION.PUSH);
})
.然后(函数(){
console.log('remote Connected?',remote.Connected())
返回远程推送(
['refs/heads/master:refs/heads/master'],
无效的
repo.defaultSignature(),
“推到主机上”
)
})
.然后(函数(){
console.log('远程推送!')
})
.catch(函数(原因){
控制台日志(原因);
})

希望这能有所帮助。

Rafael的解决方案确实有效,尽管我可能会补充说,不需要执行
。将
连接到服务器。以下是推动回购的最低方法:

  • 仅使用用户名和密码身份验证推送到主机:

            //Previous calls
    
            }).then(function() {
                return repo.getRemote("origin"); //Get origin remote
            }).then(function(remote) {
                return remote.push(["refs/heads/master:refs/heads/master"], {
                    callbacks: {
                        credentials: function(url, userName) {
                            console.log("Requesting creds");
    
                            return NodeGit.Cred.userpassPlaintextNew("[USERNAME]", "[PASSWORD]");
                        }
                    }
                });
            }).then(function(err) {
                console.log("Push error number:");
                console.log(err);
            }).catch(function(err) {
                console.log(err);
            }).done(function(commitId) {
                console.log("All done");
            })
    
  • 通过SSH身份验证推送到myBranch:

            //Previous calls
    
            }).then(function() {
                return repo.getRemote("origin"); //Get origin remote
            }).then(function(remote) {
                return remote.push(["refs/heads/myBranch:refs/heads/myBranch"], {
                    callbacks: {
                        credentials: function(url, userName) {
                            console.log("Requesting creds");
                            return NodeGit.Cred.sshKeyFromAgent(userName);
                        }
                    }
                });
            }).then(function(err) {
                console.log("Push error number:");
                console.log(err);
            }).catch(function(err) {
                console.log(err);
            }).done(function(commitId) {
                console.log("All done");
            })
    

我目前也在努力解决这个问题。这个文档非常模糊,API也没有那么大的帮助。我怀疑会有
repo.createCommit
方法以某种方式参与其中,但这确实令人困惑。你发现什么了吗?哦,我完全错过了这个,非常有用的例子。。。试图编辑帖子,但它不允许我-行中缺少一个“nodegit.Signature.now”(“提交人名称”)commiter@email.com”;
如果我只想执行与“git commit-am”等效的操作,您能说明需要多少吗?我只是尝试调用repo.createcommittenhead,而不添加任何(新的?)即使我完全按照它说的去做,它也会进入createCommit调用,而这永远不会完成。