Skip to content

Commit

Permalink
fixed the rest of the concerns, refactored portions of the gitGraphPa…
Browse files Browse the repository at this point in the history
…rser test to handle async actions
  • Loading branch information
Austin Fulbright authored and Austin Fulbright committed Aug 5, 2024
1 parent 9ed38cc commit 2a38d46
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 446 deletions.
72 changes: 26 additions & 46 deletions packages/mermaid/src/diagrams/git/gitGraphAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ function getID() {
return random({ length: 7 });
}


/**
* @param list - list of items
* @param fn - function to get the key
Expand Down Expand Up @@ -88,7 +87,7 @@ export const commit = function (msg: string, id: string, type: number, tags?: st
msg = common.sanitizeText(msg, config);
tags = tags?.map((tag) => common.sanitizeText(tag, config));
const newCommit: Commit = {
id: id ? id : state.records.seq + '-' + getId(),
id: id ? id : state.records.seq + '-' + getID(),
message: msg,
seq: state.records.seq++,
type: type ?? commitType.NORMAL,
Expand All @@ -109,12 +108,12 @@ export const branch = function (name: string, order?: number) {
throw 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}")`
);
}
state.records.branches.set(name, state.records.head != null ? state.records.head.id : null);
state.records.branchConfig.set(name, { name, order });
checkout(name);
log.debug('in createBranch');
}

state.records.branches.set(name, state.records.head != null ? state.records.head.id : null);
state.records.branchConfig.set(name, { name, order });
checkout(name);
log.debug('in createBranch');
};

export const merge = (
Expand All @@ -123,9 +122,10 @@ export const merge = (
overrideType?: number,
customTags?: string[]
): void => {
otherBranch = common.sanitizeText(otherBranch, getConfig());
const config = getConfig();
otherBranch = common.sanitizeText(otherBranch, config);
if (customId) {
customId = common.sanitizeText(customId, getConfig());
customId = common.sanitizeText(customId, config);
}
const currentBranchCheck: string | null | undefined = state.records.branches.get(
state.records.currBranch
Expand All @@ -150,7 +150,8 @@ export const merge = (
expected: ['branch abc'],
};
throw error;
} else if (currentCommit === undefined || !currentCommit) {
}
if (currentCommit === undefined || !currentCommit) {
const error: any = new Error(
`Incorrect usage of "merge". Current branch (${state.records.currBranch})has no commits`
);
Expand All @@ -162,7 +163,8 @@ export const merge = (
expected: ['commit'],
};
throw error;
} else if (!state.records.branches.has(otherBranch)) {
}
if (!state.records.branches.has(otherBranch)) {
const error: any = new Error(
'Incorrect usage of "merge". Branch to be merged (' + otherBranch + ') does not exist'
);
Expand All @@ -174,7 +176,8 @@ export const merge = (
expected: [`branch ${otherBranch}`],
};
throw error;
} else if (otherCommit === undefined || !otherCommit) {
}
if (otherCommit === undefined || !otherCommit) {
const error: any = new Error(
'Incorrect usage of "merge". Branch to be merged (' + otherBranch + ') has no commits'
);
Expand All @@ -186,7 +189,8 @@ export const merge = (
expected: ['"commit"'],
};
throw error;
} else if (currentCommit === otherCommit) {
}
if (currentCommit === otherCommit) {
const error: any = new Error('Incorrect usage of "merge". Both branches have same head');
error.hash = {
text: `merge ${otherBranch}`,
Expand All @@ -196,7 +200,8 @@ export const merge = (
expected: ['branch abc'],
};
throw error;
} else if (customId && state.records.commits.has(customId)) {
}
if (customId && state.records.commits.has(customId)) {
const error: any = new Error(
'Incorrect usage of "merge". Commit with id:' +
customId +
Expand All @@ -218,7 +223,7 @@ export const merge = (
const verifiedBranch: string = otherBranchCheck ? otherBranchCheck : ''; //figure out a cleaner way to do this

const commit: Commit = {
id: customId ? customId : state.records.seq + '-' + getId(),
id: customId ? customId : state.records.seq + '-' + getID(),
message: `merged branch ${otherBranch} into ${state.records.currBranch}`,
seq: state.records.seq++,
parents: [state.records.head == null ? null : state.records.head.id, verifiedBranch],
Expand All @@ -242,12 +247,13 @@ export const cherryPick = function (
parentCommitId: string
) {
log.debug('Entering cherryPick:', sourceId, targetId, tags);
sourceId = common.sanitizeText(sourceId, getConfig());
targetId = common.sanitizeText(targetId, getConfig());
const config = getConfig();
sourceId = common.sanitizeText(sourceId, config);
targetId = common.sanitizeText(targetId, config);

tags = tags?.map((tag) => common.sanitizeText(tag, config));

parentCommitId = common.sanitizeText(parentCommitId, getConfig());
parentCommitId = common.sanitizeText(parentCommitId, config);

if (!sourceId || !state.records.commits.has(sourceId)) {
const error: any = new Error(
Expand Down Expand Up @@ -293,8 +299,6 @@ export const cherryPick = function (
error.hash = {
text: `cherryPick ${sourceId} ${targetId}`,
token: `cherryPick ${sourceId} ${targetId}`,
line: '1',
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
expected: ['cherry-pick abc'],
};
throw error;
Expand All @@ -307,8 +311,6 @@ export const cherryPick = function (
error.hash = {
text: `cherryPick ${sourceId} ${targetId}`,
token: `cherryPick ${sourceId} ${targetId}`,
line: '1',
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
expected: ['cherry-pick abc'],
};
throw error;
Expand All @@ -323,13 +325,12 @@ export const cherryPick = function (
text: `cherryPick ${sourceId} ${targetId}`,
token: `cherryPick ${sourceId} ${targetId}`,
line: '1',
loc: { first_line: 1, last_line: 1, first_column: 1, last_column: 1 },
expected: ['cherry-pick abc'],
};
throw error;
}
const commit = {
id: state.records.seq + '-' + getId(),
id: state.records.seq + '-' + getID(),
message: `cherry-picked ${sourceCommit?.message} into ${state.records.currBranch}`,
seq: state.records.seq++,
parents: [state.records.head == null ? null : state.records.head.id, sourceCommit.id],
Expand Down Expand Up @@ -365,8 +366,6 @@ export const checkout = function (branch: string) {
expected: [`branch ${branch}`],
};
throw error;
//branches[branch] = head != null ? head.id : null;
//log.debug('in createBranch');
} else {
state.records.currBranch = branch;
const id = state.records.branches.get(state.records.currBranch);
Expand All @@ -378,25 +377,6 @@ export const checkout = function (branch: string) {
}
};

// export const reset = function (commitRef) {
// log.debug('in reset', commitRef);
// const ref = commitRef.split(':')[0];
// let parentCount = parseInt(commitRef.split(':')[1]);
// let commit = ref === 'HEAD' ? head : commits.get(branches.get(ref));
// log.debug(commit, parentCount);
// while (parentCount > 0) {
// commit = commits.get(commit.parent);
// parentCount--;
// if (!commit) {
// const err = 'Critical error - unique parent commit not found during reset';
// log.error(err);
// throw err;
// }
// }
// head = commit;
// branches[curBranch] = commit.id;
// };

/**
* @param arr - array
* @param key - key
Expand Down
Loading

0 comments on commit 2a38d46

Please sign in to comment.