// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.idea.svn.commandLine;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.svn.api.*;

import java.io.File;

import static com.intellij.openapi.vfs.VfsUtilCore.virtualToIoFile;

public class CommandParametersResolutionModule extends BaseCommandRuntimeModule {

  public CommandParametersResolutionModule(@NotNull CommandRuntime runtime) {
    super(runtime);
  }

  @Override
  public void onStart(@NotNull Command command) {
    if (command.getRepositoryUrl() == null) {
      command.setRepositoryUrl(resolveRepositoryUrl(command));
    }
    if (command.getWorkingDirectory() == null) {
      command.setWorkingDirectory(resolveWorkingDirectory(command));
    }
    command.setConfigDir(myAuthenticationService.getSpecialConfigDir().toFile());
    command.saveOriginalParameters();
  }

  @Nullable
  private Url resolveRepositoryUrl(@NotNull Command command) {
    UrlMappingRepositoryProvider urlMappingProvider = new UrlMappingRepositoryProvider(myVcs, command.getTarget());
    InfoCommandRepositoryProvider infoCommandProvider = new InfoCommandRepositoryProvider(myVcs, command.getTarget());

    Repository repository = urlMappingProvider.get();
    if (repository == null && !command.isLocalInfo()) {
      repository = infoCommandProvider.get();
    }

    return repository != null ? repository.getUrl() : null;
  }

  @NotNull
  private File resolveWorkingDirectory(@NotNull Command command) {
    Target target = command.getTarget();
    File workingDirectory = target.isFile() ? target.getFile() : null;
    // TODO: Do we really need search existing parent - or just take parent directory if target is file???
    workingDirectory = CommandUtil.findExistingParent(workingDirectory);

    return workingDirectory != null ? workingDirectory : getDefaultWorkingDirectory(myVcs.getProject());
  }

  @NotNull
  public static File getDefaultWorkingDirectory(@NotNull Project project) {
    VirtualFile baseDir = project.getBaseDir();

    return baseDir != null ? virtualToIoFile(baseDir) : CommandUtil.getHomeDirectory();
  }
}

@@ -17,10 +17,10 @@
 0-20
-17
+20
  Jet
@@ -388,13 +388,233 @@
 svn.
-api.*
+RootUrlInfo;%0Aimport org.jetbrains.idea.svn.api.InfoCommandRepositoryProvider;%0Aimport org.jetbrains.idea.svn.api.Repository;%0Aimport org.jetbrains.idea.svn.api.Url;%0Aimport org.jetbrains.idea.svn.api.UrlMappingRepositoryProvider
 ;%0A%0Ai
@@ -701,16 +701,72 @@
 oIoFile;
+%0Aimport static com.intellij.vcsUtil.VcsUtil.getFilePath;
 %0A%0Apublic
@@ -1996,21 +1996,17 @@
     
-Target target
+File file
  = c
@@ -2027,316 +2027,178 @@
 et()
-;%0A    File workingDirectory = target.isFile() ? target.getFile() : null;%0A    // TODO: Do we really need search existing parent - or just take parent directory if target is file???%0A    workingDirectory = CommandUtil.findExistingParent(workingDirectory);%0A%0A    return workingDirectory != null ? workingDirectory
+.getFile();%0A    RootUrlInfo root = file != null ? myVcs.getSvnFileUrlMapping().getWcRootForFilePath(getFilePath(file)) : null;%0A%0A    return root != null ? root.getIoFile()
  : g

Use more generic working directory for svn commands