package com.atlassian.stash.internal.scm.git.protocol.ssh;

import com.atlassian.bitbucket.scm.ScmUrlFormatter;
import com.atlassian.bitbucket.scm.ScmUrlRequest;
import com.atlassian.bitbucket.scm.git.GitScm;
import com.atlassian.bitbucket.scm.http.RepositoryUrlFragment;

import javax.annotation.Nonnull;
import java.net.URI;
import java.net.URISyntaxException;

/**
 * @since 3.4
 */
public class GitSshUrlFormatter implements ScmUrlFormatter {

    @Nonnull
    @Override
    public URI format(@Nonnull ScmUrlRequest request) throws URISyntaxException {
        URI uri = request.getBaseUrl();
        String path = RepositoryUrlFragment.fromRepository(request.getRepository()).toPath(uri.getPath());

        return new URI(uri.getScheme(), GitScm.ID, uri.getHost(), uri.getPort(),
                path + ".git", uri.getQuery(), uri.getFragment());
    }
}
