5 // Created by Pieter de Bie on 13-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "PBGitRepository.h"
10 #import "PBGitCommit.h"
11 #import "PBGitWindowController.h"
12 #import "PBGitBinary.h"
14 #import "NSFileHandleExt.h"
15 #import "PBEasyPipe.h"
17 #import "PBGitRevSpecifier.h"
19 NSString* PBGitRepositoryErrorDomain = @"GitXErrorDomain";
21 @implementation PBGitRepository
23 @synthesize revisionList, branches, currentBranch, refs, hasChanged, config;
25 - (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError
28 *outError = [NSError errorWithDomain:PBGitRepositoryErrorDomain
30 userInfo:[NSDictionary dictionaryWithObject:@"Reading files is not supported." forKey:NSLocalizedFailureReasonErrorKey]];
35 + (BOOL) isBareRepository: (NSString*) path
37 return [[PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:[NSArray arrayWithObjects:@"rev-parse", @"--is-bare-repository", nil] inDir:path] isEqualToString:@"true"];
40 + (NSURL*)gitDirForURL:(NSURL*)repositoryURL;
42 if (![PBGitBinary path])
45 NSString* repositoryPath = [repositoryURL path];
47 if ([self isBareRepository:repositoryPath])
51 // Use rev-parse to find the .git dir for the repository being opened
52 NSString* newPath = [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:[NSArray arrayWithObjects:@"rev-parse", @"--git-dir", nil] inDir:repositoryPath];
53 if ([newPath isEqualToString:@".git"])
54 return [NSURL fileURLWithPath:[repositoryPath stringByAppendingPathComponent:@".git"]];
55 if ([newPath length] > 0)
56 return [NSURL fileURLWithPath:newPath];
61 // For a given path inside a repository, return either the .git dir
62 // (for a bare repo) or the directory above the .git dir otherwise
63 + (NSURL*)baseDirForURL:(NSURL*)repositoryURL;
65 NSURL* gitDirURL = [self gitDirForURL:repositoryURL];
66 NSString* repositoryPath = [gitDirURL path];
68 if (![self isBareRepository:repositoryPath]) {
69 repositoryURL = [NSURL fileURLWithPath:[[repositoryURL path] stringByDeletingLastPathComponent]];
75 // NSFileWrapper is broken and doesn't work when called on a directory containing a large number of directories and files.
76 //because of this it is safer to implement readFromURL than readFromFileWrapper.
77 //Because NSFileManager does not attempt to recursively open all directories and file when fileExistsAtPath is called
78 //this works much better.
79 - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
81 if (![PBGitBinary path])
84 NSDictionary* userInfo = [NSDictionary dictionaryWithObject:[PBGitBinary notFoundError]
85 forKey:NSLocalizedRecoverySuggestionErrorKey];
86 *outError = [NSError errorWithDomain:PBGitRepositoryErrorDomain code:0 userInfo:userInfo];
91 BOOL isDirectory = FALSE;
92 [[NSFileManager defaultManager] fileExistsAtPath:[absoluteURL path] isDirectory:&isDirectory];
95 NSDictionary* userInfo = [NSDictionary dictionaryWithObject:@"Reading files is not supported."
96 forKey:NSLocalizedRecoverySuggestionErrorKey];
97 *outError = [NSError errorWithDomain:PBGitRepositoryErrorDomain code:0 userInfo:userInfo];
103 NSURL* gitDirURL = [PBGitRepository gitDirForURL:[self fileURL]];
106 NSDictionary* userInfo = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%@ does not appear to be a git repository.", [self fileName]]
107 forKey:NSLocalizedRecoverySuggestionErrorKey];
108 *outError = [NSError errorWithDomain:PBGitRepositoryErrorDomain code:0 userInfo:userInfo];
113 [self setFileURL:gitDirURL];
115 [self readCurrentBranch];
121 config = [[PBGitConfig alloc] initWithRepository:self.fileURL.path];
122 self.branches = [NSMutableArray array];
124 revisionList = [[PBGitRevList alloc] initWithRepository:self];
127 - (id) initWithURL: (NSURL*) path
129 if (![PBGitBinary path])
132 NSURL* gitDirURL = [PBGitRepository gitDirForURL:path];
137 [self setFileURL: gitDirURL];
141 // We don't want the window controller to display anything yet..
142 // We'll leave that to the caller of this method.
144 [self addWindowController:[[PBGitWindowController alloc] initWithRepository:self displayDefault:NO]];
152 // The fileURL the document keeps is to the .git dir, but that’s pretty
153 // useless for display in the window title bar, so we show the directory above
154 - (NSString*)displayName
156 NSString* dirName = self.fileURL.path.lastPathComponent;
157 if ([dirName isEqualToString:@".git"])
158 dirName = [self.fileURL.path stringByDeletingLastPathComponent].lastPathComponent;
159 NSString* displayName;
160 if (![[PBGitRef refFromString:[[self headRef] simpleRef]] type]) {
161 displayName = [NSString stringWithFormat:@"%@ (detached HEAD)", dirName];
163 displayName = [NSString stringWithFormat:@"%@ (branch: %@)", dirName,
164 [[self headRef] description]];
170 // Get the .gitignore file at the root of the repository
171 - (NSString*)gitIgnoreFilename
173 return [[self workingDirectory] stringByAppendingPathComponent:@".gitignore"];
176 - (BOOL)isBareRepository
178 if([self workingDirectory]) {
179 return [PBGitRepository isBareRepository:[self workingDirectory]];
185 // Overridden to create our custom window controller
186 - (void)makeWindowControllers
189 [self addWindowController: [[PBGitWindowController alloc] initWithRepository:self displayDefault:YES]];
193 - (PBGitWindowController *)windowController
195 if ([[self windowControllers] count] == 0)
198 return [[self windowControllers] objectAtIndex:0];
201 - (void) addRef: (PBGitRef *) ref fromParameters: (NSArray *) components
203 NSString* type = [components objectAtIndex:1];
206 if ([type isEqualToString:@"tag"] && [components count] == 4)
207 sha = [components objectAtIndex:3];
209 sha = [components objectAtIndex:2];
211 NSMutableArray* curRefs;
212 if (curRefs = [refs objectForKey:sha])
213 [curRefs addObject:ref];
215 [refs setObject:[NSMutableArray arrayWithObject:ref] forKey:sha];
218 // reloadRefs: reload all refs in the repository, like in readRefs
219 // To stay compatible, this does not remove a ref from the branches list
220 // even after it has been deleted.
221 // returns YES when a ref was changed
227 refs = [NSMutableDictionary dictionary];
229 NSString* output = [PBEasyPipe outputForCommand:[PBGitBinary path]
230 withArgs:[NSArray arrayWithObjects:@"for-each-ref", @"--format=%(refname) %(objecttype) %(objectname)"
231 " %(*objectname)", @"refs", nil]
232 inDir: self.fileURL.path];
233 NSArray* lines = [output componentsSeparatedByString:@"\n"];
235 for (NSString* line in lines) {
236 // If its an empty line, skip it (e.g. with empty repositories)
237 if ([line length] == 0)
240 NSArray* components = [line componentsSeparatedByString:@" "];
242 // First do the ref matching. If this ref is new, add it to our ref list
243 PBGitRef *newRef = [PBGitRef refFromString:[components objectAtIndex:0]];
244 PBGitRevSpecifier* revSpec = [[PBGitRevSpecifier alloc] initWithRef:newRef];
245 if ([self addBranch:revSpec] != revSpec)
248 // Also add this ref to the refs list
249 [self addRef:newRef fromParameters:components];
252 // Add an "All branches" option in the branches list
253 [self addBranch:[PBGitRevSpecifier allBranchesRevSpec]];
254 [self addBranch:[PBGitRevSpecifier localBranchesRevSpec]];
256 [[[self windowController] window] setTitle:[self displayName]];
267 [self.revisionList reload];
271 - (PBGitRevSpecifier *)headRef
276 NSString* branch = [self parseSymbolicReference: @"HEAD"];
277 if (branch && [branch hasPrefix:@"refs/heads/"])
278 _headRef = [[PBGitRevSpecifier alloc] initWithRef:[PBGitRef refFromString:branch]];
280 _headRef = [[PBGitRevSpecifier alloc] initWithRef:[PBGitRef refFromString:@"HEAD"]];
285 // Returns either this object, or an existing, equal object
286 - (PBGitRevSpecifier*) addBranch: (PBGitRevSpecifier*) rev
288 if ([[rev parameters] count] == 0)
289 rev = [self headRef];
291 // First check if the branch doesn't exist already
292 for (PBGitRevSpecifier* r in branches)
293 if ([rev isEqualTo: r])
296 [self willChangeValueForKey:@"branches"];
297 [branches addObject: rev];
298 [self didChangeValueForKey:@"branches"];
302 - (BOOL)removeBranch:(PBGitRevSpecifier *)rev
304 for (PBGitRevSpecifier *r in branches) {
305 if ([rev isEqualTo:r]) {
306 [self willChangeValueForKey:@"branches"];
307 [branches removeObject:r];
308 [self didChangeValueForKey:@"branches"];
315 - (void) readCurrentBranch
317 self.currentBranch = [self addBranch: [self headRef]];
320 - (NSString *) workingDirectory
322 if ([self.fileURL.path hasSuffix:@"/.git"])
323 return [self.fileURL.path substringToIndex:[self.fileURL.path length] - 5];
324 else if ([[self outputForCommand:@"rev-parse --is-inside-work-tree"] isEqualToString:@"true"])
325 return [PBGitBinary path];
330 - (int) returnValueForCommand:(NSString *)cmd
333 [self outputForCommand:cmd retValue: &i];
337 - (NSFileHandle*) handleForArguments:(NSArray *)args
339 NSString* gitDirArg = [@"--git-dir=" stringByAppendingString:self.fileURL.path];
340 NSMutableArray* arguments = [NSMutableArray arrayWithObject: gitDirArg];
341 [arguments addObjectsFromArray: args];
342 return [PBEasyPipe handleForCommand:[PBGitBinary path] withArgs:arguments];
345 - (NSFileHandle*) handleInWorkDirForArguments:(NSArray *)args
347 NSString* gitDirArg = [@"--git-dir=" stringByAppendingString:self.fileURL.path];
348 NSMutableArray* arguments = [NSMutableArray arrayWithObject: gitDirArg];
349 [arguments addObjectsFromArray: args];
350 return [PBEasyPipe handleForCommand:[PBGitBinary path] withArgs:arguments inDir:[self workingDirectory]];
353 - (NSFileHandle*) handleForCommand:(NSString *)cmd
355 NSArray* arguments = [cmd componentsSeparatedByString:@" "];
356 return [self handleForArguments:arguments];
359 - (NSString*) outputForCommand:(NSString *)cmd
361 NSArray* arguments = [cmd componentsSeparatedByString:@" "];
362 return [self outputForArguments: arguments];
365 - (NSString*) outputForCommand:(NSString *)str retValue:(int *)ret;
367 NSArray* arguments = [str componentsSeparatedByString:@" "];
368 return [self outputForArguments: arguments retValue: ret];
371 - (NSString*) outputForArguments:(NSArray*) arguments
373 return [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir: self.fileURL.path];
376 - (NSString*) outputInWorkdirForArguments:(NSArray*) arguments
378 return [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir: [self workingDirectory]];
381 - (NSString*) outputInWorkdirForArguments:(NSArray *)arguments retValue:(int *)ret
383 return [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir:[self workingDirectory] retValue: ret];
386 - (NSString*) outputForArguments:(NSArray *)arguments retValue:(int *)ret
388 return [PBEasyPipe outputForCommand:[PBGitBinary path] withArgs:arguments inDir: self.fileURL.path retValue: ret];
391 - (NSString*) outputForArguments:(NSArray *)arguments inputString:(NSString *)input retValue:(int *)ret
393 return [PBEasyPipe outputForCommand:[PBGitBinary path]
395 inDir:[self workingDirectory]
400 - (NSString *)outputForArguments:(NSArray *)arguments inputString:(NSString *)input byExtendingEnvironment:(NSDictionary *)dict retValue:(int *)ret
402 return [PBEasyPipe outputForCommand:[PBGitBinary path]
404 inDir:[self workingDirectory]
405 byExtendingEnvironment:dict
410 - (BOOL)executeHook:(NSString *)name output:(NSString **)output
412 return [self executeHook:name withArgs:[NSArray array] output:output];
415 - (BOOL)executeHook:(NSString *)name withArgs:(NSArray *)arguments output:(NSString **)output
417 NSString *hookPath = [[[[self fileURL] path] stringByAppendingPathComponent:@"hooks"] stringByAppendingPathComponent:name];
418 if (![[NSFileManager defaultManager] isExecutableFileAtPath:hookPath])
421 NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
422 [self fileURL].path, @"GIT_DIR",
423 [[self fileURL].path stringByAppendingPathComponent:@"index"], @"GIT_INDEX_FILE",
428 NSString *_output = [PBEasyPipe outputForCommand:hookPath withArgs:arguments inDir:[self workingDirectory] byExtendingEnvironment:info inputString:nil retValue:&ret];
436 - (NSString *)parseReference:(NSString *)reference
439 NSString *ref = [self outputForArguments:[NSArray arrayWithObjects: @"rev-parse", @"--verify", reference, nil] retValue: &ret];
446 - (NSString*) parseSymbolicReference:(NSString*) reference
448 NSString* ref = [self outputForArguments:[NSArray arrayWithObjects: @"symbolic-ref", @"-q", reference, nil]];
449 if ([ref hasPrefix:@"refs/"])
457 NSLog(@"Dealloc of repository");