Ejemplo n.º 1
0
 @NotNull
 public static List<VcsDirectoryMapping> addMapping(
     @NotNull List<VcsDirectoryMapping> existingMappings,
     @NotNull String path,
     @NotNull String vcs) {
   List<VcsDirectoryMapping> mappings = new ArrayList<VcsDirectoryMapping>(existingMappings);
   for (Iterator<VcsDirectoryMapping> iterator = mappings.iterator(); iterator.hasNext(); ) {
     VcsDirectoryMapping mapping = iterator.next();
     if (mapping.isDefaultMapping() && StringUtil.isEmptyOrSpaces(mapping.getVcs())) {
       LOG.debug("Removing <Project> -> <None> mapping");
       iterator.remove();
     } else if (FileUtil.pathsEqual(mapping.getDirectory(), path)) {
       if (!StringUtil.isEmptyOrSpaces(mapping.getVcs())) {
         LOG.warn(
             "Substituting existing mapping ["
                 + path
                 + "] -> ["
                 + mapping.getVcs()
                 + "] with ["
                 + vcs
                 + "]");
       } else {
         LOG.debug("Removing [" + path + "] -> <None> mapping");
       }
       iterator.remove();
     }
   }
   mappings.add(new VcsDirectoryMapping(path, vcs));
   return mappings;
 }
 @Override
 @Nullable
 public VcsRoot getVcsRootObjectFor(final VirtualFile file) {
   final VcsDirectoryMapping mapping = myMappings.getMappingFor(file);
   if (mapping == null) {
     return null;
   }
   final String directory = mapping.getDirectory();
   final AbstractVcs vcs = findVcsByName(mapping.getVcs());
   if (directory.isEmpty()) {
     return new VcsRoot(vcs, myDefaultVcsRootPolicy.getVcsRootFor(file));
   }
   return new VcsRoot(vcs, LocalFileSystem.getInstance().findFileByPath(directory));
 }
  public void readDirectoryMappings(final Element element) {
    myMappings.clear();

    final List<VcsDirectoryMapping> mappingsList = new ArrayList<VcsDirectoryMapping>();
    boolean haveNonEmptyMappings = false;
    for (Element child : element.getChildren(ELEMENT_MAPPING)) {
      final String vcs = child.getAttributeValue(ATTRIBUTE_VCS);
      if (vcs != null && !vcs.isEmpty()) {
        haveNonEmptyMappings = true;
      }
      VcsDirectoryMapping mapping =
          new VcsDirectoryMapping(child.getAttributeValue(ATTRIBUTE_DIRECTORY), vcs);
      mappingsList.add(mapping);

      Element rootSettingsElement = child.getChild(ELEMENT_ROOT_SETTINGS);
      if (rootSettingsElement != null) {
        String className = rootSettingsElement.getAttributeValue(ATTRIBUTE_CLASS);
        AbstractVcs vcsInstance = findVcsByName(mapping.getVcs());
        if (vcsInstance != null && className != null) {
          final VcsRootSettings rootSettings = vcsInstance.createEmptyVcsRootSettings();
          if (rootSettings != null) {
            try {
              rootSettings.readExternal(rootSettingsElement);
              mapping.setRootSettings(rootSettings);
            } catch (InvalidDataException e) {
              LOG.error(
                  "Failed to load VCS root settings class "
                      + className
                      + " for VCS "
                      + vcsInstance.getClass().getName(),
                  e);
            }
          }
        }
      }
    }
    boolean defaultProject =
        Boolean.TRUE.toString().equals(element.getAttributeValue(ATTRIBUTE_DEFAULT_PROJECT));
    // run autodetection if there's no VCS in default project and
    if (haveNonEmptyMappings || !defaultProject) {
      myMappingsLoaded = true;
    }
    myMappings.setDirectoryMappings(mappingsList);
  }
 public void writeDirectoryMappings(final Element element) {
   if (myProject.isDefault()) {
     element.setAttribute(ATTRIBUTE_DEFAULT_PROJECT, Boolean.TRUE.toString());
   }
   for (VcsDirectoryMapping mapping : getDirectoryMappings()) {
     Element child = new Element(ELEMENT_MAPPING);
     child.setAttribute(ATTRIBUTE_DIRECTORY, mapping.getDirectory());
     child.setAttribute(ATTRIBUTE_VCS, mapping.getVcs());
     final VcsRootSettings rootSettings = mapping.getRootSettings();
     if (rootSettings != null) {
       Element rootSettingsElement = new Element(ELEMENT_ROOT_SETTINGS);
       rootSettingsElement.setAttribute(ATTRIBUTE_CLASS, rootSettings.getClass().getName());
       try {
         rootSettings.writeExternal(rootSettingsElement);
         child.addContent(rootSettingsElement);
       } catch (WriteExternalException e) {
         // don't add element
       }
     }
     element.addContent(child);
   }
 }