package com.github.blindpirate.gogradle.vcs.git;

import com.github.blindpirate.gogradle.util.Assert;
import groovy.lang.Closure;
import org.codehaus.groovy.runtime.InvokerHelper;
import org.gradle.util.ConfigureUtil;

public class GitRepository {
    private boolean all;
    private Object urlPattern;
    private Object namePattern;
    private String privateKeyFilePath;
    private String username;
    private String password;

    public void all() {
        this.all = true;
    }

    public void url(Object url) {
        urlPattern = url;
    }

    public void name(Object name) {
        namePattern = name;
    }

    public void credentials(Closure closure) {
        ConfigureUtil.configure(closure, this);
    }

    public void privateKeyFile(String path) {
        privateKeyFilePath = path;
    }

    public void username(String username) {
        this.username = username;
    }

    public void password(String password) {
        this.password = password;
    }

    public boolean match(String name, String url) {
        if (all) {
            return true;
        }

        Assert.isTrue(namePattern != null || urlPattern != null);

        if (urlPattern != null) {
            return urlMatch(url);
        } else if (namePattern != null) {
            return nameMatch(name);
        } else {
            return urlMatch(url) && nameMatch(name);
        }
    }

    private boolean nameMatch(String name) {
        return (Boolean) InvokerHelper.invokeMethod(namePattern, "isCase", name);
    }

    private boolean urlMatch(String url) {
        return (Boolean) InvokerHelper.invokeMethod(urlPattern, "isCase", url);
    }

    public String getPrivateKeyFilePath() {
        return privateKeyFilePath;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

@@ -106,77 +106,593 @@
 ort 
-groovy.lang.Closure;%0Aimport org.codehaus.groovy.runtime.InvokerHelper
+com.github.blindpirate.gogradle.util.StringUtils;%0Aimport com.jcraft.jsch.JSch;%0Aimport com.jcraft.jsch.JSchException;%0Aimport com.jcraft.jsch.Session;%0Aimport groovy.lang.Closure;%0Aimport org.codehaus.groovy.runtime.InvokerHelper;%0Aimport org.eclipse.jgit.api.TransportCommand;%0Aimport org.eclipse.jgit.transport.JschConfigSessionFactory;%0Aimport org.eclipse.jgit.transport.OpenSshConfig;%0Aimport org.eclipse.jgit.transport.SshSessionFactory;%0Aimport org.eclipse.jgit.transport.SshTransport;%0Aimport org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;%0Aimport org.eclipse.jgit.util.FS
 ;%0Aim
@@ -2384,11 +2384,1019 @@
 d;%0A    %7D
+%0A%0A    public void configure(TransportCommand%3C?, ?%3E command) %7B%0A        if (StringUtils.isNotBlank(privateKeyFilePath)) %7B%0A            SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() %7B%0A                @Override%0A                protected void configure(OpenSshConfig.Host host, Session session) %7B%0A                    session.setConfig(%22StrictHostKeyChecking%22, %22no%22);%0A                %7D%0A%0A                @Override%0A                protected JSch createDefaultJSch(FS fs) throws JSchException %7B%0A                    JSch defaultJSch = super.createDefaultJSch(fs);%0A                    defaultJSch.addIdentity(privateKeyFilePath);%0A                    return defaultJSch;%0A                %7D%0A            %7D;%0A            command.setTransportConfigCallback(transport -%3E%0A                    SshTransport.class.cast(transport).setSshSessionFactory(sshSessionFactory));%0A        %7D else %7B%0A            command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));%0A        %7D%0A    %7D
 %0A%7D%0A

Add support for TransportCommand with credentials