All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.jonasrutishauser.maven.wagon.git.GitConfiguration Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package com.github.jonasrutishauser.maven.wagon.git;

/*
 * Copyright (C) 2017 Jonas Rutishauser
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.
 * If not, see .
 */

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GitConfiguration
{

    /**
     * URL format: git:(//[identifier]/)?[url]((![branch])?!/?[path])?
     */
    private static final Pattern URL_PATTERN =
        Pattern.compile( "git:(//[^/]+/)?(?[^!]+)((!(?[^!]+))?!/?(?[^!]+)?)?" );

    private final String url;

    private final Optional branch;

    private final Optional path;

    private Path workingDirectory;

    private GitConfiguration( String url, Optional branch, Optional path )
    {
        this.url = url;
        this.branch = branch;
        this.path = path;
    }

    public static GitConfiguration parse( String url )
    {
        Matcher matcher = URL_PATTERN.matcher( url );
        if ( !matcher.matches() )
        {
            throw new IllegalArgumentException( "URL not valid" );
        }
        return new GitConfiguration( matcher.group( "url" ), Optional.ofNullable( matcher.group( "branch" ) ),
                                     Optional.ofNullable( matcher.group( "path" ) ).map( Paths::get ) );
    }

    public String getUrl()
    {
        return url;
    }

    public Optional getBranch()
    {
        return branch;
    }

    public Optional getPath()
    {
        return path;
    }

    public Path getWorkingDirectory()
        throws IOException
    {
        if ( workingDirectory == null )
        {
            workingDirectory = Files.createTempDirectory( "wagon-git-" );
        }
        return workingDirectory;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy