mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-02-04 07:13:25 +08:00
Added branch, merge and checkout error handling scenarios
This commit is contained in:
parent
01970d2bbd
commit
c1dead1187
@ -2,8 +2,8 @@ import { log } from '../../logger';
|
|||||||
import { random } from '../../utils';
|
import { random } from '../../utils';
|
||||||
let commits = {};
|
let commits = {};
|
||||||
let head = null;
|
let head = null;
|
||||||
let branches = { master: head };
|
let branches = { main: head };
|
||||||
let curBranch = 'master';
|
let curBranch = 'main';
|
||||||
let direction = 'LR';
|
let direction = 'LR';
|
||||||
let seq = 0;
|
let seq = 0;
|
||||||
|
|
||||||
@ -101,13 +101,85 @@ export const commit = function (msg, id, type, tag) {
|
|||||||
export const branch = function (name) {
|
export const branch = function (name) {
|
||||||
if (!branches[name]) {
|
if (!branches[name]) {
|
||||||
branches[name] = head != null ? head.id : null;
|
branches[name] = head != null ? head.id : null;
|
||||||
|
checkout(name);
|
||||||
log.debug('in createBranch');
|
log.debug('in createBranch');
|
||||||
|
} else {
|
||||||
|
let error = new Error(
|
||||||
|
'Trying to create an existing branch. (Help: Either use a new name if you want create a new branch or try using "checkout ' +
|
||||||
|
name +
|
||||||
|
'")'
|
||||||
|
);
|
||||||
|
error.hash = {
|
||||||
|
text: 'branch ' + name,
|
||||||
|
token: 'branch ' + name,
|
||||||
|
line: '1',
|
||||||
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
|
expected: ['"checkout ' + name + '"'],
|
||||||
|
};
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const merge = function (otherBranch) {
|
export const merge = function (otherBranch) {
|
||||||
const currentCommit = commits[branches[curBranch]];
|
const currentCommit = commits[branches[curBranch]];
|
||||||
const otherCommit = commits[branches[otherBranch]];
|
const otherCommit = commits[branches[otherBranch]];
|
||||||
|
if (curBranch === otherBranch) {
|
||||||
|
let error = new Error('Incorrect usage of "merge". Cannot merge a branch to itself');
|
||||||
|
error.hash = {
|
||||||
|
text: 'merge ' + otherBranch,
|
||||||
|
token: 'merge ' + otherBranch,
|
||||||
|
line: '1',
|
||||||
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
|
expected: ['branch abc'],
|
||||||
|
};
|
||||||
|
throw error;
|
||||||
|
} else if (!currentCommit) {
|
||||||
|
let error = new Error(
|
||||||
|
'Incorrect usage of "merge". Current branch (' + curBranch + ')has no commits'
|
||||||
|
);
|
||||||
|
error.hash = {
|
||||||
|
text: 'merge ' + otherBranch,
|
||||||
|
token: 'merge ' + otherBranch,
|
||||||
|
line: '1',
|
||||||
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
|
expected: ['commit'],
|
||||||
|
};
|
||||||
|
throw error;
|
||||||
|
} else if (!branches[otherBranch]) {
|
||||||
|
let error = new Error(
|
||||||
|
'Incorrect usage of "merge". Branch to be merged (' + otherBranch + ') does not exist'
|
||||||
|
);
|
||||||
|
error.hash = {
|
||||||
|
text: 'merge ' + otherBranch,
|
||||||
|
token: 'merge ' + otherBranch,
|
||||||
|
line: '1',
|
||||||
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
|
expected: ['branch ' + otherBranch],
|
||||||
|
};
|
||||||
|
throw error;
|
||||||
|
} else if (!otherCommit) {
|
||||||
|
let error = new Error(
|
||||||
|
'Incorrect usage of "merge". Branch to be merged (' + otherBranch + ') has no commits'
|
||||||
|
);
|
||||||
|
error.hash = {
|
||||||
|
text: 'merge ' + otherBranch,
|
||||||
|
token: 'merge ' + otherBranch,
|
||||||
|
line: '1',
|
||||||
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
|
expected: ['"commit"'],
|
||||||
|
};
|
||||||
|
throw error;
|
||||||
|
} else if (currentCommit === otherCommit) {
|
||||||
|
let error = new Error('Incorrect usage of "merge". Both branches have same head');
|
||||||
|
error.hash = {
|
||||||
|
text: 'merge ' + otherBranch,
|
||||||
|
token: 'merge ' + otherBranch,
|
||||||
|
line: '1',
|
||||||
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
|
expected: ['branch abc'],
|
||||||
|
};
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
// if (isReachableFrom(currentCommit, otherCommit)) {
|
// if (isReachableFrom(currentCommit, otherCommit)) {
|
||||||
// log.debug('Already merged');
|
// log.debug('Already merged');
|
||||||
// return;
|
// return;
|
||||||
@ -136,12 +208,24 @@ export const merge = function (otherBranch) {
|
|||||||
export const checkout = function (branch) {
|
export const checkout = function (branch) {
|
||||||
log.debug('in checkout');
|
log.debug('in checkout');
|
||||||
if (!branches[branch]) {
|
if (!branches[branch]) {
|
||||||
branches[branch] = head != null ? head.id : null;
|
let error = new Error(
|
||||||
log.debug('in createBranch');
|
'Trying to checkout branch which is not yet created. (Help try using "branch ' + branch + '")'
|
||||||
|
);
|
||||||
|
error.hash = {
|
||||||
|
text: 'checkout ' + branch,
|
||||||
|
token: 'checkout ' + branch,
|
||||||
|
line: '1',
|
||||||
|
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
|
||||||
|
expected: ['"branch ' + branch + '"'],
|
||||||
|
};
|
||||||
|
throw error;
|
||||||
|
//branches[branch] = head != null ? head.id : null;
|
||||||
|
//log.debug('in createBranch');
|
||||||
|
} else {
|
||||||
|
curBranch = branch;
|
||||||
|
const id = branches[curBranch];
|
||||||
|
head = commits[id];
|
||||||
}
|
}
|
||||||
curBranch = branch;
|
|
||||||
const id = branches[curBranch];
|
|
||||||
head = commits[id];
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// export const reset = function (commitRef) {
|
// export const reset = function (commitRef) {
|
||||||
@ -219,8 +303,8 @@ export const prettyPrint = function () {
|
|||||||
export const clear = function () {
|
export const clear = function () {
|
||||||
commits = {};
|
commits = {};
|
||||||
head = null;
|
head = null;
|
||||||
branches = { master: head };
|
branches = { main: head };
|
||||||
curBranch = 'master';
|
curBranch = 'main';
|
||||||
seq = 0;
|
seq = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user