Skip to content

Commit 942f596

Browse files
committed
asynchronous tree walking and nested directories
1 parent 091882c commit 942f596

File tree

8 files changed

+300
-62
lines changed

8 files changed

+300
-62
lines changed

example/convenience-tree.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,19 @@ git.repo( '../.git', function( err, repo ) {
66
repo.branch( 'master', function( err, branch ) {
77
if( err ) { throw err; }
88

9-
branch.tree().walk( function( idx, entry ) {
10-
console.log( entry.name );
11-
console.log( entry.content );
9+
branch.tree().walk().on('entry', function( idx, entry ) {
10+
//console.log(entry.entry);
11+
console.log( entry.name, entry.attributes );
12+
//console.log( entry.content );
1213
});
14+
15+
//branch.tree().entry('example/raw-blob.js', function( entry ) {
16+
// if( entry ) {
17+
// console.log(entry.name);
18+
// }
19+
// else {
20+
// console.log('not found');
21+
// }
22+
//});
1323
});
1424
});

include/tree.h

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Copyright (c) 2011, Tim Branyen @tbranyen <tim@tabdeveloper.com>
88
#include <v8.h>
99
#include <node.h>
1010
#include <node_events.h>
11+
#include <string>
1112

1213
#include "../vendor/libgit2/include/git2.h"
1314

@@ -48,13 +49,12 @@ class GitTree : public EventEmitter {
4849
/**
4950
* Lookup a tree object from a repository.
5051
*
51-
* @param tree pointer to the looked up tree
5252
* @param repo the repo to use when locating the tree.
5353
* @param id identity of the tree to locate.
5454
*
5555
* @return 0 on success; error code otherwise
5656
*/
57-
int Lookup(git_tree** tree, git_repository* repo, const git_oid* id);
57+
int Lookup(git_repository* repo, const git_oid* id);
5858
/**
5959
* Get number of entries in the looked up tree.
6060
*
@@ -93,9 +93,16 @@ class GitTree : public EventEmitter {
9393
*/
9494
static Handle<Value> New(const Arguments& args);
9595

96+
static Handle<Value> Lookup(const Arguments& args);
97+
static int EIO_Lookup(eio_req *req);
98+
static int EIO_AfterLookup(eio_req *req);
9699
static Handle<Value> EntryCount(const Arguments& args);
97100
static Handle<Value> EntryByIndex(const Arguments& args);
101+
static int EIO_EntryByIndex(eio_req *req);
102+
static int EIO_AfterEntryByIndex(eio_req *req);
98103
static Handle<Value> EntryByName(const Arguments& args);
104+
static int EIO_EntryByName(eio_req *req);
105+
static int EIO_AfterEntryByName(eio_req *req);
99106
static Handle<Value> SortEntries(const Arguments& args);
100107
static Handle<Value> ClearEntries(const Arguments& args);
101108

@@ -104,6 +111,34 @@ class GitTree : public EventEmitter {
104111
* Internal reference to git_tree object
105112
*/
106113
git_tree* tree;
114+
/**
115+
* Structure to handle async lookups
116+
*/
117+
struct lookup_request {
118+
GitTree* tree;
119+
GitRepo* repo;
120+
GitOid* oid;
121+
int err;
122+
Persistent<Function> callback;
123+
};
124+
/**
125+
* Structure to handle async entryByIndex
126+
*/
127+
struct entryindex_request {
128+
GitTree* tree;
129+
GitTreeEntry* entry;
130+
int idx;
131+
Persistent<Function> callback;
132+
};
133+
/**
134+
* Structure to handle async entryByName
135+
*/
136+
struct entryname_request {
137+
GitTree* tree;
138+
GitTreeEntry* entry;
139+
std::string name;
140+
Persistent<Function> callback;
141+
};
107142
};
108143

109144
#endif

lib/commit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ var _Commit = function( obj ) {
114114
event.emit( 'end', commits );
115115
}
116116
else {
117-
event.emit( 'commit', index, commit );
117+
event.emit( 'commit', commit );
118118
commits.push( commit );
119119
}
120120
});

lib/tree.js

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,50 @@ var _Tree = function( obj, tree ) {
2525
enumerable: true
2626
});
2727

28-
// Synchronous walk
29-
self.walk = function( callback ) {
30-
if( !callback ) { return; }
31-
28+
self.walk = function( repo ) {
3229
var entry
33-
, i;
34-
35-
for( i=0, len=self.length; i<len; i++ ) {
36-
entry = git.entry( self.repo );
37-
38-
self.tree.entryByIndex( entry.entry, i );
39-
40-
if( callback.apply( entry, [ i, entry ] ) === false ) {
41-
break;
42-
}
30+
, i
31+
, len = self.length
32+
, repo = repo || self.repo
33+
, event = new events.EventEmitter();
34+
35+
function next(i) {
36+
var dir;
37+
var tree;
38+
39+
entry = git.entry( repo );
40+
41+
self.tree.entryByIndex(entry.entry, i, function() {
42+
if(entry.isFile()) {
43+
event.emit( 'entry', i, entry );
44+
}
45+
else {
46+
dir = entry.name;
47+
tree = entry.tree();
48+
49+
!tree.error && tree.walk( repo ).on( 'entry', function( i, entry ) {
50+
entry.dir += dir + '/';
51+
event.emit( 'entry', i, entry );
52+
});
53+
}
54+
55+
i<len-1 && next(i=i+1);
56+
});
4357
}
44-
};
45-
46-
//self.walk = function( callback ) {
47-
// if( !callback ) { return; }
4858

49-
// var entry
50-
// , i
51-
// , entries
52-
// , event = new events.EventEmitter();
59+
next(0);
5360

54-
// for( i=0, len=self.length; i<len; i++ ) {
55-
// entry = git.entry();
56-
57-
// self.tree.entryByIndex( entry.entry, i );
58-
// event.emit( 'entry', [ err, i, entry ] );
59-
60-
// entries.push( entry );
61-
// }
62-
63-
// event.emit( 'end', entries );
61+
return event;
62+
};
6463

65-
// return event;
66-
//};
64+
self.entry = function( name, callback ) {
65+
if( !callback ) { return; }
6766

68-
self.entry = function( name ) {
6967
var entry = git.entry( self.repo );
7068

71-
self.tree.entryByName( entry.entry, name );
72-
73-
return entry;
69+
self.tree.entryByName( entry.entry, name, function( valid ) {
70+
callback(valid ? entry : undefined);
71+
});
7472
};
7573

7674
return self;

lib/tree_entry.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ var _TreeEntry = function( obj ) {
1111
self.repo = obj;
1212
}
1313

14+
self.dir = '';
15+
1416
Object.defineProperty( self, 'name', {
1517
get: function() {
16-
return self.entry.name();
18+
return self.dir + self.entry.name();
1719
},
1820
enumerable: true
1921
});
@@ -32,6 +34,16 @@ var _TreeEntry = function( obj ) {
3234
enumerable: true
3335
});
3436

37+
Object.defineProperty( self, 'id', {
38+
get: function() {
39+
var oid = git.oid();
40+
self.entry.id( oid.oid );
41+
42+
return oid;
43+
},
44+
enumerable: true
45+
});
46+
3547
Object.defineProperty( self, 'content', {
3648
get: function() {
3749
if( self.isFile() ) {
@@ -55,6 +67,20 @@ var _TreeEntry = function( obj ) {
5567
return self.attributes === 16384;
5668
};
5769

70+
self.tree = function() {
71+
var tree = new git.raw.Tree( self.repo );
72+
if( tree.error ) {
73+
return git.error( tree.error );
74+
}
75+
else {
76+
if( tree.lookup(self.repo, self.id.oid) ) {
77+
return git.error( tree.error );
78+
}
79+
}
80+
81+
return git.tree( tree );
82+
};
83+
5884
return self;
5985
};
6086

0 commit comments

Comments
 (0)