package com.vectrace.MercurialEclipse.commands;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.SortedSet;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;

import com.vectrace.MercurialEclipse.MercurialEclipsePlugin;
import com.vectrace.MercurialEclipse.exception.HgException;
import com.vectrace.MercurialEclipse.model.ChangeSet;
import com.vectrace.MercurialEclipse.model.ChangeSet.Direction;
import com.vectrace.MercurialEclipse.preferences.MercurialPreferenceConstants;
import com.vectrace.MercurialEclipse.storage.HgRepositoryLocation;

/*******************************************************************************
 * Copyright (c) 2005-2008 VecTrace (Zingo Andersen) and others. All rights
 * reserved. This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0 which accompanies this
 * distribution, and is available at http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors: Bastian Doetsch - implementation
 ******************************************************************************/
public class HgIncomingClient extends AbstractParseChangesetClient {

    /**
     * Gets all File Revisions that are incoming and saves them in a bundle
     * file. There can be more than one revision per file as this method obtains
     * all new changesets.
     * 
     * @param proj
     * @param repositories
     * @return Map containing all revisions of the IResources contained in the
     *         Changesets. The sorting is ascending by date.
     * @throws HgException
     */
    public static Map<IResource, SortedSet<ChangeSet>> getHgIncoming(
            IProject proj, HgRepositoryLocation repository) throws HgException {
        HgCommand command = new HgCommand("incoming", proj, false);
        command
                .setUsePreferenceTimeout(MercurialPreferenceConstants.PULL_TIMEOUT);
        File bundleFile = getBundleFile(proj, repository);
        File temp = new File(bundleFile.getAbsolutePath() + ".temp."
                + System.currentTimeMillis());
        try {
            command.addOptions("--debug", "--template", TEMPLATE_WITH_FILES,
                    "--bundle", temp.getCanonicalPath(), repository.getUrl());
            String result = command.executeToString();
            if (result.contains("no changes found")) {
                return null;
            }
            Map<IResource, SortedSet<ChangeSet>> revisions = createMercurialRevisions(
                    result, proj, TEMPLATE_WITH_FILES, SEP_CHANGE_SET,
                    SEP_TEMPLATE_ELEMENT, Direction.INCOMING, repository,
                    bundleFile, START);
            temp.renameTo(bundleFile);
            return revisions;
        } catch (HgException hg) {
            if (hg.getMessage().contains("return code: 1")) {
                return null;
            }
            throw new HgException(hg.getMessage(), hg);
        } catch (IOException e) {
            throw new HgException(e.getMessage(), e);
        }
    }

    public static File getBundleFile(IProject proj, HgRepositoryLocation loc) {
        String strippedLocation = loc.getUrl().replace('/', '_').replace(':',
                '.');
        return MercurialEclipsePlugin.getDefault().getStateLocation().append(
                MercurialEclipsePlugin.BUNDLE_FILE_PREFIX + "."
                        + proj.getName() + "." + strippedLocation + ".hg")
                .toFile();
    }
}

@@ -1998,99 +1998,106 @@
 UT);
-%0D%0A        File bundleFile = getBundleFile(proj, repository);%0D%0A        File temp = new 
+                        %0D%0A        try %7B%0D%0A            final File bundleFile = File.createTemp
 File(
+%22
 bund
@@ -2106,37 +2106,60 @@
 File
-.getAbsolutePath() +
+-%22.concat(proj.getName()).concat(%22-%22),
  %22.t
-e
 mp
-.
 %22
+,null);
 %0D%0A  
@@ -2172,57 +2172,34 @@
     
-    + System.currentTimeMillis());%0D%0A        try %7B
+bundleFile.deleteOnExit();
 %0D%0A  
@@ -2310,25 +2310,30 @@
 e%22, 
-temp.getCanonical
+bundleFile.getAbsolute
 Path
@@ -2355,24 +2355,50 @@
 getUrl());%0D%0A
+                        %0D%0A
             
@@ -2822,48 +2822,8 @@
 );%0D%0A
-            temp.renameTo(bundleFile);%0D%0A
     
@@ -2985,32 +2985,82 @@
 %0A            %7D%0D%0A
+            MercurialEclipsePlugin.logError(hg);%0D%0A
             thro
@@ -3131,24 +3131,73 @@
 ption e) %7B%0D%0A
+            MercurialEclipsePlugin.logError(e);%0D%0A
             
@@ -3260,448 +3260,8 @@
   %7D%0D
-%0A%0D%0A    public static File getBundleFile(IProject proj, HgRepositoryLocation loc) %7B%0D%0A        String strippedLocation = loc.getUrl().replace('/', '_').replace(':',%0D%0A                '.');%0D%0A        return MercurialEclipsePlugin.getDefault().getStateLocation().append(%0D%0A                MercurialEclipsePlugin.BUNDLE_FILE_PREFIX + %22.%22%0D%0A                        + proj.getName() + %22.%22 + strippedLocation + %22.hg%22)%0D%0A                .toFile();%0D%0A    %7D%0D
 %0A%7D%0D%0A

create bundle-files differently to enhance robustness