Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit 0a62910

Browse files
committed
Detect .git as project root
- Add .git as file or dir as root detection - Fixes #190
1 parent 5b3c051 commit 0a62910

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

src/main/java/org/springframework/cli/util/IoUtils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,15 @@ public static Path getWorkingDirectory() {
5555
}
5656

5757
public static boolean inProjectRootDirectory(Path path) {
58+
Path dotGit = path.resolve(".git");
59+
if (Files.exists(dotGit)) {
60+
return true;
61+
}
5862
Path pomFile = path.resolve("pom.xml");
59-
return Files.exists(pomFile);
63+
if (Files.exists(pomFile)) {
64+
return true;
65+
}
66+
return false;
6067
}
6168

6269
public static Path getProjectPath(String path) {
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cli.util;
18+
19+
import java.io.IOException;
20+
import java.nio.file.FileSystem;
21+
import java.nio.file.Files;
22+
import java.nio.file.Path;
23+
24+
import com.google.common.jimfs.Jimfs;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Test;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
public class IoUtilsTests {
31+
32+
private FileSystem fileSystem;
33+
34+
@BeforeEach
35+
public void setupTests() {
36+
fileSystem = Jimfs.newFileSystem();
37+
}
38+
39+
@Test
40+
public void inProjectRootDirectoryJustPom() throws IOException {
41+
Path root = fileSystem.getPath("");
42+
Path pom = root.resolve("pom.xml");
43+
Files.createFile(pom);
44+
boolean inProjectRootDirectory = IoUtils.inProjectRootDirectory(root);
45+
assertThat(inProjectRootDirectory).isTrue();
46+
}
47+
48+
@Test
49+
public void testInProjectRootDirectoryDotgitfile() throws IOException {
50+
Path root = fileSystem.getPath("");
51+
Path dotgit = root.resolve(".git");
52+
Files.createFile(dotgit);
53+
boolean inProjectRootDirectory = IoUtils.inProjectRootDirectory(root);
54+
assertThat(inProjectRootDirectory).isTrue();
55+
}
56+
57+
@Test
58+
public void testInProjectRootDirectoryDotgitdir() throws IOException {
59+
Path root = fileSystem.getPath("");
60+
Path dotgit = root.resolve(".git");
61+
Files.createDirectories(dotgit);
62+
boolean inProjectRootDirectory = IoUtils.inProjectRootDirectory(root);
63+
assertThat(inProjectRootDirectory).isTrue();
64+
}
65+
66+
@Test
67+
public void testInProjectRootDirectoryPomAndDotgitfile() throws IOException {
68+
Path root = fileSystem.getPath("");
69+
Path pom = root.resolve("pom.xml");
70+
Path dotgit = root.resolve(".git");
71+
Files.createFile(pom);
72+
Files.createFile(dotgit);
73+
boolean inProjectRootDirectory = IoUtils.inProjectRootDirectory(root);
74+
assertThat(inProjectRootDirectory).isTrue();
75+
}
76+
77+
}

0 commit comments

Comments
 (0)