# File lib/fastcst/command/sync.rb, line 128
128:        def run
129:            
130:            begin
131:                ftp = connect(@site, @user, @password, @directory)
132: 
133:                remote_index = nil
134:                begin
135:                    # attempt to get the yaml file, if it fails then we need to upload the works
136:                    Dir.chdir @repo.work_dir do
137:                        ftp.getbinaryfile("index.yaml")
138:                        remote_index = YAML.load_file("index.yaml")
139:                        File.unlink("index.yaml")
140:                    end
141:                rescue
142:                    UI.event :warn, "No index.yaml file found at #@directory on #@site (#$!)" 
143:                    remote_index = {'Changesets' => [], 'Revision Path' => nil}
144:                    
145:                    # we also need to make the root directory now
146:                    UI.event :warn, "Creating 'root' directory to hold changesets"
147:                    ftp.mkdir "root"
148:                end
149:                
150:                remote_cs = Set.new(remote_index['Changesets'])
151:                local_cs = Set.new(@repo.list_changesets)
152:                
153:                # find out what is new in our repository vs the remote
154:                new_cs = local_cs - remote_cs
155:                
156:                if new_cs.length == 0
157:                    UI.event :exit, "Remote repository is the same. Done."
158:                    return
159:                end
160:                
161:                ftp.chdir "root"
162:                new_cs.each do |id|
163:                    cs_path, md = @repo.find_changeset(id)
164:                    UI.event :upload, "#{md['Revision']} -- #{md['ID']}"
165:                    Dir.chdir cs_path do
166:                        ftp.mkdir id
167:                        ftp.chdir id
168:                        Distribution.upload(ftp, "meta-data.yaml")
169:                        ftp.chdir ".."
170:                    end
171:                end
172:                
173:                ftp.chdir ".."
174:                
175:                # build the index.yaml and upload it
176:                UI.start_finish("Uploading index.yaml") do
177:                    Dir.chdir @repo.work_dir do
178:                        local_index = {'Changesets' => local_cs.sort, 'Revision Path' => @repo['Path']}
179:                        File.open("index.yaml", "w") { |f| YAML.dump(local_index, f) }
180:                        ftp.putbinaryfile("index.yaml", "index.yaml")
181:                        File.unlink "index.yaml"
182:                    end
183:                end
184:                
185:            ensure
186:                ftp.close if ftp
187:            end
188:        end