@fab1o/git
v1.4.0
Published
A wrapper for command line git with promises
Downloads
2
Readme
@fab1o/git
A wrapper for command line git with promises
npm install @fab1o/git
How to use it
This is a wrapper for command line Git so, you must have git installed in your linux / mac machine (it has not been tested in windows yet).
- Common sintax is:
var { Git, GitSync } = require('@fab1o/git');
var git = new Git('/tmp/gitTemp');
var gitSync = new GitSync('/tmp/gitTemp');
//You can also create it only with Git() and set the working path later
git.[git command]([string parameters], [options])
.then(function(res){
// Then
}).
fail(function(err){
// Fail
});
// or calling it synchronously
try {
const res = gitSync.[git command]([string parameters], [options]);
}
catch(err) {
// Fail
}
=======
Some examples
- To Git init /tmp/git folder, add all files on it, commit, add a new remote and push master to it
var { Git } = require('@fab1o/git');
//Variables
var gitFolder = '/tmp/gitTemp';
var remoteName = 'origin';
var remoteUrl = 'https://example.remote.repo';
//Create a new Git object
var git = new Git(gitFolder);
//Execute the chain
git
.init()
.then(function (res) {
return git.add("*", { cwd: "/tmp/git" });
})
.then(function (res) {
return git.commit('-m "My commit"');
})
.then(function (res) {
return git.remote("add " + remoteName + " " + remoteUrl);
})
.then(function (res) {
return git.push("-u " + remoteName + " master");
})
.then(function (res) {
console.log("Success: ", res);
})
.fail(function (err) {
console.error(err);
});
- To commit staged files with message "My commit" on the last working folder if any or current one
git.commit('-m "My commit"')
.then(function(msg){
console.log(msg)
}).fail(function(err){
console.log(err);
});
=======
API
Initially, following commands are available:
- add Add file contents to the index
- bisect Find by binary search the change that introduced a bug
- branch List, create, or delete branches
- checkout Checkout a branch or paths to the working tree
- clone Clone a repository into a new directory
- commit Record changes to the repository
- diff Show changes between commits, commit and working tree, etc
- direct Allows the direct execution of a git command that is not available in the API yet
- fetch Download objects and refs from another repository
- grep Print lines matching a pattern
- init Create an empty git repository or reinitialize an existing one
- log Show commit logs
- merge Join two or more development histories together
- mv Move or rename a file, a directory, or a symlink
- pull Fetch from and integrate with another repository or a local branch
- pullRequest Creates a Pull Request
- push Update remote refs along with associated objects
- rebase Forward-port local commits to the updated upstream head
- remote Manage set of tracked repositories
- reset Reset current HEAD to the specified state
- rm Remove files from the working tree and from the index
- show Show various types of objects
- status Show the working tree status
- tag Create, list, delete or verify a tag object signed with GPG
Options parameter is to tweak the 'exec' command as described in: https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
There is a special situation. Once you stablish cwd in the options param, it will be maintained through the rest of the commands
Direct
git.direct(command,options);
Git direct allows the direct execution of a git command that is not available in the API yet
- Examples
var myGitRepo = '/tmp/gitTemp'; //This is where the command will be executed
var git = new Git(myGitRepo);
git.direct('init')
.then(function(res){ //Equivalent to 'git init'
git.direct('add *'); //Equivalent to 'git add *'
}).then(function(err){
console.error(err)
});
Add
git.add(command, options)
Same as 'git add [command]'
- To add all files in /tmp/git and the commit them
git.add('*', {cwd:'/tmp/git'}).then(function(msg){
return git.commit('-m "My commit"');
}).then(function(res){
console.log(res);
}.fail(function(err){
console.error(err);
});
Bisect
git.bisect(command, options);
Same as 'git bisect [command]'
Branch
git.branch(command, options);
Same as 'git branch [command]'
- To get current branch
git.branch().then(function(res){
console.log(res) // master
}).catch(function(err){
console.log(err();
});
Checkout
git.checkout(command, options);
Same as 'git checkout [command]'
- To change to branch test
git.checkout('test').then(function(res){
console.log(res);
}).catch(function(err){
console.error(err);
});
Clone
git.clone(command, options);
Same as 'git clone [command]'
- To clone a git repo on current folder
git.clone('https://github.com/sayden/git-command-line.git').then(function(res){
console.log(res);
}).catch(function(err){
console.error(err);
});
- To clone a git repo on /tmp
git.clone('https://github.com/sayden/git-command-line.git /tmp').then(function(res){
console.log(res);
}).catch(function(err){
console.error(err);
});
Commit
git.commit(command, options);
Same as 'git commit [command]'
- Examples
var myGitRepo = '/tmp/gitTemp'; //This is where the command will be executed
var git = new Git(myGitRepo);
git.commit('-m "My commit"', {cwd:myGitRepo}) //Equivalent to 'git commit -m "My commit"'
.then(function(msg){
console.log(msg)
}).fail(function(err){
console.log(err);
});
Diff
git.diff(command, options);
Same as 'git diff [command]'
Fetch
git.fetch(command, options);
Same as 'git fetch [command]'
Grep
git.grep(command, options);
Same as 'git grep [command]'
Init
git.init(command, options);
Same as 'git init [command]'
Log
git.log(command, options);
Same as 'git log [command]'
Merge
git.merge(command, options);
Same as 'git merge [command]'
MV
git.mv(command, options);
Same as 'git mv [command]'
Pull
git.pull(command, options);
Same as 'git pull [command]'
Push
git.push(command, options);
Same as 'git push [command]'
Rebase
git.rebase(command, options);
Same as 'git rebase [command]'
Remote
git.remote(command, options);
Same as 'git remote [command]'
Reset
git.reset(command, options);
Same as 'git reset [command]'
RM
git.rm(command, options);
Same as 'git rm [command]'
Show
git.show(command, options);
Same as 'git show [command]' or simple 'git show' if no param specified
Status
git.status(command, options);
Same as 'git status [command]' or simply 'git status' if no param specified
- Examples
git.status()
.then(function(res){
console.log(res);
}).fail(function(err){
console.log(err);
});
//Or...
git.status('-h')
.then(function(res){
console.log(res);
}).fail(function(err){
console.log(err);
});
Tag
git.tag(command, options);
Same as 'git tag [command]'
- Examples
git.tag('0.1.0').then(function(res){
console.log(res);
git.cwd = newPath
Sets the working path for the following git commands
git.logging = boolean
Sets the logging of the git command line responses