FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Rewrite Commit js convenience wrapper. · nodegit/nodegit@64ae9de · GitHub

Repository navigation

Commit 64ae9de

Browse files
committed
Rewrite Commit js convenience wrapper.
1 parent 7342eaa commit 64ae9de

1 file changed

Lines changed: 122 additions & 146 deletions

File tree

‎lib/commit.js‎

Lines changed: 122 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,152 @@
11
var git = require( '../' ),
2-
utilities = require('./utilities'),
3-
events = require( 'events' );
2+
success = require('./utilities').success,
3+
events = require('events');
44

55
/**
66
* Convenience commit constructor.
77
*
88
* @param {RawCommit|Null} rawCommit
99
* @return {Commit}
1010
*/
11-
var Commit = function(rawCommit) {
12-
var self = {};
11+
var Commit = function(rawRepo, rawCommit) {
1312

14-
if(rawCommit && rawCommit instanceof git.raw.Commit) {
15-
self.commit = rawCommit;
16-
} else {
17-
self.commit = new git.raw.Commit();
13+
if (!(rawRepo instanceof git.raw.Repo)) {
14+
throw git.error('First parameter for Commit must be a raw repo');
1815
}
1916

20-
/**
21-
* Fetch the commit's details asynchronously.
22-
*
23-
* @param {Function} callback
24-
*/
25-
self.fetchDetails = function(callback) {
26-
var error = null;
27-
self.commit.fetchDetails(function(error, details) {
28-
if (error) {
29-
error = git.error(error);
30-
}
31-
utilities.applyProperties(details, self);
32-
callback(error);
33-
});
34-
};
17+
this.rawRepo = rawRepo;
3518

36-
/**
37-
* Fetch the commit's details synchronously.
38-
*/
39-
self.fetchDetailsSync = function(callback) {
40-
var details = self.commit.fetchDetailsSync();
41-
return utilities.applyProperties(details, self);
42-
};
19+
if (rawCommit instanceof git.raw.Commit) {
20+
this.rawCommit = rawCommit;
21+
} else {
22+
this.rawCommit = new git.raw.Commit();
23+
}
24+
};
4325

44-
/**
45-
* Look up the commit referenced by oid, replace self.commit
46-
* with the result.
47-
*
48-
* @param {Repo} repo
49-
* @param {Oid|String|RawOid} oid Raw or convenience OID object or SHA string
50-
* @param {Function} callback
51-
*/
52-
self.lookup = function(repo, oid, callback) {
53-
self.repo = repo;
54-
if (typeof oid !== 'string' && !(oid instanceof git.raw.Oid)) {
55-
oid = oid.getRawOid();
26+
/**
27+
* Look up the commit referenced by oid, replace this.commit with the result.
28+
*
29+
* @param {git.oid|git.raw.Oid|String} oid
30+
* @param {Function} callback
31+
*/
32+
Commit.prototype.lookup = function(oid, callback) {
33+
if (typeof oid !== 'string' && !(oid instanceof git.raw.Oid)) {
34+
oid = oid.getRawOid();
35+
}
36+
var self = this;
37+
self.rawCommit.lookup(self.rawRepo, oid, function commitLookup(error, rawCommit) {
38+
if (success(error, callback)) {
39+
self.rawCommit = rawCommit;
40+
callback(null, self);
5641
}
57-
self.commit.lookup(repo, oid, function(error, commit) {
58-
if (error) {
59-
callback(git.error(error), null);
60-
return;
61-
}
62-
self.commit = commit;
63-
self.fetchDetails(function(error) {
64-
if (error) {
65-
callback(git.error(error), null);
66-
return;
67-
}
68-
callback(null, self);
69-
});
70-
});
71-
};
42+
});
43+
};
7244

73-
self.tree = function() {
74-
var tree = new git.raw.Tree(self.repo);
75-
if(tree.error) {
76-
return git.error(tree.error);
77-
} else {
78-
self.commit.tree(tree);
45+
/**
46+
* Retrieve the tree for this commit.
47+
*
48+
* @param {Function} callback
49+
*/
50+
Commit.prototype.tree = function(callback) {
51+
var self = this;
52+
self.rawCommit.tree(function commitTree(error, rawTree) {
53+
if (success(error, callback)) {
54+
callback(null, new git.tree(self.rawRepo, rawTree));
7955
}
80-
return git.tree(self.repo, tree);
81-
};
82-
83-
self.file = function(path, callback) {
84-
self.tree().entry(path, callback);
85-
};
86-
87-
/**
88-
* Walk the history of this commit.
89-
*
90-
* @return {Event} Event emits 'commit', with error, commit and 'end', with
91-
* error, commits[]
92-
*/
93-
self.history = function() {
94-
var revwalk = git.revwalk(self.repo),
95-
event = new events.EventEmitter(),
96-
commits = [];
97-
98-
revwalk.walk(self.id, function(error, index, commit, noMoreCommits) {
99-
if(error) {
100-
event.emit('end', error, commits);
101-
return false;
102-
}
56+
});
57+
};
10358

104-
if (noMoreCommits) {
105-
event.emit('end', null, commits);
106-
return;
59+
/**
60+
* Retrieve the file represented by path for this commit.
61+
*
62+
* @param {String} path
63+
* @param {Function} callback
64+
*/
65+
Commit.prototype.file = function(path, callback) {
66+
this.tree(function commitFile(error, tree) {
67+
if (!success(error, callback)) {
68+
return;
69+
}
70+
tree.entry(path, function(error, entry) {
71+
if (success(error, callback)) {
72+
callback(null, entry);
10773
}
108-
event.emit('commit', null, commit);
109-
commits.push(commit);
11074
});
75+
});
76+
};
11177

112-
return event;
113-
};
78+
/**
79+
* Walk the history of this commit.
80+
*
81+
* @return {Event} Event emits 'commit', with error, commit and 'end', with
82+
* error, commits[]
83+
*/
84+
Commit.prototype.history = function() {
85+
var revwalk = git.revwalk(self.repo),
86+
event = new events.EventEmitter(),
87+
commits = [];
88+
89+
revwalk.walk(self.id, function(error, index, commit, noMoreCommits) {
90+
if(error) {
91+
event.emit('end', error, commits);
92+
return false;
93+
}
11494

115-
/**
116-
* Retrieve the commit's parent at the given position asynchronously.
117-
*
118-
* @param {Integer} position
119-
*/
120-
self.parent = function(position, callback) {
121-
var parent = null;
122-
self.commit.parent(position, function processParent(errorCode, parent) {
123-
var error = null;
124-
if (errorCode) {
125-
error = git.error(errorCode);
126-
return callback(error, null);
127-
}
128-
var parentCommit = new Commit(parent);
129-
parentCommit.fetchDetails(function returnParent(error) {
130-
callback(error, parentCommit);
131-
});
132-
});
133-
};
95+
if (noMoreCommits) {
96+
event.emit('end', null, commits);
97+
return;
98+
}
99+
event.emit('commit', null, commit);
100+
commits.push(commit);
101+
});
134102

135-
/**
136-
* Retrieve the commit's parent at the given positino synchronously.
137-
*
138-
* @param {Integer} position
139-
* @return {Commit}
140-
*/
141-
self.parentSync = function(position) {
142-
var parent = new Commit(self.commit.parentSync(position));
143-
return parent.fetchDetailsSync();
144-
};
103+
return event;
104+
};
105+
106+
/**
107+
* Retrieve the commit's parent at the given position asynchronously.
108+
*
109+
* @param {Integer} position
110+
*/
111+
Commit.prototype.parent = function(position, callback) {
112+
self.rawCommit.parent(position, function processParent(error, rawParent) {
113+
if (success(error, callback)) {
114+
callback(null, new Commit(this.rawRepo, rawParent));
115+
}
116+
});
117+
};
145118

146-
/**
147-
* Get a diff tree showing changes between this commit and its parent(s).
148-
* Assumes commit has been populated with fetchDetails|fetchDetailsSync
149-
*
150-
* @param {Function} callback Called with error (null if no error) and the
151-
* diff tree
152-
*/
153-
self.parentsDiffTrees = function(callback) {
154-
if (!self.parentCount) {
155-
callback(null, []);
119+
/**
120+
* Get a diff tree showing changes between this commit and its parent(s).
121+
*
122+
* @param {Function} callback
123+
*/
124+
Commit.prototype.parentsDiffTrees = function(callback) {
125+
var self = this;
126+
self.sha(function(error, commitSha) {
127+
if (!success(error, callback)) {
156128
return;
157129
}
158-
var parentDiffLists = [];
159-
self.parentShas.forEach(function eachParentSha(parentSha) {
160-
git.diffList(self.repo).treeToTree(parentSha, self.sha, function walkDiffList(error, diffList) {
161-
if (error) {
162-
callback(error, null);
163-
return;
164-
}
165-
parentDiffLists.push(diffList);
166-
if (parentDiffLists.length === self.parentShas.length) {
167-
callback(null, parentDiffLists);
168-
}
130+
self.parents(function commitParents(error, parents) {
131+
if (!success(error, callback)) {
132+
return;
133+
}
134+
var parentDiffLists = [];
135+
parents.forEach(function commitEachParent(parent) {
136+
parent.sha(function commitParentSha(error, parentSha) {
137+
(new git.diffList(self.repo)).treeToTree(parentSha, commitSha, function walkDiffList(error, diffList) {
138+
if (!success(error, callback)) {
139+
return;
140+
}
141+
parentDiffLists.push(diffList);
142+
if (parentDiffLists.length === parents.length) {
143+
callback(null, parentDiffLists);
144+
}
145+
});
146+
});
169147
});
170148
});
171-
};
172-
173-
return self;
149+
});
174150
};
175151

176152
exports.commit = Commit;

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL