Normalize diff.oldName and diff.newName to fix incorrect filePath when filename has spaces (#2237)

unified-ui
Tan Nhu 2024-07-17 07:47:28 +00:00 committed by Harness
parent 411eda9819
commit a00265b22b
1 changed files with 14 additions and 0 deletions

View File

@ -250,9 +250,13 @@ const ChangesInternal: React.FC<ChangesProps> = ({
if (cachedDiff.raw) {
const _diffs = Diff2Html.parse(cachedDiff.raw, DIFF2HTML_CONFIG)
.map(diff => {
diff.oldName = normalizeGitFilePath(diff.oldName)
diff.newName = normalizeGitFilePath(diff.newName)
const fileId = changedFileId([diff.oldName, diff.newName])
const containerId = `container-${fileId}`
const contentId = `content-${fileId}`
const filePath = diff.isDeleted ? diff.oldName : diff.newName
return {
@ -613,3 +617,13 @@ const shouldRetainDiffChildren = (dom: HTMLElement | null) => !!dom?.querySelect
const outterBlockName = (blockIndex: number) => `outter-${blockIndex}`
const innerBlockName = (filePath: string) => `inner-${filePath}`
const { scheduleTask } = createRequestIdleCallbackTaskPool()
// Workaround util to correct filePath which is not correctly produced by
// git itself when filename contains space
// @see https://stackoverflow.com/questions/77596606/why-does-git-add-trailing-tab-to-the-b-line-of-the-diff-when-the-file-nam
const normalizeGitFilePath = (filePath: string) => {
if (filePath && filePath.endsWith('\t') && filePath.indexOf(' ') !== -1) {
return filePath.replace(/\t$/, '')
}
return filePath
}