Skip to content

Commit fec9b6c

Browse files
author
Robert Kyriakis
committed
On failure at CreateTag, CreateBrach and RenameRef it is show detailed what the error was
1 parent 0c05990 commit fec9b6c

File tree

6 files changed

+247
-44
lines changed

6 files changed

+247
-44
lines changed

PBCreateBranchSheet.m

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,37 @@ - (IBAction) createBranch:(id)sender
8686
[self.errorMessageField setHidden:NO];
8787
return;
8888
}
89-
90-
if ([self.repository refExists:ref checkOnRemotesWithoutBranches:YES]) {
91-
[self.errorMessageField setStringValue:@"Refname already exists local as tag or branch or remote as tag!"];
92-
[self.errorMessageField setHidden:NO];
93-
return;
94-
}
95-
89+
90+
NSString *refExistsReturnMessage;
91+
if([self.repository refExists:ref checkOnRemotesWithoutBranches:YES resultMessage:&refExistsReturnMessage])
92+
{
93+
NSError *error = [NSError errorWithDomain:PBGitRepositoryErrorDomain
94+
code:0
95+
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
96+
refExistsReturnMessage, NSLocalizedDescriptionKey,
97+
@"Select other branchname.", NSLocalizedRecoverySuggestionErrorKey,
98+
nil]
99+
];
100+
[[NSAlert alertWithError:error]runModal];
101+
return;
102+
}
103+
else
104+
{
105+
if (refExistsReturnMessage != @"")
106+
{
107+
int returnButton = [[NSAlert alertWithMessageText:refExistsReturnMessage
108+
defaultButton:@"Yes"
109+
alternateButton:@"No"
110+
otherButton:nil
111+
informativeTextWithFormat:@"Still want to create the %@ %@?",[ref refishType],[ref shortName]] runModal];
112+
113+
if (returnButton == NSAlertAlternateReturn)
114+
{
115+
return;
116+
}
117+
}
118+
}
119+
96120
[self closeCreateBranchSheet:self];
97121

98122
[self.repository createBranch:name atRefish:self.startRefish];

PBCreateTagSheet.m

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,36 @@ - (IBAction) createTag:(id)sender
6868
return;
6969
}
7070

71-
if ([self.repository refExists:ref checkOnRemotesWithoutBranches:YES]) {
72-
[self.errorMessageField setStringValue:@"Refname already exists local as tag or branch or remote as tag!"];
73-
[self.errorMessageField setHidden:NO];
74-
return;
75-
}
71+
NSString *refExistsReturnMessage;
72+
if([self.repository refExists:ref checkOnRemotesWithoutBranches:YES resultMessage:&refExistsReturnMessage])
73+
{
74+
NSError *error = [NSError errorWithDomain:PBGitRepositoryErrorDomain
75+
code:0
76+
userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
77+
refExistsReturnMessage, NSLocalizedDescriptionKey,
78+
@"Select other tagname.", NSLocalizedRecoverySuggestionErrorKey,
79+
nil]
80+
];
81+
[[NSAlert alertWithError:error]runModal];
82+
return;
83+
}
84+
else
85+
{
86+
if (refExistsReturnMessage != @"")
87+
{
88+
int returnButton = [[NSAlert alertWithMessageText:refExistsReturnMessage
89+
defaultButton:@"Yes"
90+
alternateButton:@"No"
91+
otherButton:nil
92+
informativeTextWithFormat:@"Still want to create the %@ %@?",[ref refishType],[ref shortName]] runModal];
93+
94+
if (returnButton == NSAlertAlternateReturn)
95+
{
96+
return;
97+
}
98+
}
99+
}
100+
76101
[self closeCreateTagSheet:sender];
77102

78103
NSString *message = [self.tagMessageText string];

PBGitRepository.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ dispatch_queue_t PBGetWorkQueue();
8282
- (BOOL) createBranch:(NSString *)branchName atRefish:(id <PBGitRefish>)ref;
8383
- (BOOL) createTag:(NSString *)tagName message:(NSString *)message atRefish:(id <PBGitRefish>)commitSHA;
8484
- (BOOL) deleteRemote:(PBGitRef *)ref;
85-
- (BOOL) isRemoteConnected:(PBGitRef *)ref;
85+
- (BOOL) isRemoteConnected:(NSString*)remoteName;
8686
- (BOOL) deleteRemoteBranch:(PBGitRef *)ref;
8787
- (BOOL) deleteRemoteTag:(PBGitRef *)ref;
8888
- (BOOL) deleteRef:(PBGitRef *)ref;
@@ -128,9 +128,9 @@ dispatch_queue_t PBGetWorkQueue();
128128
- (BOOL)isSHAOnHeadBranch:(NSString *)testSHA;
129129
- (BOOL)isRefOnHeadBranch:(PBGitRef *)testRef;
130130
- (BOOL)checkRefFormat:(NSString *)refName;
131-
- (BOOL)refExists:(PBGitRef *)ref checkOnRemotesWithoutBranches:(BOOL)remoteCheck;
132-
- (BOOL)refExistsOnRemote:(PBGitRef*)ref remoteName:(NSString*)remote;
133-
- (BOOL)refExistsOnAnyRemote:(PBGitRef*)ref;
131+
- (BOOL)refExists:(PBGitRef *)ref checkOnRemotesWithoutBranches:(BOOL)remoteCheck resultMessage:(NSString**)result;
132+
- (BOOL)refExistsOnRemote:(PBGitRef*)ref remoteName:(NSString*)remote resultMessage:(NSString**)result;
133+
- (BOOL)refExistsOnAnyRemote:(PBGitRef*)ref resultMessage:(NSString**)result;
134134
- (BOOL)tagExistsOnRemote:(PBGitRef *)ref remoteName:(NSString *)remote;
135135
- (PBGitRef *)refForName:(NSString *)name;
136136

PBGitRepository.m

Lines changed: 151 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,14 @@ - (BOOL) checkRefFormat:(NSString *)refName
573573
return YES;
574574
}
575575

576-
- (BOOL) refExists:(PBGitRef *)ref checkOnRemotesWithoutBranches:(BOOL)remoteCheck
576+
- (BOOL)refExists:(PBGitRef *)ref checkOnRemotesWithoutBranches:(BOOL)remoteCheck resultMessage:(NSString**)result
577577
{
578578
if (!ref)
579579
{
580+
if (result)
581+
{
582+
*result = @"Ref is Nil, can't progress check existence!";
583+
}
580584
return NO;
581585
}
582586

@@ -607,44 +611,93 @@ - (BOOL) refExists:(PBGitRef *)ref checkOnRemotesWithoutBranches:(BOOL)remoteChe
607611
arguments = [NSArray arrayWithObjects:@"for-each-ref", [NSString stringWithFormat:@"%@%@",kGitXBranchRefPrefix,refShortName], nil];
608612
output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
609613
if (![output isEqualToString:@""])
614+
{
615+
if (result)
616+
{
617+
*result = [NSString stringWithFormat:@"%@ exists already local as branch!",refShortName];
618+
}
610619
return YES;
620+
}
611621

612622
// Check local refs/tags/ for ref
613623
arguments = [NSArray arrayWithObjects:@"for-each-ref", [NSString stringWithFormat:@"%@%@",kGitXTagRefPrefix,refShortName], nil];
614624
output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
615625
if (![output isEqualToString:@""])
626+
{
627+
if (result)
628+
{
629+
*result = [NSString stringWithFormat:@"%@ exists already local as tag!",refShortName];
630+
}
616631
return YES;
632+
}
617633

618634
// Check if any remote exists with refShortName
619635
NSArray *repoRemotes = [self remotes];
620636
if ([repoRemotes containsObject:refShortName])
637+
{
638+
if (result)
639+
{
640+
*result = [NSString stringWithFormat:@"%@ exists already local as remotename!",refShortName];
641+
}
621642
return YES;
622-
623-
// Check Branches and Tags on any Remotes
643+
}
644+
645+
NSMutableString *completeResults = [NSMutableString string];
646+
647+
// Check Tags on any Remotes
624648
if (repoRemotes && remoteCheck)
625649
{
650+
626651
for (int i=0; i<[repoRemotes count]; i++)
627652
{
628653
// Check Remote connection
629-
PBGitRef *remoteRef = [PBGitRef refFromString:[NSString stringWithFormat:@"%@%@",kGitXRemoteRefPrefix,[repoRemotes objectAtIndex:i]]];
630-
if ([self isRemoteConnected:remoteRef])
654+
if ([self isRemoteConnected:[repoRemotes objectAtIndex:i]])
631655
{
632656
// Check remote refs/tags/ for ref
633657
arguments = [NSArray arrayWithObjects:@"ls-remote", @"-t",[repoRemotes objectAtIndex:i] ,refShortName, nil];
634658
output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
635659
if (![output isEqualToString:@""])
660+
{
661+
[completeResults appendFormat:@"%@ exists already as tag on remote %@!",refShortName,[repoRemotes objectAtIndex:i]];
662+
if (result)
663+
{
664+
*result = completeResults;
665+
}
636666
return YES;
667+
}
668+
}
669+
else
670+
{
671+
[completeResults appendFormat:@"Remote %@ is actually not connected, can't check for existing refname there!\n",[repoRemotes objectAtIndex:i]];
637672
}
638673
}
639674
}
640675

676+
if (result)
677+
{
678+
*result = completeResults;
679+
}
641680
return NO;
642681
}
643682

644-
- (BOOL)refExistsOnRemote:(PBGitRef *)ref remoteName:(NSString *)remote
683+
684+
- (BOOL)refExistsOnRemote:(PBGitRef *)ref remoteName:(NSString *)remote resultMessage:(NSString**)result
645685
{
646-
if ((!ref) || (![self hasRemotes]))
686+
if (!remote)
687+
{
688+
if (result)
689+
{
690+
*result = @"Remotename is Nil, can't progress check existence!";
691+
}
692+
return NO;
693+
}
694+
695+
if (!ref)
647696
{
697+
if (result)
698+
{
699+
*result = [NSString stringWithFormat:@"Ref is Nil, can't progress check existence on remote %@!",remote];
700+
}
648701
return NO;
649702
}
650703

@@ -663,36 +716,112 @@ - (BOOL)refExistsOnRemote:(PBGitRef *)ref remoteName:(NSString *)remote
663716
refShortName = [ref remoteBranchName];
664717
}
665718

719+
if (![self hasRemotes])
720+
{
721+
if (result)
722+
{
723+
*result = [NSString stringWithFormat:@"Repository has no remotes, can't progress check existence from %@ %@ on remote %@!",[ref refishType],refShortName,remote];
724+
}
725+
return NO;
726+
}
727+
728+
if (![self isRemoteConnected:remote])
729+
{
730+
if (result)
731+
{
732+
*result = [NSString stringWithFormat:@"Remote %@ is actually not connected, can't check for existing refname there!",remote];
733+
}
734+
return NO;
735+
}
736+
666737
int retValue = 1;
667-
// Check remote refs/tags/ and refs/heads for ref
668-
NSArray *arguments = [NSArray arrayWithObjects:@"ls-remote", @"-t", @"-h", remote, refShortName, nil];
738+
// Check remote refs/tags/ for ref
739+
NSArray *arguments = [NSArray arrayWithObjects:@"ls-remote", @"-t", remote, refShortName, nil];
669740
NSString *output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
670741
if (![output isEqualToString:@""])
742+
{
743+
if (result)
744+
{
745+
*result = [NSString stringWithFormat:@"%@ exists as tag on remote %@!",refShortName,remote];
746+
}
671747
return YES;
748+
}
672749

750+
// Check remote refs/tags/ for ref
751+
arguments = [NSArray arrayWithObjects:@"ls-remote", @"-h", remote, refShortName, nil];
752+
output = [self outputInWorkdirForArguments:arguments retValue:&retValue];
753+
if (![output isEqualToString:@""])
754+
{
755+
if (result)
756+
{
757+
*result = [NSString stringWithFormat:@"%@ exists as branch on remote %@!",refShortName,remote];
758+
}
759+
return YES;
760+
}
761+
673762
return NO;
674763
}
675764

676-
- (BOOL)refExistsOnAnyRemote:(PBGitRef*)ref
765+
766+
- (BOOL)refExistsOnAnyRemote:(PBGitRef*)ref resultMessage:(NSString**)result
677767
{
678768
if (!ref)
679769
{
770+
if (result)
771+
{
772+
*result = @"Ref is Nil, can't progress check existence on any remotes!";
773+
}
680774
return NO;
681775
}
682-
776+
777+
NSMutableString *completeResult = [NSMutableString string];
778+
683779
if ([self hasRemotes])
684780
{
685781
NSArray *repoRemotes = [self remotes];
782+
NSString *oneResult;
686783
for (int i=0; i<[repoRemotes count]; i++)
687784
{
688-
if ([self refExistsOnRemote:ref remoteName:[repoRemotes objectAtIndex:i]])
785+
if ([self refExistsOnRemote:ref remoteName:[repoRemotes objectAtIndex:i] resultMessage:&oneResult])
786+
{
787+
if (oneResult)
788+
{
789+
[completeResult appendString:oneResult];
790+
}
791+
792+
if (result)
793+
{
794+
*result = completeResult;
795+
}
796+
689797
return YES;
798+
}
799+
else
800+
{
801+
if (oneResult)
802+
{
803+
[completeResult appendString:oneResult];
804+
}
805+
}
690806
}
691807
}
692-
808+
else
809+
{
810+
if (result)
811+
{
812+
*result = [NSString stringWithFormat:@"Repository has no remotes, can't progress check existence from %@ on any remotes!",[ref shortName]];
813+
}
814+
return NO;
815+
}
816+
817+
if (result)
818+
{
819+
*result = completeResult;
820+
}
693821
return NO;
694822
}
695823

824+
696825
- (BOOL)tagExistsOnRemote:(PBGitRef *)ref remoteName:(NSString *)remote
697826
{
698827
if ((!ref) || (![self hasRemotes]) || (![ref isTag]))
@@ -817,7 +946,7 @@ - (PBGitRef *) remoteRefForBranch:(PBGitRef *)branch error:(NSError **)error
817946
if (remoteName && ([remoteName isKindOfClass:[NSString class]] && (![remoteName isEqualToString:@""]))) {
818947
PBGitRef *remoteRef = [PBGitRef refFromString:[kGitXRemoteRefPrefix stringByAppendingString:remoteName]];
819948
// check that the remote is a valid ref and exists
820-
if ([self checkRefFormat:[remoteRef ref]] && [self refExists:remoteRef checkOnRemotesWithoutBranches:NO])
949+
if ([self checkRefFormat:[remoteRef ref]] && [self refExists:remoteRef checkOnRemotesWithoutBranches:NO resultMessage:Nil])
821950
return remoteRef;
822951
}
823952
}
@@ -894,17 +1023,17 @@ - (void) changeRemote:(PBGitRef *)ref toURL:(NSURL*)newUrl;
8941023
}
8951024

8961025

897-
- (BOOL)isRemoteConnected:(PBGitRef *)ref
1026+
- (BOOL)isRemoteConnected:(NSString*)remoteName
8981027
{
899-
if ((![ref isRemote]) || (![[self remotes] containsObject:[ref remoteName]]))
1028+
if (!remoteName)
9001029
return NO;
9011030

9021031
// Send a command to the remote and check the ExitCode
9031032
int gitRetValue = 1;
904-
NSArray *arguments = [NSArray arrayWithObjects:@"ls-remote", [ref remoteName], nil];
905-
NSLog(@"Start testing connection to remote %@",[ref remoteName]);
1033+
NSArray *arguments = [NSArray arrayWithObjects:@"ls-remote", remoteName, nil];
1034+
NSLog(@"Start testing connection to remote %@",remoteName);
9061035
[self outputInWorkdirForArguments:arguments retValue:&gitRetValue];
907-
NSLog(@"Stop testing connection to remote %@",[ref remoteName]);
1036+
NSLog(@"Stop testing connection to remote %@",remoteName);
9081037
if (gitRetValue)
9091038
{
9101039
return NO;
@@ -1385,10 +1514,10 @@ - (BOOL) deleteRemoteTag:(PBGitRef *)ref
13851514

13861515
for (int i=0; i<[remotes count]; i++)
13871516
{
1388-
if ([self refExistsOnRemote:ref remoteName:[remotes objectAtIndex:i]])
1517+
if ([self refExistsOnRemote:ref remoteName:[remotes objectAtIndex:i] resultMessage:Nil])
13891518
{
13901519
BOOL remoteConnected = YES;
1391-
if (![self isRemoteConnected:[PBGitRef refFromString:[NSString stringWithFormat:@"%@%@",kGitXRemoteRefPrefix,[remotes objectAtIndex:i]]]])
1520+
if (![self isRemoteConnected:[remotes objectAtIndex:i]])
13921521
{
13931522
NSString *info = [NSString stringWithFormat:@"Remote %@ is not conneted!",[remotes objectAtIndex:i]];
13941523
NSError *error = [NSError errorWithDomain:PBGitRepositoryErrorDomain
@@ -1459,7 +1588,7 @@ - (BOOL) deleteRef:(PBGitRef *)ref
14591588
}
14601589
else if ([ref refishType] == kGitXTagType)
14611590
{
1462-
if ([self refExistsOnAnyRemote:ref])
1591+
if ([self refExistsOnAnyRemote:ref resultMessage:Nil])
14631592
{
14641593
[self deleteRemoteTag:ref];
14651594
}

0 commit comments

Comments
 (0)