Skip to content

Commit

Permalink
Fix exit status for canceling empty message if restoring is disabled
Browse files Browse the repository at this point in the history
If restoring incomplete canceled messages setting is disabled, we should exit(0) if the initial edited content is empty. This results in a cleaner exit and is otherwise not a serious bug.
  • Loading branch information
zorgiepoo committed Oct 4, 2020
1 parent edfd9ae commit aca42bd
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions Komet/ZGEditorWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -950,45 +950,48 @@ - (void)exitWithSuccess:(BOOL)success __attribute__((noreturn))
}
else
{
// If we initially had no content and wrote an incomplete commit message,
// then save the commit message in case we may want to resume from it later
if (_initiallyContainedEmptyContent && ZGReadDefaultResumeIncompleteSession())
if (_initiallyContainedEmptyContent)
{
NSFileManager *fileManager = [[NSFileManager alloc] init];

NSString *plainString = _textView.textStorage.string;
NSUInteger commitLength = [self commitTextLengthFromPlainText:plainString commentLength:_commentSectionLength];

NSString *content = [plainString substringToIndex:commitLength];
NSString *trimmedContent = [content stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
if (trimmedContent.length > 0)
// If we initially had no content and wrote an incomplete commit message,
// then save the commit message in case we may want to resume from it later
if (ZGReadDefaultResumeIncompleteSession())
{
NSError *applicationSupportQueryError = nil;
NSURL *applicationSupportURL = [fileManager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&applicationSupportQueryError];
if (applicationSupportURL == nil)
{
NSLog(@"Failed to find application support directory: %@", applicationSupportQueryError);
}
else
NSFileManager *fileManager = [[NSFileManager alloc] init];

NSString *plainString = _textView.textStorage.string;
NSUInteger commitLength = [self commitTextLengthFromPlainText:plainString commentLength:_commentSectionLength];

NSString *content = [plainString substringToIndex:commitLength];
NSString *trimmedContent = [content stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
if (trimmedContent.length > 0)
{
NSURL *supportDirectory = [applicationSupportURL URLByAppendingPathComponent:APP_SUPPORT_DIRECTORY_NAME];
NSString *projectName = [self projectName];

NSError *createSupportDirectoryError = nil;
if (![fileManager createDirectoryAtURL:supportDirectory withIntermediateDirectories:YES attributes:nil error:&createSupportDirectoryError])
NSError *applicationSupportQueryError = nil;
NSURL *applicationSupportURL = [fileManager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:&applicationSupportQueryError];
if (applicationSupportURL == nil)
{
NSLog(@"Failed to create application support directory: %@", createSupportDirectoryError);
NSLog(@"Failed to find application support directory: %@", applicationSupportQueryError);
}
else
{
NSURL *lastCommitURL = [supportDirectory URLByAppendingPathComponent:projectName];
NSURL *supportDirectory = [applicationSupportURL URLByAppendingPathComponent:APP_SUPPORT_DIRECTORY_NAME];
NSString *projectName = [self projectName];

if (lastCommitURL != nil)
NSError *createSupportDirectoryError = nil;
if (![fileManager createDirectoryAtURL:supportDirectory withIntermediateDirectories:YES attributes:nil error:&createSupportDirectoryError])
{
NSError *writeError = nil;
if (![trimmedContent writeToURL:lastCommitURL atomically:YES encoding:NSUTF8StringEncoding error:&writeError])
NSLog(@"Failed to create application support directory: %@", createSupportDirectoryError);
}
else
{
NSURL *lastCommitURL = [supportDirectory URLByAppendingPathComponent:projectName];

if (lastCommitURL != nil)
{
NSLog(@"Failed to write last commit message with error: %@", writeError);
NSError *writeError = nil;
if (![trimmedContent writeToURL:lastCommitURL atomically:YES encoding:NSUTF8StringEncoding error:&writeError])
{
NSLog(@"Failed to write last commit message with error: %@", writeError);
}
}
}
}
Expand Down

0 comments on commit aca42bd

Please sign in to comment.