Repository: jhipster/jhipster-lite
Pull request #10388 (2024-07-25 08:25:03)

fix(git): allow committing when project folder is a subfolder of a git repository
Following #10369


Patch:
diff --git a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/git/GitCommitException.java b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/git/GitCommitException.java
index 19208de4b..82cc5d21d 100644
--- a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/git/GitCommitException.java
+++ b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/git/GitCommitException.java
@@ -7,4 +7,8 @@ class GitCommitException extends GeneratorException {
   public GitCommitException(String message, Throwable cause) {
     super(internalServerError(GitErrorKey.COMMIT_ERROR).message(message).cause(cause));
   }
+
+  public GitCommitException(String message) {
+    super(internalServerError(GitErrorKey.COMMIT_ERROR).message(message));
+  }
 }
diff --git a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/git/JGitGitRepository.java b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/git/JGitGitRepository.java
index 8610b2e6a..fd974236b 100644
--- a/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/git/JGitGitRepository.java
+++ b/src/main/java/tech/jhipster/lite/module/infrastructure/secondary/git/JGitGitRepository.java
@@ -2,6 +2,7 @@ package tech.jhipster.lite.module.infrastructure.secondary.git;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Optional;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.api.errors.JGitInternalException;
@@ -13,6 +14,7 @@ import tech.jhipster.lite.module.domain.git.GitCommitMessage;
 import tech.jhipster.lite.module.domain.git.GitRepository;
 import tech.jhipster.lite.module.domain.properties.JHipsterProjectFolder;
 import tech.jhipster.lite.shared.error.domain.Assert;
+import tech.jhipster.lite.shared.generation.domain.ExcludeFromGeneratedCodeCoverage;
 
 @Repository
 class JGitGitRepository implements GitRepository {
@@ -37,20 +39,23 @@ class JGitGitRepository implements GitRepository {
   }
 
   private boolean isGit(JHipsterProjectFolder folder) {
-    FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
-    repositoryBuilder.findGitDir(folderFile(folder));
+    return findGitDirectory(folder).isPresent();
+  }
 
-    return repositoryBuilder.getGitDir() != null;
+  private Optional<File> findGitDirectory(JHipsterProjectFolder folder) {
+    FileRepositoryBuilder repositoryBuilder = new FileRepositoryBuilder();
+    return Optional.ofNullable(repositoryBuilder.findGitDir(folderFile(folder)).getGitDir());
   }
 
   @Override
+  @ExcludeFromGeneratedCodeCoverage(reason = "IOException are hard to test")
   public void commitAll(JHipsterProjectFolder folder, GitCommitMessage message) {
     Assert.notNull("folder", folder);
     Assert.notNull("message", message);
 
-    File folderFile = folderFile(folder);
+    File gitRepository = findGitDirectory(folder).orElseThrow(() -> new GitCommitException("Can't commit in non existing git repository"));
 
-    try (Git gitFolder = Git.open(folderFile)) {
+    try (Git gitFolder = Git.open(gitRepository)) {
       gitFolder.add().setUpdate(true).addFilepattern(".").call(); // stage modified and deleted
       gitFolder.add().addFilepattern(".").call(); // stage modified and new
 


Test patch:
diff --git a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/git/JGitGitRepositoryTest.java b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/git/JGitGitRepositoryTest.java
index 02ff14705..38517af45 100644
--- a/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/git/JGitGitRepositoryTest.java
+++ b/src/test/java/tech/jhipster/lite/module/infrastructure/secondary/git/JGitGitRepositoryTest.java
@@ -5,7 +5,9 @@ import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import ch.qos.logback.classic.Level;
 import java.io.IOException;
-import java.nio.file.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.nio.file.attribute.PosixFilePermission;
 import java.util.Set;
 import org.junit.jupiter.api.Nested;
@@ -93,6 +95,19 @@ class JGitGitRepositoryTest {
       assertThat(GitTestUtil.getCommits(path)).contains("Add dummy.txt");
       assertThat(GitTestUtil.getCurrentBranch(path)).contains("main");
     }
+
+    @Test
+    void shouldCommitAllFilesInSubfolderOfAlreadyInitializedGitRepository() throws IOException {
+      Path gitDirectory = gitInit();
+      Path subFolder = gitDirectory.resolve("subFolder");
+      Files.createDirectories(subFolder);
+      Files.copy(Paths.get("src/test/resources/projects/files/dummy.txt"), subFolder.resolve("dummy.txt"));
+
+      git.commitAll(new JHipsterProjectFolder(subFolder.toString()), "Add dummy.txt");
+
+      assertThat(GitTestUtil.getCommits(subFolder)).contains("Add dummy.txt");
+      assertThat(GitTestUtil.getCurrentBranch(subFolder)).contains("main");
+    }
   }
 
   private Path gitInit() throws IOException {