/**
   * 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;
  }