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>
23 @implementation PBGitRevList
26 - (id)initWithRepository:(PBGitRepository *)repo
29 [repository addObserver:self forKeyPath:@"currentBranch" options:0 context:nil];
36 [self readCommitsForce: YES];
39 - (void) readCommitsForce: (BOOL) force
41 // We use refparse to get the commit sha that we will parse. That way,
42 // we can check if the current branch is the same as the previous one
43 // and in that case we don't have to reload the revision list.
45 // If no branch is selected, don't do anything
46 if (![repository currentBranch])
49 PBGitRevSpecifier* newRev = [repository currentBranch];
50 NSString* newSha = nil;
52 if (!force && newRev && [newRev isSimpleRef]) {
53 newSha = [repository parseReference:[newRev simpleRef]];
54 if ([newSha isEqualToString:lastSha])
59 NSThread * commitThread = [[NSThread alloc] initWithTarget: self selector: @selector(walkRevisionListWithSpecifier:) object:newRev];
63 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
64 change:(NSDictionary *)change context:(void *)context
66 if (object == repository)
67 [self readCommitsForce: NO];
70 - (void) walkRevisionListWithSpecifier: (PBGitRevSpecifier*) rev
72 NSDate *start = [NSDate date];
73 NSMutableArray* revisions = [NSMutableArray array];
74 PBGitGrapher* g = [[PBGitGrapher alloc] initWithRepository: repository];
75 std::map<string, NSStringEncoding> encodingMap;
77 NSString *formatString = @"--pretty=format:%H\01%e\01%an\01%s\01%P\01%at";
78 BOOL showSign = [rev hasLeftRight];
81 formatString = [formatString stringByAppendingString:@"\01%m"];
83 NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"log", @"-z", @"--early-output", @"--topo-order", @"--children", formatString, nil];
86 [arguments addObject:@"HEAD"];
88 [arguments addObjectsFromArray:[rev parameters]];
90 NSString *directory = rev.workingDirectory ? rev.workingDirectory.path : repository.fileURL.path;
91 NSTask *task = [PBEasyPipe taskForCommand:[PBGitBinary path] withArgs:arguments inDir:directory];
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, '\1'))
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 // If the length is < 40, then there are no commits.. quit now
116 if (sha.length() < 40)
119 sha = sha.substr(sha.length() - 40, 40);
122 // From now on, 1.2 seconds
124 getline(stream, encoding_str, '\1');
125 NSStringEncoding encoding = NSUTF8StringEncoding;
126 if (encoding_str.length())
128 if (encodingMap.find(encoding_str) != encodingMap.end()) {
129 encoding = encodingMap[encoding_str];
131 encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)[NSString stringWithUTF8String:encoding_str.c_str()]));
132 encodingMap[encoding_str] = encoding;
137 git_oid_mkstr(&oid, sha.c_str());
138 PBGitCommit* newCommit = [[PBGitCommit alloc] initWithRepository:repository andSha:oid];
141 getline(stream, author, '\1');
144 getline(stream, subject, '\1');
147 getline(stream, parentString, '\1');
148 if (parentString.size() != 0)
150 if (((parentString.size() + 1) % 41) != 0) {
151 NSLog(@"invalid parents: %i", parentString.size());
154 int nParents = (parentString.size() + 1) / 41;
155 git_oid *parents = (git_oid *)malloc(sizeof(git_oid) * nParents);
157 for (parentIndex = 0; parentIndex < nParents; ++parentIndex)
158 git_oid_mkstr(parents + parentIndex, parentString.substr(parentIndex * 41, 40).c_str());
160 newCommit.parentShas = parents;
161 newCommit.nParents = nParents;
168 [newCommit setSubject:[NSString stringWithCString:subject.c_str() encoding:encoding]];
169 [newCommit setAuthor:[NSString stringWithCString:author.c_str() encoding:encoding]];
170 [newCommit setTimestamp:time];
175 stream >> c; // Remove separator
177 if (c != '>' && c != '<' && c != '^' && c != '-')
178 NSLog(@"Error loading commits: sign not correct");
179 [newCommit setSign: c];
185 cout << "Error" << endl;
187 [revisions addObject: newCommit];
188 [g decorateCommit: newCommit];
190 if (++num % 1000 == 0)
191 [self performSelectorOnMainThread:@selector(setCommits:) withObject:revisions waitUntilDone:NO];
194 NSTimeInterval duration = [[NSDate date] timeIntervalSinceDate:start];
195 NSLog(@"Loaded %i commits in %f seconds", num, duration);
196 // Make sure the commits are stored before exiting.
197 [self performSelectorOnMainThread:@selector(setCommits:) withObject:revisions waitUntilDone:YES];
198 [task waitUntilExit];