Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show diffstat info #421

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Classes/Controllers/PBDiffWindowController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
#import <Cocoa/Cocoa.h>

@class PBGitCommit;
@class PBGitRepository;

@interface PBDiffWindowController : NSWindowController {
NSString *diff;
}

+ (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(NSString *)startCommit diffCommit:(NSString *)diffCommit repository:(PBGitRepository*) repository;
+ (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(PBGitCommit *)startCommit diffCommit:(PBGitCommit *)diffCommit;
- (id) initWithDiff:(NSString *)diff;

Expand Down
24 changes: 14 additions & 10 deletions Classes/Controllers/PBDiffWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,9 @@ - (id) initWithDiff:(NSString *)aDiff
return self;
}


+ (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(PBGitCommit *)startCommit diffCommit:(PBGitCommit *)diffCommit
+ (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(NSString *)startCommit diffCommit:(NSString *)diffCommit repository:(PBGitRepository*) repository
{
if (!startCommit)
return;

if (!diffCommit)
diffCommit = [startCommit.repository headCommit];

NSString *commitSelector = [NSString stringWithFormat:@"%@..%@", [startCommit realSha], [diffCommit realSha]];
NSString *commitSelector = [NSString stringWithFormat:@"%@..%@", startCommit, diffCommit];
NSMutableArray *arguments = [NSMutableArray arrayWithObjects:@"diff", @"--no-ext-diff", commitSelector, nil];

if (![PBGitDefaults showWhitespaceDifferences])
Expand All @@ -46,7 +39,7 @@ + (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(PBGitCommit *)
}

int retValue;
NSString *diff = [startCommit.repository outputInWorkdirForArguments:arguments retValue:&retValue];
NSString *diff = [repository outputInWorkdirForArguments:arguments retValue:&retValue];
if (retValue) {
NSLog(@"diff failed with retValue: %d for command: '%@' output: '%@'", retValue, [arguments componentsJoinedByString:@" "], diff);
return;
Expand All @@ -56,5 +49,16 @@ + (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(PBGitCommit *)
[diffController showWindow:nil];
}

+ (void) showDiffWindowWithFiles:(NSArray *)filePaths fromCommit:(PBGitCommit *)startCommit diffCommit:(PBGitCommit *)diffCommit
{
if (!startCommit)
return;

if (!diffCommit)
diffCommit = [startCommit.repository headCommit];

[PBDiffWindowController showDiffWindowWithFiles:filePaths fromCommit:[startCommit realSHA] diffCommit:[diffCommit realSHA] repository:[startCommit repository]];
}


@end
2 changes: 2 additions & 0 deletions Classes/Controllers/PBRefController.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@
- (NSArray *) menuItemsForCommit:(PBGitCommit *)commit;
- (NSArray *)menuItemsForRow:(NSInteger)rowIndex;

// invoked via Cocoa Bindings
- (void)onDoubleClick:(NSArray*)selectedObjects;

@end
61 changes: 57 additions & 4 deletions Classes/Controllers/PBRefController.m
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,26 @@ - (NSArray *)menuItemsForRow:(NSInteger)rowIndex

# pragma mark Tableview delegate methods

- (BOOL)tryToDragPatch:(PBGitCommit*)commitref
fromPoint:(NSPoint)dragPosition
inTable:(PBCommitList *)tv
toPasteboard:(NSPasteboard*)pboard
{
NSRect imageLocation;

dragPosition.x -= 16;
dragPosition.y -= 16;
imageLocation.origin = dragPosition;
imageLocation.size = NSMakeSize(32,32);

[pboard declareTypes:[NSArray arrayWithObjects:@"PBGitCommit", NSFilesPromisePboardType, nil] owner:self];
[pboard setPropertyList:[NSArray arrayWithObject:@"patch"] forType:NSFilesPromisePboardType];

NSData* data=[NSKeyedArchiver archivedDataWithRootObject:[commitref realSHA]];
[pboard setData:data forType:@"PBGitCommit"];
return YES;
}

- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard*)pboard
{

Expand All @@ -341,22 +361,22 @@ - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexe
int column = [tv columnAtPoint:location];
int subjectColumn = [tv columnWithIdentifier:@"SubjectColumn"];
if (column != subjectColumn)
return NO;
return [self tryToDragPatch:[[commitController arrangedObjects] objectAtIndex:row] fromPoint:location inTable:(PBCommitList*)tv toPasteboard:pboard];

PBGitRevisionCell *cell = (PBGitRevisionCell *)[tv preparedCellAtColumn:column row:row];
NSRect cellFrame = [tv frameOfCellAtColumn:column row:row];

int index = [cell indexAtX:(location.x - cellFrame.origin.x)];

if (index == -1)
return NO;
return [self tryToDragPatch:[[commitController arrangedObjects] objectAtIndex:row] fromPoint:location inTable:(PBCommitList*)tv toPasteboard:pboard];

PBGitRef *ref = [[[cell objectValue] refs] objectAtIndex:index];
if ([ref isTag] || [ref isRemoteBranch])
return NO;
return [self tryToDragPatch:[[commitController arrangedObjects] objectAtIndex:row] fromPoint:location inTable:(PBCommitList*)tv toPasteboard:pboard];

if ([[[historyController.repository headRef] ref] isEqualToRef:ref])
return NO;
return [self tryToDragPatch:[[commitController arrangedObjects] objectAtIndex:row] fromPoint:location inTable:(PBCommitList*)tv toPasteboard:pboard];

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:row], [NSNumber numberWithInt:index], NULL]];
[pboard declareTypes:[NSArray arrayWithObject:@"PBGitRef"] owner:self];
Expand All @@ -380,6 +400,31 @@ - (NSDragOperation)tableView:(NSTableView*)tv
return NSDragOperationNone;
}

- (NSArray *) tableView:(NSTableView *)aTableView
namesOfPromisedFilesDroppedAtDestination:(NSURL *)destination
forDraggedRowsWithIndexes:(NSIndexSet *)indexSet
{
NSMutableArray *ret=[NSMutableArray arrayWithCapacity:1];

NSInteger idx = [indexSet firstIndex];
while (idx != NSNotFound) {
PBGitCommit* aCommit=[[commitController arrangedObjects] objectAtIndex:idx];

// name the patch file based on the commit subject line (similar to what git format-patch does)
NSString* filename=[NSString stringWithFormat:@"%@.patch", [[aCommit subject] stringByReplacingOccurrencesOfString:@" " withString:@"-"]];

// xxx maybe bad if this is a big patch; however, doing this inside draggedImage:endedAt:operation: did not work
NSData *data = [[aCommit fullpatch] dataUsingEncoding:NSUTF8StringEncoding];
NSString *fullname=[NSString stringWithFormat:@"%@/%@", [destination path], filename];
[data writeToFile:fullname atomically:YES];

[ret addObject:filename];
idx = [indexSet indexGreaterThanIndex: idx];
}

return ret;
}

- (void) dropRef:(NSDictionary *)dropInfo
{
PBGitRef *ref = [dropInfo objectForKey:@"dragRef"];
Expand Down Expand Up @@ -468,4 +513,12 @@ - (void)dealloc {
historyController = nil;
}

- (void)onDoubleClick:(NSArray*)selectedObjects
{
//NSLog(@"PBRefController %@", selectedObjects);
for (PBGitCommit* commit in selectedObjects) {
[PBDiffWindowController showDiffWindowWithFiles:nil fromCommit:[[[commit parents] firstObject] SHA] diffCommit:[commit realSha] repository:[commit repository]];
}
}

@end
46 changes: 38 additions & 8 deletions Classes/Controllers/PBWebHistoryController.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,64 @@ - (void) changeContentTo: (PBGitCommit *) content
// but this caused some funny behaviour because NSTask's and NSThread's don't really
// like each other. Instead, just do it async.

NSMutableArray *taskArguments = [NSMutableArray arrayWithObjects:@"show", @"--pretty=raw", @"-M", @"--no-color", [currentSha SHA], nil];
NSMutableArray *taskArguments = [NSMutableArray arrayWithObjects:@"show", @"--numstat", @"-C", @"-C", @"--summary", @"--pretty=raw", [currentSha SHA], nil];
if (![PBGitDefaults showWhitespaceDifferences])
[taskArguments insertObject:@"-w" atIndex:1];

NSFileHandle *handle = [repository handleForArguments:taskArguments];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// Remove notification, in case we have another one running
[nc removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:nil];
[nc addObserver:self selector:@selector(commitDetailsLoaded:) name:NSFileHandleReadToEndOfFileCompletionNotification object:handle];
[nc addObserver:self selector:@selector(commitSummaryLoaded:) name:NSFileHandleReadToEndOfFileCompletionNotification object:handle];
[handle readToEndOfFileInBackgroundAndNotify];
}

- (void)commitDetailsLoaded:(NSNotification *)notification
- (void)commitSummaryLoaded:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:nil];

NSData *data = [[notification userInfo] valueForKey:NSFileHandleNotificationDataItem];
if (!data)
return;

NSString *details = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!details)
details = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
NSString *summary = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!summary)
summary = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];

if (!details)
if (!summary)
return;

[[view windowScriptObject] callWebScriptMethod:@"loadCommitDetails" withArguments:[NSArray arrayWithObject:details]];
[[view windowScriptObject] callWebScriptMethod:@"loadCommitSummary" withArguments:[NSArray arrayWithObject:summary]];

// Now load the full diff
NSMutableArray *taskArguments = [NSMutableArray arrayWithObjects:@"show", @"--pretty=raw", @"-C", @"-C", @"--no-color", [currentSha SHA], nil];
if (![PBGitDefaults showWhitespaceDifferences])
[taskArguments insertObject:@"-w" atIndex:1];

NSFileHandle *handle = [repository handleForArguments:taskArguments];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// Remove notification, in case we have another one running
[nc removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:nil];
[nc addObserver:self selector:@selector(commitFullDiffLoaded:) name:NSFileHandleReadToEndOfFileCompletionNotification object:handle];
[handle readToEndOfFileInBackgroundAndNotify];
}

- (void)commitFullDiffLoaded:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSFileHandleReadToEndOfFileCompletionNotification object:nil];

NSData *data = [[notification userInfo] valueForKey:NSFileHandleNotificationDataItem];
if (!data)
return;

NSString *fullDiff = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!fullDiff)
fullDiff = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];

if (!fullDiff)
return;

[[view windowScriptObject] callWebScriptMethod:@"loadCommitFullDiff" withArguments:[NSArray arrayWithObject:fullDiff]];
}

- (void)selectCommit:(NSString *)sha
Expand Down
1 change: 1 addition & 0 deletions Classes/git/PBGitCommit.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern NSString * const kGitXCommitType;
@property (nonatomic, strong, readonly) NSString *committer;
@property (nonatomic, strong, readonly) NSString *details;
@property (nonatomic, strong, readonly) NSString *patch;
@property (nonatomic, strong, readonly) NSString *fullpatch;
@property (nonatomic, strong, readonly) NSString *realSHA;
@property (nonatomic, strong, readonly) NSString *SVNRevision;

Expand Down
13 changes: 13 additions & 0 deletions Classes/git/PBGitCommit.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ @interface PBGitCommit ()
@property (nonatomic, strong) NSArray *parents;

@property (nonatomic, strong) NSString *patch;
@property (nonatomic, strong) NSString *fullpatch;
@property (nonatomic, strong) GTOID *sha;

@end
Expand Down Expand Up @@ -175,6 +176,18 @@ - (NSString *) patch
return self->_patch;
}

// same as above, but also uses --full-index --binary
- (NSString *) fullpatch
{
if (self->_fullpatch != nil)
return _fullpatch;

NSString *p = [self.repository outputForArguments:[NSArray arrayWithObjects:@"format-patch", @"-1", @"--stdout", @"--full-index", @"--binary", [self realSha], nil]];
// Add a GitX identifier to the patch ;)
self.fullpatch = [[p substringToIndex:[p length] -1] stringByAppendingString:@"+GitX"];
return self->_fullpatch;
}

- (PBGitTree*) tree
{
return [PBGitTree rootForCommit: self];
Expand Down
41 changes: 41 additions & 0 deletions Resources/XIBs/PBGitHistoryView.xib
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,47 @@
</object>
<int key="connectionID">445</int>
</object>
<object class="IBConnectionRecord">
<object class="IBBindingConnection" key="connection">
<string key="label">doubleClickArgument: selectedObjects</string>
<reference key="source" ref="254268962"/>
<reference key="destination" ref="391209158"/>
<object class="NSNibBindingConnector" key="connector" id="838846806">
<reference key="NSSource" ref="254268962"/>
<reference key="NSDestination" ref="391209158"/>
<string key="NSLabel">doubleClickArgument: selectedObjects</string>
<string key="NSBinding">doubleClickArgument</string>
<string key="NSKeyPath">selectedObjects</string>
<object class="NSDictionary" key="NSOptions">
<string key="NS.key.0">NSSelectorName</string>
<string key="NS.object.0">onDoubleClick:</string>
</object>
<int key="NSNibBindingConnectorVersion">2</int>
</object>
</object>
<int key="connectionID">521</int>
</object>
<object class="IBConnectionRecord">
<object class="IBBindingConnection" key="connection">
<string key="label">doubleClickTarget: self</string>
<reference key="source" ref="254268962"/>
<reference key="destination" ref="892732705"/>
<object class="NSNibBindingConnector" key="connector">
<reference key="NSSource" ref="254268962"/>
<reference key="NSDestination" ref="892732705"/>
<string key="NSLabel">doubleClickTarget: self</string>
<string key="NSBinding">doubleClickTarget</string>
<string key="NSKeyPath">self</string>
<object class="NSDictionary" key="NSOptions">
<string key="NS.key.0">NSSelectorName</string>
<string key="NS.object.0">onDoubleClick:</string>
</object>
<reference key="NSPreviousConnector" ref="838846806"/>
<int key="NSNibBindingConnectorVersion">2</int>
</object>
</object>
<int key="connectionID">524</int>
</object>
<object class="IBConnectionRecord">
<object class="IBOutletConnection" key="connection">
<string key="label">controller</string>
Expand Down
Loading