14
14
15
15
@implementation PBGitRepository
16
16
17
- @synthesize path, commits ;
18
- static NSString * gitPath = @" /usr/bin/env " ;
17
+ @synthesize path;
18
+ static NSString * gitPath;
19
19
20
- + (PBGitRepository*) repositoryWithPath : (NSString *) path
21
- {
22
- [self setGitPath ];
23
- PBGitRepository* repo = [[PBGitRepository alloc ] initWithPath: path];
24
- return repo;
25
- }
26
-
27
- - (PBGitRepository*) initWithPath : (NSString *) p
28
- {
29
- if ([p hasSuffix: @" .git" ])
30
- self.path = p;
31
- else {
32
- NSString * newPath = [PBEasyPipe outputForCommand: gitPath withArgs: [NSArray arrayWithObjects: @" rev-parse" , @" --git-dir" , nil ] inDir: p];
33
- if ([newPath isEqualToString: @" .git" ])
34
- self.path = [p stringByAppendingPathComponent: @" .git" ];
35
- else
36
- self.path = newPath;
37
- }
38
-
39
- NSLog (@" Git path is: %@ " , self.path );
40
-
41
- NSThread * commitThread = [[NSThread alloc ] initWithTarget: self selector: @selector (initializeCommits ) object: nil ];
42
- [commitThread start ];
43
- return self;
44
- }
45
-
46
-
47
- + (void ) setGitPath
20
+ + (void ) initialize
48
21
{
22
+ // Try to find the path of the Git binary
49
23
char * path = getenv (" GIT_PATH" );
50
24
if (path != nil ) {
51
25
gitPath = [NSString stringWithCString: path];
@@ -54,79 +28,38 @@ + (void) setGitPath
54
28
55
29
// No explicit path. Try it with "which"
56
30
gitPath = [PBEasyPipe outputForCommand: @" /usr/bin/which" withArgs: [NSArray arrayWithObject: @" git" ]];
57
-
31
+
58
32
if (gitPath.length == 0 ) {
59
33
NSLog (@" Git path not found. Defaulting to /opt/pieter/bin/git" );
60
34
gitPath = @" /opt/pieter/bin/git" ;
61
35
}
62
36
}
63
37
64
- - ( void ) addCommit : ( id ) obj
38
+ + (PBGitRepository*) repositoryWithPath : ( NSString *) path
65
39
{
66
- self.commits = [self .commits arrayByAddingObject: obj];
67
- }
68
40
69
- - (void ) setCommits : (NSArray *) obj
70
- {
71
- commits = obj;
41
+ PBGitRepository* repo = [[PBGitRepository alloc ] initWithPath: path];
42
+ return repo;
72
43
}
73
44
74
- - (void ) initializeCommits
45
+ - (PBGitRepository*) initWithPath : ( NSString *) p
75
46
{
76
-
77
- NSMutableArray * newArray = [NSMutableArray array ];
78
- NSDate * start = [NSDate date ];
79
- NSFileHandle * handle = [self handleForCommand: @" log --pretty=format:%H\01 %a n\01 %s \01 %P\01 %a t HEAD" ];
80
-
81
- int fd = [handle fileDescriptor ];
82
- FILE* f = fdopen (fd, " r" );
83
- int BUFFERSIZE = 2048 ;
84
- char buffer[BUFFERSIZE];
85
- buffer[BUFFERSIZE - 2 ] = 0 ;
86
-
87
- char * l;
88
- int num = 0 ;
89
- NSMutableString * currentLine = [NSMutableString string ];
90
- while (l = fgets (buffer, BUFFERSIZE, f)) {
91
- NSString *s = [NSString stringWithCString: (const char *)l encoding: NSUTF8StringEncoding];
92
- if ([s length ] == 0 )
93
- s = [NSString stringWithCString: (const char *)l encoding: NSASCIIStringEncoding];
94
- [currentLine appendString: s];
95
-
96
- // If buffer is full, we go for another round
97
- if (buffer[BUFFERSIZE - 2 ] != 0 ) {
98
- // NSLog(@"Line too long!");
99
- buffer[BUFFERSIZE - 2 ] = 0 ;
100
- continue ;
101
- }
102
-
103
- // If we are here, we currentLine is a full line.
104
- NSArray * components = [currentLine componentsSeparatedByString: @" \01 " ];
105
- if ([components count ] < 5 ) {
106
- NSLog (@" Can't split string: %@ " , currentLine);
107
- continue ;
108
- }
109
- PBGitCommit* newCommit = [[PBGitCommit alloc ] initWithRepository: self andSha: [components objectAtIndex: 0 ]];
110
- NSArray * parents = [[components objectAtIndex: 3 ] componentsSeparatedByString: @" " ];
111
- newCommit.parents = parents;
112
- newCommit.subject = [components objectAtIndex: 2 ];
113
- newCommit.author = [components objectAtIndex: 1 ];
114
- newCommit.date = [NSDate dateWithTimeIntervalSince1970: [[components objectAtIndex: 3 ] intValue ]];
115
-
116
- [newArray addObject: newCommit];
117
- num++;
118
- if (num % 10000 == 0 )
119
- [self performSelectorOnMainThread: @selector (setCommits: ) withObject: newArray waitUntilDone: NO ];
120
- currentLine = [NSMutableString string ];
47
+ if ([p hasSuffix: @" .git" ])
48
+ self.path = p;
49
+ else {
50
+ NSString * newPath = [PBEasyPipe outputForCommand: gitPath withArgs: [NSArray arrayWithObjects: @" rev-parse" , @" --git-dir" , nil ] inDir: p];
51
+ if ([newPath isEqualToString: @" .git" ])
52
+ self.path = [p stringByAppendingPathComponent: @" .git" ];
53
+ else
54
+ self.path = newPath;
121
55
}
122
56
123
- [self performSelectorOnMainThread: @selector (setCommits: ) withObject: newArray waitUntilDone: YES ];
124
- NSTimeInterval duration = [[NSDate date ] timeIntervalSinceDate: start];
125
- NSLog (@" Loaded %i commits in %f seconds" , num, duration);
126
-
127
- [NSThread exit ];
57
+ NSLog (@" Git path is: %@ " , self.path );
58
+ revisionList = [[PBGitRevList alloc ] initWithRepository: self andRevListParameters: [NSArray array ]];
59
+ return self;
128
60
}
129
61
62
+
130
63
- (NSFileHandle *) handleForArguments : (NSArray *)args
131
64
{
132
65
NSString * gitDirArg = [@" --git-dir=" stringByAppendingString: path];
0 commit comments