2020-01-01 13:55:28 +05:30
|
|
|
import { joinPaths, escapeFileUrl } from '~/lib/utils/url_utility';
|
2019-07-31 22:56:46 +05:30
|
|
|
import { __ } from '~/locale';
|
2018-05-09 12:01:36 +05:30
|
|
|
import eventHub from '../../eventhub';
|
|
|
|
import service from '../../services';
|
|
|
|
import * as types from '../mutation_types';
|
2020-06-23 00:09:42 +05:30
|
|
|
import { setPageTitleForFile } from '../utils';
|
2018-11-20 20:47:30 +05:30
|
|
|
import { viewerTypes, stageKeys } from '../../constants';
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
export const closeFile = ({ commit, state, dispatch, getters }, file) => {
|
2018-11-08 19:23:39 +05:30
|
|
|
const { path } = file;
|
2018-05-09 12:01:36 +05:30
|
|
|
const indexOfClosedFile = state.openFiles.findIndex(f => f.key === file.key);
|
|
|
|
const fileWasActive = file.active;
|
|
|
|
|
|
|
|
if (file.pending) {
|
|
|
|
commit(types.REMOVE_PENDING_TAB, file);
|
|
|
|
} else {
|
|
|
|
commit(types.TOGGLE_FILE_OPEN, path);
|
|
|
|
commit(types.SET_FILE_ACTIVE, { path, active: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.openFiles.length > 0 && fileWasActive) {
|
|
|
|
const nextIndexToOpen = indexOfClosedFile === 0 ? 0 : indexOfClosedFile - 1;
|
|
|
|
const nextFileToOpen = state.openFiles[nextIndexToOpen];
|
|
|
|
|
|
|
|
if (nextFileToOpen.pending) {
|
2018-10-15 14:42:47 +05:30
|
|
|
dispatch('updateViewer', viewerTypes.diff);
|
|
|
|
dispatch('openPendingTab', {
|
|
|
|
file: nextFileToOpen,
|
|
|
|
keyPrefix: nextFileToOpen.staged ? 'staged' : 'unstaged',
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
} else {
|
2020-11-24 15:15:51 +05:30
|
|
|
dispatch('router/push', getters.getUrlForPath(nextFileToOpen.path), { root: true });
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
|
|
|
} else if (!state.openFiles.length) {
|
2020-11-24 15:15:51 +05:30
|
|
|
dispatch('router/push', `/project/${state.currentProjectId}/tree/${state.currentBranchId}/`, {
|
|
|
|
root: true,
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
eventHub.$emit(`editor.update.model.dispose.${file.key}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setFileActive = ({ commit, state, getters, dispatch }, path) => {
|
|
|
|
const file = state.entries[path];
|
|
|
|
const currentActiveFile = getters.activeFile;
|
|
|
|
|
|
|
|
if (file.active) return;
|
|
|
|
|
|
|
|
if (currentActiveFile) {
|
|
|
|
commit(types.SET_FILE_ACTIVE, {
|
|
|
|
path: currentActiveFile.path,
|
|
|
|
active: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
commit(types.SET_FILE_ACTIVE, { path, active: true });
|
|
|
|
dispatch('scrollToTab');
|
|
|
|
};
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
export const getFileData = (
|
2019-12-26 22:10:19 +05:30
|
|
|
{ state, commit, dispatch, getters },
|
2021-01-03 14:25:43 +05:30
|
|
|
{ path, makeFileActive = true, openFile = makeFileActive, toggleLoading = true },
|
2019-02-15 15:39:39 +05:30
|
|
|
) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
const file = state.entries[path];
|
2020-03-13 15:44:24 +05:30
|
|
|
const fileDeletedAndReadded = getters.isFileDeletedAndReadded(path);
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (file.raw || (file.tempFile && !file.prevPath && !fileDeletedAndReadded))
|
|
|
|
return Promise.resolve();
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
commit(types.TOGGLE_LOADING, { entry: file, forceValue: true });
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
const url = joinPaths(
|
|
|
|
gon.relative_url_root || '/',
|
|
|
|
state.currentProjectId,
|
2020-03-13 15:44:24 +05:30
|
|
|
'-',
|
2019-12-26 22:10:19 +05:30
|
|
|
file.type,
|
|
|
|
getters.lastCommit && getters.lastCommit.id,
|
|
|
|
escapeFileUrl(file.prevPath || file.path),
|
|
|
|
);
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
return service
|
2019-12-26 22:10:19 +05:30
|
|
|
.getFileData(url)
|
|
|
|
.then(({ data }) => {
|
2019-02-15 15:39:39 +05:30
|
|
|
if (data) commit(types.SET_FILE_DATA, { data, file });
|
|
|
|
if (openFile) commit(types.TOGGLE_FILE_OPEN, path);
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
if (makeFileActive) {
|
|
|
|
setPageTitleForFile(state, file);
|
|
|
|
dispatch('setFileActive', path);
|
|
|
|
}
|
2018-05-09 12:01:36 +05:30
|
|
|
})
|
|
|
|
.catch(() => {
|
2018-11-08 19:23:39 +05:30
|
|
|
dispatch('setErrorMessage', {
|
2020-03-13 15:44:24 +05:30
|
|
|
text: __('An error occurred while loading the file.'),
|
2018-11-08 19:23:39 +05:30
|
|
|
action: payload =>
|
|
|
|
dispatch('getFileData', payload).then(() => dispatch('setErrorMessage', null)),
|
|
|
|
actionText: __('Please try again'),
|
|
|
|
actionPayload: { path, makeFileActive },
|
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
})
|
|
|
|
.finally(() => {
|
2021-01-03 14:25:43 +05:30
|
|
|
if (toggleLoading) commit(types.TOGGLE_LOADING, { entry: file, forceValue: false });
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
export const setFileMrChange = ({ commit }, { file, mrChange }) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
commit(types.SET_FILE_MERGE_REQUEST_CHANGE, { file, mrChange });
|
|
|
|
};
|
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
export const getRawFileData = ({ state, commit, dispatch, getters }, { path }) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
const file = state.entries[path];
|
2020-03-13 15:44:24 +05:30
|
|
|
const stagedFile = state.stagedFiles.find(f => f.path === path);
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
const fileDeletedAndReadded = getters.isFileDeletedAndReadded(path);
|
|
|
|
commit(types.TOGGLE_LOADING, { entry: file, forceValue: true });
|
|
|
|
return service
|
|
|
|
.getRawFileData(fileDeletedAndReadded ? stagedFile : file)
|
|
|
|
.then(raw => {
|
|
|
|
if (!(file.tempFile && !file.prevPath && !fileDeletedAndReadded))
|
|
|
|
commit(types.SET_FILE_RAW_DATA, { file, raw, fileDeletedAndReadded });
|
|
|
|
|
|
|
|
if (file.mrChange && file.mrChange.new_file === false) {
|
|
|
|
const baseSha =
|
|
|
|
(getters.currentMergeRequest && getters.currentMergeRequest.baseCommitSha) || '';
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
return service.getBaseRawFileData(file, state.currentProjectId, baseSha).then(baseRaw => {
|
2020-07-28 23:09:34 +05:30
|
|
|
commit(types.SET_FILE_BASE_RAW_DATA, {
|
|
|
|
file,
|
|
|
|
baseRaw,
|
|
|
|
});
|
|
|
|
return raw;
|
2018-11-08 19:23:39 +05:30
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
}
|
|
|
|
return raw;
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
dispatch('setErrorMessage', {
|
|
|
|
text: __('An error occurred while loading the file content.'),
|
|
|
|
action: payload =>
|
|
|
|
dispatch('getRawFileData', payload).then(() => dispatch('setErrorMessage', null)),
|
|
|
|
actionText: __('Please try again'),
|
|
|
|
actionPayload: { path },
|
2018-05-09 12:01:36 +05:30
|
|
|
});
|
2020-07-28 23:09:34 +05:30
|
|
|
throw e;
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
commit(types.TOGGLE_LOADING, { entry: file, forceValue: false });
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
};
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
export const changeFileContent = ({ commit, state, getters }, { path, content }) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
const file = state.entries[path];
|
2019-12-26 22:10:19 +05:30
|
|
|
commit(types.UPDATE_FILE_CONTENT, {
|
|
|
|
path,
|
2020-06-23 00:09:42 +05:30
|
|
|
content,
|
2019-12-26 22:10:19 +05:30
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
const indexOfChangedFile = state.changedFiles.findIndex(f => f.path === path);
|
|
|
|
|
|
|
|
if (file.changed && indexOfChangedFile === -1) {
|
2020-03-13 15:44:24 +05:30
|
|
|
commit(types.STAGE_CHANGE, { path, diffInfo: getters.getDiffInfo(path) });
|
|
|
|
} else if (!file.changed && !file.tempFile && indexOfChangedFile !== -1) {
|
2018-05-09 12:01:36 +05:30
|
|
|
commit(types.REMOVE_FILE_FROM_CHANGED, path);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
export const restoreOriginalFile = ({ dispatch, state, commit }, path) => {
|
2018-05-09 12:01:36 +05:30
|
|
|
const file = state.entries[path];
|
2020-03-13 15:44:24 +05:30
|
|
|
const isDestructiveDiscard = file.tempFile || file.prevPath;
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if (file.deleted && file.parentPath) {
|
|
|
|
dispatch('restoreTree', file.parentPath);
|
|
|
|
}
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (isDestructiveDiscard) {
|
|
|
|
dispatch('closeFile', file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file.tempFile) {
|
|
|
|
dispatch('deleteEntry', file.path);
|
|
|
|
} else {
|
|
|
|
commit(types.DISCARD_FILE_CHANGES, file.path);
|
|
|
|
}
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-11-18 11:00:15 +05:30
|
|
|
if (file.prevPath) {
|
2020-03-13 15:44:24 +05:30
|
|
|
dispatch('renameEntry', {
|
|
|
|
path: file.path,
|
|
|
|
name: file.prevName,
|
|
|
|
parentPath: file.prevParentPath,
|
|
|
|
});
|
2018-11-18 11:00:15 +05:30
|
|
|
}
|
2020-03-13 15:44:24 +05:30
|
|
|
};
|
2018-11-18 11:00:15 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
export const discardFileChanges = ({ dispatch, state, commit, getters }, path) => {
|
|
|
|
const file = state.entries[path];
|
|
|
|
const isDestructiveDiscard = file.tempFile || file.prevPath;
|
|
|
|
|
|
|
|
dispatch('restoreOriginalFile', path);
|
|
|
|
|
|
|
|
if (!isDestructiveDiscard && file.path === getters.activeFile?.path) {
|
2018-10-15 14:42:47 +05:30
|
|
|
dispatch('updateDelayViewerUpdated', true)
|
|
|
|
.then(() => {
|
2020-11-24 15:15:51 +05:30
|
|
|
dispatch('router/push', getters.getUrlForPath(file.path), { root: true });
|
2018-10-15 14:42:47 +05:30
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
throw e;
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
commit(types.REMOVE_FILE_FROM_CHANGED, path);
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
eventHub.$emit(`editor.update.model.new.content.${file.key}`, file.content);
|
|
|
|
eventHub.$emit(`editor.update.model.dispose.unstaged-${file.key}`, file.content);
|
2018-05-09 12:01:36 +05:30
|
|
|
};
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
export const stageChange = ({ commit, dispatch, getters }, path) => {
|
|
|
|
const stagedFile = getters.getStagedFile(path);
|
|
|
|
const openFile = getters.getOpenFile(path);
|
2018-10-15 14:42:47 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
commit(types.STAGE_CHANGE, { path, diffInfo: getters.getDiffInfo(path) });
|
2018-10-15 14:42:47 +05:30
|
|
|
commit(types.SET_LAST_COMMIT_MSG, '');
|
|
|
|
|
|
|
|
if (stagedFile) {
|
|
|
|
eventHub.$emit(`editor.update.model.new.content.staged-${stagedFile.key}`, stagedFile.content);
|
2018-05-09 12:01:36 +05:30
|
|
|
}
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
const file = getters.getStagedFile(path);
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (openFile && openFile.active && file) {
|
2018-11-20 20:47:30 +05:30
|
|
|
dispatch('openPendingTab', {
|
|
|
|
file,
|
|
|
|
keyPrefix: stageKeys.staged,
|
|
|
|
});
|
|
|
|
}
|
2018-10-15 14:42:47 +05:30
|
|
|
};
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
export const unstageChange = ({ commit, dispatch, getters }, path) => {
|
|
|
|
const openFile = getters.getOpenFile(path);
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
commit(types.UNSTAGE_CHANGE, { path, diffInfo: getters.getDiffInfo(path) });
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
const file = getters.getChangedFile(path);
|
2018-11-20 20:47:30 +05:30
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
if (openFile && openFile.active && file) {
|
2018-11-20 20:47:30 +05:30
|
|
|
dispatch('openPendingTab', {
|
|
|
|
file,
|
|
|
|
keyPrefix: stageKeys.unstaged,
|
|
|
|
});
|
|
|
|
}
|
2018-10-15 14:42:47 +05:30
|
|
|
};
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
export const openPendingTab = ({ commit, dispatch, getters, state }, { file, keyPrefix }) => {
|
2018-10-15 14:42:47 +05:30
|
|
|
if (getters.activeFile && getters.activeFile.key === `${keyPrefix}-${file.key}`) return false;
|
|
|
|
|
|
|
|
state.openFiles.forEach(f => eventHub.$emit(`editor.update.model.dispose.${f.key}`));
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
commit(types.ADD_PENDING_TAB, { file, keyPrefix });
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
dispatch('router/push', `/project/${state.currentProjectId}/tree/${state.currentBranchId}/`, {
|
2020-06-23 00:09:42 +05:30
|
|
|
root: true,
|
|
|
|
});
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const removePendingTab = ({ commit }, file) => {
|
|
|
|
commit(types.REMOVE_PENDING_TAB, file);
|
|
|
|
|
|
|
|
eventHub.$emit(`editor.update.model.dispose.${file.key}`);
|
|
|
|
};
|
2019-09-04 21:01:54 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
export const triggerFilesChange = (ctx, payload = {}) => {
|
2019-09-04 21:01:54 +05:30
|
|
|
// Used in EE for file mirroring
|
2021-01-29 00:20:46 +05:30
|
|
|
eventHub.$emit('ide.files.change', payload);
|
2019-09-04 21:01:54 +05:30
|
|
|
};
|