From b9a46597a90527cd2f723207df371884c569d6f3 Mon Sep 17 00:00:00 2001 From: Thomas Randolph Date: Thu, 10 Nov 2022 12:17:58 -0700 Subject: [PATCH] Handle case where selecting a tree entry parent may not have a tree Changelog: fixed --- .../diffs/utils/tree_worker_utils.js | 9 ++- .../diffs/utils/tree_worker_utils_spec.js | 64 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/diffs/utils/tree_worker_utils.js b/app/assets/javascripts/diffs/utils/tree_worker_utils.js index 985e75d1a17715..a90c1a5c64e3e7 100644 --- a/app/assets/javascripts/diffs/utils/tree_worker_utils.js +++ b/app/assets/javascripts/diffs/utils/tree_worker_utils.js @@ -62,10 +62,15 @@ export const generateTreeList = (files) => { const split = file.new_path.split('/'); split.forEach((name, i) => { - const parent = acc.treeEntries[split.slice(0, i).join('/')]; + let parent = acc.treeEntries[split.slice(0, i).join('/')]; const path = `${parent ? `${parent.path}/` : ''}${name}`; + const child = acc.treeEntries[path]; - if (!acc.treeEntries[path]) { + if (parent && !parent.tree) { + parent = null; + } + + if (!child || !child.tree) { const type = path === file.new_path ? 'blob' : 'tree'; acc.treeEntries[path] = { key: path, diff --git a/spec/frontend/diffs/utils/tree_worker_utils_spec.js b/spec/frontend/diffs/utils/tree_worker_utils_spec.js index 8113428f712809..4df5fe75004f7e 100644 --- a/spec/frontend/diffs/utils/tree_worker_utils_spec.js +++ b/spec/frontend/diffs/utils/tree_worker_utils_spec.js @@ -34,6 +34,23 @@ describe('~/diffs/utils/tree_worker_utils', () => { added_lines: 0, file_hash: 'test', }, + { + new_path: 'constructor/test/aFile.js', + deleted_file: false, + new_file: true, + removed_lines: 0, + added_lines: 42, + file_hash: 'test', + }, + { + new_path: 'submodule @ abcdef123', + deleted_file: false, + new_file: true, + removed_lines: 0, + added_lines: 1, + submodule: true, + file_hash: 'test', + }, { new_path: 'package.json', deleted_file: true, @@ -66,6 +83,7 @@ describe('~/diffs/utils/tree_worker_utils', () => { path: 'app/index.js', removedLines: 10, tempFile: false, + submodule: undefined, type: 'blob', tree: [], }, @@ -87,6 +105,7 @@ describe('~/diffs/utils/tree_worker_utils', () => { path: 'app/test/index.js', removedLines: 0, tempFile: true, + submodule: undefined, type: 'blob', tree: [], }, @@ -101,6 +120,7 @@ describe('~/diffs/utils/tree_worker_utils', () => { path: 'app/test/filepathneedstruncating.js', removedLines: 0, tempFile: true, + submodule: undefined, type: 'blob', tree: [], }, @@ -109,6 +129,45 @@ describe('~/diffs/utils/tree_worker_utils', () => { ], opened: true, }, + { + key: 'constructor', + name: 'constructor/test', + opened: true, + path: 'constructor', + tree: [ + { + addedLines: 42, + changed: true, + deleted: false, + fileHash: 'test', + key: 'constructor/test/aFile.js', + name: 'aFile.js', + parentPath: 'constructor/test/', + path: 'constructor/test/aFile.js', + removedLines: 0, + submodule: undefined, + tempFile: true, + tree: [], + type: 'blob', + }, + ], + type: 'tree', + }, + { + key: 'submodule @ abcdef123', + parentPath: '/', + path: 'submodule @ abcdef123', + name: 'submodule @ abcdef123', + type: 'blob', + changed: true, + tempFile: true, + submodule: true, + deleted: false, + fileHash: 'test', + addedLines: 1, + removedLines: 0, + tree: [], + }, { key: 'package.json', parentPath: '/', @@ -117,6 +176,7 @@ describe('~/diffs/utils/tree_worker_utils', () => { type: 'blob', changed: true, tempFile: false, + submodule: undefined, deleted: true, fileHash: 'test', addedLines: 0, @@ -135,6 +195,10 @@ describe('~/diffs/utils/tree_worker_utils', () => { 'app/test', 'app/test/index.js', 'app/test/filepathneedstruncating.js', + 'constructor', + 'constructor/test', + 'constructor/test/aFile.js', + 'submodule @ abcdef123', 'package.json', ]); }); -- GitLab