/**
   * Get a repo specified by properties. If repository for given url is unknown, returns a new
   * location.
   *
   * @return never returns null
   */
  public IHgRepositoryLocation getRepoLocation(Properties props) throws HgException {
    String user = props.getProperty("user"); // $NON-NLS-1$
    if ((user == null) || (user.length() == 0)) {
      user = null;
    }
    String password = props.getProperty("password"); // $NON-NLS-1$
    if (user == null) {
      password = null;
    }
    String url = props.getProperty("url"); // $NON-NLS-1$
    if (url == null) {
      throw new HgException(
          Messages.getString("HgRepositoryLocation.urlMustNotBeNull")); // $NON-NLS-1$
    }

    IHgRepositoryLocation location = matchRepoLocation(url);
    if (location != null) {
      if (user == null
          || user.length() == 0
          || (user.equals(location.getUser())
              && (password == null || password.equals(location.getPassword())))) {
        return location;
      }
    }

    // make a new location if no matches exist or it's a different user
    return HgRepositoryLocationParser.parseLocation(url, user, password);
  }
 public void disposeRepository(IHgRepositoryLocation hgRepo) {
   Assert.isNotNull(hgRepo);
   for (HgRoot hgRoot : rootRepos.keySet()) {
     Set<IHgRepositoryLocation> pRepos = rootRepos.get(hgRoot);
     if (pRepos != null) {
       boolean removed = false;
       synchronized (entriesLock) {
         for (IHgRepositoryLocation repo : pRepos) {
           if (repo.equals(hgRepo)) {
             removed = pRepos.remove(repo);
             break;
           }
         }
       }
       if (removed) {
         repositoryRemoved(hgRepo);
       }
     }
   }
   IHgRepositoryLocation removed = null;
   synchronized (repoHistory) {
     for (IHgRepositoryLocation loc : repoHistory) {
       if (loc.equals(hgRepo)) {
         repoHistory.remove(loc);
         removed = loc;
         break;
       }
     }
   }
   if (removed != null) {
     repositoryRemoved(removed);
   }
 }
 /**
  * Simple search on existing repos.
  *
  * @return may return null
  */
 private IHgRepositoryLocation matchRepoLocation(String url) {
   url = HgRepositoryLocationParser.trimLocation(url);
   if (StringUtils.isEmpty(url)) {
     return null;
   }
   for (IHgRepositoryLocation loc : getAllRepoLocations()) {
     if (url.equals(loc.getLocation())) {
       return loc;
     }
   }
   return null;
 }
  /**
   * Gets a repo by its URL. If URL is unknown, returns a new location, adding it to the global
   * repositories cache. Will update stored last user and password with the provided values.
   */
  public IHgRepositoryLocation updateRepoLocation(
      HgRoot hgRoot, String url, String logicalName, String user, String pass) throws HgException {
    IHgRepositoryLocation loc = matchRepoLocation(url);

    if (loc == null) {
      // in some cases url may be a repository database line
      loc = HgRepositoryLocationParser.parseLocation(logicalName, url, user, pass);
      addRepoLocation(loc);
      return loc;
    }

    boolean update = false;

    String myLogicalName = logicalName;
    String myUser = user;
    String myPass = pass;

    if (logicalName != null
        && logicalName.length() > 0
        && !logicalName.equals(loc.getLogicalName())) {
      update = true;
    } else {
      myLogicalName = loc.getLogicalName();
    }
    if (user != null && user.length() > 0 && !user.equals(loc.getUser())) {
      update = true;
    } else {
      myUser = loc.getUser();
    }
    if (pass != null && pass.length() > 0 && !pass.equals(loc.getPassword())) {
      update = true;
    } else {
      myPass = loc.getPassword();
    }

    if (update) {
      IHgRepositoryLocation updated =
          HgRepositoryLocationParser.parseLocation(
              myLogicalName, loc.getLocation(), myUser, myPass);

      synchronized (entriesLock) {
        for (Set<IHgRepositoryLocation> locs : rootRepos.values()) {
          if (locs.remove(updated)) {
            locs.add(updated);
          }
        }
      }
      synchronized (repoHistory) {
        if (repoHistory.remove(updated)) {
          repoHistory.add(updated);
        }
      }
      repositoryModified(updated);
      return updated;
    }

    return loc;
  }
  /**
   * Get a repo by its URL. If URL is unknown, returns a new location.
   *
   * @return never returns null
   */
  public IHgRepositoryLocation getRepoLocation(String url, String user, String pass)
      throws HgException {
    getProjectRepos();
    IHgRepositoryLocation location = matchRepoLocation(url);
    if (location != null) {
      if (user == null
          || user.length() == 0
          || (user.equals(location.getUser())
              && (pass == null || pass.equals(location.getPassword())))) {
        return location;
      }
    }

    // make a new location if no matches exist or it's a different user
    return HgRepositoryLocationParser.parseLocation(url, user, pass);
  }
 /**
  * Set given location as default (topmost in hg repositories)
  *
  * @param hgRoot a valid hg root (not null)
  * @param loc a valid repoository location (not null)
  */
 public void setDefaultRepository(HgRoot hgRoot, IHgRepositoryLocation loc) {
   Assert.isNotNull(hgRoot);
   Assert.isNotNull(loc);
   Set<IHgRepositoryLocation> locations = rootRepos.get(hgRoot);
   IPreferenceStore store = MercurialEclipsePlugin.getDefault().getPreferenceStore();
   store.setValue(KEY_DEF_REPO_PREFIX + getRootKey(hgRoot), loc.getLocation());
   if (locations != null && !locations.contains(loc)) {
     synchronized (entriesLock) {
       locations.add(loc);
     }
   } else {
     internalAddRepoLocation(hgRoot, loc);
   }
 }
 /**
  * Returns the default repository location for a hg root, if it is set.
  *
  * @return may return null
  */
 public IHgRepositoryLocation getDefaultRepoLocation(HgRoot hgRoot) {
   IPreferenceStore store = MercurialEclipsePlugin.getDefault().getPreferenceStore();
   String defLoc = store.getString(KEY_DEF_REPO_PREFIX + getRootKey(hgRoot));
   if (StringUtils.isEmpty(defLoc)) {
     return null;
   }
   Set<IHgRepositoryLocation> locations = rootRepos.get(hgRoot);
   if (locations != null && !locations.isEmpty()) {
     synchronized (entriesLock) {
       for (IHgRepositoryLocation repo : locations) {
         if (repo.getLocation().equals(defLoc)) {
           return repo;
         }
       }
     }
   }
   for (IHgRepositoryLocation repo : repoHistory) {
     if (repo.getLocation().equals(defLoc)) {
       internalAddRepoLocation(hgRoot, repo);
       return repo;
     }
   }
   return null;
 }
  /**
   * @param repo non null repo location
   * @return a set of projects we know managed at given location, never null
   */
  public Set<IProject> getAllRepoLocationProjects(IHgRepositoryLocation repo) {
    Set<IProject> projects = new LinkedHashSet<IProject>();

    try {
      getProjectRepos();
    } catch (Exception e) {
      MercurialEclipsePlugin.logError(e);
    }

    Set<Entry<HgRoot, Set<IHgRepositoryLocation>>> entrySet = rootRepos.entrySet();
    for (Entry<HgRoot, Set<IHgRepositoryLocation>> entry : entrySet) {
      Set<IHgRepositoryLocation> set = entry.getValue();
      if (set != null && set.contains(repo)) {
        projects.addAll(ResourceUtils.getProjects(entry.getKey()));
      }
    }
    if (projects.isEmpty() && repo.isLocal()) {
      HgRoot hgRoot = repo.toHgRoot();
      if (hgRoot != null) {
        projects.addAll(MercurialTeamProvider.getKnownHgProjects(hgRoot));
      }
    }
    return projects;
  }
 private static boolean isEmpty(IHgRepositoryLocation loc) {
   return loc == null || (loc.getLocation() == null || loc.getLocation().length() == 0);
 }