繁体   English   中英

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

[英]Nodegit: How to modify a file and push the changes?

看了一个例子,但找不到一个。 文档没有解释,我无法弄清楚。

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

Nodegit: http ://www.nodegit.org/

Nodegit文档: http ://www.nodegit.org/nodegit

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

https://github.com/nodegit/nodegit/blob/master/examples/add-and-commit.js

关于提交和推送,这里是我的代码看起来如何的片段,我希望它对你有所帮助。 我花了很多时间来解决这个问题,感谢Gitter的那些人,我最终找到了答案。

代码如下所示:

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', 'author@email.com');
    committer = nodegit.Signature.now('Commiter Name', 'commiter@email.com');

    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);
  })

希望这可以帮助。

Rafael的解决方案确实有效,尽管我可能会补充说,不需要在服务器上执行.connect 这是推送回购的最小方法:

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

      //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"); }) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM