| 836 | |
| 837 | // Helper function to format diff changes like git diff output |
| 838 | const formatDiffChanges = (changes: any[], title: string) => { |
| 839 | const isStaged = title.includes('Staged'); |
| 840 | const filteredChanges = changes.filter(c => { |
| 841 | const diffToCheck = isStaged ? c.stagedDiff : c.unstagedDiff; |
| 842 | return (diffToCheck && diffToCheck.length > 0) || c.classification.type === 'deleted'; |
| 843 | }); |
| 844 | |
| 845 | if (filteredChanges.length === 0) return ''; |
| 846 | |
| 847 | return ( |
| 848 | `${title}:\n` + |
| 849 | filteredChanges |
| 850 | .map(c => { |
| 851 | const diffToUse = isStaged ? c.stagedDiff : c.unstagedDiff; |
| 852 | |
| 853 | // Handle special cases for new/deleted files |
| 854 | if (c.classification.type === 'untracked' || c.classification.type === 'added') { |
| 855 | const output = |
| 856 | `diff --git a/${c.filepath} b/${c.filepath}\n` + |
| 857 | `new file mode 100644\n` + |
| 858 | `index 0000000..${Math.random().toString(36).slice(2, 9)}\n` + |
| 859 | `--- /dev/null\n` + |
| 860 | `+++ b/${c.filepath}\n`; |
| 861 | if (!diffToUse) return output; |
| 862 | |
| 863 | return ( |
| 864 | output + |
| 865 | diffToUse |
| 866 | .map((change: any) => { |
| 867 | const lines = change.value.split('\n').filter((line: string) => line !== ''); |
| 868 | return lines.map((line: string) => `+${line}`).join('\n'); |
| 869 | }) |
| 870 | .join('\n') + |
| 871 | '\n' |
| 872 | ); |
| 873 | } |
| 874 | |
| 875 | if (c.classification.type === 'deleted') { |
| 876 | return ( |
| 877 | `diff --git a/${c.filepath} b/${c.filepath}\n` + |
| 878 | `deleted file mode 100644\n` + |
| 879 | `index ${Math.random().toString(36).slice(2, 9)}..0000000\n` + |
| 880 | `--- a/${c.filepath}\n` + |
| 881 | `+++ /dev/null\n` + |
| 882 | (diffToUse |
| 883 | ? diffToUse |
| 884 | .map((change: any) => { |
| 885 | const lines = change.value.split('\n').filter((line: string) => line !== ''); |
| 886 | return lines.map((line: string) => `-${line}`).join('\n'); |
| 887 | }) |
| 888 | .join('\n') + '\n' |
| 889 | : '') |
| 890 | ); |
| 891 | } |
| 892 | |
| 893 | // Handle modified files |
| 894 | if (!diffToUse || diffToUse.length === 0) return ''; |
| 895 | |