Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit a21992d

Browse files
committed
Fix for undo discard of files with same name
1 parent 853ea19 commit a21992d

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

lib/models/discard-history.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export default class DiscardHistory {
127127
await this.expandBlobToFile(path.join(tempFolderPath, `${filePath}-before-discard`), beforeSha);
128128
const commonBasePath = !afterSha ? null :
129129
await this.expandBlobToFile(path.join(tempFolderPath, `${filePath}-after-discard`), afterSha);
130-
const resultPath = path.join(tempFolderPath, `~${path.basename(filePath)}-merge-result`);
130+
const resultPath = path.join(tempFolderPath, path.join(path.dirname(filePath), `~${path.basename(filePath)}-merge-result`));
131131
return {filePath, commonBasePath, theirsPath, resultPath, theirsSha: beforeSha, commonBaseSha: afterSha};
132132
});
133133
return await Promise.all(pathPromises);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import DiscardHistory from '../../lib/models/discard-history';
2+
import Repository from '../../lib/models/repository';
3+
import path from 'path';
4+
import fs from 'fs-extra';
5+
import {
6+
cloneRepository, setUpLocalAndRemoteRepositories, getHeadCommitOnRemote,
7+
assertDeepPropertyVals, assertEqualSortedArraysByKey, FAKE_USER, wireUpObserver, expectEvents,
8+
} from '../helpers';
9+
import { formatWithOptions } from 'util';
10+
11+
describe('DiscardHistory', () => {
12+
describe('restoreLastDiscardInTempFiles', () => {
13+
let repository;
14+
15+
it('Should undo discard of files with same name', async function() {
16+
// Setup
17+
const workdir = await cloneRepository('three-files');
18+
repository = new Repository(workdir);
19+
await repository.getLoadPromise();
20+
// Create 2 files with same name
21+
fs.mkdirSync(path.join(workdir, 'sub1'));
22+
fs.mkdirSync(path.join(workdir, 'sub2'));
23+
const file1 = path.join(workdir, 'sub1', 'a.txt')
24+
const file2 = path.join(workdir, 'sub2', 'a.txt');
25+
fs.writeFileSync(file1, 'foo1\n', {encoding: 'utf8'});
26+
fs.writeFileSync(file2, 'foo2\n', {encoding: 'utf8'});
27+
28+
console.log('New file created');
29+
await repository.updateDiscardHistory();
30+
const isSafe = function() { return true; };
31+
const unstagedChanges = await repository.getUnstagedChanges();
32+
const files = ['sub2/a.txt', 'sub1/a.txt'];
33+
await repository.storeBeforeAndAfterBlobs(files, isSafe, () => {
34+
files.pop();
35+
files.pop();
36+
});
37+
38+
// Discard changes and restore last discard
39+
await repository.discardWorkDirChangesForPaths(unstagedChanges.map(c => c.filePath));
40+
const results = await repository.restoreLastDiscardInTempFiles(isSafe, null);
41+
42+
// Check results
43+
results.map((merge_result, i) => {
44+
const {filePath, resultPath, conflict} = merge_result;
45+
if(path.dirname(filePath).endsWith('1'))
46+
assert.equal(fs.readFileSync(resultPath, 'utf8'), 'foo1\n');
47+
else
48+
assert.equal(fs.readFileSync(resultPath, 'utf8'), 'foo2\n');
49+
});
50+
});
51+
52+
});
53+
54+
describe('expandBlobsToFilesInTempFolder', () => {
55+
56+
it('Should produce unique resultPaths', async () => {
57+
const history = new DiscardHistory(null, () => {
58+
return null;
59+
}, null, null);
60+
// simulate snapshots
61+
const snapshots = [
62+
{filePath: 'subfolder1/file', beforeSha: 'beforeSha', afterSha: 'afterSha'},
63+
{filePath: 'subfolder2/file', beforeSha: 'beforeSha', afterSha: 'afterSha'},
64+
];
65+
66+
// Perform test
67+
const results = await history.expandBlobsToFilesInTempFolder(snapshots);
68+
69+
// Check results
70+
assert.isFalse(results[0].resultPath === results[1].resultPath);
71+
});
72+
73+
});
74+
});

0 commit comments

Comments
 (0)