@@ -35,13 +35,20 @@ Tree.prototype.entry = function(path, callback) {
3535 * @param {Entry|null } entry The tree entry object or null.
3636 */
3737 var self = this ;
38- self . rawTree . entryByPath ( path , function ( error , rawEntry ) {
38+ self . rawTree . getEntryByPath ( path , function ( error , rawEntry ) {
3939 if ( success ( error , callback ) ) {
4040 callback ( null , new git . entry ( self . repo , rawEntry ) ) ;
4141 }
4242 } ) ;
4343} ;
4444
45+ /**
46+ * Retrieve the number of entries in this tree.
47+ */
48+ Tree . prototype . size = function ( ) {
49+ return self . rawTree . size ( ) ;
50+ } ;
51+
4552/**
4653 * Walk the tree.
4754 *
@@ -53,39 +60,49 @@ Tree.prototype.entry = function(path, callback) {
5360 * @return {EventEmitter }
5461 */
5562Tree . prototype . walk = function ( blobsOnly ) {
56- blobsOnly = typeof blobsOnly === 'undefined' ? true : blobsOnly ;
63+ if ( typeof blobsOnly == 'undefined' ) blobsOnly = true ;
5764
5865 var self = this ,
5966 event = new events . EventEmitter ( ) ,
60- entries = [ ] ;
67+ entries = [ ] ,
68+ errors = [ ] ;
6169
62- var total = 0 ;
70+ var total = 1 ;
6371
64- self . rawTree . walk ( blobsOnly , function treeWalkEntries ( error , rawEntries ) {
65- rawEntries . forEach ( function treeWalkEntryEmitter ( rawEntry ) {
66- var entry = new git . entry ( self . repo , rawEntry ) ;
67- entries . push ( entry ) ;
68- /**
69- * Entry event.
70- *
71- * @event Tree#entry
72- *
73- * @param {GitError|null } error An error object if there was an issue, null otherwise.
74- * @param {Entry } entry The tree entry.
75- */
76- event . emit ( 'entry' , null , entry ) ;
77- } ) ;
78- } , function treeWalkEnd ( error ) {
79- /**
80- * End event.
81- *
82- * @event Tree#end
83- *
84- * @param {GitError|null } error An error object if there was an issue, null otherwise.
85- * @param {Entry[] } entries The tree entries.
86- */
87- event . emit ( 'end' , error ? new git . error ( error . message , error . code ) : null , entries ) ;
88- } ) ;
72+ function dfs ( error , tree ) {
73+ total -- ;
74+
75+ var size = tree . rawTree . size ( ) ;
76+ if ( error ) return errors . push ( error ) ;
77+
78+ for ( var i = 0 ; i < size ; i ++ ) {
79+ var rawEntry = tree . rawTree . entryByIndex ( i ) ,
80+ entry = new git . entry ( tree . repo , rawEntry ) ;
81+ if ( ! blobsOnly || entry . isFile ( ) ) {
82+ /**
83+ * Entry event.
84+ *
85+ * @event Tree#entry
86+ *
87+ * @param {GitError|null } error An error object if there was an issue, null otherwise.
88+ * @param {Entry } entry The tree entry.
89+ */
90+ event . emit ( 'entry' , entry ) ;
91+ entries . push ( entry ) ;
92+ }
93+
94+ if ( entry . isTree ( ) ) {
95+ total ++ ;
96+ entry . getTree ( dfs ) ;
97+ }
98+ }
99+ if ( total === 0 )
100+ event . emit ( 'end' , errors . length ? errors : null , entries ) ;
101+ }
102+
103+ event . start = function ( e ) {
104+ dfs ( null , self ) ;
105+ } ;
89106
90107 return event ;
91108} ;
0 commit comments