I am trying to use JGit to automate some processes. I am able to successfully clone repo and modify files from local repo but was wondering if this would be possible if we could utilize the InMemoryRepository and load the files into memory, change the file required, and push the change to remote repo, then do away with the repo in memory.
I am able to get the file correctly from memoryRepository but am unsure how I can actually change the file and commit the branch with the change to the remote repo.
DfsRepositoryDescription repoDesc = new DfsRepositoryDescription();
InMemoryRepository repo = new InMemoryRepository(repoDesc);
Git git = new Git(repo);
git.fetch()
.setRemote("url")
.setRefSpecs("+refs/heads/*:refs/heads/*").setCredentialsProvider(credentialsProvider) // Set permission
// validation
.call();
repo.getObjectDatabase();
ObjectId lastCommitId = repo.resolve("refs/heads/master");
RevWalk revWalk = new RevWalk(repo);
RevCommit commit = revWalk.parseCommit(lastCommitId);
RevTree tree = commit.getTree();
TreeWalk treeWalk = new TreeWalk(repo);
treeWalk.addTree(tree);
treeWalk.setRecursive(true);
treeWalk.setFilter(PathFilter.create("pom.xml"));
if (!treeWalk.next()) {
return null;
}
ObjectId objectId = treeWalk.getObjectId(0);
ObjectLoader loader = repo.open(objectId);
InputStream streamm = loader.openStream();
From the inputStream I able to create xml object and modify it appropriately but not sure how I can create the xml file and commit it like we would do it if we were modifying it locally where we could create local branch and commit everything locally before pushing to remote repo.
Any help or tips would be appreciate, thanks!