Permalink
Browse files

Add Diff to contextual menus

    - added to menus for refs, commits, and files
    - show a message when there are no changes
  • Loading branch information...
1 parent dc56af4 commit c19d2c80582359211ec0bacede1262abd48130bc @brotherbard committed Feb 27, 2010
View
@@ -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
View
@@ -7,6 +7,8 @@
//
#import "PBDiffWindowController.h"
+#import "PBGitRepository.h"
+#import "PBGitCommit.h"
@implementation PBDiffWindowController
@@ -21,4 +23,32 @@ - (id) initWithDiff:(NSString *)aDiff
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
View
@@ -16,6 +16,7 @@
#import "PBAddRemoteSheet.h"
#import "PBGitSidebarController.h"
#import "PBGitGradientBarView.h"
+#import "PBDiffWindowController.h"
#define QLPreviewPanel NSClassFromString(@"QLPreviewPanel")
@@ -294,6 +295,10 @@ - (void) checkoutFiles:(id)sender
[repository checkoutFiles:files fromRefish:realCommit];
}
+- (void) diffFilesAction:(id)sender
+{
+ [PBDiffWindowController showDiffWindowWithFiles:[sender representedObject] fromCommit:realCommit diffCommit:nil];
+}
- (NSMenu *)contextMenuForTreeView
{
@@ -315,6 +320,15 @@ - (NSArray *)menuItemsForPaths:(NSArray *)paths
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 @@ - (NSArray *)menuItemsForPaths:(NSArray *)paths
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];
View
@@ -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;
View
@@ -12,6 +12,7 @@
#import "PBCreateBranchSheet.h"
#import "PBCreateTagSheet.h"
#import "PBGitDefaults.h"
+#import "PBDiffWindowController.h"
@implementation PBRefController
@@ -204,6 +205,19 @@ - (void) copyPatch:(PBRefMenuItem *)sender
}
+#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
View
@@ -66,6 +66,10 @@ + (NSArray *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitReposito
// 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 @@ + (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target
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 @@ + (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target
[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
View
@@ -35,7 +35,10 @@ - (void) showDiff: (NSString *) diff
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
@@ -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;
+}
@@ -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";
+}
+
@@ -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>

0 comments on commit c19d2c8

Please sign in to comment.