|
1 | 1 | var git = require('../'), |
2 | 2 | events = require('events'); |
3 | 3 |
|
4 | | -var GitDiffList = function(rawRepo, rawDiffList) { |
5 | | - var self = { |
6 | | - repo: rawRepo, |
7 | | - diffList: rawDiffList || new git.raw.DiffList() |
8 | | - }; |
| 4 | +/** |
| 5 | + * Convenience diff list class. |
| 6 | + * |
| 7 | + * @param {git.raw.Repo} rawRepo |
| 8 | + * @param {[type]} rawDiffList [description] |
| 9 | + */ |
| 10 | +var DiffList = function(rawRepo, rawDiffList) { |
| 11 | + if (!(rawRepo instanceof git.raw.Repo)) { |
| 12 | + throw new git.error('First parameter for DiffList must be a raw repo'); |
| 13 | + } |
| 14 | + this.rawRepo = rawRepo; |
9 | 15 |
|
10 | | - self.walk = function() { |
11 | | - var event = new events.EventEmitter(), |
12 | | - allFileDeltas = []; |
| 16 | + if (rawDiffList instanceof git.raw.DiffList) { |
| 17 | + this.rawDiffList = rawDiffList; |
| 18 | + } else { |
| 19 | + this.rawDiffList = new git.raw.DiffList(); |
| 20 | + } |
| 21 | +}; |
13 | 22 |
|
14 | | - process.nextTick(function() { |
15 | | - self.diffList.walk(function fileCallback(error, fileDeltas) { |
16 | | - if (error) { |
17 | | - event.emit('end', error, null); |
18 | | - } |
19 | | - fileDeltas.forEach(function(fileDelta) { |
20 | | - event.emit('file', null, fileDelta); |
21 | | - allFileDeltas.push(fileDelta); |
22 | | - }); |
23 | | - }, function hunkCallback(error, diffHunk) { |
24 | | - // @todo implement? |
25 | | - }, function lineCallback(error, diffLine) { |
26 | | - // @todo implement? |
27 | | - }, function endCallback(error) { |
28 | | - event.emit('end', error, allFileDeltas); |
29 | | - }); |
30 | | - }); |
| 23 | +/** |
| 24 | + * Refer to vendor/libgit2/include/git2/diff.h for delta type definitions. |
| 25 | + * |
| 26 | + * @readonly |
| 27 | + * @enum {Integer} |
| 28 | + */ |
| 29 | +DiffList.prototype.deltaTypes = { |
| 30 | + /** 0 */ GIT_DELTA_UNMODIFIED: git.raw.DiffList.deltaTypes.GIT_DELTA_UNMODIFIED, |
| 31 | + /** 1 */ GIT_DELTA_ADDED: git.raw.DiffList.deltaTypes.GIT_DELTA_ADDED, |
| 32 | + /** 2 */ GIT_DELTA_DELETED: git.raw.DiffList.deltaTypes.GIT_DELTA_DELETED, |
| 33 | + /** 3 */ GIT_DELTA_MODIFIED: git.raw.DiffList.deltaTypes.GIT_DELTA_MODIFIED, |
| 34 | + /** 4 */ GIT_DELTA_RENAMED: git.raw.DiffList.deltaTypes.GIT_DELTA_RENAMED, |
| 35 | + /** 5 */ GIT_DELTA_COPIED: git.raw.DiffList.deltaTypes.GIT_DELTA_COPIED, |
| 36 | + /** 6 */ GIT_DELTA_IGNORED: git.raw.DiffList.deltaTypes.GIT_DELTA_IGNORED, |
| 37 | + /** 7 */ GIT_DELTA_UNTRACKED: git.raw.DiffList.deltaTypes.GIT_DELTA_UNTRACKED, |
| 38 | + /** 8 */ GIT_DELTA_TYPECHANGE: git.raw.DiffList.deltaTypes.GIT_DELTA_TYPECHANGE |
| 39 | +}; |
31 | 40 |
|
32 | | - return event; |
33 | | - }; |
| 41 | +/** |
| 42 | + * Refer to vendor/libgit2/include/git2/diff.h for line origin type definitions. |
| 43 | + * |
| 44 | + * @readOnly |
| 45 | + * @enum {String} |
| 46 | + */ |
| 47 | +DiffList.prototype.lineOriginTypes = { |
| 48 | + /** ' ' */ GIT_DIFF_LINE_CONTEXT: git.raw.DiffList.lineOriginTypes.GIT_DIFF_LINE_CONTEXT, |
| 49 | + /** '+' */ GIT_DIFF_LINE_ADDITION: git.raw.DiffList.lineOriginTypes.GIT_DIFF_LINE_ADDITION, |
| 50 | + /** '-' */ GIT_DIFF_LINE_DELETION: git.raw.DiffList.lineOriginTypes.GIT_DIFF_LINE_DELETION, |
| 51 | + /** '\n' */ GIT_DIFF_LINE_ADD_EOFNL: git.raw.DiffList.lineOriginTypes.GIT_DIFF_LINE_ADD_EOFNL, |
| 52 | + /** '' */ GIT_DIFF_LINE_DEL_EOFNL: git.raw.DiffList.lineOriginTypes.GIT_DIFF_LINE_DEL_EOFNL, |
| 53 | + /** 'F' */ GIT_DIFF_LINE_FILE_HDR: git.raw.DiffList.lineOriginTypes.GIT_DIFF_LINE_FILE_HDR, |
| 54 | + /** 'H' */ GIT_DIFF_LINE_HUNK_HDR: git.raw.DiffList.lineOriginTypes.GIT_DIFF_LINE_HUNK_HDR, |
| 55 | + /** 'B' */ GIT_DIFF_LINE_BINARY: git.raw.DiffList.lineOriginTypes.GIT_DIFF_LINE_BINARY |
| 56 | +}; |
34 | 57 |
|
35 | | - self.treeToTree = function(oldSha, newSha, callback) { |
36 | | - self.diffList.treeToTree(self.repo, oldSha, newSha, function(error, rawDifflist) { |
37 | | - if (error) { |
38 | | - callback(error, null); |
39 | | - return; |
40 | | - } |
41 | | - self.diffList = rawDifflist; |
42 | | - callback(null, self); |
| 58 | +DiffList.prototype.walk = function() { |
| 59 | + var event = new events.EventEmitter(), |
| 60 | + allFileDeltas = [], |
| 61 | + self = this; |
| 62 | + |
| 63 | + self.rawDiffList.walk(function fileCallback(error, fileDeltas) { |
| 64 | + if (error) { |
| 65 | + event.emit('end', new git.error(error.message, error.code), null); |
| 66 | + } |
| 67 | + fileDeltas.forEach(function(fileDelta) { |
| 68 | + event.emit('file', null, fileDelta); |
| 69 | + allFileDeltas.push(fileDelta); |
43 | 70 | }); |
44 | | - }; |
45 | | - |
46 | | - /** |
47 | | - * Add libgit2 delta types to git.diffList object. |
48 | | - * |
49 | | - * Refer to vendor/libgit2/include/git2/diff.h for definitions. |
50 | | - */ |
51 | | - for (var deltaType in git.raw.DiffList.deltaTypes) { |
52 | | - self[deltaType] = git.raw.DiffList.deltaTypes[deltaType]; |
53 | | - } |
| 71 | + }, function endCallback(error) { |
| 72 | + event.emit('end', error ? new git.error(error.message, error.code) : null, allFileDeltas); |
| 73 | + }); |
54 | 74 |
|
55 | | - /** |
56 | | - * Add libgit2 line origin types to git.diffList object. |
57 | | - * |
58 | | - * Refer to vendor/libgit2/include/git2/diff.h for definitions. |
59 | | - */ |
60 | | - for (var lineOriginType in git.raw.DiffList.lineOriginTypes) { |
61 | | - self[lineOriginType] = git.raw.DiffList.lineOriginTypes[lineOriginType]; |
62 | | - } |
| 75 | + return event; |
| 76 | +}; |
63 | 77 |
|
64 | | - return self; |
| 78 | +DiffList.prototype.treeToTree = function(oldSha, newSha, callback) { |
| 79 | + var self = this; |
| 80 | + self.rawDiffList.treeToTree(self.rawRepo, oldSha, newSha, function(error, rawDifflist) { |
| 81 | + if (error) { |
| 82 | + callback(new git.error(error.message, error.code), null); |
| 83 | + return; |
| 84 | + } |
| 85 | + self.rawDiffList = rawDifflist; |
| 86 | + callback(null, self); |
| 87 | + }); |
65 | 88 | }; |
66 | 89 |
|
67 | | -exports.diffList = GitDiffList; |
| 90 | +exports.diffList = DiffList; |
0 commit comments