/**
   * 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;
  }
 /**
  * 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;
 }
 /**
  * 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;
 }
 private static boolean isEmpty(IHgRepositoryLocation loc) {
   return loc == null || (loc.getLocation() == null || loc.getLocation().length() == 0);
 }