Skip to content

Commit 438aa76

Browse files
committed
feature: support to use relative path as submodule's url when adding new submodule (#1339)
Signed-off-by: leo <[email protected]>
1 parent ece51fb commit 438aa76

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

src/Commands/Submodule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public Submodule(string repo)
1313

1414
public bool Add(string url, string relativePath, bool recursive)
1515
{
16-
Args = $"submodule add {url} \"{relativePath}\"";
16+
Args = $"-c protocol.file.allow=always submodule add \"{url}\" \"{relativePath}\"";
1717
if (!Exec())
1818
return false;
1919

src/Models/Remote.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ public static bool IsValidURL(string url)
5050
return true;
5151
}
5252

53-
return url.EndsWith(".git", StringComparison.Ordinal) && Directory.Exists(url);
53+
var localPath = url;
54+
if (url.StartsWith("file://", StringComparison.Ordinal))
55+
localPath = url.Substring(7);
56+
57+
return Directory.Exists(localPath);
5458
}
5559

5660
public bool TryGetVisitURL(out string url)

src/ViewModels/AddSubmodule.cs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
23
using System.IO;
34
using System.Threading.Tasks;
45

@@ -35,24 +36,33 @@ public AddSubmodule(Repository repo)
3536

3637
public static ValidationResult ValidateURL(string url, ValidationContext ctx)
3738
{
38-
if (!Models.Remote.IsValidURL(url))
39-
return new ValidationResult("Invalid repository URL format");
40-
return ValidationResult.Success;
39+
if (ctx.ObjectInstance is AddSubmodule)
40+
{
41+
if (!Models.Remote.IsValidURL(url) &&
42+
!url.StartsWith("./", StringComparison.Ordinal) &&
43+
!url.StartsWith("../", StringComparison.Ordinal))
44+
return new ValidationResult("Invalid repository URL format");
45+
46+
return ValidationResult.Success;
47+
}
48+
49+
return new ValidationResult("Missing validation context");
4150
}
4251

4352
public static ValidationResult ValidateRelativePath(string path, ValidationContext ctx)
4453
{
45-
if (Path.Exists(path))
54+
if (ctx.ObjectInstance is AddSubmodule asm)
4655
{
47-
return new ValidationResult("Give path is exists already!");
48-
}
56+
if (!path.StartsWith("./", StringComparison.Ordinal))
57+
return new ValidationResult("Path must be relative to this repository!");
58+
59+
if (Path.Exists(Path.GetFullPath(path, asm._repo.FullPath)))
60+
return new ValidationResult("Give path is exists already!");
4961

50-
if (Path.IsPathRooted(path))
51-
{
52-
return new ValidationResult("Path must be relative to this repository!");
62+
return ValidationResult.Success;
5363
}
54-
55-
return ValidationResult.Success;
64+
65+
return new ValidationResult("Missing validation context");
5666
}
5767

5868
public override Task<bool> Sure()

0 commit comments

Comments
 (0)