Remove use of deprecated stringWithCString
[GitX.git] / PBGitTree.m
blob51fadd2d7a275cc195fd6d38bcb63d09ae0dc4a2
1 //
2 //  PBGitTree.m
3 //  GitTest
4 //
5 //  Created by Pieter de Bie on 15-06-08.
6 //  Copyright 2008 __MyCompanyName__. All rights reserved.
7 //
9 #import "PBGitTree.h"
10 #import "PBGitCommit.h"
11 #import "NSFileHandleExt.h"
12 #import "PBEasyPipe.h"
13 #import "PBEasyFS.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];
23         tree.parent = nil;
24         tree.leaf = NO;
25         tree.sha = [c realSha];
26         tree.repository = c.repository;
27         tree.path = @"";
28         return tree;
31 + (PBGitTree*) treeForTree: (PBGitTree*) prev andPath: (NSString*) path;
33         PBGitTree* tree = [[self alloc] init];
34         tree.parent = prev;
35         tree.sha = prev.sha;
36         tree.repository = prev.repository;
37         tree.path = path;
38         return tree;
41 - init
43         children = nil;
44         localFileName = nil;
45         leaf = YES;
46         return self;
49 - (NSString*) refSpec
51         return [NSString stringWithFormat:@"%@:%@", self.sha, self.fullPath];
54 - (BOOL) isLocallyCached
56         NSFileManager* fs = [NSFileManager defaultManager];
57         if (localFileName && [fs fileExistsAtPath:localFileName])
58         {
59                 NSDate* mtime = [[fs attributesOfItemAtPath:localFileName error: nil] objectForKey:NSFileModificationDate];
60                 if ([mtime compare:localMtime] == 0)
61                         return YES;
62         }
63         return NO;
66 - (NSString*) contents
68         if (!leaf)
69                 return [NSString stringWithFormat:@"This is a tree with path %@", [self fullPath]];
71         NSData* data = nil;
72         
73         if ([self isLocallyCached])
74                 data = [NSData dataWithContentsOfFile: localFileName];
75         else {
76                 NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
77                 data = [handle readDataToEndOfFile];
78         }
79         
80         NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
81         if (!string) {
82                 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
83         }
84         return string;
87 - (void) saveToFolder: (NSString *) dir
89         NSString* newName = [dir stringByAppendingPathComponent:path];
91         if (leaf) {
92                 NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
93                 NSData* data = [handle readDataToEndOfFile];
94                 [data writeToFile:newName atomically:YES];
95         } else { // Directory
96                 [[NSFileManager defaultManager] createDirectoryAtPath:newName attributes:nil];
97                 for (PBGitTree* child in [self children])
98                         [child saveToFolder: newName];
99         }
102 - (NSString*) tmpDirWithContents
104         if (leaf)
105                 return nil;
107         if (!localFileName)
108                 localFileName = [PBEasyFS tmpDirWithPrefix: path];
110         for (PBGitTree* child in [self children]) {
111                 [child saveToFolder: localFileName];
112         }
113         
114         return localFileName;
117         
119 - (NSString*) tmpFileNameForContents
121         if (!leaf)
122                 return [self tmpDirWithContents];
123         
124         if ([self isLocallyCached])
125                 return localFileName;
126         
127         if (!localFileName)
128                 localFileName = [[PBEasyFS tmpDirWithPrefix: sha] stringByAppendingPathComponent:path];
129         
130         NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
131         NSData* data = [handle readDataToEndOfFile];
132         [data writeToFile:localFileName atomically:YES];
133         
134         NSFileManager* fs = [NSFileManager defaultManager];
135         localMtime = [[fs attributesOfItemAtPath:localFileName error: nil] objectForKey:NSFileModificationDate];
137         return localFileName;
140 - (NSArray*) children
142         if (children != nil)
143                 return children;
144         
145         NSString* ref = [self refSpec];
147         NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", ref, nil]];
148         [handle readLine];
149         [handle readLine];
150         
151         NSMutableArray* c = [NSMutableArray array];
152         
153         NSString* p = [handle readLine];
154         while (p.length > 0) {
155                 BOOL isLeaf = ([p characterAtIndex:p.length - 1] != '/');
156                 if (!isLeaf)
157                         p = [p substringToIndex:p.length -1];
159                 PBGitTree* child = [PBGitTree treeForTree:self andPath:p];
160                 child.leaf = isLeaf;
161                 [c addObject: child];
162                 
163                 p = [handle readLine];
164         }
165         children = c;
166         return c;
169 - (NSString*) fullPath
171         if (!parent)
172                 return @"";
173         
174         if ([parent.fullPath isEqualToString:@""])
175                 return self.path;
176         
177         return [parent.fullPath stringByAppendingPathComponent: self.path];
180 - (void) finalize
182         if (localFileName)
183                 [[NSFileManager defaultManager] removeFileAtPath:localFileName handler:nil];
184         [super finalize];
186 @end