@@ -69,6 +69,20 @@ - (void) changeContentToMultipleSelectionMessage {
69
69
[[self script ] callWebScriptMethod: @" showMultipleSelectionMessage" withArguments: arguments];
70
70
}
71
71
72
+ static NSString *deltaTypeName (GTDiffDeltaType t) {
73
+ switch (t) {
74
+ case GTDiffFileDeltaUnmodified: return @" unmodified" ;
75
+ case GTDiffFileDeltaAdded: return @" added" ;
76
+ case GTDiffFileDeltaDeleted: return @" removed" ;
77
+ case GTDiffFileDeltaModified: return @" modified" ;
78
+ case GTDiffFileDeltaRenamed: return @" renamed" ;
79
+ case GTDiffFileDeltaCopied: return @" copied" ;
80
+ case GTDiffFileDeltaIgnored: return @" ignored" ;
81
+ case GTDiffFileDeltaUntracked: return @" untracked" ;
82
+ case GTDiffFileDeltaTypeChange: return @" type changed" ;
83
+ }
84
+ }
85
+
72
86
- (void ) changeContentToCommit: (PBGitCommit *)commit
73
87
{
74
88
// The sha is the same, but refs may have changed. reload it lazy
@@ -78,7 +92,7 @@ - (void) changeContentToCommit:(PBGitCommit *)commit
78
92
return ;
79
93
}
80
94
81
- NSArray *arguments = [ NSArray arrayWithObjects: commit, [[[historyController repository ] headRef ] simpleRef ], nil ];
95
+ NSArray *arguments = @[ commit, [[[historyController repository ] headRef ] simpleRef ]];
82
96
id scriptResult = [[self script ] callWebScriptMethod: @" loadCommit" withArguments: arguments];
83
97
if (!scriptResult) {
84
98
// the web view is not really ready for scripting???
@@ -87,39 +101,60 @@ - (void) changeContentToCommit:(PBGitCommit *)commit
87
101
}
88
102
currentOID = commit.OID ;
89
103
90
- // Now we load the extended details. We used to do this in a separate thread,
91
- // but this caused some funny behaviour because NSTask's and NSThread's don't really
92
- // like each other. Instead, just do it async.
93
-
94
- NSMutableArray *taskArguments = [NSMutableArray arrayWithObjects: @" show" , @" --numstat" , @" -M" , @" --summary" , @" --pretty=raw" , currentOID.SHA, nil ];
104
+ GTDiffFindOptionsFlags flags = GTDiffFindOptionsFlagsFindRenames;
95
105
if (![PBGitDefaults showWhitespaceDifferences ]) {
96
- [taskArguments insertObject: @" -w " atIndex: 1 ] ;
106
+ flags |= GTDiffFindOptionsFlagsIgnoreWhitespace ;
97
107
}
98
-
99
- NSFileHandle *handle = [repository handleForArguments: taskArguments];
100
- NSNotificationCenter *nc = [NSNotificationCenter defaultCenter ];
101
- // Remove notification, in case we have another one running
102
- [nc removeObserver: self name: NSFileHandleReadToEndOfFileCompletionNotification object: nil ];
103
- [nc addObserver: self selector: @selector (commitSummaryLoaded: ) name: NSFileHandleReadToEndOfFileCompletionNotification object: handle];
104
- [handle readToEndOfFileInBackgroundAndNotify ];
105
- }
106
-
107
- - (void )commitSummaryLoaded: (NSNotification *)notification
108
- {
109
- [[NSNotificationCenter defaultCenter ] removeObserver: self name: NSFileHandleReadToEndOfFileCompletionNotification object: nil ];
110
-
111
- NSData *data = [[notification userInfo ] valueForKey: NSFileHandleNotificationDataItem ];
112
- if (!data)
108
+ NSError *err = nil ;
109
+ GTDiff *d = // TODO async?
110
+ [GTDiff diffOldTree: commit.gtCommit.parents.firstObject.tree
111
+ withNewTree: commit.gtCommit.tree
112
+ inRepository: [repository gtRepo ]
113
+ options: @{
114
+ GTDiffFindOptionsFlagsKey : @(flags)
115
+ }
116
+ error: &err];
117
+ if (!d) {
118
+ NSLog (@" Commit summary diff error: %@ " , err);
113
119
return ;
114
-
115
- NSString *summary = [[NSString alloc ] initWithData: data encoding: NSUTF8StringEncoding];
116
- if (!summary)
117
- summary = [[NSString alloc ] initWithData: data encoding: NSISOLatin1StringEncoding];
118
-
119
- if (!summary)
120
+ }
121
+ NSMutableArray *fileDeltas = [NSMutableArray array ];
122
+ [d enumerateDeltasUsingBlock: ^(GTDiffDelta *_Nonnull delta, BOOL *_Nonnull stop) {
123
+ NSUInteger numLinesAdded = 0 ;
124
+ NSUInteger numLinesRemoved = 0 ;
125
+ NSError *err = nil ;
126
+ GTDiffPatch *patch = [delta generatePatch: &err];
127
+ if (patch) {
128
+ numLinesAdded = patch.addedLinesCount ;
129
+ numLinesRemoved = patch.deletedLinesCount ;
130
+ } else {
131
+ NSLog (@" generatePatch error: %@ " , err);
132
+ }
133
+ [fileDeltas addObject: @{
134
+ @" filename" : delta.newFile .path ,
135
+ @" oldFilename" : delta.oldFile .path ,
136
+ @" newFilename" : delta.newFile .path ,
137
+ @" changeType" : deltaTypeName (delta.type ),
138
+ @" numLinesAdded" : @(numLinesAdded),
139
+ @" numLinesRemoved" : @(numLinesRemoved),
140
+ @" binary" :
141
+ [NSNumber numberWithBool: (delta.flags & GTDiffFileFlagBinary) != 0 ],
142
+ }];
143
+ }];
144
+ NSDictionary *summaryDict = @{
145
+ @" filesInfo" : fileDeltas,
146
+ };
147
+ NSString *summary =
148
+ [[NSString alloc ] initWithData: [NSJSONSerialization dataWithJSONObject: summaryDict
149
+ options: 0
150
+ error: &err]
151
+ encoding: NSUTF8StringEncoding];
152
+ if (!summary) {
153
+ NSLog (@" Commit summary JSON error: %@ " , err);
120
154
return ;
155
+ }
121
156
122
- [self .view.windowScriptObject callWebScriptMethod: @" loadCommitSummary" withArguments: [ NSArray arrayWithObject: summary]];
157
+ [self .view.windowScriptObject callWebScriptMethod: @" loadCommitSummary" withArguments: @[ summary]];
123
158
124
159
// Now load the full diff
125
160
NSMutableArray *taskArguments = [NSMutableArray arrayWithObjects: @" show" , @" --pretty=raw" , @" -M" , @" --no-color" , currentOID.SHA, nil ];
0 commit comments