MCPcopy
hub / github.com/di-sukharev/opencommit / splitDiff

Function splitDiff

src/generateCommitMessageFromGitDiff.ts:262–296  ·  view source on GitHub ↗
(diff: string, maxChangeLength: number)

Source from the content-addressed store, hash-verified

260}
261
262function splitDiff(diff: string, maxChangeLength: number) {
263 const lines = diff.split('\n');
264 const splitDiffs = [] as string[];
265 let currentDiff = '';
266
267 if (maxChangeLength <= 0) {
268 throw new Error(GenerateCommitMessageErrorEnum.outputTokensTooHigh);
269 }
270
271 for (let line of lines) {
272 // If a single line exceeds maxChangeLength, split it into multiple lines
273 while (tokenCount(line) > maxChangeLength) {
274 const subLine = line.substring(0, maxChangeLength);
275 line = line.substring(maxChangeLength);
276 splitDiffs.push(subLine);
277 }
278
279 // Check the tokenCount of the currentDiff and the line separately
280 if (tokenCount(currentDiff) + tokenCount('\n' + line) > maxChangeLength) {
281 // If adding the next line would exceed the maxChangeLength, start a new diff
282 splitDiffs.push(currentDiff);
283 currentDiff = line;
284 } else {
285 // Otherwise, add the line to the current diff
286 currentDiff += '\n' + line;
287 }
288 }
289
290 // Add the last diff
291 if (currentDiff) {
292 splitDiffs.push(currentDiff);
293 }
294
295 return splitDiffs;
296}
297
298export const getCommitMsgsPromisesFromFileDiffs = async (
299 diff: string,

Callers 1

Calls 2

tokenCountFunction · 0.90
pushMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…