2020-05-29 14:08:26 -04:00
|
|
|
import { commitActionTypes } from '~/ide/constants';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { commitActionForFile } from '~/ide/stores/utils';
|
2020-05-29 14:08:26 -04:00
|
|
|
import createFileDiff from './create_file_diff';
|
|
|
|
|
|
|
|
const getDeletedParents = (entries, file) => {
|
|
|
|
const parent = file.parentPath && entries[file.parentPath];
|
|
|
|
|
|
|
|
if (parent && parent.deleted) {
|
|
|
|
return [parent, ...getDeletedParents(entries, parent)];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
};
|
|
|
|
|
|
|
|
const filesWithChanges = ({ stagedFiles = [], changedFiles = [], entries = {} }) => {
|
|
|
|
// We need changed files to overwrite staged, so put them at the end.
|
|
|
|
const changes = stagedFiles.concat(changedFiles).reduce((acc, file) => {
|
|
|
|
const key = file.path;
|
|
|
|
const action = commitActionForFile(file);
|
|
|
|
const prev = acc[key];
|
|
|
|
|
|
|
|
// If a file was deleted, which was previously added, then we should do nothing.
|
|
|
|
if (action === commitActionTypes.delete && prev && prev.action === commitActionTypes.create) {
|
|
|
|
delete acc[key];
|
|
|
|
} else {
|
|
|
|
acc[key] = { action, file };
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
// We need to clean "move" actions, because we can only support 100% similarity moves at the moment.
|
|
|
|
// This is because the previous file's content might not be loaded.
|
|
|
|
Object.values(changes)
|
2020-12-23 13:10:19 -05:00
|
|
|
.filter((change) => change.action === commitActionTypes.move)
|
|
|
|
.forEach((change) => {
|
2020-05-29 14:08:26 -04:00
|
|
|
const prev = changes[change.file.prevPath];
|
|
|
|
|
|
|
|
if (!prev) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change.file.content === prev.file.content) {
|
|
|
|
// If content is the same, continue with the move but don't do the prevPath's delete.
|
|
|
|
delete changes[change.file.prevPath];
|
|
|
|
} else {
|
|
|
|
// Otherwise, treat the move as a delete / create.
|
|
|
|
Object.assign(change, { action: commitActionTypes.create });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Next, we need to add deleted directories by looking at the parents
|
|
|
|
Object.values(changes)
|
2020-12-23 13:10:19 -05:00
|
|
|
.filter((change) => change.action === commitActionTypes.delete && change.file.parentPath)
|
2020-05-29 14:08:26 -04:00
|
|
|
.forEach(({ file }) => {
|
|
|
|
// Do nothing if we've already visited this directory.
|
|
|
|
if (changes[file.parentPath]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
getDeletedParents(entries, file).forEach((parent) => {
|
2020-05-29 14:08:26 -04:00
|
|
|
changes[parent.path] = { action: commitActionTypes.delete, file: parent };
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return Object.values(changes);
|
|
|
|
};
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
const createDiff = (state) => {
|
2020-05-29 14:08:26 -04:00
|
|
|
const changes = filesWithChanges(state);
|
|
|
|
|
2020-12-23 13:10:19 -05:00
|
|
|
const toDelete = changes
|
|
|
|
.filter((x) => x.action === commitActionTypes.delete)
|
|
|
|
.map((x) => x.file.path);
|
2020-05-29 14:08:26 -04:00
|
|
|
|
|
|
|
const patch = changes
|
2020-12-23 13:10:19 -05:00
|
|
|
.filter((x) => x.action !== commitActionTypes.delete)
|
2020-05-29 14:08:26 -04:00
|
|
|
.map(({ file, action }) => createFileDiff(file, action))
|
|
|
|
.join('');
|
|
|
|
|
|
|
|
return {
|
|
|
|
patch,
|
|
|
|
toDelete,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default createDiff;
|