1
+ 'use strict' ;
2
+ /*jslint node: true, white: true, maxerr: 50, indent: 2 */
3
+ var fs = require ( 'fs' )
4
+ , path = require ( 'path' )
5
+ , parser = require ( './object_parser.js' )
6
+ , zlib = require ( 'zlib' )
7
+ ;
8
+
9
+ var noop = function ( ) { } ;
10
+
11
+ var objectFactory = function ( ) {
12
+ var that , sha1ToFile , getData , getObject ;
13
+ that = { } ;
14
+
15
+ sha1ToFile = function ( sha1 , dir ) {
16
+ return path . join ( dir , 'objects' , sha1 . slice ( 0 , 2 ) , sha1 . slice ( 2 , sha1 . length ) ) ;
17
+ } ;
18
+
19
+ getData = function ( sha1 , dir , cb ) {
20
+ var callback , filename ;
21
+
22
+ if ( typeof sha1 === "function" ) {
23
+ cb = sha1 ;
24
+ sha1 = undefined ;
25
+ }
26
+
27
+ if ( typeof dir === "function" ) {
28
+ cb = dir ;
29
+ dir = undefined ;
30
+ }
31
+
32
+ callback = cb || noop ;
33
+ filename = sha1ToFile ( sha1 , dir ) ;
34
+
35
+ fs . readFile ( filename , function ( err , data ) {
36
+ if ( err ) {
37
+ return cb ( err , null ) ;
38
+ }
39
+
40
+ zlib . inflate ( data , function ( err , data ) {
41
+ if ( err ) {
42
+ return cb ( err , null ) ;
43
+ }
44
+ cb ( null , data ) ;
45
+ } ) ;
46
+ } ) ;
47
+ } ;
48
+
49
+ getObject = function ( sha1 , dir , cb ) {
50
+ var callback ;
51
+
52
+ callback = cb || noop ;
53
+
54
+ getData ( sha1 , dir , function ( err , data ) {
55
+ if ( err ) {
56
+ return callback ( err , null ) ;
57
+ }
58
+ return callback ( null , parser . parse ( data ) ) ;
59
+ } ) ;
60
+ } ;
61
+
62
+ that . getObject = getObject ;
63
+ return that ;
64
+ } ;
65
+
66
+
67
+
68
+ module . exports = objectFactory ( ) ;
69
+
70
+ //var objects = objectFactory()
71
+ // , blob = "303ff981c488b812b6215f7db7920dedb3b59d9a"
72
+ // , tree = "324e16519b70c18a9c92db30aa800912ec7f41be"
73
+ // , commit = "e42d12f3d9f9c47bdd79a0bb837cfdf50d4a58af"
74
+ // , dir= "../../test/TestGitRepo/.git"
75
+ // ;
76
+
77
+ // objects.getData(tree, dir, function(err, data){
78
+ // console.log(data.toString());
79
+ // })
80
+
81
+
82
+ // objects.getObject(commit,dir,function(err, data) {
83
+ // console.log(data);
84
+ // })
85
+
86
+ // objects.getObject(tree,dir,function(err, data) {
87
+ // console.log(data);
88
+ // })
89
+
90
+ // objects.getObject(blob,dir,function(err, data) {
91
+ // console.log(data);
92
+ // })
0 commit comments