* Get the status of all files with their content diffs and classification. * This method returns the filepath, git status information, file classification, * and detailed line-by-line diffs for staged and unstaged changes. * * For each file, you can have: * - stagedDiff: Line-by-line
()
| 644 | * @returns Array of file status objects with detailed diff information |
| 645 | */ |
| 646 | async diff() { |
| 647 | const baseOpts = this._baseOpts; |
| 648 | const classifyStatusFn = this.classifyStatus.bind(this); |
| 649 | |
| 650 | // Adopted from statusMatrix of isomorphic-git https://github.com/isomorphic-git/isomorphic-git/blob/main/src/api/statusMatrix.js#L157 |
| 651 | const status: { |
| 652 | filepath: string; |
| 653 | head: { name: string; status: git.HeadStatus }; |
| 654 | workdir: { name: string; status: git.WorkdirStatus }; |
| 655 | stage: { name: string; status: git.StageStatus }; |
| 656 | classification: { type: GitFileStatus; symbol: GitFileStatusSymbol }; |
| 657 | stagedDiff?: Change[]; |
| 658 | unstagedDiff?: Change[]; |
| 659 | }[] = await git.walk({ |
| 660 | ...baseOpts, |
| 661 | trees: [ |
| 662 | // What the latest commit on the current branch looks like |
| 663 | git.TREE({ ref: 'HEAD' }), |
| 664 | // What the working directory looks like |
| 665 | git.WORKDIR(), |
| 666 | // What the index (staging area) looks like |
| 667 | git.STAGE(), |
| 668 | ], |
| 669 | map: async function map(filepath, [head, workdir, stage]) { |
| 670 | if (baseOpts.legacyDiff) { |
| 671 | const isInsomniaFile = |
| 672 | filepath.startsWith(GIT_INSOMNIA_DIR_NAME) || filepath.startsWith('insomnia.') || filepath === '.'; |
| 673 | if (!isInsomniaFile) { |
| 674 | return null; |
| 675 | } |
| 676 | } else { |
| 677 | // We want to inspect the repo root '.', directories (so the walk keeps |
| 678 | // recursing into them), and yaml files. Skip files with a non-yaml |
| 679 | // extension and dotfiles such as .gitignore/.DS_Store (whose extension |
| 680 | // is empty) — but keep yaml dotfiles like .spectral.yaml. |
| 681 | const ext = path.extname(filepath).toLowerCase(); |
| 682 | const base = path.basename(filepath); |
| 683 | const isYamlFile = ext === '.yaml'; |
| 684 | const isDotfile = base !== '.' && base.startsWith('.'); |
| 685 | if (!isYamlFile && (ext || isDotfile)) { |
| 686 | return null; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | if ( |
| 691 | await git.isIgnored({ |
| 692 | ...baseOpts, |
| 693 | filepath, |
| 694 | }) |
| 695 | ) { |
| 696 | return null; |
| 697 | } |
| 698 | const [headType, workdirType, stageType] = await Promise.all([ |
| 699 | head && head.type(), |
| 700 | workdir && workdir.type(), |
| 701 | stage && stage.type(), |
| 702 | ]); |
| 703 |