Skip to content

Commit 01fbc0c

Browse files
committed
Add getStatus convenience method to repository
1 parent 5b4c1ec commit 01fbc0c

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

lib/repository.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ var Commit = require("./commit");
88
var Remote = require("./remote");
99
var Promise = require("nodegit-promise");
1010
var normalizeOptions = require("./util/normalize_options");
11+
var Status = require("./status");
12+
var StatusFile = require("./status_file");
1113

1214
var TreeBuilder = NodeGit.Treebuilder;
1315
var Repository = NodeGit.Repository;
@@ -648,5 +650,28 @@ Repository.initExt = function(repo_path, opts) {
648650
return initExt(repo_path, opts);
649651
};
650652

653+
/**
654+
* Get the status of a repo to it's working directory
655+
*
656+
* @param {obj} opts
657+
* @return {Object} Promise object.
658+
*/
659+
Repository.prototype.getStatus = function(opts) {
660+
var statuses = [];
661+
var statusCallback = function(path, status) {
662+
statuses.push(new StatusFile(path, status));
663+
};
664+
665+
if (!opts) {
666+
opts = {
667+
flags: Status.OPT.INCLUDE_UNTRACKED +
668+
Status.OPT.RECURSE_UNTRACKED_DIRS
669+
};
670+
}
671+
672+
return Status.foreachExt(this, opts, statusCallback).then(function() {
673+
return statuses;
674+
});
675+
};
651676

652677
module.exports = Repository;

test/tests/repository.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,21 @@ describe("Repository", function() {
7575

7676
assert(sig instanceof Signature);
7777
});
78+
79+
it("gets statuses with StatusFile", function() {
80+
var fileName = "my-new-file-that-shouldnt-exist";
81+
var fileContent = "new file";
82+
var repo = this.repository;
83+
var filePath = path.join(repo.workdir(), fileName);
84+
85+
return fse.writeFile(filePath, fileContent)
86+
.then(function() {
87+
return repo.getStatus().then(function(statuses) {
88+
assert.equal(statuses.length, 1);
89+
assert.equal(statuses[0].path(), fileName);
90+
assert.ok(statuses[0].isNew());
91+
return fse.unlink(filePath);
92+
});
93+
});
94+
});
7895
});

0 commit comments

Comments
 (0)