Skip to content

Commit c19d2c8

Browse files
committed
Add Diff to contextual menus
- added to menus for refs, commits, and files - show a message when there are no changes
1 parent dc56af4 commit c19d2c8

10 files changed

+103
-3
lines changed

PBDiffWindowController.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88

99
#import <Cocoa/Cocoa.h>
1010

11+
@class PBGitCommit;
1112

1213
@interface PBDiffWindowController : NSWindowController {
1314
NSString *diff;
1415
}
1516

16-
- initWithDiff:(NSString *)diff;
17+
+ (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(PBGitCommit *)startCommit diffCommit:(PBGitCommit *)diffCommit;
18+
- (id) initWithDiff:(NSString *)diff;
19+
1720
@property (readonly) NSString *diff;
1821
@end

PBDiffWindowController.m

+30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
//
88

99
#import "PBDiffWindowController.h"
10+
#import "PBGitRepository.h"
11+
#import "PBGitCommit.h"
1012

1113

1214
@implementation PBDiffWindowController
@@ -21,4 +23,32 @@ - (id) initWithDiff:(NSString *)aDiff
2123
return self;
2224
}
2325

26+
27+
+ (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(PBGitCommit *)startCommit diffCommit:(PBGitCommit *)diffCommit
28+
{
29+
if (!startCommit)
30+
return;
31+
32+
if (!diffCommit)
33+
diffCommit = [startCommit.repository headCommit];
34+
35+
NSString *commitSelector = [NSString stringWithFormat:@"%@..%@", [startCommit realSha], [diffCommit realSha]];
36+
NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"diff", commitSelector, nil];
37+
if (filePaths) {
38+
[arguments addObject:@"--"];
39+
[arguments addObjectsFromArray:filePaths];
40+
}
41+
42+
int retValue;
43+
NSString *diff = [startCommit.repository outputInWorkdirForArguments:arguments retValue:&retValue];
44+
if (retValue) {
45+
NSLog(@"diff failed with retValue: %d for command: '%@' output: '%@'", retValue, [arguments componentsJoinedByString:@" "], diff);
46+
return;
47+
}
48+
49+
PBDiffWindowController *diffController = [[PBDiffWindowController alloc] initWithDiff:[diff copy]];
50+
[diffController showWindow:nil];
51+
}
52+
53+
2454
@end

PBGitHistoryController.m

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "PBAddRemoteSheet.h"
1717
#import "PBGitSidebarController.h"
1818
#import "PBGitGradientBarView.h"
19+
#import "PBDiffWindowController.h"
1920
#define QLPreviewPanel NSClassFromString(@"QLPreviewPanel")
2021

2122

@@ -294,6 +295,10 @@ - (void) checkoutFiles:(id)sender
294295
[repository checkoutFiles:files fromRefish:realCommit];
295296
}
296297

298+
- (void) diffFilesAction:(id)sender
299+
{
300+
[PBDiffWindowController showDiffWindowWithFiles:[sender representedObject] fromCommit:realCommit diffCommit:nil];
301+
}
297302

298303
- (NSMenu *)contextMenuForTreeView
299304
{
@@ -315,6 +320,15 @@ - (NSArray *)menuItemsForPaths:(NSArray *)paths
315320
NSMenuItem *historyItem = [[NSMenuItem alloc] initWithTitle:multiple? @"Show history of files" : @"Show history of file"
316321
action:@selector(showCommitsFromTree:)
317322
keyEquivalent:@""];
323+
324+
PBGitRef *headRef = [[repository headRef] ref];
325+
NSString *headRefName = [headRef shortName];
326+
NSString *diffTitle = [NSString stringWithFormat:@"Diff %@ with %@", multiple ? @"files" : @"file", headRefName];
327+
BOOL isHead = [[realCommit realSha] isEqualToString:[repository headSHA]];
328+
NSMenuItem *diffItem = [[NSMenuItem alloc] initWithTitle:diffTitle
329+
action:isHead ? nil : @selector(diffFilesAction:)
330+
keyEquivalent:@""];
331+
318332
NSMenuItem *checkoutItem = [[NSMenuItem alloc] initWithTitle:multiple ? @"Checkout files" : @"Checkout file"
319333
action:@selector(checkoutFiles:)
320334
keyEquivalent:@""];
@@ -325,7 +339,7 @@ - (NSArray *)menuItemsForPaths:(NSArray *)paths
325339
action:@selector(openFilesAction:)
326340
keyEquivalent:@""];
327341

328-
NSArray *menuItems = [NSArray arrayWithObjects:historyItem, checkoutItem, finderItem, openFilesItem, nil];
342+
NSArray *menuItems = [NSArray arrayWithObjects:historyItem, diffItem, checkoutItem, finderItem, openFilesItem, nil];
329343
for (NSMenuItem *item in menuItems) {
330344
[item setTarget:self];
331345
[item setRepresentedObject:filePaths];

PBRefController.h

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- (void) createBranch:(PBRefMenuItem *)sender;
3838
- (void) copySHA:(PBRefMenuItem *)sender;
3939
- (void) copyPatch:(PBRefMenuItem *)sender;
40+
- (void) diffWithHEAD:(PBRefMenuItem *)sender;
4041
- (void) createTag:(PBRefMenuItem *)sender;
4142
- (void) showTagInfoSheet:(PBRefMenuItem *)sender;
4243

PBRefController.m

+14
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "PBCreateBranchSheet.h"
1313
#import "PBCreateTagSheet.h"
1414
#import "PBGitDefaults.h"
15+
#import "PBDiffWindowController.h"
1516

1617
@implementation PBRefController
1718

@@ -204,6 +205,19 @@ - (void) copyPatch:(PBRefMenuItem *)sender
204205
}
205206

206207

208+
#pragma mark Diff
209+
210+
- (void) diffWithHEAD:(PBRefMenuItem *)sender
211+
{
212+
PBGitCommit *commit = nil;
213+
if ([[sender refish] refishType] == kGitXCommitType)
214+
commit = (PBGitCommit *)[sender refish];
215+
else
216+
commit = [historyController.repository commitForRef:[sender refish]];
217+
218+
[PBDiffWindowController showDiffWindowWithFiles:nil fromCommit:commit diffCommit:nil];
219+
}
220+
207221
#pragma mark Tags
208222

209223
- (void) createTag:(PBRefMenuItem *)sender

PBRefMenuItem.m

+7
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ + (NSArray *) defaultMenuItemsForRef:(PBGitRef *)ref inRepository:(PBGitReposito
6666
// view tag info
6767
if ([ref isTag])
6868
[items addObject:[PBRefMenuItem itemWithTitle:@"View tag info…" action:@selector(showTagInfoSheet:) enabled:YES]];
69+
70+
// Diff
71+
NSString *diffTitle = [NSString stringWithFormat:@"Diff with %@", headRefName];
72+
[items addObject:[PBRefMenuItem itemWithTitle:diffTitle action:@selector(diffWithHEAD:) enabled:!isHead]];
6973
[items addObject:[PBRefMenuItem separatorItem]];
7074

7175
// merge ref
@@ -141,6 +145,7 @@ + (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target
141145

142146
NSString *headBranchName = [[[commit.repository headRef] ref] shortName];
143147
BOOL isOnHeadBranch = [commit isOnHeadBranch];
148+
BOOL isHead = [[commit realSha] isEqualToString:[commit.repository headSHA]];
144149

145150
[items addObject:[PBRefMenuItem itemWithTitle:@"Checkout Commit" action:@selector(checkout:) enabled:YES]];
146151
[items addObject:[PBRefMenuItem separatorItem]];
@@ -151,6 +156,8 @@ + (NSArray *) defaultMenuItemsForCommit:(PBGitCommit *)commit target:(id)target
151156

152157
[items addObject:[PBRefMenuItem itemWithTitle:@"Copy SHA" action:@selector(copySHA:) enabled:YES]];
153158
[items addObject:[PBRefMenuItem itemWithTitle:@"Copy Patch" action:@selector(copyPatch:) enabled:YES]];
159+
NSString *diffTitle = [NSString stringWithFormat:@"Diff with %@", headBranchName];
160+
[items addObject:[PBRefMenuItem itemWithTitle:diffTitle action:@selector(diffWithHEAD:) enabled:!isHead]];
154161
[items addObject:[PBRefMenuItem separatorItem]];
155162

156163
// merge commit

PBWebDiffController.m

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ - (void) showDiff: (NSString *) diff
3535
return;
3636

3737
id script = [view windowScriptObject];
38-
[script callWebScriptMethod:@"showDiff" withArguments: [NSArray arrayWithObject:diff]];
38+
if ([diff length] == 0)
39+
[script callWebScriptMethod:@"setMessage" withArguments:[NSArray arrayWithObject:@"There are no differences"]];
40+
else
41+
[script callWebScriptMethod:@"showDiff" withArguments:[NSArray arrayWithObject:diff]];
3942
}
4043

4144
@end

html/views/diff/diffWindow.css

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#message {
2+
margin-left: 20px;
3+
margin-right: 20px;
4+
margin-top: 40px;
5+
text-align: center;
6+
font-size: 200%;
7+
padding: 20px;
8+
width: auto;
9+
10+
background-color: #B4D7FF;
11+
border: 2px solid #45A1FE;
12+
13+
-webkit-border-radius: 10px;
14+
}

html/views/diff/diffWindow.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// for diffs shown in the PBDiffWindow
2+
3+
var setMessage = function(message) {
4+
$("message").style.display = "";
5+
$("message").innerHTML = message.escapeHTML();
6+
$("diff").style.display = "none";
7+
}
8+

html/views/diff/index.html

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
<script src="../../lib/diffHighlighter.js" type="text/javascript" charset="utf-8"></script>
88
<script src="../../lib/keyboardNavigation.js" type="text/javascript" charset="utf-8"></script>
99

10+
<link rel="stylesheet" href="diffWindow.css" type="text/css" media="screen" title="no title" charset="utf-8">
11+
<script src="diffWindow.js" type="text/javascript" charset="utf-8"></script>
12+
1013
<script type="text/javascript" charset="utf-8">
1114
var showDiff = function(diff) {
1215
highlightDiff(diff, $("diff"));
@@ -15,5 +18,8 @@
1518
</head>
1619

1720
<body>
21+
<div id="message" style="display:none">
22+
There are no differences
23+
</div>
1824
<div id='diff'></div>
1925
</body>

0 commit comments

Comments
 (0)