5 // Created by Pieter de Bie on 17-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "PBGitRevList.h"
10 #import "PBGitRepository.h"
11 #import "PBGitCommit.h"
12 #import "PBGitGrapher.h"
13 #import "PBGitRevSpecifier.h"
16 #include <ext/stdio_filebuf.h>
22 @implementation PBGitRevList
25 - initWithRepository: (id) repo
28 [repository addObserver:self forKeyPath:@"currentBranch" options:0 context:nil];
35 [self readCommitsForce: YES];
38 - (void) readCommitsForce: (BOOL) force
40 // We use refparse to get the commit sha that we will parse. That way,
41 // we can check if the current branch is the same as the previous one
42 // and in that case we don't have to reload the revision list.
44 // If no branch is selected, don't do anything
45 if (![repository currentBranch])
48 PBGitRevSpecifier* newRev = [repository currentBranch];
49 NSString* newSha = nil;
51 if (!force && newRev && [newRev isSimpleRef]) {
52 newSha = [repository parseReference:[newRev simpleRef]];
53 if ([newSha isEqualToString:lastSha])
58 NSThread * commitThread = [[NSThread alloc] initWithTarget: self selector: @selector(walkRevisionListWithSpecifier:) object:newRev];
62 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
63 change:(NSDictionary *)change context:(void *)context
65 if (object == repository)
66 [self readCommitsForce: NO];
69 - (void) walkRevisionListWithSpecifier: (PBGitRevSpecifier*) rev
71 NSDate *start = [NSDate date];
72 NSMutableArray* revisions = [NSMutableArray array];
73 PBGitGrapher* g = [[PBGitGrapher alloc] initWithRepository: repository];
75 NSMutableArray* arguments;
76 BOOL showSign = [rev hasLeftRight];
79 arguments = [NSMutableArray arrayWithObjects:@"log", @"-z", @"--early-output", @"--topo-order", @"--pretty=format:%H%x00%an%x00%s%x00%P%x00%at%x00%m", nil];
81 arguments = [NSMutableArray arrayWithObjects:@"log", @"-z", @"--early-output", @"--topo-order", @"--pretty=format:%H%x00%an%x00%s%x00%P%x00%at", nil];
84 [arguments addObject:@"HEAD"];
86 [arguments addObjectsFromArray:[rev parameters]];
88 if ([rev hasPathLimiter])
89 [arguments insertObject:@"--children" atIndex:1];
91 NSTask *task = [PBEasyPipe taskForCommand:[PBGitBinary path] withArgs:arguments inDir:[repository fileURL].path];
93 NSFileHandle* handle = [task.standardOutput fileHandleForReading];
95 int fd = [handle fileDescriptor];
96 __gnu_cxx::stdio_filebuf<char> buf(fd, std::ios::in);
97 std::istream stream(&buf);
102 if (!getline(stream, sha, '\0'))
105 // We reached the end of some temporary output. Show what we have
106 // until now, and then start again. The sha of the next thing is still
107 // in this buffer. So, we use a substring of current input.
108 if (sha[1] == 'i') // Matches 'Final output'
111 [self performSelectorOnMainThread:@selector(setCommits:) withObject:revisions waitUntilDone:NO];
112 g = [[PBGitGrapher alloc] initWithRepository: repository];
113 revisions = [NSMutableArray array];
115 sha = sha.substr(sha.length() - 40, 40);
118 // From now on, 1.2 seconds
120 git_oid_mkstr(&oid, sha.c_str());
121 PBGitCommit* newCommit = [[PBGitCommit alloc] initWithRepository:repository andSha:oid];
124 getline(stream, author, '\0');
127 getline(stream, subject, '\0');
130 getline(stream, parentString, '\0');
131 if (parentString.size() != 0)
133 if (((parentString.size() + 1) % 41) != 0) {
134 NSLog(@"invalid parents: %i", parentString.size());
137 int nParents = (parentString.size() + 1) / 41;
138 git_oid *parents = (git_oid *)malloc(sizeof(git_oid) * nParents);
140 for (parentIndex = 0; parentIndex < nParents; ++parentIndex)
141 git_oid_mkstr(parents + parentIndex, parentString.substr(parentIndex * 41, 40).c_str());
143 newCommit.parentShas = parents;
144 newCommit.nParents = nParents;
153 cout << "Error" << endl;
155 [newCommit setSubject:[NSString stringWithUTF8String:subject.c_str()]];
156 [newCommit setAuthor:[NSString stringWithUTF8String:author.c_str()]];
157 [newCommit setTimestamp:time];
162 if (c != '>' && c != '<' && c != '^' && c != '-')
163 NSLog(@"Error loading commits: sign not correct");
164 [newCommit setSign: c];
166 if (c != '\0' && !stream.eof())
167 NSLog(@"Error: unexpected char (expected '0', was: %c)", c);
170 [revisions addObject: newCommit];
171 [g decorateCommit: newCommit];
173 if (++num % 1000 == 0)
174 [self performSelectorOnMainThread:@selector(setCommits:) withObject:revisions waitUntilDone:NO];
177 NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:start];
178 NSLog(@"Loaded %i commits in %f seconds", num, duration);
179 // Make sure the commits are stored before exiting.
180 [self performSelectorOnMainThread:@selector(setCommits:) withObject:revisions waitUntilDone:YES];
181 [task waitUntilExit];