forked from dgrijalva/gitx
-
Notifications
You must be signed in to change notification settings - Fork 76
/
PBDiffWindowController.m
83 lines (63 loc) · 2.64 KB
/
PBDiffWindowController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//
// PBDiffWindowController.m
// GitX
//
// Created by Pieter de Bie on 13-10-08.
// Copyright 2008 Pieter de Bie. All rights reserved.
//
#import "PBDiffWindowController.h"
#import "PBGitRepository.h"
#import "PBGitCommit.h"
#import "PBGitDefaults.h"
#import "GLFileView.h"
#import "PBWebCommitController.h"
@implementation PBDiffWindowController
@synthesize diff;
- (id) initWithDiff:(NSString *)aDiff
{
if (!(self = [super initWithWindowNibName:@"PBDiffWindow"]))
return nil;
diff = 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 *args = [NSMutableArray arrayWithObjects:@"diff", @"--no-ext-diff", commitSelector, nil];
if (![PBGitDefaults showWhitespaceDifferences])
[args insertObject:@"-w" atIndex:1];
if (filePaths) {
[args addObject:@"--"];
[args addObjectsFromArray:filePaths];
}
int retValue;
NSString *diff = [startCommit.repository outputInWorkdirForArguments:args retValue:&retValue];
if (retValue) {
DLog(@"diff failed with retValue: %d for command: '%@' output: '%@'", retValue, [args componentsJoinedByString:@" "], diff);
return;
}
// File Stats
args = [NSMutableArray arrayWithObjects:@"show", @"--numstat", @"--summary", @"--pretty=raw", [startCommit realSha], [diffCommit realSha], nil];
if (![PBGitDefaults showWhitespaceDifferences])
[args insertObject:@"-w" atIndex:1];
NSString *details = [startCommit.repository outputInWorkdirForArguments:args];
NSMutableDictionary *stats = [PBWebCommitController parseStats:details];
// File list
args = [NSMutableArray arrayWithObjects:@"diff-tree", @"--root", @"-r", @"-C90%", @"-M90%", nil];
[args addObject:[startCommit realSha]];
[args addObject:[diffCommit realSha]];
NSString *dt = [startCommit.repository outputInWorkdirForArguments:args];
NSString *fileList = [GLFileView parseDiffTree:dt withStats:stats];
// Hunk list
NSString *hunks = [GLFileView parseDiff:diff];
hunks=[hunks stringByReplacingOccurrencesOfString:@"{SHA_PREV}" withString:[startCommit realSha]];
hunks=[hunks stringByReplacingOccurrencesOfString:@"{SHA}" withString:[diffCommit realSha]];
NSString *html = [NSString stringWithFormat:@"%@%@",fileList,hunks];
PBDiffWindowController *diffController = [[PBDiffWindowController alloc] initWithDiff:html];
[diffController showWindow:nil];
}
@end