2024-02-05 23:08:37 -08:00
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.ViewModels
|
2024-06-06 00:31:02 -07:00
|
|
|
|
{
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public class CreateBranch : Popup
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
[Required(ErrorMessage = "Branch name is required!")]
|
|
|
|
|
[RegularExpression(@"^[\w\-/\.]+$", ErrorMessage = "Bad branch name format!")]
|
|
|
|
|
[CustomValidation(typeof(CreateBranch), nameof(ValidateBranchName))]
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string Name
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get => _name;
|
|
|
|
|
set => SetProperty(ref _name, value, true);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public object BasedOn
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
}
|
2024-06-06 00:31:02 -07:00
|
|
|
|
|
2024-05-29 01:42:47 -07:00
|
|
|
|
public Models.DealWithLocalChanges PreAction
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-05-25 04:40:30 -07:00
|
|
|
|
get => _preAction;
|
|
|
|
|
set => SetProperty(ref _preAction, value);
|
|
|
|
|
}
|
2024-06-06 00:31:02 -07:00
|
|
|
|
|
2024-05-25 04:40:30 -07:00
|
|
|
|
public bool CheckoutAfterCreated
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
} = true;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public CreateBranch(Repository repo, Models.Branch branch)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
2024-03-07 17:29:54 -08:00
|
|
|
|
_baseOnRevision = branch.FullName;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (!branch.IsLocal && repo.Branches.Find(x => x.IsLocal && x.Name == branch.Name) == null)
|
|
|
|
|
{
|
2024-03-08 02:01:29 -08:00
|
|
|
|
Name = branch.Name;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
BasedOn = branch;
|
|
|
|
|
View = new Views.CreateBranch() { DataContext = this };
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public CreateBranch(Repository repo, Models.Commit commit)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
|
|
|
|
_baseOnRevision = commit.SHA;
|
|
|
|
|
|
|
|
|
|
BasedOn = commit;
|
|
|
|
|
View = new Views.CreateBranch() { DataContext = this };
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public CreateBranch(Repository repo, Models.Tag tag)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo = repo;
|
|
|
|
|
_baseOnRevision = tag.SHA;
|
|
|
|
|
|
|
|
|
|
BasedOn = tag;
|
|
|
|
|
View = new Views.CreateBranch() { DataContext = this };
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public static ValidationResult ValidateBranchName(string name, ValidationContext ctx)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var creator = ctx.ObjectInstance as CreateBranch;
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (creator == null)
|
|
|
|
|
return new ValidationResult("Missing runtime context to create branch!");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var b in creator._repo.Branches)
|
|
|
|
|
{
|
2024-07-01 19:23:21 -07:00
|
|
|
|
if (b.FriendlyName == name)
|
2024-03-31 01:54:29 -07:00
|
|
|
|
return new ValidationResult("A branch with same name already exists!");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ValidationResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public override Task<bool> Sure()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_repo.SetWatcherEnabled(false);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
return Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
if (CheckoutAfterCreated)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
bool needPopStash = false;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (_repo.WorkingCopyChangesCount > 0)
|
|
|
|
|
{
|
2024-05-29 01:42:47 -07:00
|
|
|
|
if (_preAction == Models.DealWithLocalChanges.StashAndReaply)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SetProgressDescription("Adding untracked changes...");
|
|
|
|
|
var succ = new Commands.Add(_repo.FullPath).Exec();
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (succ)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SetProgressDescription("Stash local changes");
|
|
|
|
|
succ = new Commands.Stash(_repo.FullPath).Push("CREATE_BRANCH_AUTO_STASH");
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (!succ)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
needPopStash = true;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
2024-05-29 01:42:47 -07:00
|
|
|
|
else if (_preAction == Models.DealWithLocalChanges.Discard)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SetProgressDescription("Discard local changes...");
|
|
|
|
|
Commands.Discard.All(_repo.FullPath);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetProgressDescription($"Create new branch '{_name}'");
|
|
|
|
|
new Commands.Checkout(_repo.FullPath).Branch(_name, _baseOnRevision, SetProgressDescription);
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (needPopStash)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
SetProgressDescription("Re-apply local changes...");
|
|
|
|
|
new Commands.Stash(_repo.FullPath).Pop("stash@{0}");
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-06-02 19:17:47 -07:00
|
|
|
|
SetProgressDescription($"Create new branch '{_name}'");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Commands.Branch.Create(_repo.FullPath, _name, _baseOnRevision);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CallUIThread(() => _repo.SetWatcherEnabled(true));
|
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly Repository _repo = null;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
private string _name = null;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly string _baseOnRevision = null;
|
2024-06-19 00:29:40 -07:00
|
|
|
|
private Models.DealWithLocalChanges _preAction = Models.DealWithLocalChanges.DoNothing;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|