Skip to content

Commit 4947ecf

Browse files
committed
Add Repository.getStatusExt
1 parent 93f3ce0 commit 4947ecf

4 files changed

Lines changed: 89 additions & 5 deletions

File tree

lib/repository.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var Promise = require("nodegit-promise");
1010
var normalizeOptions = require("./util/normalize_options");
1111
var Status = require("./status");
1212
var StatusFile = require("./status_file");
13+
var StatusList = require("./status_list");
1314

1415
var TreeBuilder = NodeGit.Treebuilder;
1516
var Repository = NodeGit.Repository;
@@ -727,17 +728,17 @@ Repository.initExt = function(repo_path, opts) {
727728
* Get the status of a repo to it's working directory
728729
*
729730
* @param {obj} opts
730-
* @return {Object} Promise object.
731+
* @return {Array<StatusFile>}
731732
*/
732733
Repository.prototype.getStatus = function(opts) {
733734
var statuses = [];
734735
var statusCallback = function(path, status) {
735-
statuses.push(new StatusFile(path, status));
736+
statuses.push(new StatusFile({path: path, status: status}));
736737
};
737738

738739
if (!opts) {
739740
opts = {
740-
flags: Status.OPT.INCLUDE_UNTRACKED +
741+
flags: Status.OPT.INCLUDE_UNTRACKED |
741742
Status.OPT.RECURSE_UNTRACKED_DIRS
742743
};
743744
}
@@ -747,4 +748,34 @@ Repository.prototype.getStatus = function(opts) {
747748
});
748749
};
749750

751+
/**
752+
* Get extended statuses of a repo to it's working directory. Status entries
753+
* have `status`, `headToIndex` delta, and `indexToWorkdir` deltas
754+
*
755+
* @param {obj} opts
756+
* @return {Array<StatusEntry>}
757+
*/
758+
Repository.prototype.getStatusExt = function(opts) {
759+
var statuses = [];
760+
761+
if (!opts) {
762+
opts = {
763+
flags: Status.OPT.INCLUDE_UNTRACKED |
764+
Status.OPT.RECURSE_UNTRACKED_DIRS |
765+
Status.OPT.RENAMES_INDEX_TO_WORKDIR |
766+
Status.OPT.RENAMES_HEAD_TO_INDEX |
767+
Status.OPT.RENAMES_FROM_REWRITES
768+
};
769+
}
770+
771+
return StatusList.create(this, opts).then(function(list) {
772+
for (var i = 0; i < list.entrycount(); i++) {
773+
var entry = Status.byIndex(list, i);
774+
statuses.push(new StatusFile({entry: entry}));
775+
}
776+
777+
return statuses;
778+
});
779+
};
780+
750781
module.exports = Repository;

lib/status_file.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,19 @@ var NodeGit = require("../");
22

33
var Status = NodeGit.Status;
44

5-
var StatusFile = function(path, status) {
5+
var StatusFile = function(args) {
6+
var path = args.path;
7+
var status = args.status;
8+
var entry = args.entry;
9+
10+
if (entry) {
11+
status = entry.status();
12+
if (entry.indexToWorkdir()) {
13+
path = entry.indexToWorkdir().newFile().path();
14+
} else {
15+
path = entry.headToIndex().newFile().path();
16+
}
17+
}
618

719
var codes = Status.STATUS;
820

@@ -20,6 +32,7 @@ var StatusFile = function(path, status) {
2032

2133
var data = {
2234
path: path,
35+
entry: entry,
2336
statusBit: status,
2437
statuses: getStatus()
2538
};
@@ -31,6 +44,20 @@ var StatusFile = function(path, status) {
3144
statusBit: function() {
3245
return data.statusBit;
3346
},
47+
headToIndex: function() {
48+
if (data.entry) {
49+
return entry.headToIndex();
50+
} else {
51+
return undefined;
52+
}
53+
},
54+
indexToWorkdir: function() {
55+
if (data.entry) {
56+
return entry.indexToWorkdir();
57+
} else {
58+
return undefined;
59+
}
60+
},
3461
path: function() {
3562
return data.path;
3663
},

test/tests/repository.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,30 @@ describe("Repository", function() {
110110
});
111111
});
112112
});
113+
114+
it("gets extended statuses", function() {
115+
var fileName = "my-new-file-that-shouldnt-exist.file";
116+
var fileContent = "new file from repository test";
117+
var repo = this.repository;
118+
var filePath = path.join(repo.workdir(), fileName);
119+
120+
return fse.writeFile(filePath, fileContent)
121+
.then(function() {
122+
return repo.getStatusExt().then(function(statuses) {
123+
assert.equal(statuses.length, 1);
124+
assert.equal(statuses[0].path(), fileName);
125+
assert.equal(statuses[0].indexToWorkdir().newFile().path(), fileName);
126+
assert.ok(statuses[0].isNew());
127+
});
128+
})
129+
.then(function() {
130+
return fse.remove(filePath);
131+
})
132+
.catch(function (e) {
133+
return fse.remove(filePath)
134+
.then(function() {
135+
return Promise.reject(e);
136+
});
137+
});
138+
});
113139
});

test/tests/status_file.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe("StatusFile", function() {
1010
var statusCode = Status.STATUS.WT_NEW;
1111

1212
before(function() {
13-
this.status = new StatusFile(pathName, statusCode);
13+
this.status = new StatusFile({path: pathName, status: statusCode});
1414
});
1515

1616
it("passes the path to the working function", function() {

0 commit comments

Comments
 (0)