This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
repository.test.js
1653 lines (1369 loc) · 62.7 KB
/
repository.test.js
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
import fs from 'fs';
import path from 'path';
import dedent from 'dedent-js';
import temp from 'temp';
import util from 'util';
import compareSets from 'compare-sets';
import isEqual from 'lodash.isequal';
import {CompositeDisposable, Disposable} from 'event-kit';
import Repository from '../../lib/models/repository';
import {expectedDelegates} from '../../lib/models/repository-states';
import FileSystemChangeObserver from '../../lib/models/file-system-change-observer';
import {
cloneRepository, setUpLocalAndRemoteRepositories, getHeadCommitOnRemote,
assertDeepPropertyVals, assertEqualSortedArraysByKey,
} from '../helpers';
import {getPackageRoot, writeFile, copyFile, fsStat, getTempDir} from '../../lib/helpers';
describe('Repository', function() {
it('delegates all state methods', function() {
const missing = expectedDelegates.filter(delegateName => {
return Repository.prototype[delegateName] === undefined;
});
// For convenience, write the delegate list to the console when there are any missing (in addition to failing the
// test.)
if (missing.length > 0) {
const formatted = util.inspect(expectedDelegates);
// eslint-disable-next-line no-console
console.log(`Expected delegates for your copy-and-paste convenience:\n\n---\n${formatted}\n---\n`);
}
assert.lengthOf(missing, 0);
});
describe('initial states', function() {
let repository;
afterEach(async function() {
repository && await repository.destroy();
});
it('begins in the Loading state with a working directory', async function() {
const workdir = await cloneRepository('three-files');
repository = new Repository(workdir);
assert.isTrue(repository.isInState('Loading'));
});
it('begins in the Absent state with .absent()', function() {
repository = Repository.absent();
assert.isTrue(repository.isInState('Absent'));
});
it('begins in an AbsentGuess state with .absentGuess()', function() {
repository = Repository.absentGuess();
assert.isTrue(repository.isInState('AbsentGuess'));
assert.isFalse(repository.showGitTabLoading());
assert.isTrue(repository.showGitTabInit());
});
it('begins in a LoadingGuess state with .guess()', function() {
repository = Repository.loadingGuess();
assert.isTrue(repository.isInState('LoadingGuess'));
assert.isTrue(repository.showGitTabLoading());
assert.isFalse(repository.showGitTabInit());
});
});
describe('getGitDirectoryPath', function() {
it('returns the correct git directory path', async function() {
const workingDirPath = await cloneRepository('three-files');
const workingDirPathWithGitFile = await getTempDir();
await writeFile(path.join(workingDirPathWithGitFile, '.git'), `gitdir: ${path.join(workingDirPath, '.git')}`);
const repository = new Repository(workingDirPath);
assert.equal(repository.getGitDirectoryPath(), path.join(workingDirPath, '.git'));
const repositoryWithGitFile = new Repository(workingDirPathWithGitFile);
await assert.async.equal(repositoryWithGitFile.getGitDirectoryPath(), path.join(workingDirPath, '.git'));
});
it('returns null for absent/loading repositories', function() {
const repo = Repository.absent();
repo.getGitDirectoryPath();
});
});
describe('init', function() {
it('creates a repository in the given dir and returns the repository', async function() {
const soonToBeRepositoryPath = fs.realpathSync(temp.mkdirSync());
const repo = new Repository(soonToBeRepositoryPath);
assert.isTrue(repo.isLoading());
await repo.getLoadPromise();
assert.isTrue(repo.isEmpty());
await repo.init();
assert.isTrue(repo.isPresent());
assert.equal(repo.getWorkingDirectoryPath(), soonToBeRepositoryPath);
});
});
describe('clone', function() {
it('clones a repository from a URL to a directory and returns the repository', async function() {
const upstreamPath = await cloneRepository('three-files');
const destDir = fs.realpathSync(temp.mkdirSync());
const repo = new Repository(destDir);
const clonePromise = repo.clone(upstreamPath);
assert.isTrue(repo.isLoading());
await clonePromise;
assert.isTrue(repo.isPresent());
assert.equal(repo.getWorkingDirectoryPath(), destDir);
});
it('clones a repository when the directory does not exist yet', async function() {
const upstreamPath = await cloneRepository('three-files');
const parentDir = fs.realpathSync(temp.mkdirSync());
const destDir = path.join(parentDir, 'subdir');
const repo = new Repository(destDir);
await repo.clone(upstreamPath, destDir);
assert.isTrue(repo.isPresent());
assert.equal(repo.getWorkingDirectoryPath(), destDir);
});
});
describe('staging and unstaging files', function() {
it('can stage and unstage modified files', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'a.txt'), 'qux\nfoo\nbar\n', 'utf8');
const [patch] = await repo.getUnstagedChanges();
const filePath = patch.filePath;
await repo.stageFiles([filePath]);
repo.refresh();
assert.deepEqual(await repo.getUnstagedChanges(), []);
assert.deepEqual(await repo.getStagedChanges(), [patch]);
await repo.unstageFiles([filePath]);
repo.refresh();
assert.deepEqual(await repo.getUnstagedChanges(), [patch]);
assert.deepEqual(await repo.getStagedChanges(), []);
});
it('can stage and unstage removed files', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.unlinkSync(path.join(workingDirPath, 'subdir-1', 'b.txt'));
const [patch] = await repo.getUnstagedChanges();
const filePath = patch.filePath;
await repo.stageFiles([filePath]);
repo.refresh();
assert.deepEqual(await repo.getUnstagedChanges(), []);
assert.deepEqual(await repo.getStagedChanges(), [patch]);
await repo.unstageFiles([filePath]);
repo.refresh();
assert.deepEqual(await repo.getUnstagedChanges(), [patch]);
assert.deepEqual(await repo.getStagedChanges(), []);
});
it('can stage and unstage renamed files', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.renameSync(path.join(workingDirPath, 'c.txt'), path.join(workingDirPath, 'subdir-1', 'd.txt'));
const patches = await repo.getUnstagedChanges();
const filePath1 = patches[0].filePath;
const filePath2 = patches[1].filePath;
await repo.stageFiles([filePath1, filePath2]);
repo.refresh();
assertEqualSortedArraysByKey(await repo.getStagedChanges(), patches, 'filePath');
assert.deepEqual(await repo.getUnstagedChanges(), []);
await repo.unstageFiles([filePath1, filePath2]);
repo.refresh();
assertEqualSortedArraysByKey(await repo.getUnstagedChanges(), patches, 'filePath');
assert.deepEqual(await repo.getStagedChanges(), []);
});
it('can stage and unstage added files, including those in added directories', async function() {
const workingDirPath = await cloneRepository('three-files');
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'e.txt'), 'qux', 'utf8');
fs.mkdirSync(path.join(workingDirPath, 'new-folder'));
fs.writeFileSync(path.join(workingDirPath, 'new-folder', 'b.txt'), 'bar\n', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'new-folder', 'c.txt'), 'baz\n', 'utf8');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
const unstagedChanges = await repo.getUnstagedChanges();
assert.equal(unstagedChanges.length, 3);
await repo.stageFiles([unstagedChanges[0].filePath, unstagedChanges[2].filePath]);
repo.refresh();
assert.deepEqual(await repo.getUnstagedChanges(), [unstagedChanges[1]]);
assert.deepEqual(await repo.getStagedChanges(), [unstagedChanges[0], unstagedChanges[2]]);
await repo.unstageFiles([unstagedChanges[0].filePath]);
repo.refresh();
assert.deepEqual(await repo.getUnstagedChanges(), [unstagedChanges[0], unstagedChanges[1]]);
assert.deepEqual(await repo.getStagedChanges(), [unstagedChanges[2]]);
});
it('can unstage and retrieve staged changes relative to HEAD~', async function() {
const workingDirPath = await cloneRepository('multiple-commits');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'file.txt'), 'three\nfour\n', 'utf8');
assert.deepEqual(await repo.getStagedChangesSinceParentCommit(), [
{
filePath: 'file.txt',
status: 'modified',
},
]);
assertDeepPropertyVals(await repo.getFilePatchForPath('file.txt', {staged: true, amending: true}), {
oldPath: 'file.txt',
newPath: 'file.txt',
status: 'modified',
hunks: [
{
lines: [
{status: 'deleted', text: 'two', oldLineNumber: 1, newLineNumber: -1},
{status: 'added', text: 'three', oldLineNumber: -1, newLineNumber: 1},
],
},
],
});
await repo.stageFiles(['file.txt']);
repo.refresh();
assertDeepPropertyVals(await repo.getFilePatchForPath('file.txt', {staged: true, amending: true}), {
oldPath: 'file.txt',
newPath: 'file.txt',
status: 'modified',
hunks: [
{
lines: [
{status: 'deleted', text: 'two', oldLineNumber: 1, newLineNumber: -1},
{status: 'added', text: 'three', oldLineNumber: -1, newLineNumber: 1},
{status: 'added', text: 'four', oldLineNumber: -1, newLineNumber: 2},
],
},
],
});
await repo.stageFilesFromParentCommit(['file.txt']);
repo.refresh();
assert.deepEqual(await repo.getStagedChangesSinceParentCommit(), []);
});
});
describe('getFilePatchForPath', function() {
it('returns cached FilePatch objects if they exist', async function() {
const workingDirPath = await cloneRepository('multiple-commits');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'new-file.txt'), 'foooooo', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'file.txt'), 'qux\nfoo\nbar\n', 'utf8');
await repo.stageFiles(['file.txt']);
const unstagedFilePatch = await repo.getFilePatchForPath('new-file.txt');
const stagedFilePatch = await repo.getFilePatchForPath('file.txt', {staged: true});
const stagedFilePatchDuringAmend = await repo.getFilePatchForPath('file.txt', {staged: true, amending: true});
assert.equal(await repo.getFilePatchForPath('new-file.txt'), unstagedFilePatch);
assert.equal(await repo.getFilePatchForPath('file.txt', {staged: true}), stagedFilePatch);
assert.equal(await repo.getFilePatchForPath('file.txt', {staged: true, amending: true}), stagedFilePatchDuringAmend);
});
it('returns new FilePatch object after repository refresh', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'a.txt'), 'qux\nfoo\nbar\n', 'utf8');
const filePatchA = await repo.getFilePatchForPath('a.txt');
assert.equal(await repo.getFilePatchForPath('a.txt'), filePatchA);
repo.refresh();
assert.notEqual(await repo.getFilePatchForPath('a.txt'), filePatchA);
assert.deepEqual(await repo.getFilePatchForPath('a.txt'), filePatchA);
});
});
describe('isPartiallyStaged(filePath)', function() {
it('returns true if specified file path is partially staged', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'a.txt'), 'modified file', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'new-file.txt'), 'foo\nbar\nbaz\n', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'b.txt'), 'blah blah blah', 'utf8');
fs.unlinkSync(path.join(workingDirPath, 'c.txt'));
assert.isFalse(await repo.isPartiallyStaged('a.txt'));
assert.isFalse(await repo.isPartiallyStaged('b.txt'));
assert.isFalse(await repo.isPartiallyStaged('c.txt'));
assert.isFalse(await repo.isPartiallyStaged('new-file.txt'));
await repo.stageFiles(['a.txt', 'b.txt', 'c.txt', 'new-file.txt']);
repo.refresh();
assert.isFalse(await repo.isPartiallyStaged('a.txt'));
assert.isFalse(await repo.isPartiallyStaged('b.txt'));
assert.isFalse(await repo.isPartiallyStaged('c.txt'));
assert.isFalse(await repo.isPartiallyStaged('new-file.txt'));
// modified on both
fs.writeFileSync(path.join(workingDirPath, 'a.txt'), 'more mods', 'utf8');
// modified in working directory, added on index
fs.writeFileSync(path.join(workingDirPath, 'new-file.txt'), 'foo\nbar\nbaz\nqux\n', 'utf8');
// deleted in working directory, modified on index
fs.unlinkSync(path.join(workingDirPath, 'b.txt'));
// untracked in working directory, deleted on index
fs.writeFileSync(path.join(workingDirPath, 'c.txt'), 'back baby', 'utf8');
repo.refresh();
assert.isTrue(await repo.isPartiallyStaged('a.txt'));
assert.isTrue(await repo.isPartiallyStaged('b.txt'));
assert.isTrue(await repo.isPartiallyStaged('c.txt'));
assert.isTrue(await repo.isPartiallyStaged('new-file.txt'));
});
});
describe('applyPatchToIndex', function() {
it('can stage and unstage modified files', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'a.txt'), 'qux\nfoo\nbar\n', 'utf8');
const unstagedPatch1 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt'));
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'a.txt'), 'qux\nfoo\nbar\nbaz\n', 'utf8');
repo.refresh();
const unstagedPatch2 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt'));
await repo.applyPatchToIndex(unstagedPatch1);
repo.refresh();
const stagedPatch1 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt'), {staged: true});
assert.deepEqual(stagedPatch1, unstagedPatch1);
let unstagedChanges = (await repo.getUnstagedChanges()).map(c => c.filePath);
let stagedChanges = (await repo.getStagedChanges()).map(c => c.filePath);
assert.deepEqual(unstagedChanges, [path.join('subdir-1', 'a.txt')]);
assert.deepEqual(stagedChanges, [path.join('subdir-1', 'a.txt')]);
await repo.applyPatchToIndex(unstagedPatch1.getUnstagePatch());
repo.refresh();
const unstagedPatch3 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt'));
assert.deepEqual(unstagedPatch3, unstagedPatch2);
unstagedChanges = (await repo.getUnstagedChanges()).map(c => c.filePath);
stagedChanges = (await repo.getStagedChanges()).map(c => c.filePath);
assert.deepEqual(unstagedChanges, [path.join('subdir-1', 'a.txt')]);
assert.deepEqual(stagedChanges, []);
});
});
describe('commit', function() {
let realPath = '';
beforeEach(function() {
realPath = process.env.PATH;
});
afterEach(function() {
process.env.PATH = realPath;
});
it('creates a commit that contains the staged changes', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
assert.equal((await repo.getLastCommit()).getMessage(), 'Initial commit');
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'a.txt'), 'qux\nfoo\nbar\n', 'utf8');
const unstagedPatch1 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt'));
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'a.txt'), 'qux\nfoo\nbar\nbaz\n', 'utf8');
repo.refresh();
await repo.applyPatchToIndex(unstagedPatch1);
await repo.commit('Commit 1');
assert.equal((await repo.getLastCommit()).getMessage(), 'Commit 1');
repo.refresh();
assert.deepEqual(await repo.getStagedChanges(), []);
const unstagedChanges = await repo.getUnstagedChanges();
assert.equal(unstagedChanges.length, 1);
const unstagedPatch2 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt'));
await repo.applyPatchToIndex(unstagedPatch2);
await repo.commit('Commit 2');
assert.equal((await repo.getLastCommit()).getMessage(), 'Commit 2');
repo.refresh();
assert.deepEqual(await repo.getStagedChanges(), []);
assert.deepEqual(await repo.getUnstagedChanges(), []);
});
it('amends the last commit when the amend option is set to true', async function() {
const workingDirPath = await cloneRepository('multiple-commits');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
const lastCommit = await repo.git.getHeadCommit();
const lastCommitParent = await repo.git.getCommit('HEAD~');
await repo.commit('amend last commit', {amend: true, allowEmpty: true});
const amendedCommit = await repo.git.getHeadCommit();
const amendedCommitParent = await repo.git.getCommit('HEAD~');
assert.notDeepEqual(lastCommit, amendedCommit);
assert.deepEqual(lastCommitParent, amendedCommitParent);
});
it('throws an error when there are unmerged files', async function() {
const workingDirPath = await cloneRepository('merge-conflict');
const repository = new Repository(workingDirPath);
await repository.getLoadPromise();
try {
await repository.git.merge('origin/branch');
} catch (e) {
// expected
}
assert.equal(await repository.isMerging(), true);
const mergeBase = await repository.getLastCommit();
try {
await repository.commit('Merge Commit');
} catch (e) {
assert.isAbove(e.code, 0);
assert.match(e.command, /commit/);
}
assert.equal(await repository.isMerging(), true);
assert.equal((await repository.getLastCommit()).getSha(), mergeBase.getSha());
});
it('wraps the commit message body at 72 characters', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'a.txt'), 'qux\nfoo\nbar\n', 'utf8');
await repo.stageFiles([path.join('subdir-1', 'a.txt')]);
await repo.commit([
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor',
'',
'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.',
].join('\n'));
const message = (await repo.getLastCommit()).getMessage();
assert.deepEqual(message.split('\n'), [
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor',
'',
'Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi',
'ut aliquip ex ea commodo consequat.',
]);
});
it('strips out comments', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'a.txt'), 'qux\nfoo\nbar\n', 'utf8');
await repo.stageFiles([path.join('subdir-1', 'a.txt')]);
await repo.commit([
'Make a commit',
'',
'# Comments:',
'# blah blah blah',
'# other stuff',
].join('\n'));
assert.deepEqual((await repo.getLastCommit()).getMessage(), 'Make a commit');
});
it('clears the stored resolution progress');
it('executes hook scripts with a sane environment', async function() {
const workingDirPath = await cloneRepository('three-files');
const scriptDirPath = path.join(getPackageRoot(), 'test', 'scripts');
await copyFile(
path.join(scriptDirPath, 'hook.sh'),
path.join(workingDirPath, '.git', 'hooks', 'pre-commit'),
);
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
process.env.PATH = `${scriptDirPath}:${process.env.PATH}`;
await assert.isRejected(repo.commit('hmm'), /didirun\.sh did run/);
});
});
describe('fetch(branchName)', function() {
it('brings commits from the remote and updates remote branch, and does not update branch', async function() {
const {localRepoPath} = await setUpLocalAndRemoteRepositories({remoteAhead: true});
const localRepo = new Repository(localRepoPath);
await localRepo.getLoadPromise();
let remoteHead, localHead;
remoteHead = await localRepo.git.getCommit('origin/master');
localHead = await localRepo.git.getCommit('master');
assert.equal(remoteHead.message, 'second commit');
assert.equal(localHead.message, 'second commit');
await localRepo.fetch('master');
remoteHead = await localRepo.git.getCommit('origin/master');
localHead = await localRepo.git.getCommit('master');
assert.equal(remoteHead.message, 'third commit');
assert.equal(localHead.message, 'second commit');
});
});
describe('pull()', function() {
it('updates the remote branch and merges into local branch', async function() {
const {localRepoPath} = await setUpLocalAndRemoteRepositories({remoteAhead: true});
const localRepo = new Repository(localRepoPath);
await localRepo.getLoadPromise();
let remoteHead, localHead;
remoteHead = await localRepo.git.getCommit('origin/master');
localHead = await localRepo.git.getCommit('master');
assert.equal(remoteHead.message, 'second commit');
assert.equal(localHead.message, 'second commit');
await localRepo.pull('master');
remoteHead = await localRepo.git.getCommit('origin/master');
localHead = await localRepo.git.getCommit('master');
assert.equal(remoteHead.message, 'third commit');
assert.equal(localHead.message, 'third commit');
});
});
describe('push()', function() {
it('sends commits to the remote and updates ', async function() {
const {localRepoPath, remoteRepoPath} = await setUpLocalAndRemoteRepositories();
const localRepo = new Repository(localRepoPath);
await localRepo.getLoadPromise();
let localHead, localRemoteHead, remoteHead;
localHead = await localRepo.git.getCommit('master');
localRemoteHead = await localRepo.git.getCommit('origin/master');
assert.deepEqual(localHead, localRemoteHead);
await localRepo.commit('fourth commit', {allowEmpty: true});
await localRepo.commit('fifth commit', {allowEmpty: true});
localHead = await localRepo.git.getCommit('master');
localRemoteHead = await localRepo.git.getCommit('origin/master');
remoteHead = await getHeadCommitOnRemote(remoteRepoPath);
assert.notDeepEqual(localHead, remoteHead);
assert.equal(remoteHead.message, 'third commit');
assert.deepEqual(remoteHead, localRemoteHead);
await localRepo.push('master');
localHead = await localRepo.git.getCommit('master');
localRemoteHead = await localRepo.git.getCommit('origin/master');
remoteHead = await getHeadCommitOnRemote(remoteRepoPath);
assert.deepEqual(localHead, remoteHead);
assert.equal(remoteHead.message, 'fifth commit');
assert.deepEqual(remoteHead, localRemoteHead);
});
});
describe('getAheadCount(branchName) and getBehindCount(branchName)', function() {
it('returns the number of commits ahead and behind the remote', async function() {
const {localRepoPath} = await setUpLocalAndRemoteRepositories({remoteAhead: true});
const localRepo = new Repository(localRepoPath);
await localRepo.getLoadPromise();
assert.equal(await localRepo.getBehindCount('master'), 0);
assert.equal(await localRepo.getAheadCount('master'), 0);
await localRepo.fetch('master');
assert.equal(await localRepo.getBehindCount('master'), 1);
assert.equal(await localRepo.getAheadCount('master'), 0);
await localRepo.commit('new commit', {allowEmpty: true});
await localRepo.commit('another commit', {allowEmpty: true});
assert.equal(await localRepo.getBehindCount('master'), 1);
assert.equal(await localRepo.getAheadCount('master'), 2);
});
});
describe('getRemoteForBranch(branchName)', function() {
it('returns the remote associated to the supplied branch name', async function() {
const {localRepoPath} = await setUpLocalAndRemoteRepositories({remoteAhead: true});
const localRepo = new Repository(localRepoPath);
await localRepo.getLoadPromise();
const remote0 = await localRepo.getRemoteForBranch('master');
assert.isTrue(remote0.isPresent());
assert.equal(remote0.getName(), 'origin');
await localRepo.git.exec(['remote', 'rename', 'origin', 'foo']);
localRepo.refresh();
const remote1 = await localRepo.getRemoteForBranch('master');
assert.isTrue(remote1.isPresent());
assert.equal(remote1.getName(), 'foo');
await localRepo.git.exec(['remote', 'rm', 'foo']);
localRepo.refresh();
const remote2 = await localRepo.getRemoteForBranch('master');
assert.isFalse(remote2.isPresent());
});
});
describe('merge conflicts', function() {
describe('getMergeConflicts()', function() {
it('returns a promise resolving to an array of MergeConflict objects', async function() {
const workingDirPath = await cloneRepository('merge-conflict');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
await assert.isRejected(repo.git.merge('origin/branch'), /CONFLICT/);
repo.refresh();
let mergeConflicts = await repo.getMergeConflicts();
const expected = [
{
filePath: 'added-to-both.txt',
status: {
file: 'modified',
ours: 'added',
theirs: 'added',
},
},
{
filePath: 'modified-on-both-ours.txt',
status: {
file: 'modified',
ours: 'modified',
theirs: 'modified',
},
},
{
filePath: 'modified-on-both-theirs.txt',
status: {
file: 'modified',
ours: 'modified',
theirs: 'modified',
},
},
{
filePath: 'removed-on-branch.txt',
status: {
file: 'equivalent',
ours: 'modified',
theirs: 'deleted',
},
},
{
filePath: 'removed-on-master.txt',
status: {
file: 'added',
ours: 'deleted',
theirs: 'modified',
},
},
];
assertDeepPropertyVals(mergeConflicts, expected);
fs.unlinkSync(path.join(workingDirPath, 'removed-on-branch.txt'));
repo.refresh();
mergeConflicts = await repo.getMergeConflicts();
expected[3].status.file = 'deleted';
assertDeepPropertyVals(mergeConflicts, expected);
});
it('returns an empty array if the repo has no merge conflicts', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
const mergeConflicts = await repo.getMergeConflicts();
assert.deepEqual(mergeConflicts, []);
});
});
describe('stageFiles([path])', function() {
it('updates the staged changes accordingly', async function() {
const workingDirPath = await cloneRepository('merge-conflict');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
await assert.isRejected(repo.git.merge('origin/branch'));
repo.refresh();
const mergeConflictPaths = (await repo.getMergeConflicts()).map(c => c.filePath);
assert.deepEqual(mergeConflictPaths, ['added-to-both.txt', 'modified-on-both-ours.txt', 'modified-on-both-theirs.txt', 'removed-on-branch.txt', 'removed-on-master.txt']);
let stagedFilePatches = await repo.getStagedChanges();
assert.deepEqual(stagedFilePatches.map(patch => patch.filePath), []);
await repo.stageFiles(['added-to-both.txt']);
repo.refresh();
stagedFilePatches = await repo.getStagedChanges();
assert.deepEqual(stagedFilePatches.map(patch => patch.filePath), ['added-to-both.txt']);
// choose version of the file on head
fs.writeFileSync(path.join(workingDirPath, 'modified-on-both-ours.txt'), 'master modification\n', 'utf8');
await repo.stageFiles(['modified-on-both-ours.txt']);
repo.refresh();
stagedFilePatches = await repo.getStagedChanges();
// nothing additional to stage
assert.deepEqual(stagedFilePatches.map(patch => patch.filePath), ['added-to-both.txt']);
// choose version of the file on branch
fs.writeFileSync(path.join(workingDirPath, 'modified-on-both-ours.txt'), 'branch modification\n', 'utf8');
await repo.stageFiles(['modified-on-both-ours.txt']);
repo.refresh();
stagedFilePatches = await repo.getStagedChanges();
assert.deepEqual(stagedFilePatches.map(patch => patch.filePath), ['added-to-both.txt', 'modified-on-both-ours.txt']);
// remove file that was deleted on branch
fs.unlinkSync(path.join(workingDirPath, 'removed-on-branch.txt'));
await repo.stageFiles(['removed-on-branch.txt']);
repo.refresh();
stagedFilePatches = await repo.getStagedChanges();
assert.deepEqual(stagedFilePatches.map(patch => patch.filePath), ['added-to-both.txt', 'modified-on-both-ours.txt', 'removed-on-branch.txt']);
// remove file that was deleted on master
fs.unlinkSync(path.join(workingDirPath, 'removed-on-master.txt'));
await repo.stageFiles(['removed-on-master.txt']);
repo.refresh();
stagedFilePatches = await repo.getStagedChanges();
// nothing additional to stage
assert.deepEqual(stagedFilePatches.map(patch => patch.filePath), ['added-to-both.txt', 'modified-on-both-ours.txt', 'removed-on-branch.txt']);
});
});
describe('pathHasMergeMarkers()', function() {
it('returns true if and only if the file has merge markers', async function() {
const workingDirPath = await cloneRepository('merge-conflict');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
await assert.isRejected(repo.git.merge('origin/branch'));
assert.isTrue(await repo.pathHasMergeMarkers('added-to-both.txt'));
assert.isFalse(await repo.pathHasMergeMarkers('removed-on-master.txt'));
fs.writeFileSync(path.join(workingDirPath, 'file-with-chevrons.txt'), dedent`
no branch name:
>>>>>>>
<<<<<<<
not enough chevrons:
>>> HEAD
<<< branch
too many chevrons:
>>>>>>>>> HEAD
<<<<<<<<< branch
too many words after chevrons:
>>>>>>> blah blah blah
<<<<<<< blah blah blah
not at line beginning:
foo >>>>>>> bar
baz <<<<<<< qux
`);
assert.isFalse(await repo.pathHasMergeMarkers('file-with-chevrons.txt'));
assert.isFalse(await repo.pathHasMergeMarkers('nonexistent-file.txt'));
});
});
describe('abortMerge()', function() {
describe('when the working directory is clean', function() {
it('resets the index and the working directory to match HEAD', async function() {
const workingDirPath = await cloneRepository('merge-conflict-abort');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
await assert.isRejected(repo.git.merge('origin/spanish'));
assert.equal(await repo.isMerging(), true);
await repo.abortMerge();
assert.equal(await repo.isMerging(), false);
});
});
describe('when a dirty file in the working directory is NOT under conflict', function() {
it('successfully aborts the merge and does not affect the dirty file', async function() {
const workingDirPath = await cloneRepository('merge-conflict-abort');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
await assert.isRejected(repo.git.merge('origin/spanish'));
fs.writeFileSync(path.join(workingDirPath, 'fruit.txt'), 'a change\n');
assert.equal(await repo.isMerging(), true);
await repo.abortMerge();
assert.equal(await repo.isMerging(), false);
assert.equal((await repo.getStagedChanges()).length, 0);
assert.equal((await repo.getUnstagedChanges()).length, 1);
assert.equal(fs.readFileSync(path.join(workingDirPath, 'fruit.txt')), 'a change\n');
});
});
describe('when a dirty file in the working directory is under conflict', function() {
it('throws an error indicating that the abort could not be completed', async function() {
const workingDirPath = await cloneRepository('merge-conflict-abort');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
await assert.isRejected(repo.git.merge('origin/spanish'));
fs.writeFileSync(path.join(workingDirPath, 'animal.txt'), 'a change\n');
const stagedChanges = await repo.getStagedChanges();
const unstagedChanges = await repo.getUnstagedChanges();
assert.equal(await repo.isMerging(), true);
try {
await repo.abortMerge();
assert.fail(null, null, 'repo.abortMerge() unexepctedly succeeded');
} catch (e) {
assert.match(e.command, /^git .* merge --abort/);
}
assert.equal(await repo.isMerging(), true);
assert.deepEqual(await repo.getStagedChanges(), stagedChanges);
assert.deepEqual(await repo.getUnstagedChanges(), unstagedChanges);
});
});
});
});
describe('discardWorkDirChangesForPaths()', function() {
it('can discard working directory changes in modified files', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'a.txt'), 'qux\nfoo\nbar\n', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'b.txt'), 'qux\nfoo\nbar\n', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'new-file.txt'), 'hello there', 'utf8');
const unstagedChanges = await repo.getUnstagedChanges();
assert.equal(unstagedChanges.length, 3);
await repo.discardWorkDirChangesForPaths(unstagedChanges.map(c => c.filePath));
repo.refresh();
assert.deepEqual(await repo.getUnstagedChanges(), []);
});
it('can discard working directory changes in removed files', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.unlinkSync(path.join(workingDirPath, 'subdir-1', 'a.txt'));
fs.unlinkSync(path.join(workingDirPath, 'subdir-1', 'b.txt'));
const unstagedChanges = await repo.getUnstagedChanges();
assert.equal(unstagedChanges.length, 2);
await repo.discardWorkDirChangesForPaths(unstagedChanges.map(c => c.filePath));
repo.refresh();
assert.deepEqual(await repo.getUnstagedChanges(), []);
});
it('can discard working directory changes added files', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'e.txt'), 'qux', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'f.txt'), 'qux', 'utf8');
const unstagedChanges = await repo.getUnstagedChanges();
assert.equal(unstagedChanges.length, 2);
await repo.discardWorkDirChangesForPaths(unstagedChanges.map(c => c.filePath));
repo.refresh();
assert.deepEqual(await repo.getUnstagedChanges(), []);
});
});
describe('maintaining discard history across repository instances', function() {
it('restores the history', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo1 = new Repository(workingDirPath);
await repo1.getLoadPromise();
fs.writeFileSync(path.join(workingDirPath, 'a.txt'), 'qux\nfoo\nbar\n', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'b.txt'), 'woohoo', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'c.txt'), 'yayeah', 'utf8');
const isSafe = () => true;
await repo1.storeBeforeAndAfterBlobs(['a.txt'], isSafe, () => {
fs.writeFileSync(path.join(workingDirPath, 'a.txt'), 'foo\nbar\n', 'utf8');
}, 'a.txt');
await repo1.storeBeforeAndAfterBlobs(['b.txt', 'c.txt'], isSafe, () => {
fs.writeFileSync(path.join(workingDirPath, 'b.txt'), 'woot', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'c.txt'), 'yup', 'utf8');
});
const repo1HistorySha = repo1.createDiscardHistoryBlob();
const repo2 = new Repository(workingDirPath);
await repo2.getLoadPromise();
const repo2HistorySha = repo2.createDiscardHistoryBlob();
assert.deepEqual(repo2HistorySha, repo1HistorySha);
});
it('is resilient to missing history blobs', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo1 = new Repository(workingDirPath);
await repo1.getLoadPromise();
await repo1.setConfig('atomGithub.historySha', '1111111111111111111111111111111111111111');
// Should not throw
await repo1.updateDiscardHistory();
// Also should not throw
const repo2 = new Repository(workingDirPath);
await repo2.getLoadPromise();
});
});
describe('cache invalidation', function() {
// These tests do a *lot* of git operations
this.timeout(Math.max(20000, this.timeout() * 2));
const preventDefault = event => event.preventDefault();
beforeEach(function() {
window.addEventListener('unhandledrejection', preventDefault);
});
afterEach(function() {
window.removeEventListener('unhandedrejection', preventDefault);
});
function filesWithinRepository(repository) {
const relativePaths = [];
const descend = async (currentDirectory, relativeBase) => {
const files = await new Promise((readdirResolve, readdirReject) => {
return fs.readdir(currentDirectory, (err, result) => {
if (err) {
readdirReject(err);
} else {
readdirResolve(result);
}
});
});
const stats = await Promise.all(
files
.map(file => fsStat(path.join(currentDirectory, file))
.then(stat => ({file, stat}))),
);
const subdirs = [];
for (const {file, stat} of stats) {
if (stat.isFile()) {
relativePaths.push(path.join(relativeBase, file));
}
if (stat.isDirectory() && file !== '.git') {
subdirs.push(file);
}
}
return Promise.all(
subdirs.map(subdir => descend(path.join(currentDirectory, subdir), path.join(relativeBase, subdir))),
);
};
return descend(repository.getWorkingDirectoryPath(), '').then(() => relativePaths);
}
async function getCacheReaderMethods(options) {
const repository = options.repository;
const calls = new Map();
calls.set(
'getStatusBundle',
() => repository.getStatusBundle(),
);
calls.set(
'getHeadDescription',
() => repository.getHeadDescription(),
);
calls.set(
'getStagedChangesSinceParentCommit',
() => repository.getStagedChangesSinceParentCommit(),