GitX version 0.5
[GitX.git] / PBGitTree.m
blob722b29489a5371f3cb1efea430ad391fd6011b5c
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.sha;
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];
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         return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
83 - (void) saveToFolder: (NSString *) dir
85         NSString* newName = [dir stringByAppendingPathComponent:path];
87         if (leaf) {
88                 NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
89                 NSData* data = [handle readDataToEndOfFile];
90                 [data writeToFile:newName atomically:YES];
91         } else { // Directory
92                 [[NSFileManager defaultManager] createDirectoryAtPath:newName attributes:nil];
93                 for (PBGitTree* child in children)
94                         [child saveToFolder: newName];
95         }
98 - (NSString*) tmpDirWithContents
100         if (leaf)
101                 return nil;
103         if (!localFileName)
104                 localFileName = [PBEasyFS tmpDirWithPrefix: path];
106         for (PBGitTree* child in [self children]) {
107                 [child saveToFolder: localFileName];
108         }
109         
110         return localFileName;
113         
115 - (NSString*) tmpFileNameForContents
117         if (!leaf)
118                 return [self tmpDirWithContents];
119         
120         if ([self isLocallyCached])
121                 return localFileName;
122         
123         if (!localFileName)
124                 localFileName = [[PBEasyFS tmpDirWithPrefix: sha] stringByAppendingPathComponent:path];
125         
126         NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", [self refSpec], nil]];
127         NSData* data = [handle readDataToEndOfFile];
128         [data writeToFile:localFileName atomically:YES];
129         
130         NSFileManager* fs = [NSFileManager defaultManager];
131         localMtime = [[fs attributesOfItemAtPath:localFileName error: nil] objectForKey:NSFileModificationDate];
133         return localFileName;
136 - (NSArray*) children
138         if (children != nil)
139                 return children;
140         
141         NSString* ref = [self refSpec];
143         NSFileHandle* handle = [repository handleForArguments:[NSArray arrayWithObjects:@"show", ref, nil]];
144         [handle readLine];
145         [handle readLine];
146         
147         NSMutableArray* c = [NSMutableArray array];
148         
149         NSString* p = [handle readLine];
150         while (p.length > 0) {
151                 BOOL isLeaf = ([p characterAtIndex:p.length - 1] != '/');
152                 if (!isLeaf)
153                         p = [p substringToIndex:p.length -1];
155                 PBGitTree* child = [PBGitTree treeForTree:self andPath:p];
156                 child.leaf = isLeaf;
157                 [c addObject: child];
158                 
159                 p = [handle readLine];
160         }
161         children = c;
162         return c;
165 - (NSString*) fullPath
167         if (!parent)
168                 return @"";
169         
170         if ([parent.fullPath isEqualToString:@""])
171                 return self.path;
172         
173         return [parent.fullPath stringByAppendingPathComponent: self.path];
176 - (void) finalize
178         if (localFileName)
179                 [[NSFileManager defaultManager] removeFileAtPath:localFileName handler:nil];
180         [super finalize];
182 @end