/
GitIndex.java
1105 lines (996 loc) · 29.9 KB
/
GitIndex.java
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Aptana Studio
* Copyright (c) 2005-2012 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the GNU Public License (GPL) v3 (with exceptions).
* Please see the license.html included with this distribution for details.
* Any modifications to this file must keep this entire header intact.
*/
package com.aptana.git.core.model;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.core.runtime.jobs.IJobManager;
import org.eclipse.core.runtime.jobs.Job;
import com.aptana.core.IFilter;
import com.aptana.core.IMap;
import com.aptana.core.ShellExecutable;
import com.aptana.core.logging.IdeLog;
import com.aptana.core.util.ArrayUtil;
import com.aptana.core.util.CollectionsUtil;
import com.aptana.core.util.IOUtil;
import com.aptana.core.util.StringUtil;
import com.aptana.git.core.GitPlugin;
import com.aptana.git.core.IDebugScopes;
public class GitIndex
{
/**
* File extensions we check against and use to assume if a file may be binary (to not show a diff/content in various
* UI views)
*/
private static final String[] BINARY_EXTENSIONS = new String[] {
".pdf", ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".o", ".class", ".zip", ".gz", ".tar", ".ico", ".so", ".jar" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$ //$NON-NLS-10$ //$NON-NLS-11$ //$NON-NLS-12$ //$NON-NLS-13$ //$NON-NLS-14$
private GitRepository repository;
private boolean amend;
/**
* The list of changed files that is a copy of the above list. Only copied at the very end of the refresh, so it
* always contains the full listing from last finished refresh call.
*/
List<ChangedFile> changedFiles;
private Object changedFilesLock = new Object();
private boolean notify;
private List<ChangedFile> files;
/**
* Service which launches the refresh commands in threads.
*/
private ExecutorService es;
/**
* A job which gathers up refresh requests and runs them.
*/
private GitIndexRefreshJob refreshJob;
GitIndex(GitRepository repository)
{
Assert.isNotNull(repository, "GitIndex requires a repository"); //$NON-NLS-1$
this.repository = repository;
this.refreshJob = new GitIndexRefreshJob(this);
this.es = Executors.newFixedThreadPool(3);
}
/**
* Used by callers who don't need to wait for it to finish so we can squash together repeated calls when they come
* rapid-fire.
*/
synchronized void scheduleBatchRefresh()
{
if (refreshJob != null)
{
refreshJob.refreshAll();
}
}
/**
* Run a refresh synchronously. FIXME Should this even be visible to callers? We should pick up file events via
* watcher to refresh whenever we really need to. This should become default visibility.
*
* @param monitor
* @return
*/
public IStatus refresh(IProgressMonitor monitor)
{
SubMonitor sub = SubMonitor.convert(monitor, 100);
try
{
return refresh(true, null, sub.newChild(100));
}
finally
{
sub.done();
}
}
/**
* If the filePaths is empty, do batch operations!
*
* @param notify
* @param filePaths
* @param monitor
* @return
*/
IStatus refresh(boolean notify, Collection<IPath> filePaths, IProgressMonitor monitor)
{
SubMonitor sub = SubMonitor.convert(monitor, 100);
if (sub.isCanceled())
{
return Status.CANCEL_STATUS;
}
this.notify = notify;
final Set<String> filePathStrings = new HashSet<String>(CollectionsUtil.map(filePaths,
new IMap<IPath, String>()
{
public String map(IPath location)
{
return location.toPortableString();
}
}));
// If we don't run this, we end up showing files as unstaged when they're no longer modified!
IStatus result;
synchronized (this)
{
repository.forceWrite(); // Do we only want to try the lock if we're in UI thread?
result = GitExecutable.instance().runInBackground(repository.workingDirectory(), "update-index", "-q", //$NON-NLS-1$ //$NON-NLS-2$
"--unmerged", "--ignore-missing", "--refresh"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
repository.exitWriteProcess();
}
if (result == null) // couldn't even execute!
{
return new Status(IStatus.ERROR, GitPlugin.getPluginId(), "Failed to execute git update-index"); //$NON-NLS-1$
}
if (!result.isOK())
{
IdeLog.logError(GitPlugin.getDefault(), "Unable to run update-index: " + result.getMessage()); //$NON-NLS-1$
return result;
}
Set<Callable<IStatus>> jobs = new HashSet<Callable<IStatus>>(3);
jobs.add(new UntrackedFilesRefreshJob(this, filePathStrings));
jobs.add(new UnstagedFilesRefreshJob(this, filePathStrings));
jobs.add(new StagedFilesRefreshJob(this, filePathStrings));
// Last chance to cancel...
if (monitor != null && monitor.isCanceled())
{
return Status.CANCEL_STATUS;
}
// Now create a new temporary list so we can build it up...
this.files = Collections.synchronizedList(new ArrayList<ChangedFile>());
// Schedule all the jobs
MultiStatus errors = new MultiStatus(GitPlugin.PLUGIN_ID, 1,
"Errors occurred while grabbing changed file listings", null); //$NON-NLS-1$
try
{
List<Future<IStatus>> futures = es.invokeAll(jobs);
// Now wait for them to finish
for (Future<IStatus> future : futures)
{
while (!future.isDone())
{
if (monitor != null && monitor.isCanceled())
{
future.cancel(true);
}
Thread.yield();
}
// When done, get their result
try
{
IStatus futureResult = future.get();
if (!futureResult.isOK())
{
errors.merge(futureResult);
}
}
catch (CancellationException ce)
{
// ignore
}
catch (ExecutionException e)
{
Throwable t = e.getCause();
errors.merge(new Status(IStatus.ERROR, GitPlugin.PLUGIN_ID, t.getMessage(), t));
}
}
}
catch (InterruptedException e)
{
IdeLog.logError(GitPlugin.getDefault(), e);
}
// Copy the last full list of changed files we built up on refresh. Used to pass along the delta
Collection<ChangedFile> preRefresh;
synchronized (this.changedFilesLock)
{
if (this.changedFiles != null)
{
preRefresh = new ArrayList<ChangedFile>(this.changedFiles.size());
for (ChangedFile file : this.changedFiles)
{
preRefresh.add(new ChangedFile(file));
}
}
else
{
preRefresh = new ArrayList<ChangedFile>(0);
}
// Now wipe any existing ChangedFile entries for any of the filePaths and add the ones we generated in
// dictionary
if (CollectionsUtil.isEmpty(filePathStrings))
{
this.changedFiles = new ArrayList<ChangedFile>();
}
else
{
this.changedFiles = CollectionsUtil.filter(this.changedFiles, new IFilter<ChangedFile>()
{
public boolean include(ChangedFile item)
{
return !filePathStrings.contains(item.portablePath);
}
});
}
if (!CollectionsUtil.isEmpty(this.files))
{
this.changedFiles.addAll(this.files);
}
}
// Don't hold onto temp list in memory!
this.files = null;
postIndexChange(preRefresh, this.changedFiles);
sub.done();
if (!errors.isOK())
{
return errors;
}
return Status.OK_STATUS;
}
private void postIndexChange(Collection<ChangedFile> preChangeFiles, Collection<ChangedFile> postChangeFiles)
{
if (this.notify)
{
this.repository.fireIndexChangeEvent(preChangeFiles, postChangeFiles);
}
else
{
this.notify = true;
}
}
/**
* Makes a copy of the internal list of changed files so that iterating won't ever result in a
* ConcurrentModificationException. try to avoid use if possible, since a deep copy is made which can be expensive.
* This method populates the changedFiles collection lazily on first demand.
*
* @return
*/
public List<ChangedFile> changedFiles()
{
boolean isNull = false;
synchronized (this.changedFilesLock)
{
isNull = (this.changedFiles == null);
}
if (isNull)
{
// Don't want to call back to fireIndexChangeEvent yet!
IStatus status = refresh(false, null, new NullProgressMonitor());
if (!status.isOK())
{
IdeLog.logError(GitPlugin.getDefault(), status.getMessage());
return Collections.emptyList();
}
}
synchronized (this.changedFilesLock)
{
if (this.changedFiles == null)
{
return Collections.emptyList();
}
List<ChangedFile> copy = new ArrayList<ChangedFile>(this.changedFiles.size());
for (ChangedFile file : this.changedFiles)
{
copy.add(new ChangedFile(file));
}
return copy;
}
}
public IStatus stageFiles(Collection<ChangedFile> stageFiles)
{
if (CollectionsUtil.isEmpty(stageFiles))
{
// no-op
return Status.OK_STATUS;
}
StringBuffer input = new StringBuffer(stageFiles.size() * stageFiles.iterator().next().getPath().length());
for (ChangedFile file : stageFiles)
{
input.append(file.getPath()).append('\n');
}
@SuppressWarnings("nls")
IStatus result = repository.executeWithInput(input.toString(), "update-index", "--add", "--remove", "--stdin");
if (result == null)
{
return new Status(IStatus.ERROR, GitPlugin.PLUGIN_ID, "Failed to stage files. Process failed to run."); //$NON-NLS-1$;
}
if (!result.isOK())
{
IdeLog.logError(GitPlugin.getDefault(),
MessageFormat.format("Failed to stage files: {0}", result.getMessage()), IDebugScopes.DEBUG); //$NON-NLS-1$
return result;
}
ArrayList<ChangedFile> preFiles = new ArrayList<ChangedFile>(stageFiles.size());
// Update the staged/unstaged flags in the passed in copy of changed files, and our internal list of changed
// files.
for (ChangedFile file : stageFiles)
{
preFiles.add(new ChangedFile(file));
synchronized (changedFilesLock)
{
if (this.changedFiles != null)
{
int index = Collections.binarySearch(this.changedFiles, file);
if (index >= 0)
{
ChangedFile orig = this.changedFiles.get(index);
orig.hasUnstagedChanges = false;
orig.hasStagedChanges = true;
}
}
}
file.hasUnstagedChanges = false;
file.hasStagedChanges = true;
}
preFiles.trimToSize();
postIndexChange(preFiles, stageFiles);
return result;
}
public IStatus unstageFiles(Collection<ChangedFile> unstageFiles)
{
if (CollectionsUtil.isEmpty(unstageFiles))
{
// no-op, return OK
return Status.OK_STATUS;
}
StringBuilder input = new StringBuilder();
for (ChangedFile file : unstageFiles)
{
input.append(file.indexInfo());
}
IStatus result = repository.executeWithInput(input.toString(), "update-index", "-z", "--index-info"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
if (result == null)
{
return new Status(IStatus.ERROR, GitPlugin.PLUGIN_ID, "Failed to unstage files. Process failed to run."); //$NON-NLS-1$
}
if (!result.isOK())
{
IdeLog.logError(GitPlugin.getDefault(),
MessageFormat.format("Failed to stage files: {0}", result.getMessage()), IDebugScopes.DEBUG); //$NON-NLS-1$
return result;
}
// Update the staged/unstaged flags in the passed in copy of changed files, and our internal list of changed
// files.
ArrayList<ChangedFile> preFiles = new ArrayList<ChangedFile>(unstageFiles.size());
for (ChangedFile file : unstageFiles)
{
preFiles.add(new ChangedFile(file));
synchronized (this.changedFilesLock)
{
if (this.changedFiles != null)
{
int index = Collections.binarySearch(this.changedFiles, file);
if (index >= 0)
{
ChangedFile orig = this.changedFiles.get(index);
orig.hasUnstagedChanges = true;
orig.hasStagedChanges = false;
}
}
}
file.hasUnstagedChanges = true;
file.hasStagedChanges = false;
}
preFiles.trimToSize();
postIndexChange(preFiles, unstageFiles);
return result;
}
public IStatus discardChangesForFiles(Collection<ChangedFile> discardFiles)
{
StringBuilder input = new StringBuilder();
for (ChangedFile file : discardFiles)
{
input.append(file.getPath()).append("\0"); //$NON-NLS-1$
}
IStatus result = repository.executeWithInput(input.toString(),
"checkout-index", "--index", "--quiet", "--force", "-z", "--stdin"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
if (result == null)
{
return new Status(IStatus.ERROR, GitPlugin.PLUGIN_ID, "Failed to revert files. Process failed to run."); //$NON-NLS-1$
}
if (!result.isOK())
{
IdeLog.logError(GitPlugin.getDefault(),
MessageFormat.format("Failed to revert files: {0}", result.getMessage()), IDebugScopes.DEBUG); //$NON-NLS-1$
return result;
}
ArrayList<ChangedFile> preFiles = new ArrayList<ChangedFile>(discardFiles.size());
for (ChangedFile file : discardFiles)
{
preFiles.add(new ChangedFile(file));
file.hasUnstagedChanges = false;
}
preFiles.trimToSize();
postIndexChange(preFiles, discardFiles);
return result;
}
public boolean commit(String commitMessage)
{
boolean success = doCommit(commitMessage);
if (!success)
{
return false;
}
repository.hasChanged();
if (amend)
{
this.amend = false;
}
else
{
refresh(new NullProgressMonitor());
}
return true;
}
private boolean doCommit(String commitMessage)
{
if (Platform.OS_WIN32.equals(Platform.getOS()))
{
commitMessage = commitMessage.replace("\"", "\\\""); //$NON-NLS-1$ //$NON-NLS-2$
}
IStatus result = repository.execute(GitRepository.ReadWrite.WRITE, repository.workingDirectory(),
ShellExecutable.getEnvironment(repository.workingDirectory()), "commit", "-m", commitMessage); //$NON-NLS-1$ //$NON-NLS-2$
return result != null && result.isOK();
}
/**
* Returns the summary of all commits between two revisions.
*
* @param sha1
* SHA commit hash, or name of a ref (i.e. 'refs/heads/master')
* @param sha2
* SHA commit hash, or name of a ref (i.e. 'refs/heads/master')
* @return
*/
String[] commitsBetween(String sha1, String sha2)
{
// Speed up the most common case of when the two SHAs are the same for refs!
if (sha1.startsWith(GitRef.REFS))
{
sha1 = repository.toSHA(GitRef.refFromString(sha1));
}
if (sha2.startsWith(GitRef.REFS))
{
sha2 = repository.toSHA(GitRef.refFromString(sha2));
}
if (sha1.equals(sha2))
{
return ArrayUtil.NO_STRINGS;
}
IStatus status = repository.execute(GitRepository.ReadWrite.READ, "log", "--pretty=format:\"%s\"", //$NON-NLS-1$ //$NON-NLS-2$
sha1 + ".." + sha2); //$NON-NLS-1$
if (status == null || !status.isOK() || status.getMessage().trim().length() == 0)
{
return ArrayUtil.NO_STRINGS;
}
return status.getMessage().split("[\r\n]+"); //$NON-NLS-1$ // $codepro.audit.disable platformSpecificLineSeparator
}
/**
* @param file
* the ChangedFile to generate a diff for.
* @param staged
* Whether the file is staged or not
* @param contextLines
* number of lines to show context for. default for underlying command is 3.
* @return
*/
public String diffForFile(ChangedFile file, boolean staged, int contextLines)
{
if (hasBinaryAttributes(file))
{
return Messages.GitIndex_BinaryDiff_Message;
}
String parameter = "-U" + contextLines; //$NON-NLS-1$
if (staged)
{
String indexPath = ":0:" + file.portablePath; //$NON-NLS-1$
if (file.status == ChangedFile.Status.NEW)
{
IStatus status = repository.execute(GitRepository.ReadWrite.READ, "show", indexPath); //$NON-NLS-1$
return status.getMessage();
}
IStatus result = repository.execute(GitRepository.ReadWrite.READ, "diff-index", parameter, "--cached", //$NON-NLS-1$ //$NON-NLS-2$
GitRepository.HEAD, "--", file.portablePath); //$NON-NLS-1$
if (result == null || !result.isOK())
{
return null;
}
return result.getMessage();
}
// unstaged
if (file.status == ChangedFile.Status.NEW)
{
try
{
return IOUtil.read(new FileInputStream(workingDirectory().append(file.portablePath).toFile()),
IOUtil.UTF_8); // $codepro.audit.disable
// closeWhereCreated
}
catch (FileNotFoundException e)
{
return null;
}
}
IStatus result = repository.execute(GitRepository.ReadWrite.READ,
"diff-files", parameter, "--", file.portablePath); //$NON-NLS-1$ //$NON-NLS-2$
return result.getMessage();
}
public boolean hasBinaryAttributes(ChangedFile file)
{
IStatus result = repository.execute(GitRepository.ReadWrite.READ, "check-attr", "binary", file.getPath()); //$NON-NLS-1$ //$NON-NLS-2$
String output = result.getMessage();
output = output.trim();
if (output.endsWith("binary: set")) //$NON-NLS-1$
{
return true;
}
if (output.endsWith("binary: unset")) //$NON-NLS-1$
{
return false;
}
if (output.endsWith("binary: unspecified")) //$NON-NLS-1$
{
// try common filename-extensions
for (String extension : BINARY_EXTENSIONS)
{
if (file.getPath().endsWith(extension))
{
return true;
}
}
}
return false;
}
/**
* For use in telling if a given resource is a changed file, or is a folder containing changes underneath it.
*
* @param resource
* @return
*/
protected boolean resourceOrChildHasChanges(IResource resource)
{
List<ChangedFile> changedFiles = changedFiles();
if (CollectionsUtil.isEmpty(changedFiles))
{
return false;
}
IPath workingDirectory = repository.workingDirectory();
IPath resourcePath = resource.getLocation();
for (ChangedFile changedFile : changedFiles)
{
IPath fullPath = workingDirectory.append(changedFile.getPath()).makeAbsolute();
if (resourcePath.isPrefixOf(fullPath))
{
return true;
}
}
return false;
}
public boolean isDirty()
{
return !changedFiles().isEmpty();
}
protected boolean hasUnresolvedMergeConflicts()
{
List<ChangedFile> changedFiles = changedFiles();
if (CollectionsUtil.isEmpty(changedFiles))
{
return false;
}
for (ChangedFile changedFile : changedFiles)
{
if (changedFile.hasUnmergedChanges() && changedFile.hasUnstagedChanges())
{
return true;
}
}
return false;
}
public Set<IResource> getChangedResources()
{
Set<IResource> resources = new HashSet<IResource>();
List<ChangedFile> changedFiles = changedFiles();
if (!CollectionsUtil.isEmpty(changedFiles))
{
for (ChangedFile changedFile : changedFiles)
{
IResource resource = getResourceForChangedFile(changedFile);
if (resource != null)
{
resources.add(resource);
}
}
}
return resources;
}
IFile getResourceForChangedFile(ChangedFile changedFile)
{
return ResourcesPlugin.getWorkspace().getRoot()
.getFileForLocation(workingDirectory().append(changedFile.getPath()));
}
protected ChangedFile getChangedFileForResource(IResource resource)
{
if (resource == null || resource.getLocationURI() == null)
{
return null;
}
IPath resourcePath = resource.getLocation();
List<ChangedFile> changedFiles = changedFiles();
if (!CollectionsUtil.isEmpty(changedFiles))
{
for (ChangedFile changedFile : changedFiles)
{
IPath fullPath = workingDirectory().append(changedFile.getPath());
if (resourcePath.equals(fullPath))
{
return changedFile;
}
}
}
return null;
}
private IPath workingDirectory()
{
return repository.workingDirectory();
}
/**
* Gets the list of changed files that are underneath the given container.
*
* @param container
* @return
*/
protected List<ChangedFile> getChangedFilesForContainer(IContainer container)
{
if (container == null || container.getLocationURI() == null)
{
return Collections.emptyList();
}
List<ChangedFile> changedFiles = changedFiles();
if (CollectionsUtil.isEmpty(changedFiles))
{
return Collections.emptyList();
}
IPath resourcePath = container.getLocation();
List<ChangedFile> filtered = new ArrayList<ChangedFile>();
IPath workingDirectory = repository.workingDirectory();
for (ChangedFile changedFile : changedFiles)
{
IPath fullPath = workingDirectory.append(changedFile.getPath()).makeAbsolute();
if (resourcePath.isPrefixOf(fullPath))
{
filtered.add(changedFile);
}
}
return filtered;
}
/**
* Aschedules a job to refresh the passed in filepaths. paths are expected to be relative to the repo root/working
* dir!
*
* @param paths
* A {@link Collection} of relative {@link IPath} - relative to the {@link #workingDirectory()}
*/
public void refreshAsync(final Collection<IPath> paths)
{
if (refreshJob != null)
{
refreshJob.refresh(paths);
}
}
private abstract static class FilesRefreshJob implements Callable<IStatus>
{
protected GitRepository repo;
protected GitIndex index;
protected Set<String> filePaths;
private FilesRefreshJob(GitIndex index, Set<String> filePaths)
{
this.index = index;
this.repo = index.repository;
this.filePaths = filePaths;
}
protected List<String> linesFromNotification(String string)
{
// FIXME: throw an error?
if (string == null)
{
return Collections.emptyList();
}
// Strip trailing null
if (string.endsWith("\0")) //$NON-NLS-1$
{
string = string.substring(0, string.length() - 1);
}
if (string.length() == 0)
{
return Collections.emptyList();
}
return StringUtil.tokenize(string, "\0"); //$NON-NLS-1$
}
protected Map<String, List<String>> dictionaryForLines(List<String> lines)
{
Map<String, List<String>> dictionary = new HashMap<String, List<String>>(lines.size() / 2);
// Fill the dictionary with the new information. These lines are in the form of:
// :00000 :0644 OTHER INDEX INFORMATION
// Filename
Assert.isTrue(lines.size() % 2 == 0, "Lines must have an even number of lines: " + lines); //$NON-NLS-1$
Iterator<String> iter = lines.iterator();
while (iter.hasNext())
{
String fileStatus = iter.next();
String fileName = iter.next();
dictionary.put(fileName, StringUtil.tokenize(fileStatus, " ")); //$NON-NLS-1$
}
return dictionary;
}
protected void addFilesFromDictionary(Map<String, List<String>> dictionary, boolean staged, boolean tracked)
{
if (index.files == null)
{
return;
}
// Iterate over all existing files
synchronized (index.files)
{
for (ChangedFile file : index.files)
{
synchronized (dictionary)
{
List<String> fileStatus = dictionary.get(file.portablePath);
// Object found, this is still a cached / uncached thing
if (fileStatus != null)
{
if (tracked)
{
String mode = fileStatus.get(0).substring(1);
String sha = fileStatus.get(2);
file.commitBlobSHA = sha;
file.commitBlobMode = mode;
if (staged)
{
file.hasStagedChanges = true;
}
else
{
file.hasUnstagedChanges = true;
}
if (fileStatus.get(4).equals("D")) //$NON-NLS-1$
{
file.status = ChangedFile.Status.DELETED;
}
else if (fileStatus.get(4).equals("U")) //$NON-NLS-1$
{
file.status = ChangedFile.Status.UNMERGED;
}
}
else
{
// Untracked file, set status to NEW, only unstaged changes
file.hasStagedChanges = false;
file.hasUnstagedChanges = true;
file.status = ChangedFile.Status.NEW;
}
// We handled this file, remove it from the dictionary
dictionary.remove(file.portablePath);
}
else
{
// Object not found in the dictionary, so let's reset its appropriate
// change (stage or untracked) if necessary.
// Staged dictionary, so file does not have staged changes
if (staged)
{
file.hasStagedChanges = false;
}
// Tracked file does not have unstaged changes, file is not new,
// so we can set it to No. (If it would be new, it would not
// be in this dictionary, but in the "other dictionary").
else if (tracked && file.status != ChangedFile.Status.NEW)
{
file.hasUnstagedChanges = false;
}
// Unstaged, untracked dictionary ("Other" files), and file
// is indicated as new (which would be untracked), so let's
// remove it
else if (!tracked && file.status == ChangedFile.Status.NEW)
{
file.hasUnstagedChanges = false;
}
}
}
}
}
// Do new files only if necessary
if (dictionary.isEmpty())
{
return;
}
// All entries left in the dictionary haven't been accounted for
// above, so we need to add them to the "files" array
synchronized (dictionary)
{
for (String path : dictionary.keySet())
{
List<String> fileStatus = dictionary.get(path);
ChangedFile.Status status = ChangedFile.Status.MODIFIED;
if (fileStatus.get(4).equals("D")) //$NON-NLS-1$
{
status = ChangedFile.Status.DELETED;
}
else if (fileStatus.get(4).equals("U")) //$NON-NLS-1$
{
status = ChangedFile.Status.UNMERGED;
}
else if (fileStatus.get(0).equals(":000000")) //$NON-NLS-1$
{
status = ChangedFile.Status.NEW;
}
else
{
status = ChangedFile.Status.MODIFIED;
}
ChangedFile file = new ChangedFile(path, status);
if (tracked)
{
file.commitBlobMode = fileStatus.get(0).substring(1);
file.commitBlobSHA = fileStatus.get(2);
}
file.hasStagedChanges = staged;
file.hasUnstagedChanges = !staged;
synchronized (index.files)
{
index.files.add(file);
}
}
}
}
}
private static final class StagedFilesRefreshJob extends FilesRefreshJob
{
private StagedFilesRefreshJob(GitIndex index, Set<String> filePaths)
{
super(index, filePaths);
}
public IStatus call() throws Exception
{
// HEAD vs filesystem
List<String> args = CollectionsUtil.newList("diff-index", "--cached", //$NON-NLS-1$ //$NON-NLS-2$
"-z", GitRepository.HEAD); //$NON-NLS-1$
if (!CollectionsUtil.isEmpty(filePaths))
{
args.add("--"); //$NON-NLS-1$
args.addAll(filePaths);
}
IStatus result = repo.execute(GitRepository.ReadWrite.READ, args.toArray(new String[args.size()]));
if (result != null && result.isOK())
{
readStagedFiles(result.getMessage());
}
return Status.OK_STATUS;
}
private void readStagedFiles(String string)
{
List<String> lines = linesFromNotification(string);
Map<String, List<String>> dic = dictionaryForLines(lines);
addFilesFromDictionary(dic, true, true);
}
}