Commit c8c1ed46 authored by Phil Hughes's avatar Phil Hughes

correctly commits changes

parent 91e1f3c9
<script> <script>
import { mapActions, mapState, mapGetters } from 'vuex'; import { mapState } from 'vuex';
import * as consts from '../../stores/modules/commit/constants'; import * as consts from '../../stores/modules/commit/constants';
import RadioGroup from './radio_group.vue';
export default { export default {
components: {
RadioGroup,
},
data() { data() {
return { return {
COMMIT_TO_CURRENT_BRANCH: consts.COMMIT_TO_CURRENT_BRANCH, COMMIT_TO_CURRENT_BRANCH: consts.COMMIT_TO_CURRENT_BRANCH,
...@@ -14,65 +18,26 @@ ...@@ -14,65 +18,26 @@
...mapState([ ...mapState([
'currentBranchId', 'currentBranchId',
]), ]),
...mapState('commit', [
'commitAction',
]),
...mapGetters('commit', [
'newBranchName',
]),
},
methods: {
...mapActions('commit', [
'updateCommitAction',
'updateBranchName',
]),
}, },
}; };
</script> </script>
<template> <template>
<div> <div class="append-bottom-15">
<fieldset> <radio-group
<label> :value="COMMIT_TO_CURRENT_BRANCH"
<input :label="`Commit to ${currentBranchId} branch`"
type="radio" :checked="true"
name="commit-action" />
:value="COMMIT_TO_CURRENT_BRANCH" <radio-group
@change="updateCommitAction($event.target.value)" :value="COMMIT_TO_NEW_BRANCH"
checked label="Create a new branch"
/> :show-input="true"
Commit to <strong>{{ currentBranchId }}</strong> branch />
</label> <radio-group
</fieldset> :value="COMMIT_TO_NEW_BRANCH_MR"
<fieldset> label="Create a new branch and merge request"
<label> :show-input="true"
<input />
type="radio"
name="commit-action"
:value="COMMIT_TO_NEW_BRANCH"
@change="updateCommitAction($event.target.value)"
/>
Create a new branch
</label>
<template v-if="commitAction === '2'">
<input
type="text"
class="form-control input-sm"
:placeholder="newBranchName"
@input="updateBranchName($event.target.value)"
/>
</template>
</fieldset>
<fieldset>
<label>
<input
type="radio"
name="commit-action"
:value="COMMIT_TO_NEW_BRANCH_MR"
@change="updateCommitAction($event.target.value)"
/>
Create a new branch and merge request
</label>
</fieldset>
</div> </div>
</template> </template>
<script>
import { mapActions, mapState, mapGetters } from 'vuex';
export default {
props: {
value: {
type: String,
required: true,
},
label: {
type: String,
required: true,
},
checked: {
type: Boolean,
required: false,
default: false,
},
showInput: {
type: Boolean,
required: false,
default: false,
},
},
computed: {
...mapState('commit', [
'commitAction',
]),
...mapGetters('commit', [
'newBranchName',
]),
},
methods: {
...mapActions('commit', [
'updateCommitAction',
]),
},
};
</script>
<template>
<fieldset>
<label>
<input
type="radio"
name="commit-action"
:value="value"
@change="updateCommitAction($event.target.value)"
:checked="checked"
v-once
/>
{{ label }}
</label>
<template
v-if="commitAction === value && showInput"
>
<input
type="text"
class="form-control input-sm"
:placeholder="newBranchName"
@input="updateBranchName($event.target.value)"
/>
</template>
</fieldset>
</template>
<script> <script>
import { mapState, mapActions, mapGetters } from 'vuex'; import { mapState, mapActions, mapGetters } from 'vuex';
import * as consts from '../stores/modules/commit/constants';
import tooltip from '../../vue_shared/directives/tooltip'; import tooltip from '../../vue_shared/directives/tooltip';
import icon from '../../vue_shared/components/icon.vue'; import icon from '../../vue_shared/components/icon.vue';
import modal from '../../vue_shared/components/modal.vue'; import modal from '../../vue_shared/components/modal.vue';
...@@ -28,11 +29,6 @@ export default { ...@@ -28,11 +29,6 @@ export default {
required: true, required: true,
}, },
}, },
data() {
return {
showNewBranchModal: false,
};
},
computed: { computed: {
...mapState([ ...mapState([
'currentProjectId', 'currentProjectId',
...@@ -56,7 +52,6 @@ export default { ...@@ -56,7 +52,6 @@ export default {
}, },
methods: { methods: {
...mapActions([ ...mapActions([
'checkCommitStatus',
'getTreeData', 'getTreeData',
'setPanelCollapsedStatus', 'setPanelCollapsedStatus',
]), ]),
...@@ -64,31 +59,18 @@ export default { ...@@ -64,31 +59,18 @@ export default {
'updateCommitMessage', 'updateCommitMessage',
'discardDraft', 'discardDraft',
'commitChanges', 'commitChanges',
'updateCommitAction',
]), ]),
makeCommit() {
this.commitChanges();
},
tryCommit() {
this.submitCommitLoading = true;
this.checkCommitStatus()
.then((branchChanged) => {
if (branchChanged) {
this.showNewBranchModal = true;
} else {
this.makeCommit();
}
})
.catch(() => {
this.submitCommitLoading = false;
});
},
toggleCollapsed() { toggleCollapsed() {
this.setPanelCollapsedStatus({ this.setPanelCollapsedStatus({
side: 'right', side: 'right',
collapsed: !this.rightPanelCollapsed, collapsed: !this.rightPanelCollapsed,
}); });
}, },
forceCreateNewBranch() {
return this.updateCommitAction(consts.COMMIT_TO_NEW_BRANCH)
.then(() => this.commitChanges());
},
}, },
}; };
</script> </script>
...@@ -101,15 +83,16 @@ export default { ...@@ -101,15 +83,16 @@ export default {
}" }"
> >
<modal <modal
v-if="showNewBranchModal" id="ide-create-branch-modal"
:primary-button-label="__('Create new branch')" :primary-button-label="__('Create new branch')"
kind="primary" kind="success"
:title="__('Branch has changed')" :title="__('Branch has changed')"
:text="__(`This branch has changed since @submit="forceCreateNewBranch"
you started editing. Would you like to create a new branch?`)" >
@cancel="showNewBranchModal = false" <template slot="body">
@submit="makeCommit(true)" {{ __(`This branch has changed since you started editing. Would you like to create a new branch?`) }}
/> </template>
</modal>
<commit-files-list <commit-files-list
title="Staged" title="Staged"
:file-list="changedFiles" :file-list="changedFiles"
...@@ -121,7 +104,7 @@ you started editing. Would you like to create a new branch?`)" ...@@ -121,7 +104,7 @@ you started editing. Would you like to create a new branch?`)"
> >
<form <form
class="form-horizontal multi-file-commit-form" class="form-horizontal multi-file-commit-form"
@submit.prevent.stop="makeCommit" @submit.prevent.stop="commitChanges"
v-if="!rightPanelCollapsed" v-if="!rightPanelCollapsed"
> >
<div class="multi-file-commit-fieldset"> <div class="multi-file-commit-fieldset">
...@@ -141,7 +124,7 @@ you started editing. Would you like to create a new branch?`)" ...@@ -141,7 +124,7 @@ you started editing. Would you like to create a new branch?`)"
:disabled="commitButtonDisabled" :disabled="commitButtonDisabled"
container-class="btn btn-success btn-sm pull-left" container-class="btn btn-success btn-sm pull-left"
:label="__('Commit')" :label="__('Commit')"
@click="makeCommit" @click="commitChanges"
/> />
<button <button
type="button" type="button"
......
...@@ -50,22 +50,6 @@ export const setResizingStatus = ({ commit }, resizing) => { ...@@ -50,22 +50,6 @@ export const setResizingStatus = ({ commit }, resizing) => {
commit(types.SET_RESIZING_STATUS, resizing); commit(types.SET_RESIZING_STATUS, resizing);
}; };
export const checkCommitStatus = ({ state }) =>
service
.getBranchData(state.currentProjectId, state.currentBranchId)
.then(({ data }) => {
const { id } = data.commit;
const selectedBranch =
state.projects[state.currentProjectId].branches[state.currentBranchId];
if (selectedBranch.workingReference !== id) {
return true;
}
return false;
})
.catch(() => flash('Error checking branch data. Please try again.', 'alert', document, null, false, true));
export const createTempEntry = ( export const createTempEntry = (
{ state, dispatch }, { state, dispatch },
{ projectId, branchId, parent, name, type, content = '', base64 = false }, { projectId, branchId, parent, name, type, content = '', base64 = false },
......
import * as types from './mutation_types'; import * as types from './mutation_types';
import * as consts from './constants';
import * as rootTypes from '../../mutation_types'; import * as rootTypes from '../../mutation_types';
import service from '../../../services'; import service from '../../../services';
import flash from '../../../../flash'; import flash from '../../../../flash';
...@@ -20,7 +21,24 @@ export const updateBranchName = ({ commit }, branchName) => { ...@@ -20,7 +21,24 @@ export const updateBranchName = ({ commit }, branchName) => {
commit(types.UPDATE_NEW_BRANCH_NAME, branchName); commit(types.UPDATE_NEW_BRANCH_NAME, branchName);
}; };
export const checkCommitStatus = ({ rootState }) =>
service
.getBranchData(rootState.currentProjectId, rootState.currentBranchId)
.then(({ data }) => {
const { id } = data.commit;
const selectedBranch =
rootState.projects[rootState.currentProjectId].branches[rootState.currentBranchId];
if (selectedBranch.workingReference !== id) {
return true;
}
return false;
})
.catch(() => flash('Error checking branch data. Please try again.', 'alert', document, null, false, true));
export const commitChanges = ({ commit, state, getters, dispatch, rootState }) => { export const commitChanges = ({ commit, state, getters, dispatch, rootState }) => {
const newBranch = state.commitAction !== consts.COMMIT_TO_CURRENT_BRANCH;
const payload = { const payload = {
branch: getters.branchName, branch: getters.branchName,
commit_message: state.commitMessage, commit_message: state.commitMessage,
...@@ -30,71 +48,78 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState }) = ...@@ -30,71 +48,78 @@ export const commitChanges = ({ commit, state, getters, dispatch, rootState }) =
content: f.content, content: f.content,
encoding: f.base64 ? 'base64' : 'text', encoding: f.base64 ? 'base64' : 'text',
})), })),
start_branch: undefined, start_branch: newBranch ? rootState.currentBranchId : undefined,
}; };
const getCommitStatus = newBranch ? Promise.resolve(false) : dispatch('checkCommitStatus');
commit(types.UPDATE_LOADING, true); commit(types.UPDATE_LOADING, true);
console.log(payload); getCommitStatus.then(branchChanged => new Promise((resolve) => {
if (branchChanged) {
// show the modal with a Bootstrap call
$('#ide-create-branch-modal').modal('show');
} else {
resolve();
}
}))
.then(() => service.commit(rootState.currentProjectId, payload))
.then(({ data }) => {
const { branch } = payload;
return; if (!data.short_id) {
flash(data.message, 'alert', document, null, false, true);
return;
}
service const selectedProject = rootState.projects[rootState.currentProjectId];
.commit(state.currentProjectId, payload) const lastCommit = {
.then(({ data }) => { commit_path: `${selectedProject.web_url}/commit/${data.id}`,
const { branch } = payload; commit: {
if (!data.short_id) { message: data.message,
flash(data.message, 'alert', document, null, false, true); authored_date: data.committed_date,
return; },
} };
const selectedProject = state.projects[state.currentProjectId]; let commitMsg = `Your changes have been committed. Commit ${data.short_id}`;
const lastCommit = {
commit_path: `${selectedProject.web_url}/commit/${data.id}`,
commit: {
message: data.message,
authored_date: data.committed_date,
},
};
let commitMsg = `Your changes have been committed. Commit ${data.short_id}`;
if (data.stats) {
commitMsg += ` with ${data.stats.additions} additions, ${data.stats.deletions} deletions.`;
}
commit(rootTypes.SET_LAST_COMMIT_MSG, commitMsg, { root: true });
if (false) {
dispatch('discardAllChanges', null, { root: true });
dispatch(
'redirectToUrl',
`${selectedProject.web_url}/merge_requests/new?merge_request[source_branch]=${branch}&merge_request[target_branch]=${rottState.currentBranchId}`,
{ root: true },
);
} else {
commit(rootTypes.SET_BRANCH_WORKING_REFERENCE, {
projectId: state.currentProjectId,
branchId: state.currentBranchId,
reference: data.id,
}, { root: true });
state.changedFiles.forEach((entry) => { if (data.stats) {
commit(rootTypes.SET_LAST_COMMIT_DATA, { commitMsg += ` with ${data.stats.additions} additions, ${data.stats.deletions} deletions.`;
entry, }
lastCommit,
}, { root: true });
});
dispatch('discardAllChanges', null, { root: true }); commit(rootTypes.SET_LAST_COMMIT_MSG, commitMsg, { root: true });
window.scrollTo(0, 0); if (state.commitAction === consts.COMMIT_TO_NEW_BRANCH_MR) {
} dispatch('discardAllChanges', null, { root: true });
}) dispatch(
.catch((err) => { 'redirectToUrl',
let errMsg = 'Error committing changes. Please try again.'; `${selectedProject.web_url}/merge_requests/new?merge_request[source_branch]=${branch}&merge_request[target_branch]=${rootState.currentBranchId}`,
if (err.response.data && err.response.data.message) { { root: true },
errMsg += ` (${stripHtml(err.response.data.message)})`; );
} } else {
flash(errMsg, 'alert', document, null, false, true); commit(rootTypes.SET_BRANCH_WORKING_REFERENCE, {
window.dispatchEvent(new Event('resize')); projectId: rootState.currentProjectId,
}); branchId: rootState.currentBranchId,
reference: data.id,
}, { root: true });
rootState.changedFiles.forEach((entry) => {
commit(rootTypes.SET_LAST_COMMIT_DATA, {
entry,
lastCommit,
}, { root: true });
});
dispatch('discardAllChanges', null, { root: true });
window.scrollTo(0, 0);
}
})
.catch((err) => {
let errMsg = 'Error committing changes. Please try again.';
if (err.response.data && err.response.data.message) {
errMsg += ` (${stripHtml(err.response.data.message)})`;
}
flash(errMsg, 'alert', document, null, false, true);
window.dispatchEvent(new Event('resize'));
});
}; };
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