IDEADEV-41714 ("Collecting Information on Changes" hangs entire GUI) r=yole
[fedora-idea.git] / plugins / svn4idea / src / org / jetbrains / idea / svn / branchConfig / DefaultConfigLoader.java
blob12f122fc4ab6285bcdf92329058cb53ddd65b5d8
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.jetbrains.idea.svn.branchConfig;
18 import com.intellij.openapi.diagnostic.Logger;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.vfs.VirtualFile;
21 import org.jetbrains.annotations.NonNls;
22 import org.jetbrains.annotations.Nullable;
23 import org.jetbrains.idea.svn.SvnVcs;
24 import org.jetbrains.idea.svn.integrate.SvnBranchItem;
25 import org.tmatesoft.svn.core.*;
26 import org.tmatesoft.svn.core.internal.util.SVNPathUtil;
27 import org.tmatesoft.svn.core.internal.wc.admin.SVNEntry;
28 import org.tmatesoft.svn.core.internal.wc.admin.SVNWCAccess;
29 import org.tmatesoft.svn.core.wc.SVNLogClient;
30 import org.tmatesoft.svn.core.wc.SVNRevision;
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.List;
36 public class DefaultConfigLoader {
37 private static final Logger LOG = Logger.getInstance("#org.jetbrains.idea.svn.branchConfig.DefaultConfigLoader");
38 @NonNls private static final String DEFAULT_TRUNK_NAME = "trunk";
39 @NonNls private static final String DEFAULT_BRANCHES_NAME = "branches";
40 @NonNls private static final String DEFAULT_TAGS_NAME = "tags";
42 private DefaultConfigLoader() {
45 @Nullable
46 public static SvnBranchConfigurationNew loadDefaultConfiguration(final Project project, final VirtualFile vcsRoot) {
47 try {
48 SVNURL baseUrl = null;
49 final SVNWCAccess wcAccess = SVNWCAccess.newInstance(null);
50 File rootFile = new File(vcsRoot.getPath());
51 wcAccess.open(rootFile, false, 0);
52 try {
53 SVNEntry entry = wcAccess.getEntry(rootFile, false);
54 if (entry != null) {
55 baseUrl = entry.getSVNURL();
57 else {
58 LOG.info("Directory is not a working copy: " + vcsRoot.getPresentableUrl());
59 return null;
62 finally {
63 wcAccess.close();
66 final SvnBranchConfigurationNew result = new SvnBranchConfigurationNew();
67 result.setTrunkUrl(baseUrl.toString());
68 while(true) {
69 final String s = SVNPathUtil.tail(baseUrl.getPath());
70 if (s.equalsIgnoreCase(DEFAULT_TRUNK_NAME) || s.equalsIgnoreCase(DEFAULT_BRANCHES_NAME) || s.equalsIgnoreCase(DEFAULT_TAGS_NAME)) {
71 final SVNURL rootPath = baseUrl.removePathTail();
72 SVNLogClient client = SvnVcs.getInstance(project).createLogClient();
73 client.doList(rootPath, SVNRevision.UNDEFINED, SVNRevision.HEAD, false, false, new ISVNDirEntryHandler() {
74 public void handleDirEntry(final SVNDirEntry dirEntry) throws SVNException {
75 if (("".equals(dirEntry.getRelativePath())) || (! SVNNodeKind.DIR.equals(dirEntry.getKind()))) {
76 // do not use itself or files
77 return;
80 if (dirEntry.getName().toLowerCase().endsWith(DEFAULT_TRUNK_NAME)) {
81 result.setTrunkUrl(rootPath.appendPath(dirEntry.getName(), false).toString());
83 else {
84 result.addBranches(rootPath.appendPath(dirEntry.getName(), false).toString(),
85 new InfoStorage<List<SvnBranchItem>>(new ArrayList<SvnBranchItem>(0), InfoReliability.defaultValues));
88 });
90 break;
92 if (SVNPathUtil.removeTail(baseUrl.getPath()).length() == 0) {
93 break;
95 baseUrl = baseUrl.removePathTail();
97 return result;
99 catch (SVNException e) {
100 LOG.info(e);
101 return null;