# File lib/fastcst/command/apply.rb, line 36
 36:         def run
 37:             @id = @repo.resolve_id(@rev, @id)
 38:             target = File.dirname(@repo.path)  # need to know the source root to apply at
 39:             originals = File.expand_path(@repo.originals_dir)  # need the full path
 40:             
 41:             if @id
 42:                 # found an id, but need to verify that it's a child of the current revision
 43:                 parent_id = @repo['Path'].pop
 44:                 
 45:                 # a special case is if we're bootstrapping an empty dir, in which case
 46:                 # the path is empty so there will be no parent.  This is allowed.
 47:                 if @repo['Path'].empty? or parent_id == @repo.find_parent_of(@id)
 48:                     # alright, looks like we're in business
 49:                     cs_path, md = @repo.find_changeset(@id)
 50:                     data_file = nil
 51:                     journal_file = nil
 52: 
 53:                     Dir.chdir cs_path do
 54:                         md['Contents'].each do |info|
 55:                             name, digest, purpose = info['Name'], info['Digest'], info['Purpose']
 56:                             if digest != Digest::MD5.hexdigest(File.read(name))
 57:                                 UI.failure :security, "#{name} MD5 digest does not match. Aborting."
 58:                                 return
 59:                             end
 60:                             
 61:                             if purpose == "data"
 62:                                 # found the data file
 63:                                 data_file = name
 64:                             elsif purpose == "journal"
 65:                                 # found the journal file
 66:                                 journal_file = name
 67:                             end
 68:                             
 69:                             # found them, don't bother looking further
 70:                             if data_file and journal_file
 71:                                 break
 72:                             end
 73:                         end
 74:                         
 75:                         # apply_changeset closes the streams for us
 76:                         UI.start_finish("Applying to main directory") do
 77:                             journal_in = Zlib::GzipReader.new(File.open(journal_file))
 78:                             data_in = Zlib::GzipReader.new(File.open(data_file))
 79:                             ChangeSet.apply_changeset(journal_in, data_in, target, @test_run)
 80:                         end
 81: 
 82:                         if not File.exist? "undo.yaml" and not @test_run
 83:                             # the undo changeset has to go in the reverse direction
 84:                             UI.start_finish("Creating 'undo' revision") do
 85:                                 changes = ChangeSet.make_changeset("undo", target, originals)
 86:                             end
 87:                         end
 88:                         
 89:                         # and apply it to the originals directory to sync them up
 90:                         UI.start_finish("Applying to the repository originals directory") do
 91:                             journal_in = Zlib::GzipReader.new(File.open(journal_file))
 92:                             data_in = Zlib::GzipReader.new(File.open(data_file))
 93:                             ChangeSet.apply_changeset(journal_in, data_in, @repo.originals_dir, @test_run)
 94:                         end
 95:                         
 96:                         # update our current path (but not if we're testing)
 97:                         @repo['Path'] = @repo['Path'] << @id unless @test_run
 98:                     end
 99:                 else
100:                     UI.failure :constraint, "Sorry, can't apply a revision unless it is a child of the current one."
101:                 end
102:             else
103:                 UI.failure :search, "Could not find a matching revision"
104:             end
105:         end