Skip to content

Commit

Permalink
Add --no-check-certificate flag #10
Browse files Browse the repository at this point in the history
  • Loading branch information
bozaro committed Jan 18, 2016
1 parent 123a780 commit 357092b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 29 deletions.
24 changes: 14 additions & 10 deletions build.gradle
@@ -1,7 +1,10 @@
buildscript {
repositories {
mavenLocal()
mavenCentral()
jcenter()
}

dependencies {
classpath "com.github.ben-manes:gradle-versions-plugin:0.12.0"
}
}

Expand All @@ -16,19 +19,20 @@ repositories {

apply plugin: "idea"
apply plugin: "java"
apply plugin: "com.github.ben-manes.versions"

dependencies {
compile "org.jetbrains:annotations:13.0"
compile "org.eclipse.jgit:org.eclipse.jgit:3.6.2.201501210735-r"
compile "org.mapdb:mapdb:1.0.6"
compile "org.slf4j:slf4j-simple:1.7.7"
compile "org.jetbrains:annotations:15.0"
compile "org.eclipse.jgit:org.eclipse.jgit:4.1.1.201511131810-r"
compile "org.mapdb:mapdb:1.0.8"
compile "org.slf4j:slf4j-simple:1.7.13"
compile "org.jgrapht:jgrapht-core:0.9.1"
compile "com.beust:jcommander:1.35"
compile "com.beust:jcommander:1.48"

compile "ru.bozaro.gitlfs:gitlfs-pointer:0.9.0-SNAPSHOT"
compile "ru.bozaro.gitlfs:gitlfs-client:0.9.0-SNAPSHOT"
compile "ru.bozaro.gitlfs:gitlfs-pointer:0.9.0"
compile "ru.bozaro.gitlfs:gitlfs-client:0.9.0"

testCompile "org.testng:testng:6.8.8"
testCompile "org.testng:testng:6.9.10"
}

sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down
54 changes: 35 additions & 19 deletions src/main/java/git/lfs/migrate/Main.java
Expand Up @@ -2,7 +2,10 @@

import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpStatus;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.eclipse.jgit.errors.InvalidPatternException;
import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
Expand All @@ -29,6 +32,7 @@
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.security.GeneralSecurityException;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -44,7 +48,7 @@ public class Main {
@NotNull
private static final Logger log = LoggerFactory.getLogger(Main.class);

public static void main(@NotNull String[] args) throws IOException, InterruptedException, ExecutionException, InvalidPatternException {
public static void main(@NotNull String[] args) throws Exception {
final CmdArgs cmd = new CmdArgs();
final JCommander jc = new JCommander(cmd);
jc.parse(args);
Expand All @@ -53,32 +57,43 @@ public static void main(@NotNull String[] args) throws IOException, InterruptedE
return;
}
final long time = System.currentTimeMillis();
final AuthProvider auth;
final Client client;
if (cmd.lfs != null) {
auth = new BasicAuthProvider(URI.create(cmd.lfs));
client = createClient(new BasicAuthProvider(URI.create(cmd.lfs)), cmd);
} else if (cmd.git != null) {
auth = AuthHelper.create(cmd.git);
client = createClient(AuthHelper.create(cmd.git), cmd);
} else {
auth = null;
client = null;
}
if (!checkLfsAuthenticate(auth)) {
if (!checkLfsAuthenticate(client)) {
return;
}
if (cmd.checkLfs) {
if (auth == null) {
if (client == null) {
log.error("Git LFS server is not defined.");
}
return;
}
processRepository(cmd.src, cmd.dst, cmd.cache, auth, cmd.writeThreads, cmd.uploadThreads, cmd.globs.toArray(new String[cmd.globs.size()]));
processRepository(cmd.src, cmd.dst, cmd.cache, client, cmd.writeThreads, cmd.uploadThreads, cmd.globs.toArray(new String[cmd.globs.size()]));
log.info("Convert time: {}", System.currentTimeMillis() - time);
}

private static boolean checkLfsAuthenticate(@Nullable AuthProvider auth) throws IOException {
if (auth == null)
@NotNull
private static Client createClient(@NotNull AuthProvider auth, @NotNull CmdArgs cmd) throws GeneralSecurityException {
final HttpClientBuilder httpBuilder = HttpClients.custom();
if (cmd.noCheckCertificate) {
httpBuilder.setSSLHostnameVerifier((hostname, session) -> true);
httpBuilder.setSSLContext(SSLContexts.custom()
.loadTrustMaterial((chain, authType) -> true)
.build());
}
return new Client(auth, httpBuilder.build());
}

private static boolean checkLfsAuthenticate(@Nullable Client client) throws IOException {
if (client == null)
return true;
final Meta meta = new Meta("0123456789012345678901234567890123456789012345678901234567890123", 42);
Client client = new Client(auth);
try {
BatchRes response = client.postBatch(
new BatchReq(Operation.Upload, Collections.singletonList(
Expand Down Expand Up @@ -113,7 +128,7 @@ private static boolean checkLfsAuthenticate(@Nullable AuthProvider auth) throws
return false;
}

public static void processRepository(@NotNull File srcPath, @NotNull File dstPath, @NotNull File cachePath, @Nullable AuthProvider auth, int writeThreads, int uploadThreads, @NotNull String... globs) throws IOException, InterruptedException, ExecutionException, InvalidPatternException {
public static void processRepository(@NotNull File srcPath, @NotNull File dstPath, @NotNull File cachePath, @Nullable Client client, int writeThreads, int uploadThreads, @NotNull String... globs) throws IOException, InterruptedException, ExecutionException, InvalidPatternException {
removeDirectory(dstPath);
dstPath.mkdirs();

Expand All @@ -135,7 +150,7 @@ public static void processRepository(@NotNull File srcPath, @NotNull File dstPat

final ConcurrentMap<TaskKey, ObjectId> converted = new ConcurrentHashMap<>();
log.info("Converting object without dependencies in " + writeThreads + " threads...", totalObjects);
try (HttpUploader uploader = createHttpUploader(srcRepo, auth, uploadThreads)) {
try (HttpUploader uploader = createHttpUploader(srcRepo, client, uploadThreads)) {
processMultipleThreads(converter, graph, srcRepo, dstRepo, converted, uploader, writeThreads);
}

Expand Down Expand Up @@ -163,8 +178,8 @@ public static void processRepository(@NotNull File srcPath, @NotNull File dstPat
}

@Nullable
private static HttpUploader createHttpUploader(@NotNull Repository repository, @Nullable AuthProvider auth, int uploadThreads) {
return auth == null ? null : new HttpUploader(repository, auth, uploadThreads);
private static HttpUploader createHttpUploader(@NotNull Repository repository, @Nullable Client client, int uploadThreads) {
return client == null ? null : new HttpUploader(repository, client, uploadThreads);
}

private static void processMultipleThreads(@NotNull GitConverter converter, @NotNull SimpleDirectedGraph<TaskKey, DefaultEdge> graph, @NotNull Repository srcRepo, @NotNull Repository dstRepo, @NotNull ConcurrentMap<TaskKey, ObjectId> converted, @Nullable HttpUploader uploader, int threads) throws IOException, InterruptedException {
Expand Down Expand Up @@ -317,9 +332,9 @@ public static class HttpUploader implements GitConverter.Uploader, AutoCloseable
@NotNull
private final AtomicInteger total = new AtomicInteger();

public HttpUploader(@NotNull Repository repository, @NotNull AuthProvider auth, int threads) {
public HttpUploader(@NotNull Repository repository, @NotNull Client client, int threads) {
this.pool = Executors.newFixedThreadPool(threads);
this.uploader = new BatchUploader(new Client(auth), pool);
this.uploader = new BatchUploader(client, pool);
this.repository = repository;
}

Expand Down Expand Up @@ -420,12 +435,13 @@ public static class CmdArgs {
private int uploadThreads = 4;
@Parameter(names = {"--check-lfs"}, description = "Check LFS server settings and exit")
private boolean checkLfs = false;
@Parameter(names = {"--no-check-certificate"}, description = "Don't check the server certificate against the available certificate authorities")
private boolean noCheckCertificate = false;

@Parameter(description = "LFS file glob patterns")
@NotNull
private List<String> globs = new ArrayList<>();
@Parameter(names = {"-h", "--help"}, description = "Show help", help = true)
private boolean help = false;
}

}

0 comments on commit 357092b

Please sign in to comment.