Skip to content

Commit 3e4a311

Browse files
author
John Haley
committed
Make diff functions async
1 parent 090634f commit 3e4a311

File tree

6 files changed

+229
-106
lines changed

6 files changed

+229
-106
lines changed

generate/input/descriptor.json

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,21 +1263,32 @@
12631263
"ignore": true
12641264
},
12651265
"git_patch_from_diff": {
1266-
"isAsync": false
1266+
"isAsync": true,
1267+
"return": {
1268+
"isErrorCode": true
1269+
}
12671270
},
12681271
"git_patch_get_hunk": {
12691272
"args": {
12701273
"out": {
12711274
"returnName": "hunk"
12721275
},
12731276
"lines_in_hunk": {
1277+
"shouldAlloc": true,
1278+
"returnName": "linesInHunk",
12741279
"isReturn": true
12751280
}
12761281
},
1277-
"isAsync": false
1282+
"isAsync": true,
1283+
"return": {
1284+
"isErrorCode": true
1285+
}
12781286
},
12791287
"git_patch_get_line_in_hunk": {
1280-
"isAsync": false
1288+
"isAsync": true,
1289+
"return": {
1290+
"isErrorCode": true
1291+
}
12811292
},
12821293
"git_patch_line_stats": {
12831294
"ignore": true

lib/convenient_hunk.js

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
var NodeGit = require("../");
2+
var Promise = require("nodegit-promise");
23
var ConvenientLine = NodeGit.ConvenientLine;
34

4-
function ConvenientHunk(raw, i) {
5-
this.raw = raw;
5+
function ConvenientHunk(hunk, linesInHunk, patch, i) {
6+
this.hunk = hunk;
7+
this.linesInHunk = linesInHunk;
8+
this.patch = patch;
69
this.i = i;
710
}
811

@@ -12,27 +15,48 @@ function ConvenientHunk(raw, i) {
1215
* @return {String}
1316
*/
1417
ConvenientHunk.prototype.header = function() {
15-
return this.raw.getHunk(this.i).hunk.header();
18+
return this.hunk.header();
1619
};
1720

1821
/**
1922
* Number of lines in this hunk
2023
* @return {Number}
2124
*/
2225
ConvenientHunk.prototype.size = function() {
23-
return this.raw.numLinesInHunk(this.i);
26+
return this.linesInHunk;
2427
};
2528

2629
/**
2730
* The lines in this hunk
28-
* @return {[ConvenientLine]} array of ConvenientLines
31+
* @return Promise({[ConvenientLine]}) a promise that resolves to an array of
32+
* ConvenientLines
2933
*/
3034
ConvenientHunk.prototype.lines = function() {
35+
var _this = this;
36+
var size = _this.size();
3137
var result = [];
32-
for (var i = 0; i < this.size(); i++) {
33-
result.push(new ConvenientLine(this.raw.getLineInHunk(this.i, i), i));
38+
var linePromises = [];
39+
var i;
40+
41+
function makeLinePromise(i) {
42+
return _this.patch.getLineInHunk(_this.i, i)
43+
.then(function(line) {
44+
result.push(new ConvenientLine(line, i));
45+
});
46+
}
47+
48+
for (i = 0; i < size; i++) {
49+
linePromises.push(makeLinePromise(i));
3450
}
35-
return result;
51+
52+
return Promise.all(linePromises)
53+
.then(function() {
54+
// On each ConvenientLine we have an 'i' property. Our result should
55+
// return those lines in the correct order.
56+
return result.sort(function(a, b) {
57+
return a.i > b.i;
58+
});
59+
});
3660
};
3761

3862
NodeGit.ConvenientHunk = ConvenientHunk;

lib/convenient_patch.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
var NodeGit = require("../");
2+
var Promise = require("nodegit-promise");
23
var Diff = NodeGit.Diff;
34
var ConvenientHunk = NodeGit.ConvenientHunk;
45

5-
function ConvenientPatch(delta, patch) {
6+
function ConvenientPatch(delta, patch, i) {
67
this.delta = delta;
78
this.patch = patch;
9+
this.i = i;
810
}
911

1012
/**
@@ -33,16 +35,42 @@ ConvenientPatch.prototype.size = function() {
3335

3436
/**
3537
* The hunks in this patch
36-
* @return {[ConvenientHunk]} an array of ConvenientHunks
38+
* @return Promise({[ConvenientHunk]}) a promise that resolves to an array of
39+
* ConvenientHunks
3740
*/
3841
ConvenientPatch.prototype.hunks = function() {
42+
var _this = this;
43+
var size = _this.size();
3944
var result = [];
45+
var hunkPromises = [];
46+
var i;
47+
48+
function makeHunkPromise(i) {
49+
return _this.patch.getHunk(i)
50+
.then(function(hunkWithLineCount) {
51+
result.push(
52+
new ConvenientHunk(
53+
hunkWithLineCount.hunk,
54+
hunkWithLineCount.linesInHunk,
55+
_this.patch,
56+
i
57+
)
58+
);
59+
});
60+
}
4061

41-
for (var i = 0; i < this.size(); i++) {
42-
result.push(new ConvenientHunk(this.patch, i));
62+
for (i = 0; i < size; i++) {
63+
hunkPromises.push(makeHunkPromise(i));
4364
}
4465

45-
return result;
66+
return Promise.all(hunkPromises)
67+
.then(function() {
68+
// On each ConvenientHunk we have an 'i' property. Our result should
69+
// return those lines in the correct order.
70+
return result.sort(function(a, b) {
71+
return a.i > b.i;
72+
});
73+
});
4674
};
4775

4876
/**

lib/diff.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var NodeGit = require("../");
2+
var Promise = require("nodegit-promise");
23
var Diff = NodeGit.Diff;
34
var ConvenientPatch = NodeGit.ConvenientPatch;
45
var normalizeOptions = NodeGit.Utils.normalizeOptions;
@@ -8,17 +9,35 @@ var Patch = NodeGit.Patch;
89
/**
910
* Retrieve patches in this difflist
1011
*
11-
* @return {[ConvenientPatch]} an array of ConvenientPatches
12+
* @return Promise({[ConvenientPatch]}) a promise that resolves to an array of
13+
* ConvenientPatches
1214
*/
1315
Diff.prototype.patches = function() {
14-
var size = this.numDeltas();
16+
var _this = this;
17+
var size = _this.numDeltas();
1518
var result = [];
19+
var patchPromises = [];
20+
var i;
1621

17-
for (var i = 0; i < size; i++) {
18-
result.push(new ConvenientPatch(this.getDelta(i), Patch.fromDiff(this, i)));
22+
function makePatchPromise(i) {
23+
return Patch.fromDiff(_this, i)
24+
.then(function(patch) {
25+
result.push(new ConvenientPatch(_this.getDelta(i), patch, i));
26+
});
1927
}
2028

21-
return result;
29+
for (i = 0; i < size; i++) {
30+
patchPromises.push(makePatchPromise(i));
31+
}
32+
33+
return Promise.all(patchPromises)
34+
.then(function() {
35+
// On each ConvenientPatch we have an 'i' property. Our result should
36+
// return those patches in the correct order.
37+
return result.sort(function(a, b) {
38+
return a.i > b.i;
39+
});
40+
});
2241
};
2342

2443
// Override Diff.indexToWorkdir to normalize opts

lib/repository.js

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -798,16 +798,13 @@ Repository.prototype.rebaseBranches = function(
798798
signature)
799799
{
800800
var repo = this;
801-
var branchCommit;
802-
var upstreamCommit;
803-
var ontoCommit;
804801

805802
signature = signature || repo.defaultSignature();
806803

807804
return Promise.all([
808805
repo.getReference(branch),
809806
upstream ? repo.getReference(upstream) : null,
810-
onto ? repo.getReference(onto) : null
807+
onto ? repo.getReference(upstream) : null
811808
])
812809
.then(function(refs) {
813810
return Promise.all([
@@ -817,38 +814,17 @@ Repository.prototype.rebaseBranches = function(
817814
]);
818815
})
819816
.then(function(annotatedCommits) {
820-
branchCommit = annotatedCommits[0];
821-
upstreamCommit = annotatedCommits[1];
822-
ontoCommit = annotatedCommits[2];
823-
824-
return NodeGit.Merge.base(repo, branchCommit.id(), upstreamCommit.id());
817+
return NodeGit.Rebase.init(repo, annotatedCommits[0], annotatedCommits[1],
818+
annotatedCommits[2], signature, null);
825819
})
826-
.then(function(oid) {
827-
if (oid.toString() === branchCommit.id().toString()) {
828-
// we just need to fast-forward
829-
return repo.mergeBranches(branch, upstream)
830-
.then(function() {
831-
// checkout 'branch' to match the behavior of rebase
832-
return repo.checkoutBranch(branch);
833-
});
834-
} else if (oid.toString() === upstreamCommit.id().toString()) {
835-
// 'branch' is already on top of 'upstream'
836-
// checkout 'branch' to match the behavior of rebase
837-
return repo.checkoutBranch(branch);
820+
.then(function(rebase) {
821+
return performRebase(repo, rebase, signature);
822+
})
823+
.then(function(error) {
824+
if (error) {
825+
throw error;
838826
}
839827

840-
return NodeGit.Rebase.init(repo, branchCommit, upstreamCommit, ontoCommit,
841-
signature, null)
842-
.then(function(rebase) {
843-
return performRebase(repo, rebase, signature);
844-
})
845-
.then(function(error) {
846-
if (error) {
847-
throw error;
848-
}
849-
});
850-
})
851-
.then(function() {
852828
return repo.getBranchCommit("HEAD");
853829
});
854830
};

0 commit comments

Comments
 (0)