'Which Java class should I use to represent Git SSH URLs?

I have a Git URL like [email protected]:boostorg/core.git that I would like to represent in memory.

I cannot use URL and URI because I get format exceptions. I would like to use something more structured than a String.

Does Java provide a good class from this?



Solution 1:[1]

As far as I know there is nothing like that in Java. You can write your own mini-class to represent that via String and for validating the URL you can use regular expression like this:

For a String s you can say:

s.matches(".+\@*.+:*\.git");

You can also use other regex to extract important part of the String.

Hope this helps

Solution 2:[2]

How about creating your own class and splitting the url into fields?

Something like this:

public class GitUrl{
    private String user;
    private String domain;
    private String path;

    public GitUrl(String user, String domain, String path){
        this.user = user;
        this.domain = domain;
        this.path = path;
    }

    //[...] Getters and Setters

    @Override
    public String toString(){
        return user + "@" + domain +":" + path;
    }
}

Solution 3:[3]

You could specify it as SSH URL. From https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols:

To clone a Git repository over SSH, you can specify an ssh:// URL like this:

$ git clone ssh://[user@]server/project.git

Or you can use the shorter scp-like syntax for the SSH protocol:

$ git clone [user@]server:project.git

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Carlos3dx
Solution 3 Paul Barnes