CommitController: Make commit message editable after commit failed
[GitX.git] / PBGitRevList.mm
blobca3704f312986f97ce85f90fcafb2a19fc822df4
1 //
2 //  PBGitRevList.m
3 //  GitX
4 //
5 //  Created by Pieter de Bie on 17-06-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBGitRevList.h"
10 #import "PBGitRepository.h"
11 #import "PBGitCommit.h"
12 #import "PBGitGrapher.h"
13 #import "PBGitRevSpecifier.h"
15 #include "git/oid.h"
16 #include <ext/stdio_filebuf.h>
17 #include <iostream>
18 #include <string>
19 #include <map>
21 using namespace std;
23 @implementation PBGitRevList
25 @synthesize commits;
26 - (id)initWithRepository:(PBGitRepository *)repo
28         repository = repo;
29         [repository addObserver:self forKeyPath:@"currentBranch" options:0 context:nil];
31         return self;
34 - (void) reload
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])
47                 return;
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])
55                         return;
56         }
57         lastSha = newSha;
59         NSThread * commitThread = [[NSThread alloc] initWithTarget: self selector: @selector(walkRevisionListWithSpecifier:) object:newRev];
60         [commitThread start];
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];
80         if (showSign)
81                 formatString = [formatString stringByAppendingString:@"\01%m"];
82         
83         NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"log", @"-z", @"--early-output", @"--topo-order", @"--children", formatString, nil];
85         if (!rev)
86                 [arguments addObject:@"HEAD"];
87         else
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];
92         [task launch];
93         NSFileHandle* handle = [task.standardOutput fileHandleForReading];
94         
95         int fd = [handle fileDescriptor];
96         __gnu_cxx::stdio_filebuf<char> buf(fd, std::ios::in);
97         std::istream stream(&buf);
99         int num = 0;
100         while (true) {
101                 string sha;
102                 if (!getline(stream, sha, '\1'))
103                         break;
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'
109                 {
110                         num = 0;
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)
117                                 break;
119                         sha = sha.substr(sha.length() - 40, 40);
120                 }
122                 // From now on, 1.2 seconds
123                 string encoding_str;
124                 getline(stream, encoding_str, '\1');
125                 NSStringEncoding encoding = NSUTF8StringEncoding;
126                 if (encoding_str.length())
127                 {
128                         if (encodingMap.find(encoding_str) != encodingMap.end()) {
129                                 encoding = encodingMap[encoding_str];
130                         } else {
131                                 encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)[NSString stringWithUTF8String:encoding_str.c_str()]));
132                                 encodingMap[encoding_str] = encoding;
133                         }
134                 }
136                 git_oid oid;
137                 git_oid_mkstr(&oid, sha.c_str());
138                 PBGitCommit* newCommit = [[PBGitCommit alloc] initWithRepository:repository andSha:oid];
140                 string author;
141                 getline(stream, author, '\1');
143                 string subject;
144                 getline(stream, subject, '\1');
146                 string parentString;
147                 getline(stream, parentString, '\1');
148                 if (parentString.size() != 0)
149                 {
150                         if (((parentString.size() + 1) % 41) != 0) {
151                                 NSLog(@"invalid parents: %i", parentString.size());
152                                 continue;
153                         }
154                         int nParents = (parentString.size() + 1) / 41;
155                         git_oid *parents = (git_oid *)malloc(sizeof(git_oid) * nParents);
156                         int parentIndex;
157                         for (parentIndex = 0; parentIndex < nParents; ++parentIndex)
158                                 git_oid_mkstr(parents + parentIndex, parentString.substr(parentIndex * 41, 40).c_str());
159                         
160                         newCommit.parentShas = parents;
161                         newCommit.nParents = nParents;
162                 }
164                 int time;
165                 stream >> time;
167                 
168                 [newCommit setSubject:[NSString stringWithCString:subject.c_str() encoding:encoding]];
169                 [newCommit setAuthor:[NSString stringWithCString:author.c_str() encoding:encoding]];
170                 [newCommit setTimestamp:time];
171                 
172                 if (showSign)
173                 {
174                         char c;
175                         stream >> c; // Remove separator
176                         stream >> c;
177                         if (c != '>' && c != '<' && c != '^' && c != '-')
178                                 NSLog(@"Error loading commits: sign not correct");
179                         [newCommit setSign: c];
180                 }
182                 char c;
183                 stream >> c;
184                 if (c != '\0')
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];
192         }
193         
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];
201 @end