5 // Created by Pieter de Bie on 15-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
10 #import "PBGitCommit.h"
11 #import "NSFileHandleExt.h"
12 #import "PBEasyPipe.h"
15 @implementation PBGitTree
17 @synthesize sha, path, repository, leaf, parent;
19 + (PBGitTree*) rootForCommit:(id) commit
21 PBGitCommit* c = commit;
22 PBGitTree* tree = [[self alloc] init];
26 tree.repository = c.repository;
31 + (PBGitTree*) treeForTree: (PBGitTree*) prev andPath: (NSString*) path;
33 PBGitTree* tree = [[self alloc] init];
36 tree.repository = prev.repository;
51 return [NSString stringWithFormat:@"%@:%@", self.sha, self.fullPath];
54 - (BOOL) isLocallyCached
56 NSFileManager* fs = [NSFileManager defaultManager];
57 if (localFileName && [fs fileExistsAtPath:localFileName])
59 NSDate* mtime = [[fs attributesOfItemAtPath:localFileName error: nil] objectForKey:NSFileModificationDate];
60 if ([mtime compare:localMtime] == 0)
66 - (NSString*) contents
69 return [NSString stringWithFormat:@"This is a tree with path %@", self];
73 if ([self isLocallyCached])
74 data = [NSData dataWithContentsOfFile: localFileName];
76 NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
77 data = [handle readDataToEndOfFile];
80 return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
83 - (void) saveToFolder: (NSString *) dir
85 NSString* newName = [dir stringByAppendingPathComponent:path];
88 NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
89 NSData* data = [handle readDataToEndOfFile];
90 [data writeToFile:newName atomically:YES];
92 [[NSFileManager defaultManager] createDirectoryAtPath:newName attributes:nil];
93 for (PBGitTree* child in children)
94 [child saveToFolder: newName];
98 - (NSString*) tmpDirWithContents
104 localFileName = [PBEasyFS tmpDirWithPrefix: path];
106 for (PBGitTree* child in [self children]) {
107 [child saveToFolder: localFileName];
110 return localFileName;
115 - (NSString*) tmpFileNameForContents
118 return [self tmpDirWithContents];
120 if ([self isLocallyCached])
121 return localFileName;
124 localFileName = [[PBEasyFS tmpDirWithPrefix: sha] stringByAppendingPathComponent:path];
126 NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
127 NSData* data = [handle readDataToEndOfFile];
128 [data writeToFile:localFileName atomically:YES];
130 NSFileManager* fs = [NSFileManager defaultManager];
131 localMtime = [[fs attributesOfItemAtPath:localFileName error: nil] objectForKey:NSFileModificationDate];
133 return localFileName;
136 - (NSArray*) children
141 NSString* ref = [self refSpec];
143 NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", ref, nil]];
147 NSMutableArray* c = [NSMutableArray array];
149 NSString* p = [handle readLine];
150 while (p.length > 0) {
151 BOOL isLeaf = ([p characterAtIndex:p.length - 1] != '/');
153 p = [p substringToIndex:p.length -1];
155 PBGitTree* child = [PBGitTree treeForTree:self andPath:p];
157 [c addObject: child];
159 p = [handle readLine];
165 - (NSString*) fullPath
170 if ([parent.fullPath isEqualToString:@""])
173 return [parent.fullPath stringByAppendingPathComponent: self.path];
179 [[NSFileManager defaultManager] removeFileAtPath:localFileName handler:nil];