-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCommitDiffWorkaround.java
More file actions
406 lines (347 loc) · 14.9 KB
/
CommitDiffWorkaround.java
File metadata and controls
406 lines (347 loc) · 14.9 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package implementation.lineStatusTracker;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorKind;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vcs.VcsException;
import com.intellij.openapi.vcs.changes.Change;
import com.intellij.openapi.vcs.changes.ChangeListManager;
import com.intellij.openapi.vcs.changes.ContentRevision;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import system.Defs;
import java.awt.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Workaround for IntelliJ IDEA commit panel diff issue.
*
* Problem: When viewing diffs in the commit panel, IDEA shows the diff between the current
* working copy and the custom base revision set by Git Scope. This causes incorrect diffs
* to be displayed - the commit panel should always show diffs against HEAD (the last commit),
* not against a custom branch base.
*
* Solution: This class detects when commit panel diff editors are active and temporarily
* switches the base revision to HEAD. When the user switches away from the commit panel,
* it restores the custom base revision.
*/
public class CommitDiffWorkaround implements Disposable {
private static final Logger LOG = Defs.getLogger(CommitDiffWorkaround.class);
private static final int ACTIVATION_DELAY_MS = 300;
private final Project project;
private final AtomicBoolean disposing = new AtomicBoolean(false);
// Map: Document -> Set of commit diff Editors for that document
private final Map<Document, Set<Editor>> commitDiffEditors = new HashMap<>();
// Track which documents are currently showing HEAD base (active in commit diff)
private final Set<Document> activeCommitDiffs = new HashSet<>();
// Callback interface for base revision switching
private final BaseRevisionSwitcher baseRevisionSwitcher;
/**
* Interface for switching base revisions.
*/
public interface BaseRevisionSwitcher {
/**
* Switch to HEAD base revision for the given document.
* @param document The document to switch
* @param headContent The HEAD revision content
*/
void switchToHeadBase(@NotNull Document document, @NotNull String headContent);
/**
* Switch to custom base revision for the given document.
* @param document The document to switch
* @param customContent The custom base revision content
*/
void switchToCustomBase(@NotNull Document document, @NotNull String customContent);
/**
* Get the cached HEAD content for a document, or null if not cached.
*/
@Nullable String getCachedHeadContent(@NotNull Document document);
/**
* Get the cached custom base content for a document, or null if not cached.
*/
@Nullable String getCachedCustomBaseContent(@NotNull Document document);
/**
* Cache HEAD content for a document.
*/
void cacheHeadContent(@NotNull Document document, @NotNull String headContent);
/**
* Check if document is tracked and held.
*/
boolean isTracked(@NotNull Document document);
/**
* Mark document as showing HEAD base.
*/
void markShowingHeadBase(@NotNull Document document, boolean showing);
/**
* Check if document is showing HEAD base.
*/
boolean isShowingHeadBase(@NotNull Document document);
}
public CommitDiffWorkaround(@NotNull Project project, @NotNull BaseRevisionSwitcher switcher) {
this.project = project;
this.baseRevisionSwitcher = switcher;
}
@Override
public void dispose() {
disposing.set(true);
synchronized (this) {
commitDiffEditors.clear();
activeCommitDiffs.clear();
}
}
/**
* Check if an editor is a commit panel diff editor.
* Call this when a DIFF editor is created to determine if it needs special handling.
*/
public boolean isCommitPanelDiff(@NotNull Editor editor) {
if (editor.getEditorKind() != EditorKind.DIFF) {
return false;
}
return isInCommitToolWindowHierarchy(editor);
}
/**
* Handle commit panel diff editor created.
* Call this when a commit panel diff editor is created.
*/
public void handleCommitDiffEditorCreated(@NotNull Editor editor) {
Document doc = editor.getDocument();
VirtualFile file = FileDocumentManager.getInstance().getFile(doc);
if (file == null) {
return;
}
synchronized (this) {
// Register the editor
commitDiffEditors.computeIfAbsent(doc, k -> new HashSet<>()).add(editor);
// Pre-cache HEAD content if tracked
if (baseRevisionSwitcher.isTracked(doc)) {
if (baseRevisionSwitcher.getCachedHeadContent(doc) == null) {
String headContent = fetchHeadRevisionContent(file);
if (headContent != null) {
baseRevisionSwitcher.cacheHeadContent(doc, headContent);
}
}
}
}
// Check if a commit diff editor is currently selected - if so, activate HEAD base
scheduleActivationIfCommitDiffSelected();
}
/**
* Handle commit panel diff editor released (closed).
* Call this when a commit panel diff editor is closed.
*/
public void handleCommitDiffEditorReleased(@NotNull Editor editor) {
Document doc = editor.getDocument();
VirtualFile file = FileDocumentManager.getInstance().getFile(doc);
if (file == null) {
return;
}
synchronized (this) {
// Unregister the editor
Set<Editor> editors = commitDiffEditors.get(doc);
if (editors != null) {
editors.remove(editor);
if (editors.isEmpty()) {
commitDiffEditors.remove(doc);
}
}
// Remove from active tracking
activeCommitDiffs.remove(doc);
// Restore custom base if no longer showing in any commit diff
if (!commitDiffEditors.containsKey(doc) && baseRevisionSwitcher.isShowingHeadBase(doc)) {
restoreCustomBaseForDocument(doc);
}
}
}
/**
* Handle editor selection changed to a commit diff editor.
* Call this when the user switches to a commit panel diff tab.
*/
public void handleSwitchedToCommitDiff() {
// Delay activation to allow diff window to fully render before switching base
com.intellij.util.Alarm alarm = new com.intellij.util.Alarm(this);
alarm.addRequest(() -> {
if (disposing.get()) return;
activateHeadBaseForAllCommitDiffs();
}, ACTIVATION_DELAY_MS);
}
/**
* Handle editor selection changed away from a commit diff editor.
* Call this when the user switches away from a commit panel diff tab.
*
* Note: This safely restores ALL tracked documents. Documents that still have
* commit diff editors open will be skipped by restoreCustomBaseForDocument().
*/
public void handleSwitchedAwayFromCommitDiff() {
restoreCustomBaseForAllDocuments();
}
/**
* Check if document should skip custom base update (because it's showing HEAD base).
* Call this before applying a custom base update to check if it should be skipped.
*/
public boolean shouldSkipCustomBaseUpdate(@NotNull Document document) {
return baseRevisionSwitcher.isShowingHeadBase(document);
}
/**
* Check if there are commit diff editors open for the given document.
* Used to determine if a document's tracker should be kept alive.
*/
public boolean hasCommitDiffEditorsFor(@NotNull Document document) {
synchronized (this) {
Set<Editor> editors = commitDiffEditors.get(document);
return editors != null && !editors.isEmpty();
}
}
// Private implementation methods
private boolean isInCommitToolWindowHierarchy(@NotNull Editor editor) {
try {
Component parent = editor.getComponent();
// Walk up component hierarchy looking for DiffRequestProcessor
int depth = 0;
while (parent != null && depth < 50) {
if (parent.getClass().getName().contains("DiffRequestProcessor")) {
return true;
}
parent = parent.getParent();
depth++;
}
} catch (Exception e) {
LOG.warn("Error checking commit tool window hierarchy", e);
}
return false;
}
private void scheduleActivationIfCommitDiffSelected() {
ApplicationManager.getApplication().invokeLater(() -> {
if (disposing.get()) return;
FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
FileEditor selectedEditor = fileEditorManager.getSelectedEditor();
if (selectedEditor != null &&
selectedEditor.getClass().getSimpleName().equals("BackendDiffRequestProcessorEditor")) {
// Delay activation to allow diff window to fully render
com.intellij.util.Alarm alarm = new com.intellij.util.Alarm(this);
alarm.addRequest(() -> {
if (disposing.get()) return;
activateHeadBaseForAllCommitDiffs();
}, ACTIVATION_DELAY_MS);
}
});
}
/**
* Activate HEAD base for all documents that have commit diff editors.
*
* Design note: This activates HEAD base for ALL documents with commit diffs, not just
* the currently visible one. This is intentional - when viewing commit diffs, we enter
* "commit review mode" where all files should show diffs against HEAD, not custom base.
* This provides consistent behavior and avoids confusion when switching between files.
*
* The restoration logic (via hasCommitDiffEditorsFor checks) ensures that when commit
* diffs are closed, only documents without any remaining commit diff editors get their
* custom base restored.
*/
private void activateHeadBaseForAllCommitDiffs() {
synchronized (this) {
for (Map.Entry<Document, Set<Editor>> entry : commitDiffEditors.entrySet()) {
Document doc = entry.getKey();
Set<Editor> diffEditors = entry.getValue();
if (diffEditors.isEmpty()) {
continue;
}
VirtualFile file = FileDocumentManager.getInstance().getFile(doc);
if (file == null) {
continue;
}
if (!baseRevisionSwitcher.isTracked(doc)) {
continue;
}
if (baseRevisionSwitcher.isShowingHeadBase(doc)) {
continue;
}
// Get HEAD content (fetch if not cached)
String headContent = baseRevisionSwitcher.getCachedHeadContent(doc);
if (headContent == null) {
headContent = fetchHeadRevisionContent(file);
if (headContent != null) {
baseRevisionSwitcher.cacheHeadContent(doc, headContent);
}
}
if (headContent != null) {
baseRevisionSwitcher.markShowingHeadBase(doc, true);
activeCommitDiffs.add(doc);
String finalHeadContent = headContent;
ApplicationManager.getApplication().invokeLater(() -> {
if (disposing.get()) return;
baseRevisionSwitcher.switchToHeadBase(doc, finalHeadContent);
});
}
}
}
}
private void restoreCustomBaseForAllDocuments() {
synchronized (this) {
for (Document doc : new HashSet<>(activeCommitDiffs)) {
restoreCustomBaseForDocument(doc);
}
}
}
private void restoreCustomBaseForDocument(@NotNull Document doc) {
VirtualFile file = FileDocumentManager.getInstance().getFile(doc);
if (file == null) {
return;
}
if (!baseRevisionSwitcher.isShowingHeadBase(doc)) {
return;
}
// Fix Hole #2: Don't restore if commit diff editors are still open for this document
if (hasCommitDiffEditorsFor(doc)) {
return;
}
String customContent = baseRevisionSwitcher.getCachedCustomBaseContent(doc);
if (customContent != null) {
baseRevisionSwitcher.markShowingHeadBase(doc, false);
activeCommitDiffs.remove(doc);
ApplicationManager.getApplication().invokeLater(() -> {
if (disposing.get()) return;
baseRevisionSwitcher.switchToCustomBase(doc, customContent);
});
}
}
private String fetchHeadRevisionContent(@NotNull VirtualFile file) {
try {
if (project == null || project.isDisposed()) {
return null;
}
ChangeListManager changeListManager = ChangeListManager.getInstance(project);
Collection<Change> allChanges = changeListManager.getAllChanges();
// Find the change for this file
for (Change change : allChanges) {
VirtualFile changeFile = change.getVirtualFile();
if (changeFile != null && changeFile.equals(file)) {
// The "before" revision is HEAD
ContentRevision beforeRevision = change.getBeforeRevision();
if (beforeRevision != null) {
String content = beforeRevision.getContent();
if (content != null) {
return StringUtil.convertLineSeparators(content);
}
}
}
}
// File is not modified - current content IS HEAD
Document doc = FileDocumentManager.getInstance().getDocument(file);
if (doc != null) {
return doc.getText();
}
return null;
} catch (Exception e) {
LOG.warn("Error fetching HEAD revision content for " + file.getName(), e);
return null;
}
}
}