private File fillPatch()

in command.line/java/com/jetbrains/teamcity/command/RemoteRun.java [544:596]


  private File fillPatch(final File patchFile, final Collection<ITCResource> resources, final IProgressMonitor monitor) throws IOException {
    DataOutputStream os = null;
    LowLevelPatchBuilderImpl patcher = null;
    final HashSet<String> modifiedResources = new HashSet<String>();
    final HashSet<String> deletedResources = new HashSet<String>();
    try {
      monitor.beginTask("Preparing patch");
      os = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(patchFile)));
      patcher = new LowLevelPatchBuilderImpl(os);
      for (final ITCResource resource : resources) {
        // threat file which is not exist as deleted
        if (resource.getLocal().exists()) {
          debug("+ %s", resource.getRepositoryPath());
          final LowLevelPatchBuilder.WriteFileContent content = new PatchBuilderImpl.StreamWriteFileContent(new BufferedInputStream(new FileInputStream(resource.getLocal())), resource.getLocal().length());
          patcher.changeBinary(resource.getRepositoryPath(), (int) resource.getLocal().length(), content, false);
          modifiedResources.add(resource.getLocal().getPath());

        } else {
          debug("- %s", resource.getRepositoryPath());
          patcher.delete(resource.getRepositoryPath(), true, false);
          deletedResources.add(resource.getLocal().getPath());

        }
      }

    } finally {// finalize patching
      if (patcher != null) {
        patcher.exit(""); 
        patcher.close();
      }
      if (os != null) {
        try {
          os.close();
        } catch (IOException e) {
          //
        }
      }
      final StringBuilder patchingResult = new StringBuilder();
      if (!modifiedResources.isEmpty()) {
        patchingResult.append(String.format("%d new/modified file(s)", modifiedResources.size()));
        if (!deletedResources.isEmpty()) {
          patchingResult.append(", ");
        }
      }
      if (!deletedResources.isEmpty()) {
        patchingResult.append(String.format("%d deleted file(s): %s", deletedResources.size(), deletedResources));
      }
      monitor.status(new ProgressStatus(IProgressStatus.INFO, patchingResult.toString())); 
      monitor.done();
    }
    return patchFile;

  }