Skip to content

Commit cae8e4b

Browse files
committed
Cleanup; fixes
I work badly on my own.
1 parent bc94512 commit cae8e4b

File tree

179 files changed

+767
-10081
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+767
-10081
lines changed

ApplicationController.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,15 @@
77
//
88

99
#import <Cocoa/Cocoa.h>
10-
#import "PBGitRepository.h"
1110

12-
@interface ApplicationController : NSObject
13-
{
11+
12+
@interface ApplicationController : NSObject {
1413
IBOutlet NSWindow *window;
1514
IBOutlet id firstResponder;
16-
NSPersistentStoreCoordinator *persistentStoreCoordinator;
17-
NSManagedObjectModel *managedObjectModel;
18-
NSManagedObjectContext *managedObjectContext;
1915
}
2016

21-
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator;
22-
- (NSManagedObjectModel *)managedObjectModel;
23-
- (NSManagedObjectContext *)managedObjectContext;
24-
25-
- (IBAction)openPreferencesWindow:(id)sender;
2617
- (IBAction)showAboutPanel:(id)sender;
27-
2818
- (IBAction)installCliTool:(id)sender;
29-
30-
- (IBAction)saveAction:sender;
3119
- (IBAction)showHelp:(id)sender;
3220
- (IBAction)reportAProblem:(id)sender;
33-
3421
@end

ApplicationController.m

Lines changed: 78 additions & 184 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
//
88

99
#import "ApplicationController.h"
10+
#import "PBGitRepository.h"
1011
#import "PBGitRevisionCell.h"
1112
#import "PBGitWindowController.h"
1213
#import "PBRepositoryDocumentController.h"
1314
#import "PBServicesController.h"
1415
#import "PBGitXProtocol.h"
15-
#import "PBPrefsWindowController.h"
1616
#import "PBNSURLPathUserDefaultsTransfomer.h"
1717
#import "PBGitDefaults.h"
1818

1919

2020

2121
@implementation ApplicationController
2222

23-
- (ApplicationController *)init {
23+
- (id)init {
2424
#ifdef DEBUG_BUILD
2525
[NSApp activateIgnoringOtherApps:YES];
2626
#endif
@@ -59,55 +59,23 @@ - (void)registerServices
5959
- (void)applicationDidFinishLaunching:(NSNotification*)notification
6060
{
6161
// Make sure Git's SSH password requests get forwarded to our little UI tool:
62-
setenv( "SSH_ASKPASS", [[[NSBundle mainBundle] pathForResource: @"gitx_askpasswd" ofType: @""] UTF8String], 1 );
62+
setenv( "SSH_ASKPASS", [[[NSBundle mainBundle] pathForResource:@"gitx_askpasswd" ofType:@""] UTF8String], 1 );
6363
setenv( "DISPLAY", "localhost:0", 1 );
6464

6565
[self registerServices];
6666

67-
BOOL hasOpenedDocuments = NO;
68-
NSArray *launchedDocuments = [[[PBRepositoryDocumentController sharedDocumentController] documents] copy];
69-
70-
// Only try to open a default document if there are no documents open already.
71-
// For example, the application might have been launched by double-clicking a .git repository,
72-
// or by dragging a folder to the app icon
73-
if ([launchedDocuments count])
74-
hasOpenedDocuments = YES;
75-
76-
// Try to find the current directory, to open that as a repository
77-
if ([PBGitDefaults openCurDirOnLaunch] && !hasOpenedDocuments) {
78-
NSString *curPath = [[[NSProcessInfo processInfo] environment] objectForKey:@"PWD"];
79-
NSURL *url = nil;
80-
if (curPath)
81-
url = [NSURL fileURLWithPath:curPath];
82-
// Try to open the found URL
83-
NSError *error = nil;
84-
if (url && [[PBRepositoryDocumentController sharedDocumentController] openDocumentWithContentsOfURL:url display:YES error:&error])
85-
hasOpenedDocuments = YES;
86-
}
67+
NSArray *launchedDocuments = [[PBRepositoryDocumentController sharedDocumentController] documents];
8768

8869
// to bring the launched documents to the front
8970
for (PBGitRepository *document in launchedDocuments)
9071
[document showWindows];
91-
92-
if (![[NSApplication sharedApplication] isActive])
93-
return;
94-
95-
// The current directory was not enabled or could not be opened (most likely it’s not a git repository).
96-
// show an open panel for the user to select a repository to view
97-
if ([PBGitDefaults showOpenPanelOnLaunch] && !hasOpenedDocuments)
98-
[[PBRepositoryDocumentController sharedDocumentController] openDocument:self];
9972
}
10073

10174
- (void)windowWillClose:sender
10275
{
10376
[firstResponder terminate:sender];
10477
}
10578

106-
- (IBAction)openPreferencesWindow:(id)sender
107-
{
108-
[[PBPrefsWindowController sharedPrefsWindowController] showWindow:nil];
109-
}
110-
11179
- (IBAction)showAboutPanel:(id)sender
11280
{
11381
NSString *gitversion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleGitVersion"];
@@ -122,144 +90,87 @@ - (IBAction)showAboutPanel:(id)sender
12290
[NSApp orderFrontStandardAboutPanelWithOptions:dict];
12391
}
12492

125-
- (IBAction)installCliTool:(id)sender;
126-
{
127-
BOOL success = NO;
128-
NSString* installationPath = @"/usr/local/bin/";
129-
NSString* installationName = @"gitx";
130-
NSString* toolPath = [[NSBundle mainBundle] pathForResource:@"gitx" ofType:@""];
131-
if (toolPath) {
132-
AuthorizationRef auth;
133-
if (AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth) == errAuthorizationSuccess) {
134-
char const* mkdir_arg[] = { "-p", [installationPath UTF8String], NULL};
135-
char const* mkdir = "/bin/mkdir";
136-
AuthorizationExecuteWithPrivileges(auth, mkdir, kAuthorizationFlagDefaults, (char**)mkdir_arg, NULL);
137-
char const* arguments[] = { "-f", "-s", [toolPath UTF8String], [[installationPath stringByAppendingString: installationName] UTF8String], NULL };
138-
char const* helperTool = "/bin/ln";
139-
if (AuthorizationExecuteWithPrivileges(auth, helperTool, kAuthorizationFlagDefaults, (char**)arguments, NULL) == errAuthorizationSuccess) {
140-
int status;
141-
int pid = wait(&status);
142-
if (pid != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0)
143-
success = true;
144-
else
145-
errno = WEXITSTATUS(status);
146-
}
147-
148-
AuthorizationFree(auth, kAuthorizationFlagDefaults);
149-
}
150-
}
151-
152-
if (success) {
153-
[[NSAlert alertWithMessageText:@"Installation Complete"
154-
defaultButton:nil
155-
alternateButton:nil
156-
otherButton:nil
157-
informativeTextWithFormat:@"The gitx tool has been installed to %@", installationPath] runModal];
158-
} else {
159-
[[NSAlert alertWithMessageText:@"Installation Failed"
160-
defaultButton:nil
161-
alternateButton:nil
162-
otherButton:nil
163-
informativeTextWithFormat:@"Installation to %@ failed", installationPath] runModal];
164-
}
165-
}
166-
167-
- (NSString *)applicationSupportFolder {
168-
169-
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
170-
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
171-
return [basePath stringByAppendingPathComponent:@"GitTest"];
172-
}
173-
174-
- (NSManagedObjectModel *)managedObjectModel {
175-
return managedObjectModel ?: (managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]);
176-
}
177-
178-
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator {
179-
if (!persistentStoreCoordinator) {
180-
NSFileManager *fileManager;
181-
NSString *applicationSupportFolder = nil;
182-
NSURL *url;
183-
NSError *error;
184-
185-
fileManager = [NSFileManager defaultManager];
186-
applicationSupportFolder = [self applicationSupportFolder];
187-
if ( ![fileManager fileExistsAtPath:applicationSupportFolder isDirectory:NULL] ) {
188-
[fileManager createDirectoryAtPath:applicationSupportFolder withIntermediateDirectories:YES attributes:nil error:nil];
189-
}
190-
191-
url = [NSURL fileURLWithPath: [applicationSupportFolder stringByAppendingPathComponent: @"GitTest.xml"]];
192-
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
193-
if (![persistentStoreCoordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]){
194-
[[NSApplication sharedApplication] presentError:error];
195-
}
196-
}
197-
198-
return persistentStoreCoordinator;
199-
}
200-
201-
- (NSManagedObjectContext *) managedObjectContext {
202-
if (!managedObjectContext) {
203-
managedObjectContext = [NSManagedObjectContext new];
204-
managedObjectContext.persistentStoreCoordinator = self.persistentStoreCoordinator;
93+
- (BOOL)needsRoot {
94+
NSFileManager *fm = [NSFileManager defaultManager];
95+
96+
if ([fm fileExistsAtPath:@"/usr/local/bin/gitx"] && ![fm isWritableFileAtPath:@"/usr/local/bin/gitx"])
97+
return YES;
98+
99+
if ([fm isWritableFileAtPath:@"/usr/local/bin"]) {
100+
BOOL isdir;
101+
if (![fm fileExistsAtPath:@"/usr/local/bin" isDirectory:&isdir] && !isdir)
102+
@throw @"/usr/local/bin is not a directory.";
103+
return NO;
205104
}
206-
return managedObjectContext;
207-
}
208-
209-
- (NSUndoManager *)windowWillReturnUndoManager:(NSWindow *)window {
210-
return self.managedObjectContext.undoManager;
211-
}
212-
213-
- (IBAction) saveAction:(id)sender {
214-
id error = nil;
215-
if (![[self managedObjectContext] save:&error]) {
216-
[NSApp presentError:error];
105+
if ([fm isWritableFileAtPath:@"/usr/local"]) {
106+
BOOL isdir;
107+
if (![fm fileExistsAtPath:@"/usr/local" isDirectory:&isdir] && !isdir)
108+
@throw @"/usr/local is not a directory.";
109+
return NO;
217110
}
111+
112+
return YES;
218113
}
219114

220-
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
221-
NSError *error;
222-
int reply = NSTerminateNow;
115+
- (IBAction)installCliTool:(id)sender {
116+
id from = [[NSBundle mainBundle] pathForResource:@"gitx" ofType:@""];
117+
id to = @"/usr/local/bin/gitx";
223118

224-
if (managedObjectContext != nil) {
225-
if ([managedObjectContext commitEditing]) {
226-
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
227-
228-
// This error handling simply presents error information in a panel with an
229-
// "Ok" button, which does not include any attempt at error recovery (meaning,
230-
// attempting to fix the error.) As a result, this implementation will
231-
// present the information to the user and then follow up with a panel asking
232-
// if the user wishes to "Quit Anyway", without saving the changes.
233-
234-
// Typically, this process should be altered to include application-specific
235-
// recovery steps.
236-
237-
BOOL errorResult = [[NSApplication sharedApplication] presentError:error];
238-
239-
if (errorResult == YES) {
240-
reply = NSTerminateCancel;
241-
}
242-
243-
else {
244-
245-
int alertReturn = NSRunAlertPanel(nil, @"Could not save changes while quitting. Quit anyway?" , @"Quit anyway", @"Cancel", nil);
246-
if (alertReturn == NSAlertAlternateReturn) {
247-
reply = NSTerminateCancel;
248-
}
249-
}
119+
@try {
120+
if (self.needsRoot) {
121+
AuthorizationRef auth;
122+
if (AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &auth) == errAuthorizationSuccess)
123+
{
124+
char const* mkdir_arg[] = { "-p", [[to stringByDeletingLastPathComponent] UTF8String], NULL};
125+
AuthorizationExecuteWithPrivileges(auth, "/bin/mkdir", kAuthorizationFlagDefaults, (char**)mkdir_arg, NULL);
126+
127+
char const* arguments[] = { "-f", "-s", [from UTF8String], [to UTF8String], NULL };
128+
if (AuthorizationExecuteWithPrivileges(auth, "/bin/ln", kAuthorizationFlagDefaults, (char**)arguments, NULL) == errAuthorizationSuccess) {
129+
int status;
130+
int pid = wait(&status);
131+
if (!(pid != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0))
132+
@throw @"Could not create gitx symlink :(";
133+
} else
134+
@throw @"You canceled authorisation.";
135+
136+
AuthorizationFree(auth, kAuthorizationFlagDefaults);
250137
}
251-
}
252-
253-
else {
254-
reply = NSTerminateCancel;
138+
} else {
139+
//FIXME this is a hard link, root-route is symlink, should be same, hard links are better IMO
140+
141+
[[NSFileManager defaultManager] removeItemAtPath:to error:nil];
142+
143+
id error = nil;
144+
[[NSFileManager defaultManager] linkItemAtPath:from toPath:to error:&error];
145+
if (error)
146+
@throw error;
255147
}
148+
149+
[[NSAlert alertWithMessageText:@"Command Line Tool Installed Successfully"
150+
defaultButton:nil
151+
alternateButton:nil
152+
otherButton:nil
153+
informativeTextWithFormat:@"You now have: %@.", to] runModal];
154+
}
155+
@catch (id errmsg) {
156+
if ([errmsg isKindOfClass:[NSError class]])
157+
errmsg = [errmsg localizedFailureReason];
158+
159+
[[NSAlert alertWithMessageText:@"Could Not Install Command Line Tool"
160+
defaultButton:nil
161+
alternateButton:nil
162+
otherButton:nil
163+
informativeTextWithFormat:@"%@", errmsg] runModal];
256164
}
257-
258-
return reply;
259165
}
260166

261-
- (void)applicationWillTerminate:(NSNotification *)aNotification
262-
{
167+
- (NSString *)applicationSupportFolder {
168+
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
169+
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
170+
return [basePath stringByAppendingPathComponent:@"GitTest"];
171+
}
172+
173+
- (void)applicationWillTerminate:(NSNotification *)aNotification {
263174
[PBGitDefaults removePreviousDocumentPaths];
264175

265176
if ([PBGitDefaults openPreviousDocumentsOnLaunch]) {
@@ -274,29 +185,12 @@ - (void)applicationWillTerminate:(NSNotification *)aNotification
274185
}
275186
}
276187

277-
/**
278-
Implementation of dealloc, to release the retained variables.
279-
*/
280-
281-
- (void) dealloc {
282-
283-
managedObjectContext = nil;
284-
persistentStoreCoordinator = nil;
285-
managedObjectModel = nil;
286-
}
287-
288-
#pragma mark Help menu
289-
290-
- (IBAction)showHelp:(id)sender
291-
{
188+
- (IBAction)showHelp:(id)sender {
292189
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://gitx.frim.nl/user_manual.html"]];
293190
}
294191

295-
- (IBAction)reportAProblem:(id)sender
296-
{
297-
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://gitx.lighthouseapp.com/tickets"]];
192+
- (IBAction)reportAProblem:(id)sender {
193+
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"mailto:[email protected]"]];
298194
}
299195

300-
301-
302196
@end

0 commit comments

Comments
 (0)