git nodejs中的git命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29653989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git commands in nodejs
提问by Boba Fett likes JS
I was wandering if someone know/got into a npm package to work with git commands. I have a nodejs project that I want to do the following commands:
如果有人知道/进入 npm 包来使用 git 命令,我就在徘徊。我有一个 nodejs 项目,我想执行以下命令:
- git clone
- git create branch
- git pull request - from the created brach
- git 克隆
- git 创建分支
- git pull request - 来自创建的分支
I try some npm packages but without any success. any Help will be appreciated Thanks
我尝试了一些 npm 包,但没有任何成功。任何帮助将不胜感激谢谢
回答by Samar Panda
Using external package: You can use shelljsnpm package
使用外部包:您可以使用shelljsnpm 包
Without using external package: leveraging nodejs child_process
module. Below is the implementation
不使用外部包:利用 nodejschild_process
模块。下面是实现
1.exec_process.js
1.exec_process.js
var exec = require('child_process').exec;
var result = function(command, cb){
var child = exec(command, function(err, stdout, stderr){
if(err != null){
return cb(new Error(err), null);
}else if(typeof(stderr) != "string"){
return cb(new Error(stderr), null);
}else{
return cb(null, stdout);
}
});
}
exports.result = result;
2.temp.sh
2.temp.sh
#! /usr/bin/bash
pwd
git --version
3.app.js
3.app.js
var execProcess = require("./exec_process.js");
execProcess.result("sh temp.sh", function(err, response){
if(!err){
console.log(response);
}else {
console.log(err);
}
});
To run the application you can try node app.js
Output:
要运行该应用程序,您可以尝试node app.js
输出:
/root
git version 1.8.1.2
/root
git 版本 1.8.1.2
Similarly you can add any command in shell script file and run it using node. Demo and source code published in runnable.
同样,您可以在 shell 脚本文件中添加任何命令并使用 node 运行它。在runnable 中发布的演示和源代码。
Suggest checking git-extrasit covers all the use cases of git via commandline.
建议检查git-extras它通过命令行涵盖了 git 的所有用例。
回答by Chris Tate
I don't think any of the modules support PRs, but for cloning / branch management, try gift
:
我认为任何模块都不支持 PR,但对于克隆/分支管理,请尝试gift
:
https://www.npmjs.com/package/gift
https://www.npmjs.com/package/gift
This module appears to have the most git features. For PRs, I'd suggest creating an issue and/or contributing to this awesome module:
这个模块似乎拥有最多的 git 功能。对于 PR,我建议创建一个问题和/或为这个很棒的模块做出贡献: