diff --git a/lib/models/repository.js b/lib/models/repository.js index 6bff89e491..720c771b9a 100644 --- a/lib/models/repository.js +++ b/lib/models/repository.js @@ -175,12 +175,10 @@ export default class Repository { } refresh() { - /* eslint-disable no-console */ this.clearCachedFilePatches(); this.fileStatusesPromise = null; this.stagedChangesSinceParentCommitPromise = null; this.didUpdate(); - /* eslint-enable no-console */ } isGitRepository() { @@ -322,19 +320,23 @@ export default class Repository { async stageFiles(paths) { await this.git.stageFiles(paths); + this.refresh(); } async unstageFiles(paths) { await this.git.unstageFiles(paths); + this.refresh(); } async stageFilesFromParentCommit(paths) { await this.git.unstageFiles(paths, 'HEAD~'); + this.refresh(); } async applyPatchToIndex(filePatch) { const patchStr = filePatch.getHeaderString() + filePatch.toString(); await this.git.applyPatch(patchStr, {index: true}); + this.refresh(); } async applyPatchToWorkdir(filePatch) { @@ -563,5 +565,6 @@ export default class Repository { const absPath = path.join(this.getWorkingDirectoryPath(), filePath); return deleteFileOrFolder(absPath); })); + this.refresh(); } } diff --git a/test/models/repository.test.js b/test/models/repository.test.js index 70d1b03441..d445eb7873 100644 --- a/test/models/repository.test.js +++ b/test/models/repository.test.js @@ -74,17 +74,21 @@ describe('Repository', function() { it('can stage and unstage modified files', async function() { const workingDirPath = await cloneRepository('three-files'); const repo = await buildRepository(workingDirPath); + sinon.spy(repo, 'refresh'); + 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.equal(repo.refresh.callCount, 1); assert.deepEqual(await repo.getUnstagedChanges(), []); assert.deepEqual(await repo.getStagedChanges(), [patch]); await repo.unstageFiles([filePath]); - repo.refresh(); + + assert.equal(repo.refresh.callCount, 2); assert.deepEqual(await repo.getUnstagedChanges(), [patch]); assert.deepEqual(await repo.getStagedChanges(), []); }); @@ -97,12 +101,12 @@ describe('Repository', function() { 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(), []); }); @@ -116,12 +120,12 @@ describe('Repository', function() { 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(), []); }); @@ -134,12 +138,12 @@ describe('Repository', function() { 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(), []); }); @@ -169,7 +173,7 @@ describe('Repository', function() { }); await repo.stageFiles(['file.txt']); - repo.refresh(); + assertDeepPropertyVals(await repo.getFilePatchForPath('file.txt', {staged: true, amending: true}), { oldPath: 'file.txt', newPath: 'file.txt', @@ -186,7 +190,7 @@ describe('Repository', function() { }); await repo.stageFilesFromParentCommit(['file.txt']); - repo.refresh(); + assert.deepEqual(await repo.getStagedChangesSinceParentCommit(), []); }); }); @@ -225,6 +229,8 @@ describe('Repository', function() { it('can stage and unstage modified files', async function() { const workingDirPath = await cloneRepository('three-files'); const repo = await buildRepository(workingDirPath); + sinon.spy(repo, 'refresh'); + 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')); @@ -233,7 +239,8 @@ describe('Repository', function() { const unstagedPatch2 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt')); await repo.applyPatchToIndex(unstagedPatch1); - repo.refresh(); + + assert.equal(repo.refresh.callCount, 2); const stagedPatch1 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt'), {staged: true}); assert.deepEqual(stagedPatch1, unstagedPatch1); @@ -243,7 +250,8 @@ describe('Repository', function() { assert.deepEqual(stagedChanges, ['subdir-1/a.txt']); await repo.applyPatchToIndex(unstagedPatch1.getUnstagePatch()); - repo.refresh(); + + assert.equal(repo.refresh.callCount, 3); const unstagedPatch3 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt')); assert.deepEqual(unstagedPatch3, unstagedPatch2); unstagedChanges = (await repo.getUnstagedChanges()).map(c => c.filePath); @@ -257,6 +265,7 @@ describe('Repository', function() { it('creates a commit that contains the staged changes', async function() { const workingDirPath = await cloneRepository('three-files'); const repo = await buildRepository(workingDirPath); + sinon.spy(repo, 'refresh'); assert.equal((await repo.getLastCommit()).message, 'Initial commit'); fs.writeFileSync(path.join(workingDirPath, 'subdir-1', 'a.txt'), 'qux\nfoo\nbar\n', 'utf8'); @@ -265,8 +274,9 @@ describe('Repository', function() { repo.refresh(); await repo.applyPatchToIndex(unstagedPatch1); await repo.commit('Commit 1'); + + assert.equal(repo.refresh.callCount, 2); assert.equal((await repo.getLastCommit()).message, 'Commit 1'); - repo.refresh(); assert.deepEqual(await repo.getStagedChanges(), []); const unstagedChanges = await repo.getUnstagedChanges(); assert.equal(unstagedChanges.length, 1); @@ -274,8 +284,9 @@ describe('Repository', function() { const unstagedPatch2 = await repo.getFilePatchForPath(path.join('subdir-1', 'a.txt')); await repo.applyPatchToIndex(unstagedPatch2); await repo.commit('Commit 2'); + + assert.equal(repo.refresh.callCount, 3); assert.equal((await repo.getLastCommit()).message, 'Commit 2'); - repo.refresh(); assert.deepEqual(await repo.getStagedChanges(), []); assert.deepEqual(await repo.getUnstagedChanges(), []); }); @@ -391,7 +402,7 @@ describe('Repository', function() { }); describe('push()', function() { - it('sends commits to the remote and updates ', async function() { + it('sends commits to the remote and updates', async function() { const {localRepoPath, remoteRepoPath} = await setUpLocalAndRemoteRepositories(); const localRepo = await buildRepository(localRepoPath); @@ -545,14 +556,14 @@ describe('Repository', function() { 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']); @@ -560,21 +571,21 @@ describe('Repository', function() { // 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']); @@ -698,7 +709,7 @@ describe('Repository', function() { assert.equal(unstagedChanges.length, 3); await repo.discardWorkDirChangesForPaths(unstagedChanges.map(c => c.filePath)); - repo.refresh(); + assert.deepEqual(await repo.getUnstagedChanges(), []); }); @@ -711,7 +722,7 @@ describe('Repository', function() { assert.equal(unstagedChanges.length, 2); await repo.discardWorkDirChangesForPaths(unstagedChanges.map(c => c.filePath)); - repo.refresh(); + assert.deepEqual(await repo.getUnstagedChanges(), []); }); @@ -724,7 +735,7 @@ describe('Repository', function() { assert.equal(unstagedChanges.length, 2); await repo.discardWorkDirChangesForPaths(unstagedChanges.map(c => c.filePath)); - repo.refresh(); + assert.deepEqual(await repo.getUnstagedChanges(), []); }); });