Versioning and File Management in Java Application

Shameer Ahmed
Geek Culture
Published in
3 min readJun 16, 2021

Managing different file versions of a file can be a hassle. It can become a storage hog and can also make file management difficult. To solve such a problem, a version control system is used. It is a way to track changes in the file and make versions out of it, allowing us to revert to a specific version as needed. One such version tool is git, which can be used to manage file versions in folders. Here in this article, you will learn how to integrate git in your software to manage versioning from within your application in java.

Before diving deep into the technicalities, you need to know a few basic things about git. In git, a repository is a folder in which the file versions are managed. Every version is saved in git through a commit. Also, a completely different version line can be created which we can call a branch. The branches can be created from any commit or any other branch. To understand more about how git works you can check it out here.

In this tutorial, we will be using the Java interface of git called JGit, an eclipse-backed community.

JGit has two basic levels of API: plumbing and porcelain. The terminology for these comes from Git itself. JGit is divided into the same areas:

  • porcelain APIs — front-end for common user-level actions (similar to Git command-line tool)
  • plumbing APIs — direct access low-level repository objects

The following code can be used to create a repository using JGit

public Repository createNewRepository(String dirPath) {
// prepare a new folder
Path path = Paths.get("path/to/folder");
try {
if (!Files.exists(root)) {
Files.createDirectory(root);
}
if (!Files.exists(path)) {
Files.createDirectory(path);
}
// create the directory
Repository repository = FileRepositoryBuilder.create
(new File(path.toFile(), ".git"));
try {
repository.create();
} catch (IllegalStateException repositoryExists) {
repositoryExists.printStackTrace();
}
System.out.println(repository.getDirectory());
return repository;

} catch (IOException e) {
throw new RuntimeException("Could not initialize folder");
}

}

To fetch the Repository following code can be used:

public Repository getRepository(String dirPath) throws IOException {
Path path = Paths.get(dirPath);
Repository repository = Git.open(new File(path.toFile(),".git"))
.getRepository();
return repository;
}

The following code can be used to add files in the staging

public Message addOutputFiles(String fileName) {
String dir = "path/to/directory";

String branchName = "branchName";

try {
Repository repository = getRepository
(root.toString() + File.separator + dir);
try (Git git = new Git(repository)) {

// checkout of branch if needed
if(!git.getRepository().getBranch().equalsIgnoreCase(branchName)) {
git.checkout().setName(branchName).call();
}
// add file in staging
git.add()
.addFilepattern(approval)
.call();
} catch (Exception e) {
e.printStackTrace();
}

} catch (Exception e) {
throw new RuntimeException("Could not store the file. Error: " );
}


}

Note JGit till now does not support regex patterns or “ * ” in adding a file to staging like an option that git CLI provides.

To commit the staged files following code can be utilized:

public Message commitModelRepository( String fileName) 
throws IOException {
String dir = "path/to/directory";
try {
Repository repository = getRepository("path/to/repo");
try (Git git = new Git(repository)) {

// checkout branch if needed
if(!git.getRepository().getBranch().equalsIgnoreCase("master"))
{
git.checkout().setName("master").call();
}

// and then commit the changes
String commit = git.commit()
.setMessage("Added " + fileName)
.call().getName();
} catch (Exception e) {
e.printStackTrace();
}

} catch (Exception e) {
throw new RuntimeException("Could not store the file. Error: ");
}

}

This tutorial is just the tip of the iceberg. There can be quite a lot of use cases where we can utilize JGit for file versioning in any other type of application. It allows managing versions just like git which allows developers to easily develop versioning-based features utilizing Jgit. If you like to further drill down, details on JGit can be found at the following link and this link. To find an example on Jgit examples you can check out this link

--

--

Shameer Ahmed
Geek Culture

a Tech savvy with a curious mind, having curiosity in entrepreneurship, business, and personal development. https://shameerahmad.me/