Method: Diff::LCS.__normalize_patchset

Defined in:
lib/gems/diff-lcs-1.1.2/lib/diff/lcs.rb

.__normalize_patchset(patchset) ⇒ Object

Normalize the patchset. A patchset is always a sequence of changes, but how those changes are represented may vary, depending on how they were generated. In all cases we support, we also support the array representation of the changes. The formats are:

[ # patchset <- Diff::LCS.diff(a, b)
  [ # one or more hunks
    Diff::LCS::Change # one or more changes
  ] ]

[ # patchset, equivalent to the above
  [ # one or more hunks
    [ action, line, value ] # one or more changes
  ] ]

[ # patchset <- Diff::LCS.diff(a, b, Diff::LCS::ContextDiffCallbacks)
  #       OR <- Diff::LCS.sdiff(a, b, Diff::LCS::ContextDiffCallbacks)
  [ # one or more hunks
    Diff::LCS::ContextChange # one or more changes
  ] ]

[ # patchset, equivalent to the above
  [ # one or more hunks
    [ action, [ old line, old value ], [ new line, new value ] ]
      # one or more changes
  ] ]

[ # patchset <- Diff::LCS.sdiff(a, b)
  #       OR <- Diff::LCS.diff(a, b, Diff::LCS::SDiffCallbacks)
  Diff::LCS::ContextChange # one or more changes
]

[ # patchset, equivalent to the above
  [ action, [ old line, old value ], [ new line, new value ] ]
    # one or more changes
]

The result of this will be either of the following.

[ # patchset
  Diff::LCS::ContextChange # one or more changes
]

[ # patchset
  Diff::LCS::Change # one or more changes
]

If either of the above is provided, it will be returned as such.



1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
# File 'lib/gems/diff-lcs-1.1.2/lib/diff/lcs.rb', line 1075

def __normalize_patchset(patchset)
  patchset.map do |hunk|
    case hunk
    when Diff::LCS::ContextChange, Diff::LCS::Change
      hunk
    when Array
      if (not hunk[0].kind_of?(Array)) and hunk[1].kind_of?(Array) and hunk[2].kind_of?(Array)
        Diff::LCS::ContextChange.from_a(hunk)
      else
        hunk.map do |change|
          case change
          when Diff::LCS::ContextChange, Diff::LCS::Change
            change
          when Array
              # change[1] will ONLY be an array in a ContextChange#to_a call.
              # In Change#to_a, it represents the line (singular).
            if change[1].kind_of?(Array)
              Diff::LCS::ContextChange.from_a(change)
            else
              Diff::LCS::Change.from_a(change)
            end
          end
        end
      end
    else
      raise ArgumentError, "Cannot normalise a hunk of class #{hunk.class}."
    end
  end.flatten
end