Module: C0::Diff

Defined in:
lib/c0/diff.rb

Overview

C0DIFF (DIFF.md): atomic multi-file edits with anchored patterns. A diff is an ordinary document of shape diff: files of records, each record a pattern of units, with old␚␟new (the sub-unit of old) as the substitution. Parsing and applying run in the C core (c0_diff.h).

diff = C0::Diff.build do |d|
d.file("greeting.txt") do
  d.record { |r| r.anchor("Hello "); r.sub("world", "universe"); r.anchor("!") }
end
end
C0::Diff.apply(diff, { "greeting.txt" => "Hello world!" })
# => { "greeting.txt" => "Hello universe!" }

A record's search text (its anchors and olds) must match exactly once from the previous record's match to the end of the file. Everything the text diff does not use (keyed units, levels, references, header rows, GS, dead records or file lines, two sub-units on one unit, a sub-unit of a sub-unit, an empty search text, ␟␚␟, the same path twice) is refused with a C0::Diff::Error; malformed bytes (SUB before RS, FS or SOH among them) raise the usual C0::SyntaxError. There is no replace-all.

Defined Under Namespace

Classes: FileEdit, Pattern, Unit

Class Method Summary collapse

Class Method Details

.apply(diff, files) ⇒ Object

Apply a diff to in-memory files: a Hash (or pairs) of path => content. Everything is validated first; raises without producing anything on any error. Returns a new Hash with every input file, edited or unchanged, each content in the encoding of its input.



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/c0/diff.rb', line 63

def apply(diff, files)
  pairs = files.to_a
  inputs = pairs.map { |path, content| [C0.to_bin(path.to_s), C0.to_bin(content)] }
  outputs = Ext.diff_apply(C0.to_bin(diff), inputs)
  out = {}
  pairs.each_with_index do |(path, content), i|
    enc = content.is_a?(String) ? content.encoding : Encoding::BINARY
    out[path] = outputs[i][1].force_encoding(enc)
  end
  out
end

.apply_files(diff, base_dir = ".") ⇒ Object

Apply a diff to files on disk under base_dir, atomically: every file is validated before any is written, and each is written via a temporary file and rename. A path that leaves base_dir is refused.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/c0/diff.rb', line 78

def apply_files(diff, base_dir = ".")
  diff = C0.to_bin(diff)
  base = File.expand_path(base_dir)
  inputs = parse(diff).map do |edit|
    path = edit.path.dup.force_encoding(Encoding::UTF_8)
    target = File.expand_path(path, base)
    unless target.start_with?(base + File::SEPARATOR)
      raise Error, "path escapes the target directory: #{path}"
    end
    raise Error, "no such file: #{path}" unless File.file?(target)

    [path, File.binread(target)]
  end
  apply(diff, inputs).each do |path, content|
    target = File.expand_path(path, base)
    tmp = "#{target}.c0diff.tmp"
    File.binwrite(tmp, content)
    File.rename(tmp, target)
  end
  nil
end

.build(heading: true) ⇒ Object

Drive a fresh DiffBuilder and return its bytes.



101
# File 'lib/c0/diff.rb', line 101

def build(heading: true, &) = DiffBuilder.build(heading: heading, &)

.parse(diff) ⇒ Object

Parse a diff document (with or without its SYN heading) into FileEdits, refusing everything DIFF.md lists under "What a diff may not hold".



53
54
55
56
57
# File 'lib/c0/diff.rb', line 53

def parse(diff)
  Ext.diff_parse(C0.to_bin(diff)).map do |path, records|
    FileEdit.new(path, records.map { |units| Pattern.new(units.map { |o, n| Unit.new(o, n) }) })
  end
end