package org.jetbrains.idea.svn.copy;

import com.intellij.openapi.vcs.VcsException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.api.BaseSvnClient;
import org.jetbrains.idea.svn.commandLine.*;
import org.tmatesoft.svn.core.wc.ISVNEventHandler;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc2.SvnTarget;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Konstantin Kolosovsky.
 */
public class CmdCopyMoveClient extends BaseSvnClient implements CopyMoveClient {

  @Override
  public void copy(@NotNull File src, @NotNull File dst, boolean makeParents, boolean isMove) throws VcsException {
    List<String> parameters = new ArrayList<String>();

    CommandUtil.put(parameters, src);
    CommandUtil.put(parameters, dst, false);
    CommandUtil.put(parameters, makeParents, "--parents");

    // for now parsing of the output is not required as command is executed only for one file
    // and will be either successful or exception will be thrown
    // TODO: for now use dst as target, as if using source - then process will be started in the source folder and folder will be locked
    // TODO: And if resolving working directory to some other folder (i.e. project root) => errors in parsing (info, status) occur, as
    // TODO: base directory passed to parser do not correspond to working directory, but svn outputs relative paths
    CommandUtil.execute(myVcs, SvnTarget.fromFile(dst), isMove ? SvnCommandName.move : SvnCommandName.copy, parameters, null);
  }

  @Override
  public long copy(@NotNull SvnTarget source,
                            @NotNull SvnTarget destination,
                            @Nullable SVNRevision revision,
                            boolean makeParents,
                            @NotNull String message,
                            @Nullable CommitEventHandler handler) throws VcsException {
    if (!destination.isURL()) {
      throw new IllegalArgumentException("Only urls are supported as destination " + destination);
    }

    // TODO: Check that command fails when destination exists
    List<String> parameters = new ArrayList<String>();

    CommandUtil.put(parameters, source);
    CommandUtil.put(parameters, destination);
    CommandUtil.put(parameters, revision);
    CommandUtil.put(parameters, makeParents, "--parents");
    parameters.add("--message");
    parameters.add(message);

    // copy to url output is the same as commit output - just statuses have "copy of" suffix
    // so "Adding" will be "Adding copy of"
    SvnCommitRunner.CommandListener listener = new SvnCommitRunner.CommandListener(handler);
    // TODO: Check correctness when source is url
    if (source.isFile()) {
      listener.setBaseDirectory(source.getFile());
    }
    CommandUtil.execute(myVcs, source, SvnCommandName.copy, parameters, listener);

    return listener.getCommittedRevision();
  }

  @Override
  public void copy(@NotNull SvnTarget source,
                   @NotNull File destination,
                   @Nullable SVNRevision revision,
                   boolean makeParents,
                   @Nullable ISVNEventHandler handler) throws VcsException {
    List<String> parameters = new ArrayList<String>();

    CommandUtil.put(parameters, source);
    CommandUtil.put(parameters, destination);
    CommandUtil.put(parameters, revision);
    CommandUtil.put(parameters, makeParents, "--parents");

    File workingDirectory = CommandUtil.getHomeDirectory();
    BaseUpdateCommandListener listener = new BaseUpdateCommandListener(workingDirectory, handler);

    CommandUtil.execute(myVcs, source, workingDirectory, SvnCommandName.copy, parameters, listener);

    listener.throwWrappedIfException();
  }
}

@@ -31,16 +31,107 @@
 .copy;%0A%0A
+import com.intellij.openapi.util.SystemInfo;%0Aimport com.intellij.openapi.util.io.FileUtil;%0A
 import c
@@ -1184,122 +1184,170 @@
  // 
-TODO: for now use dst as target, as if using source - then process will be started in the source folder and folder
+Use idea home directory for directory renames which differ only by character case on case insensitive file systems - otherwise that%0A    // directory being renamed
  wil
@@ -1355,52 +1355,47 @@
  be 
+b
 locked
-%0A    // TODO: And if resolving
+ by svn process%0A    File
  working
  dir
@@ -1382,34 +1382,33 @@
     File working
- d
+D
 irectory to some
@@ -1404,210 +1404,137 @@
 ory 
-to some other folder (i.e. project root) =%3E errors in parsing (info, status) occur, as%0A    // TODO: base directory passed to parser do not correspond to working directory, but svn outputs relative paths
+=%0A      isMove && !SystemInfo.isFileSystemCaseSensitive && FileUtil.filesEqual(src, dst) ? CommandUtil.getHomeDirectory() : null;
 %0A   
@@ -1537,32 +1537,39 @@
 %0A    CommandUtil
+%0A      
 .execute(myVcs, 
@@ -1592,16 +1592,34 @@
 le(dst),
+ workingDirectory,
  isMove 

Use idea home directory as working directory for "move" operation when move paths differ only by case for case insensitive file systems