using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; namespace SourceGit.UI { /// /// Dialog to initialize git flow. /// public partial class GitFlowSetup : UserControl { private Git.Repository repo = null; private Regex regex = new Regex(@"^[\w\-/\.]+$"); /// /// Constructor. /// /// public GitFlowSetup(Git.Repository opened) { repo = opened; InitializeComponent(); } /// /// Open this dialog. /// /// public static void Show(Git.Repository repo) { var popup = App.Launcher.GetPopupManager(repo); popup?.Show(new GitFlowSetup(repo)); } /// /// Start to initialize git-flow. /// /// /// private async void Sure(object sender, RoutedEventArgs e) { var popup = App.Launcher.GetPopupManager(repo); popup?.Lock(); var master = txtMaster.Text; var dev = txtDevelop.Text; var feature = txtFeature.Text; var release = txtRelease.Text; var hotfix = txtHotfix.Text; var version = txtVersion.Text; await Task.Run(() => repo.EnableGitFlow(master, dev, feature, release, hotfix, version)); popup?.Close(true); } /// /// Cancel. /// /// /// private void Cancel(object sender, RoutedEventArgs e) { App.Launcher.GetPopupManager(repo)?.Close(); } /// /// Validate input names. /// /// /// private void ValidateNames(object sender, TextChangedEventArgs e) { if (!IsLoaded) return; var master = txtMaster.Text; var dev = txtDevelop.Text; var feature = txtFeature.Text; var release = txtRelease.Text; var hotfix = txtHotfix.Text; if (!ValidateBranch("Production", master)) return; if (!ValidateBranch("Development", dev)) return; if (dev == master) { txtValidation.Content = "Development branch is same with production!"; btnSure.IsEnabled = false; return; } if (!ValidatePrefix("Feature", feature)) return; if (!ValidatePrefix("Release", release)) return; if (!ValidatePrefix("Hotfix", hotfix)) return; txtValidation.Content = ""; btnSure.IsEnabled = true; } private bool ValidateBranch(string type, string name) { if (string.IsNullOrEmpty(name)) { txtValidation.Content = $"{type} branch name can't be empty"; btnSure.IsEnabled = false; return false; } if (!regex.IsMatch(name)) { txtValidation.Content = $"{type} branch name contains invalid characters."; btnSure.IsEnabled = false; return false; } return true; } private bool ValidatePrefix(string type, string prefix) { if (string.IsNullOrEmpty(prefix)) { txtValidation.Content = $"{type} prefix is required!"; btnSure.IsEnabled = false; return false; } if (!regex.IsMatch(prefix)) { txtValidation.Content = $"{type} prefix contains invalid characters."; btnSure.IsEnabled = false; return false; } return true; } } }