iterate.com / git

git clone https://git.iterate.com/repos/git

git / plain-diff.tsmain at 517348b

// plain-diff.ts — A CHANGE AS LINES OF PLAIN TEXT, worked out where the page is written.
//
// The page's script draws each diff with colours and comments, from the two texts. A script that
// has not loaded yet, or that did not load at all (a phone on a weak network), draws nothing: so
// pages.ts writes each diff as text first, and the script puts its own in that place when it has
// drawn. This file works that text out: the lines that went and came, with three lines around
// them, as `git diff` shows them.

/** One line of a plain diff: a line that stayed (" "), went ("-") or came ("+"), or the line
 *  that says where a part is ("@"). */
export type PlainLine = { kind: " " | "-" | "+" | "@"; text: string };

/** A change of more steps than this is shown as the old lines, then the new ones. */
const MOST_STEPS = 1500;
const AROUND = 3;

const linesOf = (text: string) => {
  const lines = text.split("\n");
  if (lines.at(-1) === "") lines.pop();
  return lines;
};

type Step = { kind: " " | "-" | "+"; text: string };

/** The fewest lines to take out and put in that turn `a` into `b` (Myers), in order. */
function stepsOf(a: string[], b: string[]): Step[] {
  const whole = (): Step[] => [
    ...a.map((text) => ({ kind: "-" as const, text })),
    ...b.map((text) => ({ kind: "+" as const, text })),
  ];
  if (a.length === 0 || b.length === 0) return whole();
  const most = Math.min(MOST_STEPS, a.length + b.length);
  const at = most + 1;
  const reach = new Int32Array(2 * most + 3);
  // what `reach` held before each step, from diagonal -(step + 1) to step + 1
  const before: Int32Array[] = [];
  let found = -1;
  for (let step = 0; step <= most && found < 0; step++) {
    before.push(reach.slice(at - step - 1, at + step + 2));
    for (let k = -step; k <= step; k += 2) {
      const down = k === -step || (k !== step && reach[at + k - 1]! < reach[at + k + 1]!);
      let x = down ? reach[at + k + 1]! : reach[at + k - 1]! + 1;
      let y = x - k;
      while (x < a.length && y < b.length && a[x] === b[y]) x++, y++;
      reach[at + k] = x;
      if (x >= a.length && y >= b.length) {
        found = step;
        break;
      }
    }
  }
  if (found < 0) return whole();
  const steps: Step[] = [];
  let x = a.length;
  let y = b.length;
  for (let step = found; step > 0; step--) {
    const was = before[step]!;
    const read = (k: number) => was[k + step + 1]!;
    const k = x - y;
    const down = k === -step || (k !== step && read(k - 1) < read(k + 1));
    const fromK = down ? k + 1 : k - 1;
    const fromX = read(fromK);
    const fromY = fromX - fromK;
    while (x > fromX && y > fromY) steps.push({ kind: " ", text: a[--x]! }), y--;
    if (down) steps.push({ kind: "+", text: b[--y]! });
    else steps.push({ kind: "-", text: a[--x]! });
  }
  while (x > 0 && y > 0) steps.push({ kind: " ", text: a[--x]! }), y--;
  return steps.reverse();
}

/** The change from `before` to `after` as the lines of a unified diff: each part that changed,
 *  with three lines around it, under a line that says where it is. No line when the two texts
 *  have the same lines. */
export function plainDiff(before: string, after: string): PlainLine[] {
  const a = linesOf(before);
  const b = linesOf(after);
  let head = 0;
  while (head < a.length && head < b.length && a[head] === b[head]) head++;
  let tail = 0;
  while (tail < a.length - head && tail < b.length - head && a[a.length - 1 - tail] === b[b.length - 1 - tail]) tail++;
  const same = (lines: string[]) => lines.map((text) => ({ kind: " " as const, text }));
  const steps: Step[] = [
    ...same(a.slice(Math.max(0, head - AROUND), head)),
    ...stepsOf(a.slice(head, a.length - tail), b.slice(head, b.length - tail)),
    ...same(a.slice(a.length - tail, a.length - tail + AROUND)),
  ];
  // the line of each side that the first step is on
  let oldLine = Math.max(0, head - AROUND) + 1;
  let newLine = oldLine;
  const changed = steps.map((step) => step.kind !== " ");
  const shown = steps.map((_, index) => {
    for (let near = Math.max(0, index - AROUND); near <= Math.min(steps.length - 1, index + AROUND); near++)
      if (changed[near]) return true;
    return false;
  });
  const lines: PlainLine[] = [];
  for (let index = 0; index < steps.length; ) {
    if (!shown[index]) {
      oldLine++, newLine++, index++;
      continue;
    }
    let end = index;
    while (end < steps.length && shown[end]) end++;
    const part = steps.slice(index, end);
    const olds = part.filter((step) => step.kind !== "+").length;
    const news = part.filter((step) => step.kind !== "-").length;
    lines.push({ kind: "@", text: `@@ -${olds ? oldLine : oldLine - 1},${olds} +${news ? newLine : newLine - 1},${news} @@` });
    lines.push(...part);
    oldLine += olds;
    newLine += news;
    index = end;
  }
  return lines;
}