// Path: src/main/java/org/echocat/gradle/plugins/golang/utils/FileUtils.java
// public static void deleteQuietly(@Nullable Path path) {
//     if (path == null || !exists(path)) {
//         return;
//     }
//     if (isRegularFile(path)) {
//         try {
//             doDelete(path);
//         } catch (final IOException ignored) {}
//         return;
//     }
//     try {
//         walkFileTree(path, new SimpleFileVisitor<Path>() {
//             @Override
//             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
//                 try {
//                     doDelete(file);
//                 } catch (final IOException ignored) {}
//                 return CONTINUE;
//             }
// 
//             @Override
//             public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
//                 try {
//                     doDelete(dir);
//                 } catch (final IOException ignored) {}
//                 return CONTINUE;
//             }
//         });
//     } catch (final IOException ignored) {}
// }
// 
// Path: src/main/java/org/echocat/gradle/plugins/golang/vcs/git/GitVcsUri.java
// @Nonnull
// public static GitVcsUri gitVcsUriFor(@Nonnull VcsReference reference) {
//     return gitVcsUriFor(reference.getUri());
// }


import com.sun.nio.sctp.IllegalReceiveException;
import org.echocat.gradle.plugins.golang.vcs.*;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.HttpTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.file.Path;
import java.util.regex.Pattern;
import static org.echocat.gradle.plugins.golang.utils.FileUtils.deleteQuietly;
import static org.echocat.gradle.plugins.golang.vcs.git.GitVcsUri.gitVcsUriFor;
        LOGGER.debug("Fetch remote refs from {}... DONE!", getReference().getUri());
        return selectFirstMatching(refs);
    }

    @Nullable
    protected Ref selectFirstMatching(@Nullable Iterable<Ref> refs) throws VcsException {
        if (refs == null) {
            return null;
        }
        for (final Ref ref : refs) {
            if (match(ref)) {
                return ref;
            }
        }
        return null;
    }

    protected boolean match(@Nullable Ref ref) throws VcsException {
        if (ref == null) {
            return false;
        }
        return match(ref.getName());
    }

    protected boolean match(@Nullable String ref) throws VcsException {
        if (ref == null) {
            return false;
        }
        String expected = getReference().getRef();
        if (expected == null) {
// Path: src/main/java/org/echocat/gradle/plugins/golang/utils/FileUtils.java
// public static void deleteQuietly(@Nullable Path path) {
//     if (path == null || !exists(path)) {
//         return;
//     }
//     if (isRegularFile(path)) {
//         try {
//             doDelete(path);
//         } catch (final IOException ignored) {}
//         return;
//     }
//     try {
//         walkFileTree(path, new SimpleFileVisitor<Path>() {
//             @Override
//             public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
//                 try {
//                     doDelete(file);
//                 } catch (final IOException ignored) {}
//                 return CONTINUE;
//             }
// 
//             @Override
//             public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
//                 try {
//                     doDelete(dir);
//                 } catch (final IOException ignored) {}
//                 return CONTINUE;
//             }
//         });
//     } catch (final IOException ignored) {}
// }
// 
// Path: src/main/java/org/echocat/gradle/plugins/golang/vcs/git/GitVcsUri.java
// @Nonnull
// public static GitVcsUri gitVcsUriFor(@Nonnull VcsReference reference) {
//     return gitVcsUriFor(reference.getUri());
// }


// Path: src/main/java/org/echocat/gradle/plugins/golang/vcs/git/GitVcsRepository.java
import com.sun.nio.sctp.IllegalReceiveException;
import org.echocat.gradle.plugins.golang.vcs.*;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.HttpTransport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.file.Path;
import java.util.regex.Pattern;
import static org.echocat.gradle.plugins.golang.utils.FileUtils.deleteQuietly;
import static org.echocat.gradle.plugins.golang.vcs.git.GitVcsUri.gitVcsUriFor;

        LOGGER.debug("Fetch remote refs from {}... DONE!", getReference().getUri());
        return selectFirstMatching(refs);
    }

    @Nullable
    protected Ref selectFirstMatching(@Nullable Iterable<Ref> refs) throws VcsException {
        if (refs == null) {
            return null;
        }
        for (final Ref ref : refs) {
            if (match(ref)) {
                return ref;
            }
        }
        return null;
    }

    protected boolean match(@Nullable Ref ref) throws VcsException {
        if (ref == null) {
            return false;
        }
        return match(ref.getName());
    }

    protected boolean match(@Nullable String ref) throws VcsException {
        if (ref == null) {
            return false;
        }
        String expected = getReference().getRef();
        if (expected == null) {
            expected = "HEAD";