Module: KDiff3

Defined in:
lib/kdiff3.rb,
lib/kdiff3/version.rb

Constant Summary collapse

TEMPFILES =

list of created Tempfiles

[]
NEWLINE =

so we don’t accidentally mess up formatting when we remove these later, we need to have a weird, non-stand sequence of characters to search and replace

"\n\-"
VERSION =
"0.9.5"

Class Method Summary collapse

Class Method Details

.merge(base: nil, yours: nil, theirs: nil, html: false) ⇒ String

performs a 3 way merge between base, a, and b a 2 way merge will be performed if either yours or theirs are left out

Parameters:

  • base (String) (defaults to: nil)

    string or file path for common ancestor

  • yours (String) (defaults to: nil)

    string or file path for your changes

  • theirs (String) (defaults to: nil)

    string or file path for their changes

  • html (Boolean) (defaults to: false)

    whether or not use an HTML diffing technique

Returns:

  • (String)

    result of merge

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kdiff3.rb', line 23

def self.merge(base: nil, yours: nil, theirs: nil, html: false)
  raise ArgumentError.new('base is required') unless base.present?
  raise ArgumentError.new('yours and/or theirs required') unless yours || theirs

  # since HTML is often compressed to conserve transfer space, and therefore
  # has few lines, we need to split up the HTML in to a multi-lined document
  if html
    base = add_new_lines(base)
    yours = add_new_lines(yours)
    theirs = add_new_lines(theirs)
  end

  base_path = tempfile(text: base, name: 'base')
  your_path = tempfile(text: yours, name: 'yours')
  their_path = tempfile(text: theirs, name: 'theirs')
  output_path = tempfile(name: 'output')

  # we don't need these open for anything
  close_tempfiles

  # the heavy lifting, courtesy of kdiff3
  exit_code = run("#{base_path} #{your_path} #{their_path} -m --auto --fail -o #{output_path}")
  conflicts_exist = exit_code == 1

  result = IO.read(output_path) unless conflicts_exist

  # clean up
  delete_tempfiles

  raise RuntimeError.new("Conflicts exist and could not be resolved") if conflicts_exist

  if html
    # remove the NEWLINES
    result.gsub!(NEWLINE, "")
  end

  result
end