From f4237efaa28e8cae5bfe99ed3d5a373d86fa2b88 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 27 May 2021 09:14:34 +0800 Subject: [PATCH] fix<*>: fix crash while creating branch with empty repository --- src/Commands/Checkout.cs | 6 ++- src/Commands/GitFlow.cs | 4 +- src/Views/Popups/CreateBranch.xaml.cs | 13 ++++--- src/Views/Widgets/Dashboard.xaml.cs | 56 ++++++++++++++------------- 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/src/Commands/Checkout.cs b/src/Commands/Checkout.cs index 40b2d7f5..dbab4947 100644 --- a/src/Commands/Checkout.cs +++ b/src/Commands/Checkout.cs @@ -20,8 +20,10 @@ namespace SourceGit.Commands { return Exec(); } - public bool Branch(string branch, string basedOn) { - Args = $"checkout -b {branch} {basedOn}"; + public bool Branch(string branch, string basedOn, Action onProgress) { + Args = $"checkout --progress -b {branch} {basedOn}"; + TraitErrorAsOutput = true; + handler = onProgress; return Exec(); } diff --git a/src/Commands/GitFlow.cs b/src/Commands/GitFlow.cs index 7acdb011..b6707b06 100644 --- a/src/Commands/GitFlow.cs +++ b/src/Commands/GitFlow.cs @@ -13,10 +13,10 @@ namespace SourceGit.Commands { var current = branches.Find(x => x.IsCurrent); var masterBranch = branches.Find(x => x.Name == master); - if (masterBranch == null) new Branch(Cwd, master).Create(current.Head); + if (masterBranch == null && current != null) new Branch(Cwd, develop).Create(current.Head); var devBranch = branches.Find(x => x.Name == develop); - if (devBranch == null) new Branch(Cwd, develop).Create(current.Head); + if (devBranch == null && current != null) new Branch(Cwd, develop).Create(current.Head); var cmd = new Config(Cwd); cmd.Set("gitflow.branch.master", master); diff --git a/src/Views/Popups/CreateBranch.xaml.cs b/src/Views/Popups/CreateBranch.xaml.cs index cbd41881..cac6fd00 100644 --- a/src/Views/Popups/CreateBranch.xaml.cs +++ b/src/Views/Popups/CreateBranch.xaml.cs @@ -61,20 +61,21 @@ namespace SourceGit.Views.Popups { return Task.Run(() => { Models.Watcher.SetEnabled(repo, false); if (checkout) { - if (AutoStash) { - var changes = new Commands.LocalChanges(repo).Result(); - if (changes.Count > 0) { + var changes = new Commands.LocalChanges(repo).Result(); + if (changes.Count > 0) { + if (AutoStash) { if (!new Commands.Stash(repo).Push(changes, "NEWBRANCH_AUTO_STASH")) { return false; } } else { - AutoStash = true; + new Commands.Discard(repo).Whole(); } } else { - new Commands.Discard(repo).Whole(); + AutoStash = false; } - new Commands.Checkout(repo).Branch(BranchName, basedOn); + UpdateProgress($"Create new branch '{BranchName}'"); + new Commands.Checkout(repo).Branch(BranchName, basedOn, UpdateProgress); if (AutoStash) new Commands.Stash(repo).Pop("stash@{0}"); } else { new Commands.Branch(repo, BranchName).Create(basedOn); diff --git a/src/Views/Widgets/Dashboard.xaml.cs b/src/Views/Widgets/Dashboard.xaml.cs index c65ec5c5..e9950d50 100644 --- a/src/Views/Widgets/Dashboard.xaml.cs +++ b/src/Views/Widgets/Dashboard.xaml.cs @@ -400,7 +400,8 @@ namespace SourceGit.Views.Widgets { } private void OpenNewBranch(object sender, RoutedEventArgs e) { - new Popups.CreateBranch(repo, repo.Branches.Find(x => x.IsCurrent)).Show(); + var current = repo.Branches.Find(x => x.IsCurrent); + if (current != null) new Popups.CreateBranch(repo, current).Show(); e.Handled = true; } @@ -684,27 +685,36 @@ namespace SourceGit.Views.Widgets { new Popups.CreateBranch(repo, branch).Show(); e.Handled = true; }; + menu.Items.Add(checkout); + menu.Items.Add(new Separator()); - var pull = new MenuItem(); - pull.Header = App.Text("BranchCM.PullInto", branch.Name, current.Name); - pull.Click += (o, e) => { - new Popups.Pull(repo, branch).Show(); - e.Handled = true; - }; + if (current != null) { + var pull = new MenuItem(); + pull.Header = App.Text("BranchCM.PullInto", branch.Name, current.Name); + pull.Click += (o, e) => { + new Popups.Pull(repo, branch).Show(); + e.Handled = true; + }; - var merge = new MenuItem(); - merge.Header = App.Text("BranchCM.Merge", branch.Name, current.Name); - merge.Click += (o, e) => { - new Popups.Merge(repo.Path, $"{branch.Remote}/{branch.Name}", current.Name).Show(); - e.Handled = true; - }; + var merge = new MenuItem(); + merge.Header = App.Text("BranchCM.Merge", branch.Name, current.Name); + merge.Click += (o, e) => { + new Popups.Merge(repo.Path, $"{branch.Remote}/{branch.Name}", current.Name).Show(); + e.Handled = true; + }; - var rebase = new MenuItem(); - rebase.Header = App.Text("BranchCM.Rebase", current.Name, branch.Name); - rebase.Click += (o, e) => { - new Popups.Rebase(repo.Path, current.Name, branch).Show(); - e.Handled = true; - }; + var rebase = new MenuItem(); + rebase.Header = App.Text("BranchCM.Rebase", current.Name, branch.Name); + rebase.Click += (o, e) => { + new Popups.Rebase(repo.Path, current.Name, branch).Show(); + e.Handled = true; + }; + + menu.Items.Add(pull); + menu.Items.Add(merge); + menu.Items.Add(rebase); + menu.Items.Add(new Separator()); + } var delete = new MenuItem(); delete.Header = App.Text("BranchCM.Delete", branch.Name); @@ -733,13 +743,7 @@ namespace SourceGit.Views.Widgets { Clipboard.SetText(branch.Remote + "/" + branch.Name); e.Handled = true; }; - - menu.Items.Add(checkout); - menu.Items.Add(new Separator()); - menu.Items.Add(pull); - menu.Items.Add(merge); - menu.Items.Add(rebase); - menu.Items.Add(new Separator()); + menu.Items.Add(delete); menu.Items.Add(new Separator()); menu.Items.Add(createBranch);