Skip to content

Commit

Permalink
Merge 952c6f0 into 090634f
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhaley81 committed Jun 13, 2015
2 parents 090634f + 952c6f0 commit 88606e6
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 73 deletions.
17 changes: 14 additions & 3 deletions generate/input/descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -1263,21 +1263,32 @@
"ignore": true
},
"git_patch_from_diff": {
"isAsync": false
"isAsync": true,
"return": {
"isErrorCode": true
}
},
"git_patch_get_hunk": {
"args": {
"out": {
"returnName": "hunk"
},
"lines_in_hunk": {
"shouldAlloc": true,
"returnName": "linesInHunk",
"isReturn": true
}
},
"isAsync": false
"isAsync": true,
"return": {
"isErrorCode": true
}
},
"git_patch_get_line_in_hunk": {
"isAsync": false
"isAsync": true,
"return": {
"isErrorCode": true
}
},
"git_patch_line_stats": {
"ignore": true
Expand Down
40 changes: 32 additions & 8 deletions lib/convenient_hunk.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
var NodeGit = require("../");
var Promise = require("nodegit-promise");
var ConvenientLine = NodeGit.ConvenientLine;

function ConvenientHunk(raw, i) {
this.raw = raw;
function ConvenientHunk(hunk, linesInHunk, patch, i) {
this.hunk = hunk;
this.linesInHunk = linesInHunk;
this.patch = patch;
this.i = i;
}

Expand All @@ -12,27 +15,48 @@ function ConvenientHunk(raw, i) {
* @return {String}
*/
ConvenientHunk.prototype.header = function() {
return this.raw.getHunk(this.i).hunk.header();
return this.hunk.header();
};

/**
* Number of lines in this hunk
* @return {Number}
*/
ConvenientHunk.prototype.size = function() {
return this.raw.numLinesInHunk(this.i);
return this.linesInHunk;
};

/**
* The lines in this hunk
* @return {[ConvenientLine]} array of ConvenientLines
* @return Promise({[ConvenientLine]}) a promise that resolves to an array of
* ConvenientLines
*/
ConvenientHunk.prototype.lines = function() {
var _this = this;
var size = _this.size();
var result = [];
for (var i = 0; i < this.size(); i++) {
result.push(new ConvenientLine(this.raw.getLineInHunk(this.i, i), i));
var linePromises = [];
var i;

function makeLinePromise(i) {
return _this.patch.getLineInHunk(_this.i, i)
.then(function(line) {
result.push(new ConvenientLine(line, i));
});
}

for (i = 0; i < size; i++) {
linePromises.push(makeLinePromise(i));
}
return result;

return Promise.all(linePromises)
.then(function() {
// On each ConvenientLine we have an 'i' property. Our result should
// return those lines in the correct order.
return result.sort(function(a, b) {
return a.i > b.i;
});
});
};

NodeGit.ConvenientHunk = ConvenientHunk;
38 changes: 33 additions & 5 deletions lib/convenient_patch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var NodeGit = require("../");
var Promise = require("nodegit-promise");
var Diff = NodeGit.Diff;
var ConvenientHunk = NodeGit.ConvenientHunk;

function ConvenientPatch(delta, patch) {
function ConvenientPatch(delta, patch, i) {
this.delta = delta;
this.patch = patch;
this.i = i;
}

/**
Expand Down Expand Up @@ -33,16 +35,42 @@ ConvenientPatch.prototype.size = function() {

/**
* The hunks in this patch
* @return {[ConvenientHunk]} an array of ConvenientHunks
* @return Promise({[ConvenientHunk]}) a promise that resolves to an array of
* ConvenientHunks
*/
ConvenientPatch.prototype.hunks = function() {
var _this = this;
var size = _this.size();
var result = [];
var hunkPromises = [];
var i;

function makeHunkPromise(i) {
return _this.patch.getHunk(i)
.then(function(hunkWithLineCount) {
result.push(
new ConvenientHunk(
hunkWithLineCount.hunk,
hunkWithLineCount.linesInHunk,
_this.patch,
i
)
);
});
}

for (var i = 0; i < this.size(); i++) {
result.push(new ConvenientHunk(this.patch, i));
for (i = 0; i < size; i++) {
hunkPromises.push(makeHunkPromise(i));
}

return result;
return Promise.all(hunkPromises)
.then(function() {
// On each ConvenientHunk we have an 'i' property. Our result should
// return those lines in the correct order.
return result.sort(function(a, b) {
return a.i > b.i;
});
});
};

/**
Expand Down
29 changes: 24 additions & 5 deletions lib/diff.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var NodeGit = require("../");
var Promise = require("nodegit-promise");
var Diff = NodeGit.Diff;
var ConvenientPatch = NodeGit.ConvenientPatch;
var normalizeOptions = NodeGit.Utils.normalizeOptions;
Expand All @@ -8,17 +9,35 @@ var Patch = NodeGit.Patch;
/**
* Retrieve patches in this difflist
*
* @return {[ConvenientPatch]} an array of ConvenientPatches
* @return Promise({[ConvenientPatch]}) a promise that resolves to an array of
* ConvenientPatches
*/
Diff.prototype.patches = function() {
var size = this.numDeltas();
var _this = this;
var size = _this.numDeltas();
var result = [];
var patchPromises = [];
var i;

for (var i = 0; i < size; i++) {
result.push(new ConvenientPatch(this.getDelta(i), Patch.fromDiff(this, i)));
function makePatchPromise(i) {
return Patch.fromDiff(_this, i)
.then(function(patch) {
result.push(new ConvenientPatch(_this.getDelta(i), patch, i));
});
}

return result;
for (i = 0; i < size; i++) {
patchPromises.push(makePatchPromise(i));
}

return Promise.all(patchPromises)
.then(function() {
// On each ConvenientPatch we have an 'i' property. Our result should
// return those patches in the correct order.
return result.sort(function(a, b) {
return a.i > b.i;
});
});
};

// Override Diff.indexToWorkdir to normalize opts
Expand Down

0 comments on commit 88606e6

Please sign in to comment.