Skip to content

Instantly share code, notes, and snippets.

@timothyklim
Created February 23, 2012 17:48
Show Gist options
  • Save timothyklim/1893998 to your computer and use it in GitHub Desktop.
Save timothyklim/1893998 to your computer and use it in GitHub Desktop.
JGit test with DHT
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
public class Main {
static public void main(String[] args) throws IOException, URISyntaxException, InvalidRemoteException {
Repository repo = org.gitective.mongo.MongoDatabase.open("git_repo_test");
try {
repo.create(true);
}
catch ( org.eclipse.jgit.storage.dht.DhtException e) {}
StoredConfig config = repo.getConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
URIish uri = new URIish("file://" + System.getProperty("user.dir") + "/test_git_repo");
remoteConfig.addURI(uri);
remoteConfig.update(config);
config.save();
repo.close();
// This code works only for non-dht code, but how I can clone repo from MongoDB? :3
// CloneCommand cloneCommand = new CloneCommand();
// cloneCommand.setURI(repo.getDirectory().getPath());
// cloneCommand.setDirectory(new File("/tmp/for_git_test"));
// cloneCommand.setBare(false);
// cloneCommand.call();
}
}
@kevinsawicki
Copy link

Did you try the example in the README?

Repository repo = MongoDatabase.open("linux-26");
repo.create(true);
StoredConfig config = repo.getConfig();
RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
URIish uri = new URIish("git://github.com/mirrors/linux-2.6.git");
remoteConfig.addURI(uri);
remoteConfig.update(config);
config.save();
RefSpec spec = new RefSpec("refs/heads/*:refs/remotes/origin/*");
Git.wrap(repo).fetch().setRemote("origin").setRefSpecs(spec).call();

@timothyklim
Copy link
Author

timothyklim commented Feb 23, 2012 via email

@kevinsawicki
Copy link

Sure, I'm going to make some updates today to make sure this works with the latest JGit and mongo drivers as well.

@kevinsawicki
Copy link

So you want to clone an existing local repository into Mongo? Or clone a repository locally that is already stored in Mongo?

@timothyklim
Copy link
Author

timothyklim commented Feb 23, 2012 via email

@kevinsawicki
Copy link

There currently isn't a way to do this besides hosting the repository backed by mongo using the JGit http server and cloning from there. You would just need to the JGit HTTP server to use the Mongo repository.

@timothyklim
Copy link
Author

OK, I will learn code deeper for cloning without http server(jgit servlet).
Thanks for help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment