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!")]
|
2024-07-21 02:43:23 -07:00
|
|
|
|
[RegularExpression(@"^[\w\-/\.#]+$", ErrorMessage = "Bad branch name format!")]
|
2024-02-05 23:08:37 -08:00
|
|
|
|
[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;
|
|
|
|
|
}
|
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-07-02 23:41:43 -07:00
|
|
|
|
get => _repo.Settings.DealWithLocalChangesOnCreateBranch;
|
|
|
|
|
set => _repo.Settings.DealWithLocalChangesOnCreateBranch = value;
|
2024-05-25 04:40:30 -07:00
|
|
|
|
}
|
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-07-02 23:41:43 -07:00
|
|
|
|
get => _repo.Settings.CheckoutBranchOnCreateBranch;
|
|
|
|
|
set => _repo.Settings.CheckoutBranchOnCreateBranch = value;
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
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-08-09 01:06:28 -07:00
|
|
|
|
_baseOnRevision = branch.IsDetachedHead ? branch.Head : 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-08-07 00:52:58 -07:00
|
|
|
|
var changes = new Commands.CountLocalChangesWithoutUntracked(_repo.FullPath).Result();
|
2024-08-07 00:38:18 -07:00
|
|
|
|
var needPopStash = false;
|
2024-08-07 00:52:58 -07:00
|
|
|
|
if (changes > 0)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-07-02 23:41:43 -07:00
|
|
|
|
if (PreAction == Models.DealWithLocalChanges.StashAndReaply)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-08-07 00:20:21 -07:00
|
|
|
|
SetProgressDescription("Stash local changes");
|
|
|
|
|
var 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-07-02 23:41:43 -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...");
|
2024-10-02 06:17:44 -07:00
|
|
|
|
Commands.Discard.All(_repo.FullPath, false);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-27 02:27:24 -07:00
|
|
|
|
CallUIThread(() =>
|
|
|
|
|
{
|
2024-09-26 05:37:48 -07:00
|
|
|
|
if (CheckoutAfterCreated)
|
|
|
|
|
{
|
|
|
|
|
_repo.AutoAddBranchFilterPostCheckout(new Models.Branch()
|
|
|
|
|
{
|
|
|
|
|
FullName = $"refs/heads/{_name}",
|
|
|
|
|
Upstream = BasedOn is Models.Branch { IsLocal: false } remoteBranch ? remoteBranch.FullName : string.Empty,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-27 02:27:24 -07:00
|
|
|
|
_repo.MarkBranchesDirtyManually();
|
|
|
|
|
_repo.SetWatcherEnabled(true);
|
|
|
|
|
});
|
2024-02-05 23:08:37 -08:00
|
|
|
|
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-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|