feature: branch name allows spaces and auto replace spaces with dashes (#917)

This commit is contained in:
leo 2025-01-24 12:10:56 +08:00
parent ab080b53b1
commit 37c4545875
No known key found for this signature in database
6 changed files with 35 additions and 10 deletions

View file

@ -6,7 +6,7 @@ namespace SourceGit.ViewModels
public class CreateBranch : Popup
{
[Required(ErrorMessage = "Branch name is required!")]
[RegularExpression(@"^[\w\-/\.#]+$", ErrorMessage = "Bad branch name format!")]
[RegularExpression(@"^[\w \-/\.#]+$", ErrorMessage = "Bad branch name format!")]
[CustomValidation(typeof(CreateBranch), nameof(ValidateBranchName))]
public string Name
{
@ -74,9 +74,10 @@ namespace SourceGit.ViewModels
if (creator == null)
return new ValidationResult("Missing runtime context to create branch!");
var fixedName = creator.FixName(name);
foreach (var b in creator._repo.Branches)
{
if (b.FriendlyName == name)
if (b.FriendlyName == fixedName)
return new ValidationResult("A branch with same name already exists!");
}
@ -86,6 +87,8 @@ namespace SourceGit.ViewModels
public override Task<bool> Sure()
{
_repo.SetWatcherEnabled(false);
var fixedName = FixName(_name);
return Task.Run(() =>
{
var succ = false;
@ -114,8 +117,8 @@ namespace SourceGit.ViewModels
}
}
SetProgressDescription($"Create new branch '{_name}'");
succ = new Commands.Checkout(_repo.FullPath).Branch(_name, _baseOnRevision, SetProgressDescription);
SetProgressDescription($"Create new branch '{fixedName}'");
succ = new Commands.Checkout(_repo.FullPath).Branch(fixedName, _baseOnRevision, SetProgressDescription);
if (needPopStash)
{
@ -125,15 +128,15 @@ namespace SourceGit.ViewModels
}
else
{
SetProgressDescription($"Create new branch '{_name}'");
succ = Commands.Branch.Create(_repo.FullPath, _name, _baseOnRevision);
SetProgressDescription($"Create new branch '{fixedName}'");
succ = Commands.Branch.Create(_repo.FullPath, fixedName, _baseOnRevision);
}
CallUIThread(() =>
{
if (succ && CheckoutAfterCreated)
{
var fake = new Models.Branch() { IsLocal = true, FullName = $"refs/heads/{_name}" };
var fake = new Models.Branch() { IsLocal = true, FullName = $"refs/heads/{fixedName}" };
if (BasedOn is Models.Branch based && !based.IsLocal)
fake.Upstream = based.FullName;
@ -153,6 +156,15 @@ namespace SourceGit.ViewModels
});
}
private string FixName(string name)
{
if (!name.Contains(' '))
return name;
var parts = name.Split(' ', System.StringSplitOptions.RemoveEmptyEntries);
return string.Join("-", parts);
}
private readonly Repository _repo = null;
private string _name = null;
private readonly string _baseOnRevision = null;