Home » Java » java – Adding submodule to non empty path with JGit-Exceptionshub

java – Adding submodule to non empty path with JGit-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am trying to add a submodule to a non empty folder using the JGit API class SubmoduleAddCommand and this is my code:

SubmoduleAddCommand submoduleAddCommand = new SubmoduleAddCommand(repository);
submoduleAddCommand.setPath(path);
submoduleAddCommand.setName(name);
submoduleAddCommand.setURI(uri);
submoduleAddCommand.call().close();
repository.close();

Destination folder contains: Folder1, Folder2, … and so on, and none of the previous folders is a submodule however they must not be removed. My target is to add the submodule and clone it to the Destination folder alongside the non-submodule folders. This results in an exception being thrown:

org.eclipse.jgit.api.errors.JGitInternalException: Destination path “Target_Submodule” already exists and is not an empty directory

and the submodule is neither added to the .gitmodules file nor cloned into the Destination folder.

Is there a way to add this submodule to the non-empty Destination folder and the .gitmodules file?

How to&Answers:

Git does not allow to add a submodule into a non-empty directory.

The SubmoduleAddCommand will clone and checkout the specified repository into the destination path. And cloning a repository requires the destination path to be empty.

Cloning into a non-empty directory could lead to all sorts of trouble if there exist files in the destination directory that also need to be created while adding the module.