Repository navigation
Expand file tree
/
Copy pathrepository.test.js
More file actions
2715 lines (2234 loc) · 106 KB
/
Copy pathrepository.test.js
File metadata and controls
2715 lines (2234 loc) · 106 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
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-extra';
import path from 'path';
import dedent from 'dedent-js';
import temp from 'temp';
import compareSets from 'compare-sets';
import isEqualWith from 'lodash.isequalwith';
import Repository from '../../lib/models/repository';
import CompositeGitStrategy from '../../lib/composite-git-strategy';
import {LargeRepoError} from '../../lib/git-shell-out-strategy';
import {nullCommit} from '../../lib/models/commit';
import {nullOperationStates} from '../../lib/models/operation-states';
import Author from '../../lib/models/author';
import {FOCUS} from '../../lib/models/workspace-change-observer';
import * as reporterProxy from '../../lib/reporter-proxy';
import {
cloneRepository, setUpLocalAndRemoteRepositories, getHeadCommitOnRemote,
assertDeepPropertyVals, assertEqualSortedArraysByKey, FAKE_USER, wireUpObserver, expectEvents,
} from '../helpers';
import {getPackageRoot, getTempDir} from '../../lib/helpers';
describe('Repository', function() {
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('inherited State methods', function() {
let repository;
beforeEach(function() {
repository = Repository.absent();
repository.destroy();
});
it('returns a null object', async function() {
// Methods that default to "false"
for (const method of [
'isLoadingGuess', 'isAbsentGuess', 'isAbsent', 'isLoading', 'isEmpty', 'isPresent', 'isTooLarge',
'isUndetermined', 'showGitTabInit', 'showGitTabInitInProgress', 'showGitTabLoading', 'showStatusBarTiles',
'hasDiscardHistory', 'isMerging', 'isRebasing', 'isCommitPushed',
]) {
assert.isFalse(await repository[method]());
}
// Methods that resolve to null
for (const method of [
'getAheadCount', 'getBehindCount', 'getConfig', 'getLastHistorySnapshots', 'getCache',
]) {
assert.isNull(await repository[method]());
}
// Methods that resolve to an empty array
for (const method of [
'getRecentCommits', 'getAuthors', 'getDiscardHistory',
]) {
assert.lengthOf(await repository[method](), 0);
}
assert.deepEqual(await repository.getStatusBundle(), {
stagedFiles: {},
unstagedFiles: {},
mergeConflictFiles: {},
branch: {
oid: null,
head: null,
upstream: null,
aheadBehind: {
ahead: null,
behind: null,
},
},
});
assert.deepEqual(await repository.getStatusesForChangedFiles(), {
stagedFiles: [],
unstagedFiles: [],
mergeConflictFiles: [],
});
assert.strictEqual(await repository.getLastCommit(), nullCommit);
assert.lengthOf((await repository.getBranches()).getNames(), 0);
assert.isTrue((await repository.getRemotes()).isEmpty());
assert.strictEqual(await repository.getHeadDescription(), '(no repository)');
assert.strictEqual(await repository.getOperationStates(), nullOperationStates);
assert.strictEqual(await repository.getCommitMessage(), '');
assert.isFalse((await repository.getFilePatchForPath('anything.txt')).anyPresent());
});
it('returns a rejecting promise', async function() {
for (const method of [
'init', 'clone', 'stageFiles', 'unstageFiles', 'stageFilesFromParentCommit', 'applyPatchToIndex',
'applyPatchToWorkdir', 'commit', 'merge', 'abortMerge', 'checkoutSide', 'mergeFile',
'writeMergeConflictToIndex', 'checkout', 'checkoutPathsAtRevision', 'undoLastCommit', 'fetch', 'pull',
'push', 'setConfig', 'unsetConfig', 'createBlob', 'expandBlobToFile', 'createDiscardHistoryBlob',
'updateDiscardHistory', 'storeBeforeAndAfterBlobs', 'restoreLastDiscardInTempFiles', 'popDiscardHistory',
'clearDiscardHistory', 'discardWorkDirChangesForPaths', 'addRemote', 'setCommitMessage',
'fetchCommitMessageTemplate',
]) {
await assert.isRejected(repository[method](), new RegExp(`${method} is not available in Destroyed state`));
}
await assert.isRejected(
repository.readFileFromIndex('file'),
/fatal: Path file does not exist \(neither on disk nor in the index\)\./,
);
await assert.isRejected(
repository.getBlobContents('abcd'),
/fatal: Not a valid object name abcd/,
);
});
});
it('accesses an OperationStates model', async function() {
const repository = new Repository(await cloneRepository());
await repository.getLoadPromise();
const os = repository.getOperationStates();
assert.isFalse(os.isPushInProgress());
assert.isFalse(os.isPullInProgress());
assert.isFalse(os.isFetchInProgress());
assert.isFalse(os.isCommitInProgress());
assert.isFalse(os.isCheckoutInProgress());
});
it('shows status bar tiles once present', async function() {
const repository = new Repository(await cloneRepository());
assert.isFalse(repository.showStatusBarTiles());
await repository.getLoadPromise();
assert.isTrue(repository.showStatusBarTiles());
});
describe('getCurrentGitHubRemote', function() {
let workdir, repository;
beforeEach(async function() {
workdir = await cloneRepository('three-files');
repository = new Repository(workdir);
await repository.getLoadPromise();
});
it('gets current GitHub remote if remote is configured', async function() {
await repository.addRemote('yes0', 'git@github.com:atom/github.git');
const remote = await repository.getCurrentGitHubRemote();
assert.strictEqual(remote.url, 'git@github.com:atom/github.git');
assert.strictEqual(remote.name, 'yes0');
});
it('returns null remote no remotes exist', async function() {
const remote = await repository.getCurrentGitHubRemote();
assert.isFalse(remote.isPresent());
});
it('returns null remote if only non-GitHub remotes exist', async function() {
await repository.addRemote('no0', 'https://sourceforge.net/some/repo.git');
const remote = await repository.getCurrentGitHubRemote();
assert.isFalse(remote.isPresent());
});
it('returns null remote if no remotes are configured and multiple GitHub remotes exist', async function() {
await repository.addRemote('yes0', 'git@github.com:atom/github.git');
await repository.addRemote('yes1', 'git@github.com:smashwilson/github.git');
const remote = await repository.getCurrentGitHubRemote();
assert.isFalse(remote.isPresent());
});
it('returns null remote before repository has loaded', async function() {
const loadingRepository = new Repository(workdir);
const remote = await loadingRepository.getCurrentGitHubRemote();
assert.isFalse(remote.isPresent());
});
});
describe('getGitDirectoryPath', function() {
it('returns the correct git directory path', async function() {
const workingDirPath = await cloneRepository('three-files');
const workingDirPathWithGitFile = await getTempDir();
await fs.writeFile(
path.join(workingDirPathWithGitFile, '.git'),
`gitdir: ${path.join(workingDirPath, '.git')}`,
{encoding: 'utf8'},
);
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 = await fs.realpath(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);
});
it('fails with an error when a repository is already present', async function() {
const workdir = await cloneRepository();
const repository = new Repository(workdir);
await repository.getLoadPromise();
await assert.isRejected(repository.init());
});
});
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 = await fs.realpath(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 = await fs.realpath(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);
});
it('fails with an error when a repository is already present', async function() {
const upstream = await cloneRepository();
const workdir = await cloneRepository();
const repository = new Repository(workdir);
await repository.getLoadPromise();
await assert.isRejected(repository.clone(upstream));
});
});
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 stage and unstage file modes without staging file contents', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
const filePath = 'a.txt';
async function indexModeAndOid(filename) {
const output = await repo.git.exec(['ls-files', '-s', '--', filename]);
const parts = output.split(' ');
return {mode: parts[0], oid: parts[1]};
}
const {mode, oid} = await indexModeAndOid(path.join(workingDirPath, filePath));
assert.equal(mode, '100644');
fs.chmodSync(path.join(workingDirPath, filePath), 0o755);
fs.writeFileSync(path.join(workingDirPath, filePath), 'qux\nfoo\nbar\n', 'utf8');
await repo.stageFileModeChange(filePath, '100755');
assert.deepEqual(await indexModeAndOid(filePath), {mode: '100755', oid});
await repo.stageFileModeChange(filePath, '100644');
assert.deepEqual(await indexModeAndOid(filePath), {mode: '100644', oid});
});
it('can stage and unstage symlink changes without staging file contents', async function() {
if (process.env.ATOM_GITHUB_SKIP_SYMLINKS) {
this.skip();
return;
}
const workingDirPath = await cloneRepository('symlinks');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
async function indexModeAndOid(filename) {
const output = await repo.git.exec(['ls-files', '-s', '--', filename]);
if (output) {
const parts = output.split(' ');
return {mode: parts[0], oid: parts[1]};
} else {
return null;
}
}
const deletedSymlinkAddedFilePath = 'symlink.txt';
fs.unlinkSync(path.join(workingDirPath, deletedSymlinkAddedFilePath));
fs.writeFileSync(path.join(workingDirPath, deletedSymlinkAddedFilePath), 'qux\nfoo\nbar\n', 'utf8');
const deletedFileAddedSymlinkPath = 'a.txt';
fs.unlinkSync(path.join(workingDirPath, deletedFileAddedSymlinkPath));
fs.symlinkSync(path.join(workingDirPath, 'regular-file.txt'), path.join(workingDirPath, deletedFileAddedSymlinkPath));
// Stage symlink change, leaving added file unstaged
assert.equal((await indexModeAndOid(deletedSymlinkAddedFilePath)).mode, '120000');
await repo.stageFileSymlinkChange(deletedSymlinkAddedFilePath);
assert.isNull(await indexModeAndOid(deletedSymlinkAddedFilePath));
const unstagedFilePatch = await repo.getFilePatchForPath(deletedSymlinkAddedFilePath, {staged: false});
assert.lengthOf(unstagedFilePatch.getFilePatches(), 1);
const [uFilePatch] = unstagedFilePatch.getFilePatches();
assert.equal(uFilePatch.getStatus(), 'added');
assert.equal(unstagedFilePatch.toString(), dedent`
diff --git a/symlink.txt b/symlink.txt
new file mode 100644
--- /dev/null
+++ b/symlink.txt
@@ -0,0 +1,3 @@
+qux
+foo
+bar\n
`);
// Unstage symlink change, leaving deleted file staged
await repo.stageFiles([deletedFileAddedSymlinkPath]);
assert.equal((await indexModeAndOid(deletedFileAddedSymlinkPath)).mode, '120000');
await repo.stageFileSymlinkChange(deletedFileAddedSymlinkPath);
assert.isNull(await indexModeAndOid(deletedFileAddedSymlinkPath));
const stagedFilePatch = await repo.getFilePatchForPath(deletedFileAddedSymlinkPath, {staged: true});
assert.lengthOf(stagedFilePatch.getFilePatches(), 1);
const [sFilePatch] = stagedFilePatch.getFilePatches();
assert.equal(sFilePatch.getStatus(), 'deleted');
assert.equal(stagedFilePatch.toString(), dedent`
diff --git a/a.txt b/a.txt
deleted file mode 100644
--- a/a.txt
+++ /dev/null
@@ -1,4 +0,0 @@
-foo
-bar
-baz
-\n
`);
});
it('sorts staged and unstaged files', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
const zShortPath = path.join('z-dir', 'a.txt');
const zFilePath = path.join(workingDirPath, zShortPath);
const wFilePath = path.join(workingDirPath, 'w.txt');
fs.mkdirSync(path.join(workingDirPath, 'z-dir'));
fs.renameSync(path.join(workingDirPath, 'a.txt'), zFilePath);
fs.renameSync(path.join(workingDirPath, 'b.txt'), wFilePath);
const unstagedChanges = await repo.getUnstagedChanges();
const unstagedPaths = unstagedChanges.map(change => change.filePath);
assert.deepStrictEqual(unstagedPaths, ['a.txt', 'b.txt', 'w.txt', zShortPath]);
await repo.stageFiles([zFilePath]);
await repo.stageFiles([wFilePath]);
const stagedChanges = await repo.getStagedChanges();
const stagedPaths = stagedChanges.map(change => change.filePath);
assert.deepStrictEqual(stagedPaths, ['w.txt', zShortPath]);
});
});
describe('getStatusBundle', function() {
it('transitions to the TooLarge state and returns empty status when too large', async function() {
const workdir = await cloneRepository();
const repository = new Repository(workdir);
await repository.getLoadPromise();
sinon.stub(repository.git, 'getStatusBundle').rejects(new LargeRepoError());
const result = await repository.getStatusBundle();
assert.isTrue(repository.isInState('TooLarge'));
assert.deepEqual(result.branch, {});
assert.deepEqual(result.stagedFiles, {});
assert.deepEqual(result.unstagedFiles, {});
assert.deepEqual(result.mergeConflictFiles, {});
});
it('propagates unrecognized git errors', async function() {
const workdir = await cloneRepository();
const repository = new Repository(workdir);
await repository.getLoadPromise();
sinon.stub(repository.git, 'getStatusBundle').rejects(new Error('oh no'));
await assert.isRejected(repository.getStatusBundle(), /oh no/);
});
it('post-processes renamed files to an addition and a deletion', async function() {
const workdir = await cloneRepository();
const repository = new Repository(workdir);
await repository.getLoadPromise();
sinon.stub(repository.git, 'getStatusBundle').resolves({
changedEntries: [],
untrackedEntries: [],
renamedEntries: [
{stagedStatus: 'R', origFilePath: 'from0.txt', filePath: 'to0.txt'},
{unstagedStatus: 'R', origFilePath: 'from1.txt', filePath: 'to1.txt'},
{stagedStatus: 'C', filePath: 'c2.txt'},
{unstagedStatus: 'C', filePath: 'c3.txt'},
],
unmergedEntries: [],
});
const result = await repository.getStatusBundle();
assert.strictEqual(result.stagedFiles['from0.txt'], 'deleted');
assert.strictEqual(result.stagedFiles['to0.txt'], 'added');
assert.strictEqual(result.unstagedFiles['from1.txt'], 'deleted');
assert.strictEqual(result.unstagedFiles['to1.txt'], 'added');
assert.strictEqual(result.stagedFiles['c2.txt'], 'added');
assert.strictEqual(result.unstagedFiles['c3.txt'], 'added');
});
});
describe('getFilePatchForPath', function() {
it('returns cached MultiFilePatch 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});
assert.equal(await repo.getFilePatchForPath('new-file.txt'), unstagedFilePatch);
assert.equal(await repo.getFilePatchForPath('file.txt', {staged: true}), stagedFilePatch);
});
it('returns new MultiFilePatch 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.isTrue((await repo.getFilePatchForPath('a.txt')).isEqual(filePatchA));
});
it('returns an empty MultiFilePatch for unknown paths', async function() {
const workingDirPath = await cloneRepository('multiple-commits');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
const patch = await repo.getFilePatchForPath('no.txt');
assert.isFalse(patch.anyPresent());
});
});
describe('getStagedChangesPatch', function() {
it('computes a multi-file patch of the staged changes', async function() {
const workdir = await cloneRepository('each-staging-group');
const repo = new Repository(workdir);
await repo.getLoadPromise();
await fs.writeFile(path.join(workdir, 'unstaged-1.txt'), 'Unstaged file');
await fs.writeFile(path.join(workdir, 'staged-1.txt'), 'Staged file');
await fs.writeFile(path.join(workdir, 'staged-2.txt'), 'Staged file');
await repo.stageFiles(['staged-1.txt', 'staged-2.txt']);
const mp = await repo.getStagedChangesPatch();
assert.lengthOf(mp.getFilePatches(), 2);
assert.deepEqual(mp.getFilePatches().map(fp => fp.getPath()), ['staged-1.txt', 'staged-2.txt']);
});
});
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.isTrue(stagedPatch1.isEqual(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.getUnstagePatchForLines(new Set([0, 1, 2])));
repo.refresh();
const unstagedPatch3 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt'));
assert.isTrue(unstagedPatch3.isEqual(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 rp = '';
beforeEach(function() {
rp = process.env.PATH;
});
afterEach(function() {
process.env.PATH = rp;
});
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()).getMessageSubject(), '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()).getMessageSubject(), '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()).getMessageSubject(), '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', {allowEmpty: true, amend: 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();
await assert.isRejected(repository.git.merge('origin/branch'));
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('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 fs.copy(
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('recording commit event with metadata', function() {
it('reports partial commits', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
sinon.stub(reporterProxy, 'addEvent');
// stage only subset of total changes
fs.writeFileSync(path.join(workingDirPath, 'a.txt'), 'qux\nfoo\nbar\n', 'utf8');
fs.writeFileSync(path.join(workingDirPath, 'b.txt'), 'qux\nfoo\nbar\nbaz\n', 'utf8');
await repo.stageFiles(['a.txt']);
repo.refresh();
// unstaged changes remain
let unstagedChanges = await repo.getUnstagedChanges();
assert.equal(unstagedChanges.length, 1);
// do partial commit
await repo.commit('Partial commit');
assert.isTrue(reporterProxy.addEvent.called);
let args = reporterProxy.addEvent.lastCall.args;
assert.strictEqual(args[0], 'commit');
assert.isTrue(args[1].partial);
// stage all remaining changes
await repo.stageFiles(['b.txt']);
repo.refresh();
unstagedChanges = await repo.getUnstagedChanges();
assert.equal(unstagedChanges.length, 0);
reporterProxy.addEvent.reset();
// do whole commit
await repo.commit('Commit everything');
assert.isTrue(reporterProxy.addEvent.called);
args = reporterProxy.addEvent.lastCall.args;
assert.strictEqual(args[0], 'commit');
assert.isFalse(args[1].partial);
});
it('reports if the commit was an amend', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
sinon.stub(reporterProxy, 'addEvent');
await repo.commit('Regular commit', {allowEmpty: true});
assert.isTrue(reporterProxy.addEvent.called);
let args = reporterProxy.addEvent.lastCall.args;
assert.strictEqual(args[0], 'commit');
assert.isFalse(args[1].amend);
reporterProxy.addEvent.reset();
await repo.commit('Amended commit', {allowEmpty: true, amend: true});
assert.isTrue(reporterProxy.addEvent.called);
args = reporterProxy.addEvent.lastCall.args;
assert.deepEqual(args[0], 'commit');
assert.isTrue(args[1].amend);
});
it('reports number of coAuthors for commit', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
sinon.stub(reporterProxy, 'addEvent');
await repo.commit('Commit with no co-authors', {allowEmpty: true});
assert.isTrue(reporterProxy.addEvent.called);
let args = reporterProxy.addEvent.lastCall.args;
assert.deepEqual(args[0], 'commit');
assert.deepEqual(args[1].coAuthorCount, 0);
reporterProxy.addEvent.reset();
await repo.commit('Commit with fabulous co-authors', {
allowEmpty: true,
coAuthors: [new Author('mona@lisa.com', 'Mona Lisa'), new Author('hubot@github.com', 'Mr. Hubot')],
});
assert.isTrue(reporterProxy.addEvent.called);
args = reporterProxy.addEvent.lastCall.args;
assert.deepEqual(args[0], 'commit');
assert.deepEqual(args[1].coAuthorCount, 2);
});
it('does not record an event if operation fails', async function() {
const workingDirPath = await cloneRepository('multiple-commits');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
sinon.stub(reporterProxy, 'addEvent');
sinon.stub(repo.git, 'commit').throws();
await assert.isRejected(repo.commit('Commit yo!'));
assert.isFalse(reporterProxy.addEvent.called);
});
});
});
describe('getCommit(sha)', function() {
it('returns the commit information for the provided sha', async function() {
const workingDirPath = await cloneRepository('multiple-commits');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
const commit = await repo.getCommit('18920c900bfa6e4844853e7e246607a31c3e2e8c');
assert.isTrue(commit.isPresent());
assert.strictEqual(commit.getSha(), '18920c900bfa6e4844853e7e246607a31c3e2e8c');
assert.strictEqual(commit.getAuthorEmail(), 'kuychaco@github.com');
assert.strictEqual(commit.getAuthorDate(), 1471113642);
assert.lengthOf(commit.getCoAuthors(), 0);
assert.strictEqual(commit.getMessageSubject(), 'second commit');
assert.strictEqual(commit.getMessageBody(), '');
});
});
describe('isCommitPushed(sha)', function() {
it('returns true if SHA is reachable from the upstream ref', async function() {
const {localRepoPath} = await setUpLocalAndRemoteRepositories('multiple-commits');
const repository = new Repository(localRepoPath);
await repository.getLoadPromise();
const sha = (await repository.getLastCommit()).getSha();
assert.isTrue(await repository.isCommitPushed(sha));
});
it('returns false if SHA is not reachable from upstream', async function() {
const {localRepoPath} = await setUpLocalAndRemoteRepositories('multiple-commits');
const repository = new Repository(localRepoPath);
await repository.getLoadPromise();
await repository.git.commit('unpushed', {allowEmpty: true});
repository.refresh();
const sha = (await repository.getLastCommit()).getSha();
assert.isFalse(await repository.isCommitPushed(sha));
});
it('returns false on a detached HEAD', async function() {
const workdir = await cloneRepository('multiple-commits');
const repository = new Repository(workdir);
await repository.getLoadPromise();
await repository.checkout('HEAD~2');
const sha = (await repository.getLastCommit()).getSha();
assert.isFalse(await repository.isCommitPushed(sha));
});
});
describe('undoLastCommit()', function() {
it('performs a soft reset', async function() {
const workingDirPath = await cloneRepository('multiple-commits');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.appendFileSync(path.join(workingDirPath, 'file.txt'), 'qqq\n', 'utf8');
await repo.git.exec(['add', '.']);
await repo.git.commit('add stuff');
const parentCommit = await repo.git.getCommit('HEAD~');
await repo.undoLastCommit();
const commitAfterReset = await repo.git.getCommit('HEAD');
assert.strictEqual(commitAfterReset.sha, parentCommit.sha);
const fp = await repo.getFilePatchForPath('file.txt', {staged: true});
assert.strictEqual(
fp.toString(),
dedent`
diff --git a/file.txt b/file.txt
--- a/file.txt
+++ b/file.txt
@@ -1,1 +1,2 @@
three
+qqq\n
`,
);
});
it('deletes the HEAD ref when only a single commit is present', async function() {
const workingDirPath = await cloneRepository('three-files');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
fs.appendFileSync(path.join(workingDirPath, 'b.txt'), 'qqq\n', 'utf8');
await repo.git.exec(['add', '.']);
await repo.undoLastCommit();
const fp = await repo.getFilePatchForPath('b.txt', {staged: true});
assert.strictEqual(
fp.toString(),
dedent`
diff --git a/b.txt b/b.txt
new file mode 100644
--- /dev/null
+++ b/b.txt
@@ -0,0 +1,2 @@
+bar
+qqq\n
`,
);
});
it('records an event', async function() {
const workingDirPath = await cloneRepository('multiple-commits');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
sinon.stub(reporterProxy, 'addEvent');
await repo.undoLastCommit();
assert.isTrue(reporterProxy.addEvent.called);
const args = reporterProxy.addEvent.lastCall.args;
assert.deepEqual(args[0], 'undo-last-commit');
assert.deepEqual(args[1], {package: 'github'});
});
it('does not record an event if operation fails', async function() {
const workingDirPath = await cloneRepository('multiple-commits');
const repo = new Repository(workingDirPath);
await repo.getLoadPromise();
sinon.stub(reporterProxy, 'addEvent');
sinon.stub(repo.git, 'reset').throws();
await assert.isRejected(repo.undoLastCommit());
assert.isFalse(reporterProxy.addEvent.called);
});