| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import {parsePatch} from 'shared/patch/parse'; |
| b69ab31 | | | 9 | import type {DiffType, ParsedDiff} from 'shared/patch/types'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | export function parsePatchAndFilter(patch: string): ReturnType<typeof parsePatch> { |
| b69ab31 | | | 12 | const result = parsePatch(patch); |
| b69ab31 | | | 13 | return result.filter( |
| b69ab31 | | | 14 | // empty patches and other weird situations can cause invalid files to get parsed, ignore these entirely |
| b69ab31 | | | 15 | diff => diff.hunks.length > 0 || diff.newFileName != null || diff.oldFileName != null, |
| b69ab31 | | | 16 | ); |
| b69ab31 | | | 17 | } |
| b69ab31 | | | 18 | |
| b69ab31 | | | 19 | /** Similar to how uncommitted changes are sorted, sort first by type, then by filename. */ |
| b69ab31 | | | 20 | export function sortFilesByType(files: Array<ParsedDiff>) { |
| b69ab31 | | | 21 | files.sort((a, b) => { |
| b69ab31 | | | 22 | if (a.type === b.type) { |
| b69ab31 | | | 23 | const pathA = a.newFileName ?? a.oldFileName ?? ''; |
| b69ab31 | | | 24 | const pathB = b.newFileName ?? b.oldFileName ?? ''; |
| b69ab31 | | | 25 | return pathA.localeCompare(pathB); |
| b69ab31 | | | 26 | } else { |
| b69ab31 | | | 27 | return ( |
| b69ab31 | | | 28 | (a.type == null ? SORT_LAST : sortKeyForType[a.type]) - |
| b69ab31 | | | 29 | (b.type == null ? SORT_LAST : sortKeyForType[b.type]) |
| b69ab31 | | | 30 | ); |
| b69ab31 | | | 31 | } |
| b69ab31 | | | 32 | }); |
| b69ab31 | | | 33 | } |
| b69ab31 | | | 34 | const SORT_LAST = 10; |
| b69ab31 | | | 35 | const sortKeyForType: Record<DiffType, number> = { |
| b69ab31 | | | 36 | Modified: 0, |
| b69ab31 | | | 37 | Renamed: 1, |
| b69ab31 | | | 38 | Added: 2, |
| b69ab31 | | | 39 | Copied: 3, |
| b69ab31 | | | 40 | Removed: 4, |
| b69ab31 | | | 41 | }; |