forked from brotherbard/gitx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PBCloneRepositoryPanel.m
216 lines (160 loc) · 6.06 KB
/
PBCloneRepositoryPanel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
//
// PBCloneRepositoryPanel.m
// GitX
//
// Created by Nathan Kinsinger on 2/7/10.
// Copyright 2010 Nathan Kinsinger. All rights reserved.
//
#import "PBCloneRepositoryPanel.h"
#import "PBRemoteProgressSheet.h"
#import "PBRepositoryDocumentController.h"
#import "PBGitDefaults.h"
@implementation PBCloneRepositoryPanel
@synthesize repositoryURL;
@synthesize destinationPath;
@synthesize errorMessage;
@synthesize repositoryAccessoryView;
@synthesize isBare;
#pragma mark -
#pragma mark PBCloneRepositoryPanel
+ (id) panel
{
return [[self alloc] initWithWindowNibName:@"PBCloneRepositoryPanel"];
}
+ (void)beginCloneRepository:(NSString *)repository toURL:(NSURL *)targetURL isBare:(BOOL)bare
{
if (!repository || [repository isEqualToString:@""] || !targetURL || [[targetURL path] isEqualToString:@""])
return;
PBCloneRepositoryPanel *clonePanel = [PBCloneRepositoryPanel panel];
[clonePanel showWindow:self];
[clonePanel.repositoryURL setStringValue:repository];
[clonePanel.destinationPath setStringValue:[targetURL path]];
clonePanel.isBare = bare;
[clonePanel clone:self];
}
- (void) awakeFromNib
{
[self window];
[self.errorMessage setStringValue:@""];
path = [PBGitDefaults recentCloneDestination];
if (path)
[self.destinationPath setStringValue:path];
browseRepositoryPanel = [NSOpenPanel openPanel];
[browseRepositoryPanel setTitle:@"Browse for git repository"];
[browseRepositoryPanel setMessage:@"Select a folder with a git repository"];
[browseRepositoryPanel setPrompt:@"Select"];
[browseRepositoryPanel setCanChooseFiles:NO];
[browseRepositoryPanel setCanChooseDirectories:YES];
[browseRepositoryPanel setAllowsMultipleSelection:NO];
[browseRepositoryPanel setCanCreateDirectories:NO];
[browseRepositoryPanel setAccessoryView:repositoryAccessoryView];
browseDestinationPanel = [NSOpenPanel openPanel];
[browseDestinationPanel setTitle:@"Browse clone destination"];
[browseDestinationPanel setMessage:@"Select a folder to clone the git repository into"];
[browseDestinationPanel setPrompt:@"Select"];
[browseDestinationPanel setCanChooseFiles:NO];
[browseDestinationPanel setCanChooseDirectories:YES];
[browseDestinationPanel setAllowsMultipleSelection:NO];
[browseDestinationPanel setCanCreateDirectories:YES];
}
- (void)showMessageSheet:(NSString *)messageText infoText:(NSString *)infoText
{
NSAlert *alert = [NSAlert alertWithMessageText:messageText
defaultButton:nil alternateButton:nil otherButton:nil
informativeTextWithFormat:infoText];
[alert beginSheetModalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(messageSheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}
- (void)showErrorSheet:(NSError *)error
{
[[NSAlert alertWithError:error] beginSheetModalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(errorSheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}
#pragma mark IBActions
- (IBAction) closeCloneRepositoryPanel:(id)sender
{
[self close];
}
- (IBAction) clone:(id)sender
{
[self.errorMessage setStringValue:@""];
NSString *url = [self.repositoryURL stringValue];
if ([url isEqualToString:@""]) {
[self.errorMessage setStringValue:@"Repository URL is required"];
return;
}
path = [self.destinationPath stringValue];
if ([path isEqualToString:@""]) {
[self.errorMessage setStringValue:@"Destination path is required"];
return;
}
NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"clone", @"--", url, path, nil];
if (isBare)
[arguments insertObject:@"--bare" atIndex:1];
NSString *description = [NSString stringWithFormat:@"Cloning repository at: %@", url];
NSString *title = @"Cloning Repository";
[PBRemoteProgressSheet beginRemoteProgressSheetForArguments:arguments title:title description:description inDir:nil windowController:self];
}
- (IBAction) browseRepository:(id)sender
{
[browseRepositoryPanel beginSheetForDirectory:nil file:nil types:nil
modalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(browseRepositorySheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}
- (IBAction) showHideHiddenFiles:(id)sender
{
// This uses undocumented OpenPanel features to show hidden files (required for 10.5 support)
NSNumber *showHidden = [NSNumber numberWithBool:[sender state] == NSOnState];
[[browseRepositoryPanel valueForKey:@"_navView"] setValue:showHidden forKey:@"showsHiddenFiles"];
}
- (IBAction) browseDestination:(id)sender
{
[browseDestinationPanel beginSheetForDirectory:nil file:nil types:nil
modalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(browseDestinationSheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}
#pragma mark Callbacks
- (void) browseRepositorySheetDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)code contextInfo:(void *)info
{
[sheet orderOut:self];
if (code == NSOKButton) {
NSURL *url = [[sheet URLs] lastObject];
[self.repositoryURL setStringValue:[url path]];
}
}
- (void) browseDestinationSheetDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)code contextInfo:(void *)info
{
[sheet orderOut:self];
if (code == NSOKButton) {
NSURL *url = [[sheet URLs] lastObject];
[self.destinationPath setStringValue:[url path]];
}
}
- (void) messageSheetDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)code contextInfo:(void *)info
{
NSURL *documentURL = [NSURL fileURLWithPath:path];
NSError *error = nil;
id document = [[PBRepositoryDocumentController sharedDocumentController] openDocumentWithContentsOfURL:documentURL display:YES error:&error];
if (!document && error)
[self showErrorSheet:error];
else {
[self close];
NSString *containingPath = [path stringByDeletingLastPathComponent];
[PBGitDefaults setRecentCloneDestination:containingPath];
[self.destinationPath setStringValue:containingPath];
[self.repositoryURL setStringValue:@""];
}
}
- (void) errorSheetDidEnd:(NSOpenPanel *)sheet returnCode:(NSInteger)code contextInfo:(void *)info
{
[self close];
}
@end