MCPcopy Create free account
hub / github.com/Kong/insomnia / formatDiffChanges

Method formatDiffChanges

packages/insomnia/src/sync/git/git-vcs.ts:842–924  ·  view source on GitHub ↗
(changes: any[], title: string)

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 3

filterMethod · 0.80
mapMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected