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];
25 tree.sha = [c realSha];
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 fullPath]];
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 NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
82 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
87 - (void) saveToFolder: (NSString *) dir
89 NSString* newName = [dir stringByAppendingPathComponent:path];
92 NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
93 NSData* data = [handle readDataToEndOfFile];
94 [data writeToFile:newName atomically:YES];
96 [[NSFileManager defaultManager] createDirectoryAtPath:newName attributes:nil];
97 for (PBGitTree* child in [self children])
98 [child saveToFolder: newName];
102 - (NSString*) tmpDirWithContents
108 localFileName = [PBEasyFS tmpDirWithPrefix: path];
110 for (PBGitTree* child in [self children]) {
111 [child saveToFolder: localFileName];
114 return localFileName;
119 - (NSString*) tmpFileNameForContents
122 return [self tmpDirWithContents];
124 if ([self isLocallyCached])
125 return localFileName;
128 localFileName = [[PBEasyFS tmpDirWithPrefix: sha] stringByAppendingPathComponent:path];
130 NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
131 NSData* data = [handle readDataToEndOfFile];
132 [data writeToFile:localFileName atomically:YES];
134 NSFileManager* fs = [NSFileManager defaultManager];
135 localMtime = [[fs attributesOfItemAtPath:localFileName error: nil] objectForKey:NSFileModificationDate];
137 return localFileName;
140 - (NSArray*) children
145 NSString* ref = [self refSpec];
147 NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", ref, nil]];
151 NSMutableArray* c = [NSMutableArray array];
153 NSString* p = [handle readLine];
154 while (p.length > 0) {
155 BOOL isLeaf = ([p characterAtIndex:p.length - 1] != '/');
157 p = [p substringToIndex:p.length -1];
159 PBGitTree* child = [PBGitTree treeForTree:self andPath:p];
161 [c addObject: child];
163 p = [handle readLine];
169 - (NSString*) fullPath
174 if ([parent.fullPath isEqualToString:@""])
177 return [parent.fullPath stringByAppendingPathComponent: self.path];
183 [[NSFileManager defaultManager] removeFileAtPath:localFileName handler:nil];