steveukx / git-js Goto Github PK
View Code? Open in Web Editor NEWA light weight interface for running git commands in any node.js application.
License: MIT License
A light weight interface for running git commands in any node.js application.
License: MIT License
When using git-js to do a pull, it fails to detect if there were changes that were only deletions. If there were no insertions, the "update" parameter returns a response saying that nothing has changed, when in fact I did delete a single line:
{"files":[],"insertions":{},"deletions":{},"summary":{"changes":0,"insertions":0,"deletions":0}}
It appears this is due to this line in git.js: _parsePull:
var fileUpdateRegex = /^\s*(.+)\s\|\s(\d+)\s([\+]+)/;
The line to capture looks like this:
src/TestComponent/TestComponent.scss | 1 -
The regular expression will capture if a minus sign is added:
var fileUpdateRegex = /^\s*(.+)\s\|\s(\d+)\s([\+\-]+)/;
Here is how we are using git-js and then check the results:
simpleGit.pull('origin', 'staging', function doPull(err, update) {
try {
console.log(JSON.stringify(update));
if (update
&& (Object.keys(update.insertions).length > 0
|| Object.keys(update.deletions).length > 0)) {
...
This would be really useful
I'm trying to checkout a hash tag referencing a commit. Is this functionality included?
Neither of the following APIs seem to work with hash values.
repo.checkout(hash, function(error, data) { ... });
repo.checkoutLocal(hash, function(error, data) { ... });
Given that there is a .then
method, it seems to imply that a Promise is returned, but that's not actually the case. So if you want a Promise, you have to crudely wrap it:
let promise = new Promise(resolve => {
simpleGit(dir)
.add('./*')
.commit('New')
.push('origin', 'master')
.then(resolve);
});
So could support be added for Promises? One big benefit is that consumers could then use .catch
for errors.
can you Give the interface reset a commit id parameter it like: git reset commitId
I'm trying to use npm simple-git in windows machine. But when I run it with node.js it shows me the following error message.
events.js:85
throw er; // Unhandled 'error' event
^
Error: spawn git ENOENT
Node version: v0.12.2
npm version: 2.7.4
Hi Guys,
This is a nice lib, thanks for your work. I ran in to an issue whereby I wanted to look at just merge changes between branches.
I'm going to push a PR for this shortly (it's really simple) - would love some feedback.
Steps to test:
In your config object, set the following:
config.merges: true,
config.branch: qa
When I create a bare repository prompted the following errors:
Error: spawn git ENOENT
at exports._errnoException (util.js: 860: 11)
at Process.ChildProcess._handle.onexit (internal / child_process.js: 178: 32)
at onErrorNT (internal / child_process.js: 344: 16)
at doNTCallback2 (node.js: 450: 9)
at process._tickCallback (node.js: 364: 17)
at Function.Module.runMain (module.js: 459: 11)
at startup (node.js: 136: 18)
at node.js: 972: 3
code:
var simpleGit = require('simple-git');
var bareDir = 'test.git';
simpleGit(bareDir).init(true, function() {
console.log("init bare done");
});
Feature to compare 2 arbitrary commits
Tested on git version 2.7.4 and 1.8.3.1
Simple-git version 1.38.0
var repo = require('simple-git')('.');
repo.status(function(err, data) {
// data.deleted is [ 'asdf', 'qwer' ]
});
repo.rm(['asdf', 'qwer'], function(err) {
// err is: fatal: pathspec 'asdf,qwer' did not match any files
});
Hi,
I am looking for a project like yours.
I would like to clone and pull a private git repository.
Can I provide a user/password with your api ?
When you use a then() block, you'll get this error with node 0.10.29 and later:
TypeError: Incorrect value of args option
at Object.exports.spawn (child_process.js:725:11)
at Git._schedule (/home/airspring/scratch/node_modules/simple-git/src/git.js:651:40)
at process._tickCallback (node.js:442:13)
Platform: OSX
simple-git: 1.3.0
It works in:
0.10.26
0.10.27
0.10.28
It fails in:
0.10.29
0.10.30
0.10.31
0.10.33
0.10.38
I also tested on Linux, and it failed there too with node 0.10.26.
I did not not test node 0.12.x
I'm suspicious of my report since it means this problem has been undetected since node 0.10.29 was released about a year ago: http://blog.nodejs.org/2014/06/16/node-v0-10-29-stable/ But it's easy to reproduce:
if (process.argv.length < 3) {
console.log('need a path to a git repo');
process.exit(-1);
}
var path_to_repo = process.argv[2];
console.log('running git status for ' + path_to_repo);
require('simple-git')(path_to_repo)
.status(function(err, status) {
})
.then(function() {
console.log('status done.');
});
Expected behavior: 'status done' prints out without any errors
Actual behavior: message is not printed. The error noted above is seen.
How can I accomplish git checkout -- <filename>
? Or rather git checkout <filename>
? The checkout()
method doesn't seem to accept file name.
Is there any way to have different credentials? can you set what credentials to use for a git call? For example, repository 1 uses my personal credentials while repository 2 uses my work credentials. Is this possible?
After rebuilding my webserver, I accidentally did not run git config --global user.email "[email protected]"
or git config --global user.name "Your Name"
. This oversight seemed to cause git.commit()
to 'hang'. Here is the general idea:
git('/path/to/repo')
.add('./*')
.commit('commit message', function(e, data) {
console.log(e); //null
console.log(data); //false
})
.revparse(['HEAD'], function(e, data) {
//not reached
});
Inside the commit()
callback, e
is set to null
and data
is set to false
. Additionally, the revparse()
callback is not executed.
Is this the intended/expected behavior? If you use git commit
from the command line before running either of the config steps, you will see a message that looks something like this:
19:00:28: *** Please tell me who you are.
Run
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
Do you think this message should be interpreted as an error? In any case, should git.commit()
and related methods provide some sort of indication that the global git config has not been set?
Just thought I'd get your opinion on this before I looked at how to do it.
Is there any possible way, outside of require
to change between multiple working directories?
I am working in a project that could have multiple git repositories and I need to switch between these folders and branch/commit/push each of them on specific route calls.
"TypeError: Cannot read property '1' of null"
This error appear when I try to commit but there is nothing to commit
I'm not sure if I just completely missed something but I can't find a implementation of submodule init
This module should not be used with any user input as it is very easy to inject code:
~/src/git-js(master ✗) node -e "require('./src/git.js')('.').add('\";echo HELLO WORLD > foo\"')"
~/src/git-js(master ✗) cat foo
HELLO WORLD
~/src/git-js(master ✗)
RT, how can I do the command git config --get remote.origin.url
to show the remote url.
Adding an option (or defaulting) to verbose output would be amazing. When cloning large repo's, the script appears to hang.
EG, when cloning, would be amazing to see the native Git output while the clone was in progress.
I'm not a Node expert, though, so let me know if that's an easy change for me to make myself on my machine.
Is there a way I can checkout a local branch? I just want to run git checkout -b develop
but it looks like I can't use checkout() or checkoutBranch(). Am I missing something or is this a feature that does not exist?
Would that be something you could possibly add?
Thanks!
Thank you for the easy to use library.
I may have missed it, but the current API doesn't seem to support git merge
, so to merge branches I am doing this:
simpleGit._run(['merge', 'master', 'branch1'], function(err, data) { ... });
Support for merge would be greatly appreciated.
Hi, I'm trying to get a diff from another branch and I'm not sure if this is the correct way:
simpleGit.diff(['HEAD remotes/origin/branch_name --name-only'], function (err, diff)...
This is the error I get:
fatal: ambiguous argument 'HEAD remotes/origin/bugfixing_v1 --name-only': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git [...] -- [...]'
Works fine when running from the command line.
Is it the right way to do it or maybe a bug?
Thank you.
Is it possible to pass in authorization (such as a GitHub token) on methods such as .clone() for private repos, instead of my current method of adding a username and password in to the remote?
I got leading single quotes for commit hash and trailing ones for emails from the result of the log
calls:
{ latest:
{ hash: '\'b1a3d56dd2065f58835d320901f0fdc905f9d5cb',
...
author_email: '[email protected]\''},
...
}
Which, git log origin/master..master
returns normally:
commit b1a3d56dd2065f58835d320901f0fdc905f9d5cb
I were using [email protected] with [email protected] on Mac.
Is this normal or a bug?
RT~. So can get the remote info with this api.
None of the git remote
methods are supported - requires support for git remote add
and git remote remove
R0.6.0 will include diff functionality, the response for which is raw output from Git. Ideally this would be parsed to give a more meaningful object than just plain text.
git push
not currently supported, pushing to default and to named remote and branch should both be possible.
When trying to use tag with 1.15, worked with 1.14
git rm
currently unsupported.
Hey @steveukx, looks like a good package here. I have a project that uses a much more janky implementation. I would like to switch over to this package, but I need a few more features. If it is good with you, I will fork and upstream the features.
The commands I need are:
git init
git submodule [add, init, update]
git ls-remote --tags
Do you have contributor guidelines? Does this all sound good?
Hi thanks for working on this library.
What do you guys think about making startPoint optional so that I can do:
simpleGit(root).checkoutBranch('my_new_branch')
that results in:
git checkout -b my_new_branch
If you guys agree with this change I can send a PR.
If i try to use the fetch() method I get this error:
C:\Users\dlowerre\full_throttle\node_modules\simple-git\src\git.js:171
then && then(err, !err && this._parsePush(data));
^
TypeError: Object # has no method '_parsePush'
There is indeed no function '_parsePush' defined for the Git object. Perhaps this should be a call to _parsePull?
It turns out I needed to do a 'pull' instead of a 'fetch' so I am not stuck on this problem, but I thought I would let you know about it.
Using 1.37 on OS X with git 2.8.4 and node 4.4.4.
My repository looks like this:
$ git tag -l
2016_06_08_NEW
2016_06_08_NEW2
2016_06_08_NEW23
repo.tags() results in
[
"2016_06_08_NEW",
"2016_06_08_NEW2",
"2016_06_08_NEW23",
""
]
So an array of 3 tags and an additional empty tag.
I fixed it for now by adding
.filter(function(item){return item !== "";})
Any idea if this a bug in my repo, in my code or in git-js? If anyone can reproduce it, I'd be happy to do a PR.
I assume it's because git adds a final \n to wrap the last line and .split("\n") returns the empty line, too.
It would be cool to bundle git.exe with the repo and alleviate the need for the user to have git installed on the system. I suppose this ticket would be to allow the configuration for a path to git from within the api.
@steveukx I am thinking of something like this:
Api | description |
---|---|
method | ..... |
method | ..... |
Let me know and I will gladly send a PR.
Whilst this command is featured in one of the examples it is not given as a command in the list of possible commands
The git.show function option evaluation differs compared to eg. git.log.
gitRepo
.log(function(err, log) {
console.log(log);
});
Gives me hashes with prepended ' and author_email's with appended '. And also the stdoutput is quoted with '
After checking out a branch which is detached, git branch command reports something akin to the following:
* (detached from First-tag) 573b9ed for todd
develop 1f5dd41 [behind 5] FirstNewFile.txt edited online with Bitbucket
feature/My-new-feature 1f5dd41 FirstNewFile.txt edited online with Bitbucket
master 1be9589 add to master
Note the format of the first line. This causes a problem in regular expression in branchSummary.js:
BranchSummary.parse = function (commit) {
var branchSummary = new BranchSummary();
commit.split('\n')
.forEach(function (line) {
var branch = /^(\*?\s+)(\S+)\s+([a-z0-9]+)\s(.*)$/.exec(line);
if (branch) {
branchSummary.push(
branch[1].charAt(0) === '*',
branch[2],
branch[3],
branch[4]
);
}
});
return branchSummary;
};
My local fix looks like this:
BranchSummary.parse = function (commit) {
var branchSummary = new BranchSummary();
commit.split('\n')
.forEach(function (line) {
if (line.charAt(2) === '(') var branch = /^(\*?\s+)\(([^)]+)\)\s+([a-z0-9]+)\s(.*)$/.exec(line)
else var branch = /^(\*?\s+)(\S+)\s+([a-z0-9]+)\s(.*)$/.exec(line);
if (branch) {
branchSummary.push(
branch[1].charAt(0) === '*',
branch[2],
branch[3],
branch[4]
);
}
});
return branchSummary;
};
There may be better other ways to deal with this but hopefully this helps.
Todd
Thank you for this easy to use library! A colleague of mine have been writing commits that included semicolons (;). Now I try to make some statistics from git log, and it fails to parse it correctly. Looking at the code, I found that for log, you use
var command = ["log", "--pretty=format:'%H;%ai;%s%d;%aN;%ae'"];
To overcome the mentioned issue, I replaced semicolons to triple-semicolons and all worked well again. (Also needed to replace in this function: Git.prototype._parseListLog
.)
Perhaps you could make it more reliable by selecting a more rare character, or adding an optional parameter for the separator. I would loved to see these changes and further use your great library!
I've gotten summary
strings with changes: 100644
(obviously the mode, not the change), file listings with ...
in them, and missing additions/deletions.
Due to how Git formats the output of pull
(even with -v
), it might be worthwhile to manually abridge the results of git diff <before-pull>...HEAD
instead of trying to parse the output of pull
.
I've been picking through the code, to be sure the functions do what I expect them to do, and surprisingly I'm finding that the descriptions are somewhat misleading, particularly when they involve a number of parameters (mergeFromTo
, pull
, etc), as the git commands being used use the parameters in a different way to what's described in the documentation.
For example, mergeFromTo
seems to indicate that it merges from
into to
(or vice versa?), but the git documentation indicates that it merges both into the current branch. Additionally, pull
indicates that it pulls a remote branch, which I'd assumed would pull the remote branch to the tracking branch, but actually, if you're not on the tracking branch, pull acts like a merge. It wouldn't surprise me if someone used this expecting some background checking out or something to merge the two specified branches.
At the very least, it might be worth linking to the relevant git docs for each function for transparency regarding both the function parameters and any option flags.
The title says it all :).
I need to force a checkout and dont see a way to do this until now.
Would it be possible to add a force flag (or a forceCheckout-method) to do that?
Would it be possible to add an option, allowing us to set the -u flag of the push command? I do
var Git = require('simple-git')(localPath);
Git.init()
.add('./*')
.commit("first commit!")
.addRemote('origin', 'some-repo-url')
.push('origin', 'master')
.then(fn);
and then i get the old and famous
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> master
Maybe there is another way of doing that and is currently available?
Regards, Dani
Great module, we're using it with great success.
I'd like to request a small feature addition - the ability to silence the error output to console. We're using this in an environment where we're curating our console output very carefully to deliver a clean experience for our build tools. We're already catching the errors that are thrown should there be an error in the git child process, so we don't need the extraneous console output.
This line here is the culprit: https://github.com/steveukx/git-js/blob/master/src/git.js#L627
Cheers
/node_modules/simple-git/src/git.js:483
next(err, !err && function () {
^
TypeError: next is not a function
Trying to execute simpleGit.getRemotes(true).then();
Would be great to be able to pass the --bare flat to init() to be able to set up a server-style bare repository
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.