Auto-fill repository name from URL when left blank
Derive the repository name from the clone URL by extracting the last path segment and stripping the .git suffix. Applied to the web controller, GitService, and telnet interface. The telnet prompt now shows the default name in brackets. Also fix flaky contextLoads test by using RANDOM_PORT. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -52,8 +52,10 @@ public class HomeController
|
||||
}
|
||||
|
||||
@PostMapping("/clone")
|
||||
public String cloneRepo(@RequestParam String url, @RequestParam String name) throws GitAPIException, IOException
|
||||
public String cloneRepo(@RequestParam String url, @RequestParam(required = false) String name) throws GitAPIException, IOException
|
||||
{
|
||||
if (name == null || name.isBlank())
|
||||
name = gitService.deriveRepositoryName(url);
|
||||
gitService.cloneRepository(url, name);
|
||||
return "redirect:/?repo=" + name;
|
||||
}
|
||||
|
||||
@@ -22,8 +22,20 @@ public class GitService
|
||||
{
|
||||
private final WebgitProperties properties;
|
||||
|
||||
public String deriveRepositoryName(String url)
|
||||
{
|
||||
String path = url.replaceAll("[/\\\\]+$", "");
|
||||
int lastSep = Math.max(path.lastIndexOf('/'), path.lastIndexOf('\\'));
|
||||
String base = lastSep >= 0 ? path.substring(lastSep + 1) : path;
|
||||
if (base.endsWith(".git"))
|
||||
base = base.substring(0, base.length() - 4);
|
||||
return base;
|
||||
}
|
||||
|
||||
public void cloneRepository(String url, String name) throws GitAPIException, IOException
|
||||
{
|
||||
if (name == null || name.isBlank())
|
||||
name = deriveRepositoryName(url);
|
||||
Path worktree = properties.getWorktreePath().resolve(name);
|
||||
Path gitDir = properties.getGitDirPath().resolve(name);
|
||||
|
||||
|
||||
@@ -88,11 +88,12 @@ public class TelnetSession implements Runnable
|
||||
if (url == null || url.isBlank())
|
||||
return;
|
||||
|
||||
out.print("Name: ");
|
||||
String defaultName = gitService.deriveRepositoryName(url.trim());
|
||||
out.print("Name [" + defaultName + "]: ");
|
||||
out.flush();
|
||||
String name = in.readLine();
|
||||
if (name == null || name.isBlank())
|
||||
return;
|
||||
name = defaultName;
|
||||
|
||||
gitService.cloneRepository(url.trim(), name.trim());
|
||||
out.println("Cloned successfully.");
|
||||
|
||||
Reference in New Issue
Block a user