Add Diff to contextual menus

- added to menus for refs, commits, and files
    - show a message when there are no changes
This commit is contained in:
Nathan Kinsinger
2010-02-27 10:06:25 -07:00
parent dc56af4aa6
commit c19d2c8058
10 changed files with 103 additions and 3 deletions
+4 -1
View File
@@ -8,11 +8,14 @@
#import <Cocoa/Cocoa.h>
@class PBGitCommit;
@interface PBDiffWindowController : NSWindowController {
NSString *diff;
}
- initWithDiff:(NSString *)diff;
+ (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(PBGitCommit *)startCommit diffCommit:(PBGitCommit *)diffCommit;
- (id) initWithDiff:(NSString *)diff;
@property (readonly) NSString *diff;
@end
+30
View File
@@ -7,6 +7,8 @@
//
#import "PBDiffWindowController.h"
#import "PBGitRepository.h"
#import "PBGitCommit.h"
@implementation PBDiffWindowController
@@ -21,4 +23,32 @@
return self;
}
+ (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(PBGitCommit *)startCommit diffCommit:(PBGitCommit *)diffCommit
{
if (!startCommit)
return;
if (!diffCommit)
diffCommit = [startCommit.repository headCommit];
NSString *commitSelector = [NSString stringWithFormat:@"%@..%@", [startCommit realSha], [diffCommit realSha]];
NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"diff", commitSelector, nil];
if (filePaths) {
[arguments addObject:@"--"];
[arguments addObjectsFromArray:filePaths];
}
int retValue;
NSString *diff = [startCommit.repository outputInWorkdirForArguments:arguments retValue:&retValue];
if (retValue) {
NSLog(@"diff failed with retValue: %d for command: '%@' output: '%@'", retValue, [arguments componentsJoinedByString:@" "], diff);
return;
}
PBDiffWindowController *diffController = [[PBDiffWindowController alloc] initWithDiff:[diff copy]];
[diffController showWindow:nil];
}
@end
+15 -1
View File
@@ -16,6 +16,7 @@
#import "PBAddRemoteSheet.h"
#import "PBGitSidebarController.h"
#import "PBGitGradientBarView.h"
#import "PBDiffWindowController.h"
#define QLPreviewPanel NSClassFromString(@"QLPreviewPanel")
@@ -294,6 +295,10 @@
[repository checkoutFiles:files fromRefish:realCommit];
}
- (void) diffFilesAction:(id)sender
{
[PBDiffWindowController showDiffWindowWithFiles:[sender representedObject] fromCommit:realCommit diffCommit:nil];
}
- (NSMenu *)contextMenuForTreeView
{
@@ -315,6 +320,15 @@
NSMenuItem *historyItem = [[NSMenuItem alloc] initWithTitle:multiple? @"Show history of files" : @"Show history of file"
action:@selector(showCommitsFromTree:)
keyEquivalent:@""];
PBGitRef *headRef = [[repository headRef] ref];
NSString *headRefName = [headRef shortName];
NSString *diffTitle = [NSString stringWithFormat:@"Diff %@ with %@", multiple ? @"files" : @"file", headRefName];
BOOL isHead = [[realCommit realSha] isEqualToString:[repository headSHA]];
NSMenuItem *diffItem = [[NSMenuItem alloc] initWithTitle:diffTitle
action:isHead ? nil : @selector(diffFilesAction:)
keyEquivalent:@""];
NSMenuItem *checkoutItem = [[NSMenuItem alloc] initWithTitle:multiple ? @"Checkout files" : @"Checkout file"
action:@selector(checkoutFiles:)
keyEquivalent:@""];
@@ -325,7 +339,7 @@
action:@selector(openFilesAction:)
keyEquivalent:@""];
NSArray *menuItems = [NSArray arrayWithObjects:historyItem, checkoutItem, finderItem, openFilesItem, nil];
NSArray *menuItems = [NSArray arrayWithObjects:historyItem, diffItem, checkoutItem, finderItem, openFilesItem, nil];
for (NSMenuItem *item in menuItems) {
[item setTarget:self];
[item setRepresentedObject:filePaths];
+1
View File
@@ -37,6 +37,7 @@
- (void) createBranch:(PBRefMenuItem *)sender;
- (void) copySHA:(PBRefMenuItem *)sender;
- (void) copyPatch:(PBRefMenuItem *)sender;
- (void) diffWithHEAD:(PBRefMenuItem *)sender;
- (void) createTag:(PBRefMenuItem *)sender;
- (void) showTagInfoSheet:(PBRefMenuItem *)sender;
+14
View File
@@ -12,6 +12,7 @@
#import "PBCreateBranchSheet.h"
#import "PBCreateTagSheet.h"
#import "PBGitDefaults.h"
#import "PBDiffWindowController.h"
@implementation PBRefController
@@ -204,6 +205,19 @@
}
#pragma mark Diff
- (void) diffWithHEAD:(PBRefMenuItem *)sender
{
PBGitCommit *commit = nil;
if ([[sender refish] refishType] == kGitXCommitType)
commit = (PBGitCommit *)[sender refish];
else
commit = [historyController.repository commitForRef:[sender refish]];
[PBDiffWindowController showDiffWindowWithFiles:nil fromCommit:commit diffCommit:nil];
}
#pragma mark Tags
- (void) createTag:(PBRefMenuItem *)sender
+7
View File
@@ -66,6 +66,10 @@
// view tag info
if ([ref isTag])
[items addObject:[PBRefMenuItem itemWithTitle:@"View tag info…" action:@selector(showTagInfoSheet:) enabled:YES]];
// Diff
NSString *diffTitle = [NSString stringWithFormat:@"Diff with %@", headRefName];
[items addObject:[PBRefMenuItem itemWithTitle:diffTitle action:@selector(diffWithHEAD:) enabled:!isHead]];
[items addObject:[PBRefMenuItem separatorItem]];
// merge ref
@@ -141,6 +145,7 @@
NSString *headBranchName = [[[commit.repository headRef] ref] shortName];
BOOL isOnHeadBranch = [commit isOnHeadBranch];
BOOL isHead = [[commit realSha] isEqualToString:[commit.repository headSHA]];
[items addObject:[PBRefMenuItem itemWithTitle:@"Checkout Commit" action:@selector(checkout:) enabled:YES]];
[items addObject:[PBRefMenuItem separatorItem]];
@@ -151,6 +156,8 @@
[items addObject:[PBRefMenuItem itemWithTitle:@"Copy SHA" action:@selector(copySHA:) enabled:YES]];
[items addObject:[PBRefMenuItem itemWithTitle:@"Copy Patch" action:@selector(copyPatch:) enabled:YES]];
NSString *diffTitle = [NSString stringWithFormat:@"Diff with %@", headBranchName];
[items addObject:[PBRefMenuItem itemWithTitle:diffTitle action:@selector(diffWithHEAD:) enabled:!isHead]];
[items addObject:[PBRefMenuItem separatorItem]];
// merge commit
+4 -1
View File
@@ -35,7 +35,10 @@
return;
id script = [view windowScriptObject];
[script callWebScriptMethod:@"showDiff" withArguments: [NSArray arrayWithObject:diff]];
if ([diff length] == 0)
[script callWebScriptMethod:@"setMessage" withArguments:[NSArray arrayWithObject:@"There are no differences"]];
else
[script callWebScriptMethod:@"showDiff" withArguments:[NSArray arrayWithObject:diff]];
}
@end
+14
View File
@@ -0,0 +1,14 @@
#message {
margin-left: 20px;
margin-right: 20px;
margin-top: 40px;
text-align: center;
font-size: 200%;
padding: 20px;
width: auto;
background-color: #B4D7FF;
border: 2px solid #45A1FE;
-webkit-border-radius: 10px;
}
+8
View File
@@ -0,0 +1,8 @@
// for diffs shown in the PBDiffWindow
var setMessage = function(message) {
$("message").style.display = "";
$("message").innerHTML = message.escapeHTML();
$("diff").style.display = "none";
}
+6
View File
@@ -7,6 +7,9 @@
<script src="../../lib/diffHighlighter.js" type="text/javascript" charset="utf-8"></script>
<script src="../../lib/keyboardNavigation.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="diffWindow.css" type="text/css" media="screen" title="no title" charset="utf-8">
<script src="diffWindow.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var showDiff = function(diff) {
highlightDiff(diff, $("diff"));
@@ -15,5 +18,8 @@
</head>
<body>
<div id="message" style="display:none">
There are no differences
</div>
<div id='diff'></div>
</body>