栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

有一个示例,说明如何在其存储库上创建/添加和提交,可以帮助您修改文件。

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

希望这可以帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369183.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号