Throw exception when clone repository name is blank
Prevents corrupting the shared drive by writing directly into the root worktree or gitdir paths when the name cannot be derived. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -36,6 +36,8 @@ public class GitService
|
|||||||
{
|
{
|
||||||
if (name == null || name.isBlank())
|
if (name == null || name.isBlank())
|
||||||
name = deriveRepositoryName(url);
|
name = deriveRepositoryName(url);
|
||||||
|
if (name.isBlank())
|
||||||
|
throw new IllegalArgumentException("Repository name must not be blank");
|
||||||
Path worktree = properties.getWorktreePath().resolve(name);
|
Path worktree = properties.getWorktreePath().resolve(name);
|
||||||
Path gitDir = properties.getGitDirPath().resolve(name);
|
Path gitDir = properties.getGitDirPath().resolve(name);
|
||||||
|
|
||||||
|
|||||||
@@ -299,4 +299,16 @@ class GitServiceTest
|
|||||||
String expectedName = gitService.deriveRepositoryName(bareRemote.toUri().toString());
|
String expectedName = gitService.deriveRepositoryName(bareRemote.toUri().toString());
|
||||||
assertTrue(Files.exists(worktreePath.resolve(expectedName)));
|
assertTrue(Files.exists(worktreePath.resolve(expectedName)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void cloneWithBlankUrlAndBlankNameThrows()
|
||||||
|
{
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> gitService.cloneRepository("", ""));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void cloneWithUrlDerivingBlankNameThrows()
|
||||||
|
{
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> gitService.cloneRepository(".git", null));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user