Commit ba63bda9 authored by Phil Hughes's avatar Phil Hughes

correctly stages and unstages files

after a commit the files are correctly reset
correctly enables the ability to have different staged & unstaged changes in the same file
parent 51c64f3f
<script> <script>
import { mapActions } from 'vuex';
import Icon from '~/vue_shared/components/icon.vue'; import Icon from '~/vue_shared/components/icon.vue';
import StageButton from './stage_button.vue'; import StageButton from './stage_button.vue';
import UnstageButton from './unstage_button.vue'; import UnstageButton from './unstage_button.vue';
...@@ -57,7 +58,7 @@ export default { ...@@ -57,7 +58,7 @@ export default {
</button> </button>
<component <component
:is="actionComponent" :is="actionComponent"
:file="file" :path="file.path"
/> />
</div> </div>
</template> </template>
...@@ -7,8 +7,8 @@ export default { ...@@ -7,8 +7,8 @@ export default {
Icon, Icon,
}, },
props: { props: {
file: { path: {
type: Object, type: String,
required: true, required: true,
}, },
}, },
...@@ -27,7 +27,7 @@ export default { ...@@ -27,7 +27,7 @@ export default {
type="button" type="button"
class="btn btn-blank append-right-5" class="btn btn-blank append-right-5"
:aria-label="__('Stage change')" :aria-label="__('Stage change')"
@click.stop="stageChange(file)" @click.stop="stageChange(path)"
> >
<icon <icon
name="mobile-issue-close" name="mobile-issue-close"
...@@ -38,7 +38,7 @@ export default { ...@@ -38,7 +38,7 @@ export default {
type="button" type="button"
class="btn btn-blank" class="btn btn-blank"
:aria-label="__('Discard change')" :aria-label="__('Discard change')"
@click.stop="discardFileChanges(file)" @click.stop="discardFileChanges(path)"
> >
<icon <icon
name="remove" name="remove"
......
...@@ -7,8 +7,8 @@ export default { ...@@ -7,8 +7,8 @@ export default {
Icon, Icon,
}, },
props: { props: {
file: { path: {
type: Object, type: String,
required: true, required: true,
}, },
}, },
...@@ -27,7 +27,7 @@ export default { ...@@ -27,7 +27,7 @@ export default {
type="button" type="button"
class="btn btn-blank" class="btn btn-blank"
:aria-label="__('Unstage change')" :aria-label="__('Unstage change')"
@click="unstageChange(file)" @click="unstageChange(path)"
> >
<icon <icon
name="history" name="history"
......
...@@ -32,9 +32,8 @@ export default { ...@@ -32,9 +32,8 @@ export default {
}, },
}, },
computed: { computed: {
...mapState(['stagedFiles', 'rightPanelCollapsed']), ...mapState(['changedFiles', 'stagedFiles', 'rightPanelCollapsed']),
...mapState('commit', ['commitMessage', 'submitCommitLoading']), ...mapState('commit', ['commitMessage', 'submitCommitLoading']),
...mapGetters(['unstagedFiles']),
...mapGetters('commit', [ ...mapGetters('commit', [
'commitButtonDisabled', 'commitButtonDisabled',
'discardDraftButtonDisabled', 'discardDraftButtonDisabled',
...@@ -74,12 +73,12 @@ export default { ...@@ -74,12 +73,12 @@ export default {
</template> </template>
</modal> </modal>
<template <template
v-if="unstagedFiles.length || stagedFiles.length" v-if="changedFiles.length || stagedFiles.length"
> >
<commit-files-list <commit-files-list
icon="unstaged" icon="unstaged"
:title="__('Unstaged')" :title="__('Unstaged')"
:file-list="unstagedFiles" :file-list="changedFiles"
action="stageAllChanges" action="stageAllChanges"
:action-btn-text="__('Stage all')" :action-btn-text="__('Stage all')"
item-action-component="stage-button" item-action-component="stage-button"
......
...@@ -69,9 +69,12 @@ export default class Model { ...@@ -69,9 +69,12 @@ export default class Model {
); );
} }
updateContent(content) { updateContent({ content, changed }) {
this.getOriginalModel().setValue(content); this.getOriginalModel().setValue(content);
this.getModel().setValue(content);
if (!changed) {
this.getModel().setValue(content);
}
} }
dispose() { dispose() {
......
...@@ -116,11 +116,11 @@ export const scrollToTab = () => { ...@@ -116,11 +116,11 @@ export const scrollToTab = () => {
}; };
export const stageAllChanges = ({ state, commit }) => { export const stageAllChanges = ({ state, commit }) => {
[...state.changedFiles].forEach(file => commit(types.STAGE_CHANGE, file)); state.changedFiles.forEach(file => commit(types.STAGE_CHANGE, file.path));
}; };
export const unstageAllChanges = ({ state, commit }) => { export const unstageAllChanges = ({ state, commit }) => {
[...state.stagedFiles].forEach(file => commit(types.UNSTAGE_CHANGE, file)); state.stagedFiles.forEach(file => commit(types.UNSTAGE_CHANGE, file.path));
}; };
export const updateViewer = ({ commit }, viewer) => { export const updateViewer = ({ commit }, viewer) => {
......
...@@ -145,10 +145,10 @@ export const discardFileChanges = ({ state, commit }, path) => { ...@@ -145,10 +145,10 @@ export const discardFileChanges = ({ state, commit }, path) => {
eventHub.$emit(`editor.update.model.content.${file.path}`, file.raw); eventHub.$emit(`editor.update.model.content.${file.path}`, file.raw);
}; };
export const stageChange = ({ commit }, file) => { export const stageChange = ({ commit }, path) => {
commit(types.STAGE_CHANGE, file); commit(types.STAGE_CHANGE, path);
}; };
export const unstageChange = ({ commit }, file) => { export const unstageChange = ({ commit }, path) => {
commit(types.UNSTAGE_CHANGE, file); commit(types.UNSTAGE_CHANGE, path);
}; };
...@@ -24,9 +24,8 @@ export const projectsWithTrees = state => ...@@ -24,9 +24,8 @@ export const projectsWithTrees = state =>
}); });
// eslint-disable-next-line no-confusing-arrow // eslint-disable-next-line no-confusing-arrow
export const currentIcon = state => export const collapseButtonIcon = state =>
state.rightPanelCollapsed ? 'angle-double-left' : 'angle-double-right'; state.rightPanelCollapsed ? 'angle-double-left' : 'angle-double-right';
export const hasChanges = state => !!state.changedFiles.length; export const hasChanges = state =>
!!state.changedFiles.length || !!state.stagedFiles.length;
export const unstagedFiles = state => state.changedFiles.filter(f => !f.staged);
...@@ -100,35 +100,22 @@ export const updateFilesAfterCommit = ( ...@@ -100,35 +100,22 @@ export const updateFilesAfterCommit = (
{ root: true }, { root: true },
); );
rootState.changedFiles.forEach(entry => { rootState.stagedFiles.forEach(file => {
commit( const changedFile = rootState.changedFiles.find(f => f.path === file.path);
rootTypes.SET_LAST_COMMIT_DATA,
{
entry,
lastCommit,
},
{ root: true },
);
eventHub.$emit(`editor.update.model.content.${entry.path}`, entry.content);
commit( commit(
rootTypes.SET_FILE_RAW_DATA, rootTypes.UPDATE_FILE_AFTER_COMMIT,
{ {
file: entry, file,
raw: entry.content, lastCommit,
}, },
{ root: true }, { root: true },
); );
commit( eventHub.$emit(`editor.update.model.content.${file.path}`, {
rootTypes.TOGGLE_FILE_CHANGED, content: file.content,
{ changed: !!changedFile,
file: entry, });
changed: false,
},
{ root: true },
);
}); });
if ( if (
......
import * as consts from './constants'; import * as consts from './constants';
export const discardDraftButtonDisabled = state => state.commitMessage === '' || state.submitCommitLoading; export const discardDraftButtonDisabled = state =>
state.commitMessage === '' || state.submitCommitLoading;
export const commitButtonDisabled = (state, getters, rootState) => export const commitButtonDisabled = (state, getters, rootState) =>
getters.discardDraftButtonDisabled || !rootState.changedFiles.length; getters.discardDraftButtonDisabled || !rootState.stagedFiles.length;
export const newBranchName = (state, _, rootState) => export const newBranchName = (state, _, rootState) =>
`${gon.current_username}-${rootState.currentBranchId}-patch-${`${new Date().getTime()}`.substr(-5)}`; `${gon.current_username}-${
rootState.currentBranchId
}-patch-${`${new Date().getTime()}`.substr(-5)}`;
export const branchName = (state, getters, rootState) => { export const branchName = (state, getters, rootState) => {
if ( if (
......
...@@ -45,3 +45,5 @@ export const UPDATE_DELAY_VIEWER_CHANGE = 'UPDATE_DELAY_VIEWER_CHANGE'; ...@@ -45,3 +45,5 @@ export const UPDATE_DELAY_VIEWER_CHANGE = 'UPDATE_DELAY_VIEWER_CHANGE';
export const CLEAR_STAGED_CHANGES = 'CLEAR_STAGED_CHANGES'; export const CLEAR_STAGED_CHANGES = 'CLEAR_STAGED_CHANGES';
export const STAGE_CHANGE = 'STAGE_CHANGE'; export const STAGE_CHANGE = 'STAGE_CHANGE';
export const UNSTAGE_CHANGE = 'UNSTAGE_CHANGE'; export const UNSTAGE_CHANGE = 'UNSTAGE_CHANGE';
export const UPDATE_FILE_AFTER_COMMIT = 'UPDATE_FILE_AFTER_COMMIT';
...@@ -104,6 +104,21 @@ export default { ...@@ -104,6 +104,21 @@ export default {
delayViewerUpdated, delayViewerUpdated,
}); });
}, },
[types.UPDATE_FILE_AFTER_COMMIT](state, { file, lastCommit }) {
const changedFile = state.changedFiles.find(f => f.path === file.path);
Object.assign(state.entries[file.path], {
raw: file.content,
changed: !!changedFile,
lastCommit: Object.assign(state.entries[file.path].lastCommit, {
id: lastCommit.commit.id,
url: lastCommit.commit_path,
message: lastCommit.commit.message,
author: lastCommit.commit.author_name,
updatedAt: lastCommit.commit.authored_date,
}),
});
},
...projectMutations, ...projectMutations,
...fileMutations, ...fileMutations,
...treeMutations, ...treeMutations,
......
import * as types from '../mutation_types'; import * as types from '../mutation_types';
import { findIndexOfFile, findEntry } from '../utils';
export default { export default {
[types.SET_FILE_ACTIVE](state, { path, active }) { [types.SET_FILE_ACTIVE](state, { path, active }) {
...@@ -76,31 +75,42 @@ export default { ...@@ -76,31 +75,42 @@ export default {
changedFiles: state.changedFiles.filter(f => f.path !== path), changedFiles: state.changedFiles.filter(f => f.path !== path),
}); });
}, },
[types.STAGE_CHANGE](state, file) { [types.STAGE_CHANGE](state, path) {
const stagedFile = findEntry(state.stagedFiles, 'blob', file.name); const stagedFile = state.stagedFiles.find(f => f.path === path);
Object.assign(file, { Object.assign(state, {
staged: true, changedFiles: state.changedFiles.filter(f => f.path !== path),
}); });
if (stagedFile) { if (stagedFile) {
Object.assign(stagedFile, { Object.assign(stagedFile, {
...file, ...state.entries[path],
}); });
} else { } else {
state.stagedFiles.push({ Object.assign(state, {
...file, stagedFiles: state.stagedFiles.concat({
...state.entries[path],
}),
}); });
} }
}, },
[types.UNSTAGE_CHANGE](state, file) { [types.UNSTAGE_CHANGE](state, path) {
const indexOfStagedFile = findIndexOfFile(state.stagedFiles, file); const changedFile = state.changedFiles.find(f => f.path === path);
const changedFile = findEntry(state.changedFiles, 'blob', file.name); const stagedFile = state.stagedFiles.find(f => f.path === path);
state.stagedFiles.splice(indexOfStagedFile, 1); if (!changedFile && stagedFile) {
Object.assign(state.entries[path], {
...stagedFile,
changed: true,
});
Object.assign(changedFile, { Object.assign(state, {
staged: false, changedFiles: state.changedFiles.concat(state.entries[path]),
});
}
Object.assign(state, {
stagedFiles: state.stagedFiles.filter(f => f.path !== path),
}); });
}, },
[types.TOGGLE_FILE_CHANGED](state, { file, changed }) { [types.TOGGLE_FILE_CHANGED](state, { file, changed }) {
......
...@@ -13,7 +13,6 @@ export const dataStructure = () => ({ ...@@ -13,7 +13,6 @@ export const dataStructure = () => ({
opened: false, opened: false,
active: false, active: false,
changed: false, changed: false,
staged: false,
lastCommitPath: '', lastCommitPath: '',
lastCommit: { lastCommit: {
id: '', id: '',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment