MCPcopy Create free account
hub / github.com/banga/git-split-diffs / iterSideBySideDiffsFormatted

Function iterSideBySideDiffsFormatted

src/iterSideBySideDiffs.ts:37–312  ·  view source on GitHub ↗
(
    context: Context,
    lines: AsyncIterable<string>
)

Source from the content-addressed store, hash-verified

35 | 'combined-diff-hunk-body';
36
37async function* iterSideBySideDiffsFormatted(
38 context: Context,
39 lines: AsyncIterable<string>
40): AsyncIterable<FormattedString> {
41 const { HORIZONTAL_SEPARATOR } = context;
42
43 let state: State = 'unknown';
44
45 // Commit metadata
46 let isFirstCommitBodyLine = false;
47
48 // File metadata
49 let fileNameA: string = '';
50 let fileNameB: string = '';
51 function* yieldFileName() {
52 yield* iterFormatFileName(context, fileNameA, fileNameB);
53 }
54
55 // Hunk metadata
56 let hunkParts: HunkPart[] = [];
57 let hunkHeaderLine: string = '';
58 async function* yieldHunk(diffType: 'unified-diff' | 'combined-diff') {
59 yield* iterFormatHunk(context, diffType, hunkHeaderLine, hunkParts);
60 for (const hunkPart of hunkParts) {
61 hunkPart.startLineNo = -1;
62 hunkPart.lines = [];
63 }
64 }
65
66 async function* flushPending() {
67 if (state === 'unified-diff' || state === 'combined-diff') {
68 yield* yieldFileName();
69 } else if (state === 'unified-diff-hunk-body') {
70 yield* yieldHunk('unified-diff');
71 } else if (state === 'combined-diff-hunk-body') {
72 yield* yieldHunk('combined-diff');
73 }
74 }
75
76 for await (const rawLine of lines) {
77 const line = rawLine.replace(ANSI_COLOR_CODE_REGEX, '');
78
79 // Update state
80 let nextState: State | null = null;
81 if (line.startsWith('commit ')) {
82 nextState = 'commit-header';
83 } else if (state === 'commit-header' && line.startsWith(' ')) {
84 nextState = 'commit-body';
85 } else if (line.startsWith('diff --git')) {
86 nextState = 'unified-diff';
87 } else if (line.startsWith('@@ ')) {
88 nextState = 'unified-diff-hunk-header';
89 } else if (state === 'unified-diff-hunk-header') {
90 nextState = 'unified-diff-hunk-body';
91 } else if (
92 line.startsWith('diff --cc') ||
93 line.startsWith('diff --combined')
94 ) {

Callers 1

iterSideBySideDiffsFunction · 0.85

Calls 6

TFunction · 0.90
iterFormatCommitBodyLineFunction · 0.90
flushPendingFunction · 0.85
appendStringMethod · 0.80
sliceMethod · 0.80

Tested by

no test coverage detected