As I mentioned in my previous blog ‘JGit by example’ , we use JGit a lot in our application, Nuxeo Studio. A fundamental part of Nuxeo Studio development is the testing of the versioning feature. While testing this, we faced a challenging question: How could we test the application using a mock remote Git repository in our unit tests? We wanted something easy to implement that could provide the same features as a proper Git server.

The solution we chose was to use a local bare repository to represent the distant repository and to clone it on a local working directory.

So, in this blog, I will explain how to create and work with bare repository using JGit.

Create a Bare Repository Using JGit

The creation of a bare repository is similar to the creation of a working directory. It is the same method Repository.create but with the bare parameter set to true.

// Create a folder in the temp folder that will act as the remote repository File remoteDir = File.createTempFile("remote", ""); remoteDir.delete(); remoteDir.mkdirs();

// Create a bare repository FileKey fileKey = FileKey.exact(remoteDir, FS.DETECTED); Repository remoteRepo = fileKey.open(false); remoteRepo.create(true);

Start to Use the Bare Repository

Now that we have created the bare repository, we can clone it to start using it in our tests. Let’s start with the creation of a new file and a first commit.

// Clone the bare repository File cloneDir = File.createTempFile("clone", ""); cloneDir.delete(); cloneDir.mkdirs(); Git git = Git.cloneRepository().setURI(remoteRepo.getDirectory().getAbsolutePath()).setDirectory(cloneDir).call();

// Let's to our first commit // Create a new file File newFile = new File(cloneDir, "myNewFile"); newFile.createNewFile(); FileUtils.write(newFile, "Test content file"); // Commit the new file git.add().addFilepattern(newFile.getName()).call(); git.commit().setMessage("First commit").setAuthor("gildas", "[email protected] ").call();

If you want more details on the JGit commands for the commit, I would recommend reading the blog ‘JGit by example’ . To fully test the bare repository, we will now push the commit on the bare repository and create a second working directory to pull the modifications.

// Push the commit on the bare repository RefSpec refSpec = new RefSpec("master"); git.push().setRemote("origin").setRefSpecs(refSpec).call();

// Create a second working directory File cloneDir2 = File.createTempFile("clone", ""); cloneDir2.delete(); cloneDir2.mkdirs(); Git git2 = Git.cloneRepository().setURI(remoteRepo.getDirectory().getAbsolutePath()).setDirectory(cloneDir2).call(); 

After cloning the bare repository, the commit that was done there is now present in the new working directory and our purpose is achieved! We now have a basic setup for testing the sharing features of Git without having to deploy a complete Git server.

The complete test class is available here. Check it out!

For information about the Nuxeo Platform, please visit our product page or request a custom demo to see how we can help your organization.