feature: support to use relative path as submodule's url when adding new submodule (#1339)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-05-21 00:10:10 +08:00
parent ece51fbd32
commit 438aa76695
No known key found for this signature in database
3 changed files with 28 additions and 14 deletions

View file

@ -13,7 +13,7 @@ namespace SourceGit.Commands
public bool Add(string url, string relativePath, bool recursive)
{
Args = $"submodule add {url} \"{relativePath}\"";
Args = $"-c protocol.file.allow=always submodule add \"{url}\" \"{relativePath}\"";
if (!Exec())
return false;

View file

@ -50,7 +50,11 @@ namespace SourceGit.Models
return true;
}
return url.EndsWith(".git", StringComparison.Ordinal) && Directory.Exists(url);
var localPath = url;
if (url.StartsWith("file://", StringComparison.Ordinal))
localPath = url.Substring(7);
return Directory.Exists(localPath);
}
public bool TryGetVisitURL(out string url)

View file

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Threading.Tasks;
@ -35,24 +36,33 @@ namespace SourceGit.ViewModels
public static ValidationResult ValidateURL(string url, ValidationContext ctx)
{
if (!Models.Remote.IsValidURL(url))
return new ValidationResult("Invalid repository URL format");
return ValidationResult.Success;
if (ctx.ObjectInstance is AddSubmodule)
{
if (!Models.Remote.IsValidURL(url) &&
!url.StartsWith("./", StringComparison.Ordinal) &&
!url.StartsWith("../", StringComparison.Ordinal))
return new ValidationResult("Invalid repository URL format");
return ValidationResult.Success;
}
return new ValidationResult("Missing validation context");
}
public static ValidationResult ValidateRelativePath(string path, ValidationContext ctx)
{
if (Path.Exists(path))
if (ctx.ObjectInstance is AddSubmodule asm)
{
return new ValidationResult("Give path is exists already!");
}
if (!path.StartsWith("./", StringComparison.Ordinal))
return new ValidationResult("Path must be relative to this repository!");
if (Path.Exists(Path.GetFullPath(path, asm._repo.FullPath)))
return new ValidationResult("Give path is exists already!");
if (Path.IsPathRooted(path))
{
return new ValidationResult("Path must be relative to this repository!");
return ValidationResult.Success;
}
return ValidationResult.Success;
return new ValidationResult("Missing validation context");
}
public override Task<bool> Sure()