From 472d36c7f970d9f3dbda3b9d7c8a7d51546bda56 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Thu, 9 Oct 2008 21:03:49 +0200 Subject: [PATCH] CommitView: Add context menu to revert changes --- GitX.xcodeproj/project.pbxproj | 6 +++ PBChangedFile.m | 18 +++++++++ PBFileChangesTableView.h | 17 +++++++++ PBFileChangesTableView.m | 24 ++++++++++++ PBGitCommitController.h | 2 + PBGitCommitController.m | 70 ++++++++++++++++++++++------------ PBGitCommitView.xib | 18 ++++++++- 7 files changed, 128 insertions(+), 27 deletions(-) create mode 100644 PBFileChangesTableView.h create mode 100644 PBFileChangesTableView.m diff --git a/GitX.xcodeproj/project.pbxproj b/GitX.xcodeproj/project.pbxproj index df281a7..edaa979 100644 --- a/GitX.xcodeproj/project.pbxproj +++ b/GitX.xcodeproj/project.pbxproj @@ -52,6 +52,7 @@ F58A8F280E043698007E3FC0 /* commits.css in Resources */ = {isa = PBXBuildFile; fileRef = F58A8F270E043698007E3FC0 /* commits.css */; }; F59116E60E843BB50072CCB1 /* PBGitCommitView.xib in Resources */ = {isa = PBXBuildFile; fileRef = F59116E50E843BB50072CCB1 /* PBGitCommitView.xib */; }; F59116E90E843BCB0072CCB1 /* PBGitCommitController.m in Sources */ = {isa = PBXBuildFile; fileRef = F59116E80E843BCB0072CCB1 /* PBGitCommitController.m */; }; + F593DF780E9E636C003A8559 /* PBFileChangesTableView.m in Sources */ = {isa = PBXBuildFile; fileRef = F593DF770E9E636C003A8559 /* PBFileChangesTableView.m */; }; F5945E170E02B0C200706420 /* PBGitRepository.m in Sources */ = {isa = PBXBuildFile; fileRef = F5945E160E02B0C200706420 /* PBGitRepository.m */; }; F5AD56790E79B78100EDAAFE /* PBCommitList.m in Sources */ = {isa = PBXBuildFile; fileRef = F5AD56780E79B78100EDAAFE /* PBCommitList.m */; }; F5B721C40E05CF7E00AF29DC /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = F5B721C20E05CF7E00AF29DC /* MainMenu.xib */; }; @@ -164,6 +165,8 @@ F59116E50E843BB50072CCB1 /* PBGitCommitView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PBGitCommitView.xib; sourceTree = ""; }; F59116E70E843BCB0072CCB1 /* PBGitCommitController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitCommitController.h; sourceTree = ""; }; F59116E80E843BCB0072CCB1 /* PBGitCommitController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitCommitController.m; sourceTree = ""; }; + F593DF760E9E636C003A8559 /* PBFileChangesTableView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBFileChangesTableView.h; sourceTree = ""; }; + F593DF770E9E636C003A8559 /* PBFileChangesTableView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBFileChangesTableView.m; sourceTree = ""; }; F5945E150E02B0C200706420 /* PBGitRepository.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBGitRepository.h; sourceTree = ""; }; F5945E160E02B0C200706420 /* PBGitRepository.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PBGitRepository.m; sourceTree = ""; }; F5AD56770E79B78100EDAAFE /* PBCommitList.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PBCommitList.h; sourceTree = ""; }; @@ -438,6 +441,8 @@ children = ( F5E927F60E883E7200056E75 /* PBChangedFile.h */, F5E927F70E883E7200056E75 /* PBChangedFile.m */, + F593DF760E9E636C003A8559 /* PBFileChangesTableView.h */, + F593DF770E9E636C003A8559 /* PBFileChangesTableView.m */, ); name = Commit; sourceTree = ""; @@ -590,6 +595,7 @@ F5140DC90E8A8EB20091E9F3 /* RoundedRectangle.m in Sources */, F56244090E9684B0002B6C44 /* PBUnsortableTableHeader.m in Sources */, F53C4DF70E97FC630022AD59 /* PBGitBinary.m in Sources */, + F593DF780E9E636C003A8559 /* PBFileChangesTableView.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/PBChangedFile.m b/PBChangedFile.m index 56434ed..500954d 100644 --- a/PBChangedFile.m +++ b/PBChangedFile.m @@ -76,6 +76,24 @@ self.hasUnstagedChanges = YES; } +- (void) forceRevertChanges +{ + [repository outputInWorkdirForArguments:[NSArray arrayWithObjects:@"checkout", @"--", path, nil]]; + self.hasUnstagedChanges = NO; +} + +- (void) revertChanges +{ + int ret = [[NSAlert alertWithMessageText:@"Revert changes" + defaultButton:nil + alternateButton:@"Cancel" + otherButton:nil + informativeTextWithFormat:@"Are you sure you wish to revert the changes in '%@'?\n\n You cannot undo this operation.", path] runModal]; + + if (ret == NSAlertDefaultReturn) + [self forceRevertChanges]; +} + + (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector { return NO; diff --git a/PBFileChangesTableView.h b/PBFileChangesTableView.h new file mode 100644 index 0000000..9a0d035 --- /dev/null +++ b/PBFileChangesTableView.h @@ -0,0 +1,17 @@ +// +// PBFileChangesTableView.h +// GitX +// +// Created by Pieter de Bie on 09-10-08. +// Copyright 2008 Pieter de Bie. All rights reserved. +// + +#import + + +@interface PBFileChangesTableView : NSTableView { + id controller; +} + +@property (retain) id controller; +@end diff --git a/PBFileChangesTableView.m b/PBFileChangesTableView.m new file mode 100644 index 0000000..91e776e --- /dev/null +++ b/PBFileChangesTableView.m @@ -0,0 +1,24 @@ +// +// PBFileChangesTableView.m +// GitX +// +// Created by Pieter de Bie on 09-10-08. +// Copyright 2008 Pieter de Bie. All rights reserved. +// + +#import "PBFileChangesTableView.h" +#import "PBGitCommitController.h" + +@implementation PBFileChangesTableView + +@synthesize controller; + +#pragma mark NSTableView overrides +- (NSMenu *)menuForEvent:(NSEvent *)theEvent +{ + if (controller) + return [(PBGitCommitController *)controller menuForTable: self]; + + return nil; +} +@end diff --git a/PBGitCommitController.h b/PBGitCommitController.h index 7f00a3d..f831748 100644 --- a/PBGitCommitController.h +++ b/PBGitCommitController.h @@ -41,6 +41,8 @@ - (void) readOtherFiles:(NSNotification *)notification; - (void) readUnstagedFiles:(NSNotification *)notification; +- (NSMenu *) menuForTable:(NSTableView *)table; + - (IBAction) refresh:(id) sender; - (IBAction) commit:(id) sender; @end diff --git a/PBGitCommitController.m b/PBGitCommitController.m index 9248969..0a25870 100644 --- a/PBGitCommitController.m +++ b/PBGitCommitController.m @@ -27,6 +27,8 @@ [unstagedTable setDoubleAction:@selector(tableClicked:)]; [cachedTable setDoubleAction:@selector(tableClicked:)]; + [unstagedTable setController: self]; + [self refresh:self]; [commitMessageView setTypingAttributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Monaco" size:12.0] forKey:NSFontAttributeName]]; @@ -158,18 +160,23 @@ } even = 0; + BOOL isNew = YES; // If the file is already added, we shouldn't add it again // but rather update it to incorporate our changes - NSArray *existingFiles = [files filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"path == '%@'", line]]; - if ([existingFiles count] != 0) { - PBChangedFile *file = [existingFiles objectAtIndex:0]; - if (cached) - file.hasCachedChanges = YES; - else - file.hasUnstagedChanges = YES; - return; + for (PBChangedFile *file in files) { + if ([file.path isEqualToString:line]) { + if (cached) + file.hasCachedChanges = YES; + else + file.hasUnstagedChanges = YES; + isNew = NO; + break; + } } + if (!isNew) + continue; + PBChangedFile *file = [[PBChangedFile alloc] initWithPath:line andRepository:repository]; if ([[fileStatus objectAtIndex:4] isEqualToString:@"D"]) file.status = DELETED; @@ -284,29 +291,14 @@ - (void) tableClicked:(NSTableView *) tableView { NSUInteger selectionIndex = [[tableView selectedRowIndexes] firstIndex]; - NSArrayController *controller, *otherController; - if ([tableView tag] == 0) { - controller = unstagedFilesController; - otherController = cachedFilesController; - } - else { - controller = cachedFilesController; - otherController = unstagedFilesController; - } - + NSArrayController *controller = [tableView tag] == 0 ? unstagedFilesController : cachedFilesController; PBChangedFile *selectedItem = [[controller arrangedObjects] objectAtIndex:selectionIndex]; - [controller removeObject:selectedItem]; + if ([tableView tag] == 0) [selectedItem stageChanges]; else [selectedItem unstageChangesAmend:amend]; - // Add the file to the other controller if it's not there yet - for (PBChangedFile *object in [otherController arrangedObjects]) - if ([[object path] isEqualToString:[selectedItem path]]) - return; - - [otherController addObject:selectedItem]; } - (void) rowClicked:(NSCell *)sender @@ -321,4 +313,32 @@ { [[tableColumn dataCell] setImage:[[[(([tableView tag] == 0) ? unstagedFilesController : cachedFilesController) arrangedObjects] objectAtIndex:rowIndex] icon]]; } + +- (NSMenu *) menuForTable:(NSTableView *)table +{ + NSUInteger selectionIndex = [[table selectedRowIndexes] firstIndex]; + PBChangedFile *selectedItem = [[unstagedFilesController arrangedObjects] objectAtIndex:selectionIndex]; + + NSMenu *a = [[NSMenu alloc] init]; + NSMenuItem *stageItem = [[NSMenuItem alloc] initWithTitle:@"Stage Changes" action:@selector(stageChanges) keyEquivalent:@""]; + [stageItem setTarget:selectedItem]; + [a addItem:stageItem]; + + // Do not add "revert" options for untracked files + if (selectedItem.status == NEW) + return a; + + NSMenuItem *revertItem = [[NSMenuItem alloc] initWithTitle:@"Revert Changes…" action:@selector(revertChanges) keyEquivalent:@""]; + [revertItem setTarget:selectedItem]; + [revertItem setAlternate:NO]; + [a addItem:revertItem]; + + NSMenuItem *revertForceItem = [[NSMenuItem alloc] initWithTitle:@"Revert Changes" action:@selector(forceRevertChanges) keyEquivalent:@""]; + [revertForceItem setTarget:selectedItem]; + [revertForceItem setAlternate:YES]; + [revertForceItem setKeyEquivalentModifierMask:NSAlternateKeyMask]; + [a addItem:revertForceItem]; + + return a; +} @end diff --git a/PBGitCommitView.xib b/PBGitCommitView.xib index ecbd2d9..281a669 100644 --- a/PBGitCommitView.xib +++ b/PBGitCommitView.xib @@ -8,7 +8,7 @@ 352.00 YES - + YES @@ -359,7 +359,7 @@ WebURLsWithTitlesPboardType - {427, 32} + {427, 14} @@ -1438,6 +1438,7 @@ 45.IBPluginDependency 46.IBPluginDependency 47.IBPluginDependency + 48.CustomClassName 48.IBPluginDependency 54.IBPluginDependency 55.IBPluginDependency @@ -1479,6 +1480,7 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + PBFileChangesTableView com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -1514,6 +1516,18 @@ YES + + PBFileChangesTableView + NSTableView + + controller + id + + + IBProjectSource + PBFileChangesTableView.h + + PBGitCommitController PBViewController