Skip to content

Commit 97222e1

Browse files
author
Robert Kyriakis
committed
Install GitCleanUpMechanism when Error occurs.
Here Clean Up Git Remotes, when adding an Remote fails
1 parent a2f41af commit 97222e1

8 files changed

+105
-6
lines changed

ApplicationController.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//
88

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

1211
@class PBCloneRepositoryPanel;
1312

@@ -21,6 +20,7 @@
2120

2221
PBCloneRepositoryPanel *cloneRepositoryPanel;
2322

23+
NSDictionary *notificationUserInfo;
2424
}
2525

2626
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator;

ApplicationController.m

+42-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
#import "PBCloneRepositoryPanel.h"
1919
#import "Sparkle/SUUpdater.h"
2020
#import "AIURLAdditions.h"
21+
#import "PBGitRepository.h"
22+
23+
@interface ApplicationController ()
24+
- (void) cleanUpRemotesOnError;
25+
@end
26+
2127

2228
@implementation ApplicationController
2329

@@ -72,7 +78,10 @@ - (void)applicationWillFinishLaunching:(NSNotification*)notification
7278

7379
- (void)applicationDidFinishLaunching:(NSNotification*)notification
7480
{
75-
[[SUUpdater sharedUpdater] setSendsSystemProfile:YES];
81+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getArguments:) name:@"GitCommandSent" object:Nil];
82+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cleanGitAfterErrorMessage:) name:@"ErrorMessageDidEnd" object:Nil];
83+
84+
[[SUUpdater sharedUpdater] setSendsSystemProfile:YES];
7685
[[SUUpdater sharedUpdater] setDelegate:self];
7786

7887
if ([PBGitDefaults useAskPasswd]) {
@@ -392,6 +401,9 @@ - (void)applicationWillTerminate:(NSNotification *)aNotification
392401
[PBGitDefaults setPreviousDocumentPaths:paths];
393402
}
394403
}
404+
405+
[self removeObserver:self forKeyPath:@"ErrorMessageDidEnd"];
406+
[self removeObserver:self forKeyPath:@"GitCommandSent"];
395407
}
396408

397409
/**
@@ -443,5 +455,34 @@ - (IBAction)reportAProblem:(id)sender
443455
}
444456

445457

458+
#pragma mark - Observer methods
459+
460+
- (void)getArguments:(NSNotification*)notification
461+
{
462+
notificationUserInfo = [notification userInfo];
463+
}
464+
465+
466+
- (void)cleanGitAfterErrorMessage:(NSNotification*)notification
467+
{
468+
// When adding a remote occurs an error the remote
469+
// will be set on git and has to be removed to clean the remotes list
470+
[self cleanUpRemotesOnError];
471+
472+
473+
474+
}
475+
476+
#pragma mark - Extensions
477+
- (void) cleanUpRemotesOnError
478+
{
479+
// check, if arguments was to add a remote
480+
if ( ([(NSString*)[notificationUserInfo valueForKey:@"Arg0"] compare:@"remote"] == NSOrderedSame) &&
481+
([(NSString*)[notificationUserInfo valueForKey:@"Arg1"] compare:@"add"] == NSOrderedSame)
482+
)
483+
{
484+
[[notificationUserInfo valueForKey:@"Repository"] deleteRemoteWithName:[notificationUserInfo valueForKey:@"Arg3"]];
485+
}
486+
}
446487

447488
@end

PBCloneRepositoryPanel.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ - (void)showMessageSheet:(NSString *)messageText infoText:(NSString *)infoText
8787
[alert beginSheetModalForWindow:[self window]
8888
modalDelegate:self
8989
didEndSelector:@selector(messageSheetDidEnd:returnCode:contextInfo:)
90-
contextInfo:NULL];
90+
contextInfo:Nil];
9191
}
9292

9393

PBGitRepository.h

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ dispatch_queue_t PBGetWorkQueue();
8181
- (BOOL) createBranch:(NSString *)branchName atRefish:(id <PBGitRefish>)ref;
8282
- (BOOL) createTag:(NSString *)tagName message:(NSString *)message atRefish:(id <PBGitRefish>)commitSHA;
8383
- (BOOL) deleteRemote:(PBGitRef *)ref;
84+
- (BOOL) deleteRemoteWithName:(NSString *)remoteName;
8485
- (BOOL) deleteRemoteBranch:(PBGitRef *)ref;
8586
- (BOOL) deleteRef:(PBGitRef *)ref;
8687

PBGitRepository.m

+21
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,27 @@ - (BOOL) deleteRemote:(PBGitRef *)ref
11991199
return YES;
12001200
}
12011201

1202+
1203+
- (BOOL) deleteRemoteWithName:(NSString *)remoteName
1204+
{
1205+
if (!remoteName)
1206+
return NO;
1207+
1208+
int retValue = 1;
1209+
NSArray *arguments = [NSArray arrayWithObjects:@"remote", @"rm", remoteName, nil];
1210+
NSString * output = [self outputForArguments:arguments retValue:&retValue];
1211+
if (retValue) {
1212+
NSString *message = [NSString stringWithFormat:@"There was an error deleting the remote: %@\n\n", remoteName];
1213+
[self.windowController showErrorSheetTitle:@"Delete remote failed!" message:message arguments:arguments output:output];
1214+
return NO;
1215+
}
1216+
1217+
[self reloadRefs];
1218+
return YES;
1219+
}
1220+
1221+
1222+
12021223
- (BOOL) deleteRemoteBranch:(PBGitRef *)ref
12031224
{
12041225
BOOL retVal = YES;

PBGitWindowController.m

+13-1
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,23 @@ - (void)showMessageSheet:(NSString *)messageText infoText:(NSString *)infoText
143143
- (void)showErrorSheet:(NSError *)error
144144
{
145145
if ([[error domain] isEqualToString:PBGitRepositoryErrorDomain])
146+
{
146147
[PBGitXMessageSheet beginMessageSheetForWindow:[self window] withError:error];
148+
}
147149
else
148-
[[NSAlert alertWithError:error] beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:nil];
150+
{
151+
[[NSAlert alertWithError:error] beginSheetModalForWindow:[self window]
152+
modalDelegate:self
153+
didEndSelector:@selector(showErrorSheetAlertDidEnd:returnCode:contextInfo:)
154+
contextInfo:Nil];
155+
}
149156
}
150157

158+
- (void) showErrorSheetAlertDidEnd:(NSAlert *)alert returnCode:(NSInteger)code contextInfo:(void *)info
159+
{
160+
[[alert window] orderOut:nil];
161+
[[NSNotificationCenter defaultCenter] postNotificationName:@"ErrorMessageDidEnd" object:self];
162+
}
151163

152164
- (void)windowDidBecomeKey:(NSNotification *)notification
153165
{

PBGitXMessageSheet.m

+13-1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ - (IBAction)closeMessageSheet:(id)sender
5656
{
5757
[NSApp endSheet:[self window]];
5858
[[self window] orderOut:self];
59+
[[NSNotificationCenter defaultCenter] postNotificationName:@"ErrorMessageDidEnd" object:self];
5960
}
6061

6162

@@ -70,10 +71,21 @@ - (void)beginMessageSheetForWindow:(NSWindow *)parentWindow withMessageText:(NSS
7071
[self setInfoString:info];
7172
[self resizeWindow];
7273

73-
[NSApp beginSheet:[self window] modalForWindow:parentWindow modalDelegate:self didEndSelector:nil contextInfo:NULL];
74+
[NSApp beginSheet:[self window]
75+
modalForWindow:parentWindow
76+
modalDelegate:self
77+
didEndSelector:@selector(messageSheetForWindowDidEnd:returnCode:contextInfo:)
78+
contextInfo:Nil];
7479
}
7580

7681

82+
- (void)messageSheetForWindowDidEnd:(NSWindow*)window returnCode:(NSInteger)code contextInfo:(void *)info
83+
{
84+
[window orderOut:Nil];
85+
}
86+
87+
88+
7789
- (void)setInfoString:(NSString *)info
7890
{
7991
NSDictionary *attributes = [NSDictionary dictionaryWithObject:[NSFont labelFontOfSize:[NSFont smallSystemFontSize]]

PBRemoteProgressSheet.m

+13-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ @implementation PBRemoteProgressSheet
4949

5050

5151
static PBRemoteProgressSheet *sheet;
52+
static PBGitRepository *repository;
5253

5354
#pragma mark -
5455
#pragma mark PBRemoteProgressSheet
@@ -64,7 +65,9 @@ + (void) beginRemoteProgressSheetForArguments:(NSArray *)args title:(NSString *)
6465

6566
+ (void) beginRemoteProgressSheetForArguments:(NSArray *)args title:(NSString *)theTitle description:(NSString *)theDescription inRepository:(PBGitRepository *)repo
6667
{
67-
[PBRemoteProgressSheet beginRemoteProgressSheetForArguments:args title:theTitle description:theDescription inDir:[repo workingDirectory] windowController:repo.windowController];
68+
repository = repo;
69+
70+
[PBRemoteProgressSheet beginRemoteProgressSheetForArguments:args title:theTitle description:theDescription inDir:[repo workingDirectory] windowController:repo.windowController];
6871
}
6972

7073

@@ -96,6 +99,15 @@ - (void) beginRemoteProgressSheetForArguments:(NSArray *)args title:(NSString *)
9699

97100
gitTask = [PBEasyPipe taskForCommand:[PBGitBinary path] withArgs:arguments inDir:dir];
98101
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskCompleted:) name:NSTaskDidTerminateNotification object:gitTask];
102+
103+
NSMutableDictionary *argDict = [NSMutableDictionary new];
104+
[argDict setValue:repository forKey:[NSString stringWithFormat:@"Repository"]];
105+
106+
for (int i=0; i<[arguments count]; i++)
107+
{
108+
[argDict setValue:[arguments objectAtIndex:i] forKey:[NSString stringWithFormat:@"Arg%d",i]];
109+
}
110+
[[NSNotificationCenter defaultCenter] postNotificationName:@"GitCommandSent" object:self userInfo:argDict];
99111

100112
// having intermittent problem with long running git tasks not sending a termination notice, so periodically check whether the task is done
101113
taskTimer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(checkTask:) userInfo:nil repeats:YES];

0 commit comments

Comments
 (0)