Skip to content

Commit 2a900f5

Browse files
committed
Various api improvements, updated readme
1 parent 3b7670f commit 2a900f5

8 files changed

Lines changed: 105 additions & 37 deletions

File tree

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ INSTALL_PATH = $(NODE_LIB_PATH)/nodegit
77

88
all: build_bindings
99

10+
update: clean config build_bindings uninstall install
11+
12+
config:
13+
@@$(BASE)/configure
14+
1015
build_bindings:
1116
@@$(NODE_BLD) build
1217

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ __ Reading a repository and commit data: __
4747
// If success err will be 0, else throw an error message.
4848
if( err ) { throw err; }
4949

50+
// Work within the master branch
51+
repo.branch( 'master', function( err, branch ) {
52+
// If success err will be 0, else throw an error message.
53+
if( err ) { throw err; }
54+
55+
// Iterate over the revision history
56+
branch.history.each( function( i, commit ) {
57+
// Emulator git log
58+
console.log( 'Author:', commit.author().name, '<' + commit.author().email + '>' );
59+
console.log( 'Date:', commit.time().toDateString() );
60+
console.log( commit.message() );
61+
});
62+
});
63+
5064
// Read a commit with a SHA1
5165
this.commit( '5f2aa9407f7b3aeb531c621c3358953841ccfc98', function( err, commit ) {
5266
// If success err will be 0, else throw an error message.

example/convenience-repo.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
var git = require( '../' );
22

3-
git.repo( '/home/tim/Projects/nodegit2/.git', function( err, repo ) {
3+
git.repo( require('path').normalize('../.git'), function( err, repo ) {
44
if( err ) { throw err; }
55

66
// Read a commit when you know the sha1
7-
//repo.commit( 'd29b7fecf71d0ef4887071ac18dc87f40c2fd4e1', function( err, commit ) {
8-
// console.log( commit.tree() );
9-
//});
7+
repo.commit( 'd29b7fecf71d0ef4887071ac18dc87f40c2fd4e1', function( err, commit ) {
8+
console.log( commit.tree() );
9+
});
1010

1111
// Read a commit when you know the name
1212
repo.head( 'master', function( err, head ) {

example/dummyrepo

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/commit.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ var git = require( '../' );
33
var _Commit = function( obj ) {
44
var self = {};
55

6+
Object.defineProperty( self, 'history', {
7+
get: function() {
8+
// Partially apply the commit
9+
var revwalk = git.revwalk( self.repo );
10+
revwalk.each = function( callback ) {
11+
return function( callback ) {
12+
return revwalk.walk.apply( self.commit, [ self.commit, callback ] );
13+
};
14+
}();
15+
16+
return revwalk;
17+
},
18+
enumerable: true
19+
});
20+
621
if( obj instanceof git.raw.Repo ) {
722
self.repo = obj;
823
self.commit = new git.raw.Commit( obj );
@@ -41,17 +56,17 @@ var _Commit = function( obj ) {
4156
return git.sig( sig );
4257
};
4358

44-
self.tree = function() {
45-
var tree = new git.raw.Tree( self.repo );
46-
if( tree.error ) {
47-
throw git.error( tree.error );
48-
}
49-
else {
50-
self.commit.tree( tree );
51-
}
59+
//self.tree = function() {
60+
// var tree = new git.raw.Tree( self.repo );
61+
// if( tree.error ) {
62+
// throw git.error( tree.error );
63+
// }
64+
// else {
65+
// self.commit.tree( tree );
66+
// }
5267

53-
return git.tree( tree );
54-
};
68+
// return git.tree( tree );
69+
//};
5570

5671
return self;
5772
};

lib/repo.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,44 @@ var _Repo = function( path, callback ) {
1010
// Internal reference to a Git repository
1111
self.repo = new git.raw.Repo();
1212

13+
14+
// Look up a branch and find its tree
15+
self.branch = function( name, callback ) {
16+
if( !callback ) { return; }
17+
18+
self.ref( 'refs/heads/' + name, function( err, ref ) {
19+
if( err ) { throw err; }
20+
21+
git.commit( self.repo ).lookup( ref.oid().oid, function() {
22+
var args = Array.prototype.slice.call( arguments );
23+
args[0] = git.util().error( args[0] );
24+
25+
callback.apply( this, args.concat( this ) );
26+
});
27+
});
28+
};
29+
1330
// Work with a specific head reference
14-
self.head = function( name, callback ) {
15-
var head = git.ref( self.repo );
31+
self.ref = function( name, callback ) {
32+
if( !callback ) { return; }
33+
34+
var ref = git.ref( self.repo );
1635

17-
self.repo.lookupRef( head.ref, 'refs/heads/'+ name, function() {
36+
self.repo.lookupRef( ref.ref, name, function() {
1837
var args = Array.prototype.slice.call( arguments );
1938
args[0] = git.util().error( args[0] );
2039

21-
callback.apply( head, args.concat( head ) );
40+
callback.apply( ref, args.concat( ref ) );
2241
});
2342
};
2443

2544
// Find a single commit
2645
self.commit = function( sha, callback ) {
27-
var oid = git.oid( sha );
28-
2946
if( !callback ) { return; }
3047

31-
var commit = git.commit( self.repo );
32-
commit.lookup( oid.oid, callback );
48+
var oid = git.oid( sha );
49+
50+
git.commit( self.repo ).lookup( oid.oid, callback );
3351
};
3452

3553
//self.find = function( name, callback ) {

lib/revwalk.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
var git = require( '../' );
22

3-
var _RevWalk = function( repo ) {
3+
var _RevWalk = function( obj ) {
44
var self = {};
55

6-
// Internal reference to a Git reference
7-
self.revwalk = revwalk || new git.raw.RevWalk( repo );
6+
if( obj instanceof git.raw.Repo ) {
7+
self.repo = obj;
8+
self.revwalk = new git.raw.RevWalk( obj );
9+
}
10+
else if( obj instanceof git.raw.RevWalk ) {
11+
self.revwalk = obj;
12+
}
813

914
// Walk will map to the next method
1015
self.walk = function( commit, callback ) {
1116
if( !callback ) { return; }
1217

1318
self.revwalk.push( commit );
1419

15-
revwalk.next( commit, function() {
20+
var _tmp = git.commit( self.repo );
21+
self.revwalk.next( _tmp.commit, function() {
1622
var args = Array.prototype.slice.call( arguments );
17-
1823
args[0] = git.util().error( args[0] );
1924

20-
21-
callback.apply( commit, args.concat( commit ) );
25+
callback.apply( _tmp, args.concat( _tmp ) );
2226
});
2327
};
2428

lib/tree.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
var git = require( '../' );
22

3-
var _Tree = function( obj ) {
3+
var _Tree = function( obj, tree ) {
44
var self = {};
55

6-
Object.defineProperty( self, 'length', {
7-
get: function() {
8-
return self.tree.entryCount();
9-
},
10-
enumerable: true
11-
});
6+
//Object.defineProperty( self, 'length', {
7+
// get: function() {
8+
// return self.tree.entryCount();
9+
// },
10+
// enumerable: true
11+
//});
12+
13+
self.each = function( callback ) {
14+
if( !callback ) { return; }
15+
16+
var commit, i;
17+
for(i=0, len=self.length; i<len; i++) {
18+
commit = git.commit( self.repo );
19+
git.commit( self.tree.entryByIndex( commit.commit, i ) );
20+
callback.apply( commit, [ i, commit ] );
21+
}
22+
};
1223

1324
// Internal references to Git references
1425
if( obj instanceof git.raw.Repo ) {
1526
// TODO: Add support for creation
27+
self.repo = obj;
28+
self.tree = tree;
1629
}
1730
else if ( obj instanceof git.raw.Tree ) {
1831
self.tree = obj;

0 commit comments

Comments
 (0)