Method: Chef::ChefFS::CommandLine.diff_entries

Defined in:
lib/chef/chef_fs/command_line.rb

.diff_entries(old_entry, new_entry, recurse_depth, get_content) ⇒ Object

Diff two known entries (could be files or dirs)



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/chef/chef_fs/command_line.rb', line 152

def self.diff_entries(old_entry, new_entry, recurse_depth, get_content)
  # If both are directories
  if old_entry.dir?
    if new_entry.dir?
      if recurse_depth == 0
        [ [ :common_subdirectories, old_entry, new_entry ] ]
      else
        Chef::ChefFS::FileSystem.child_pairs(old_entry, new_entry).parallel_map do |old_child, new_child|
          Chef::ChefFS::CommandLine.diff_entries(old_child, new_child, recurse_depth ? recurse_depth - 1 : nil, get_content)
        end.flatten(1)
      end

    # If old is a directory and new is a file
    elsif new_entry.exists?
      [ [ :directory_to_file, old_entry, new_entry ] ]

    # If old is a directory and new does not exist
    elsif new_entry.parent.can_have_child?(old_entry.name, old_entry.dir?)
      [ [ :deleted, old_entry, new_entry ] ]

    # If the new entry does not and *cannot* exist, report that.
    else
      [ [ :new_cannot_upload, old_entry, new_entry ] ]
    end

  # If new is a directory and old is a file
  elsif new_entry.dir?
    if old_entry.exists?
      [ [ :file_to_directory, old_entry, new_entry ] ]

    # If new is a directory and old does not exist
    elsif old_entry.parent.can_have_child?(new_entry.name, new_entry.dir?)
      [ [ :added, old_entry, new_entry ] ]

    # If the new entry does not and *cannot* exist, report that.
    else
      [ [ :old_cannot_upload, old_entry, new_entry ] ]
    end

  # Neither is a directory, so they are diffable with file diff
  else
    are_same, old_value, new_value = Chef::ChefFS::FileSystem.compare(old_entry, new_entry)
    if are_same
      if old_value == :none
        [ [ :both_nonexistent, old_entry, new_entry ] ]
      else
        [ [ :same, old_entry, new_entry ] ]
      end
    else
      if old_value == :none
        old_exists = false
      elsif old_value.nil?
        old_exists = old_entry.exists?
      else
        old_exists = true
      end

      if new_value == :none
        new_exists = false
      elsif new_value.nil?
        new_exists = new_entry.exists?
      else
        new_exists = true
      end

      # If one of the files doesn't exist, we only want to print the diff if the
      # other file *could be uploaded/downloaded*.
      if !old_exists && !old_entry.parent.can_have_child?(new_entry.name, new_entry.dir?)
        return [ [ :old_cannot_upload, old_entry, new_entry ] ]
      end
      if !new_exists && !new_entry.parent.can_have_child?(old_entry.name, old_entry.dir?)
        return [ [ :new_cannot_upload, old_entry, new_entry ] ]
      end

      if get_content
        # If we haven't read the values yet, get them now so that they can be diffed
        begin
          old_value = old_entry.read if old_value.nil?
        rescue Chef::ChefFS::FileSystem::NotFoundError
          old_value = :none
        end
        begin
          new_value = new_entry.read if new_value.nil?
        rescue Chef::ChefFS::FileSystem::NotFoundError
          new_value = :none
        end
      end

      if old_value == :none || (old_value.nil? && !old_entry.exists?)
        [ [ :added, old_entry, new_entry, old_value, new_value ] ]
      elsif new_value == :none
        [ [ :deleted, old_entry, new_entry, old_value, new_value ] ]
      else
        [ [ :modified, old_entry, new_entry, old_value, new_value ] ]
      end
    end
  end
rescue Chef::ChefFS::FileSystem::FileSystemError => e
  [ [ :error, old_entry, new_entry, nil, nil, e ] ]
end