function formatAuthUrl(protocol, repositoryUrl, gitCredentials) {
  const [match, auth, host, basePort, path] =
    /^(?!.+:\/\/)(?:(?<auth>.*)@)?(?<host>.*?):(?<port>\d+)?:?\/?(?<path>.*)$/.exec(repositoryUrl) || [];
  const { port, hostname, ...parsed } = parse(
    match ? `ssh://${auth ? `${auth}@` : ""}${host}${basePort ? `:${basePort}` : ""}/${path}` : repositoryUrl
  );

  return format({
    ...parsed,
    auth: gitCredentials,
    host: `${hostname}${protocol === "ssh:" ? "" : port ? `:${port}` : ""}`,
    protocol: protocol && /http[^s]/.test(protocol) ? "http" : "https",
  });
}
Machinery to format a repository URL with the given credentials

@param {String} protocol URL protocol (which should not be present in repositoryUrl)
@param {String} repositoryUrl User-given repository URL
@param {String} gitCredentials The basic auth part of the URL

@return {String} The formatted Git repository URL.