fix<*>: fix crash while creating branch with empty repository

This commit is contained in:
leo 2021-05-27 09:14:34 +08:00
parent 284e8077d6
commit f4237efaa2
4 changed files with 43 additions and 36 deletions

View file

@ -20,8 +20,10 @@ namespace SourceGit.Commands {
return Exec(); return Exec();
} }
public bool Branch(string branch, string basedOn) { public bool Branch(string branch, string basedOn, Action<string> onProgress) {
Args = $"checkout -b {branch} {basedOn}"; Args = $"checkout --progress -b {branch} {basedOn}";
TraitErrorAsOutput = true;
handler = onProgress;
return Exec(); return Exec();
} }

View file

@ -13,10 +13,10 @@ namespace SourceGit.Commands {
var current = branches.Find(x => x.IsCurrent); var current = branches.Find(x => x.IsCurrent);
var masterBranch = branches.Find(x => x.Name == master); 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); 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); var cmd = new Config(Cwd);
cmd.Set("gitflow.branch.master", master); cmd.Set("gitflow.branch.master", master);

View file

@ -61,20 +61,21 @@ namespace SourceGit.Views.Popups {
return Task.Run(() => { return Task.Run(() => {
Models.Watcher.SetEnabled(repo, false); Models.Watcher.SetEnabled(repo, false);
if (checkout) { if (checkout) {
if (AutoStash) {
var changes = new Commands.LocalChanges(repo).Result(); var changes = new Commands.LocalChanges(repo).Result();
if (changes.Count > 0) { if (changes.Count > 0) {
if (AutoStash) {
if (!new Commands.Stash(repo).Push(changes, "NEWBRANCH_AUTO_STASH")) { if (!new Commands.Stash(repo).Push(changes, "NEWBRANCH_AUTO_STASH")) {
return false; return false;
} }
} else {
AutoStash = true;
}
} else { } else {
new Commands.Discard(repo).Whole(); new Commands.Discard(repo).Whole();
} }
} else {
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}"); if (AutoStash) new Commands.Stash(repo).Pop("stash@{0}");
} else { } else {
new Commands.Branch(repo, BranchName).Create(basedOn); new Commands.Branch(repo, BranchName).Create(basedOn);

View file

@ -400,7 +400,8 @@ namespace SourceGit.Views.Widgets {
} }
private void OpenNewBranch(object sender, RoutedEventArgs e) { 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; e.Handled = true;
} }
@ -684,7 +685,10 @@ namespace SourceGit.Views.Widgets {
new Popups.CreateBranch(repo, branch).Show(); new Popups.CreateBranch(repo, branch).Show();
e.Handled = true; e.Handled = true;
}; };
menu.Items.Add(checkout);
menu.Items.Add(new Separator());
if (current != null) {
var pull = new MenuItem(); var pull = new MenuItem();
pull.Header = App.Text("BranchCM.PullInto", branch.Name, current.Name); pull.Header = App.Text("BranchCM.PullInto", branch.Name, current.Name);
pull.Click += (o, e) => { pull.Click += (o, e) => {
@ -706,6 +710,12 @@ namespace SourceGit.Views.Widgets {
e.Handled = true; e.Handled = true;
}; };
menu.Items.Add(pull);
menu.Items.Add(merge);
menu.Items.Add(rebase);
menu.Items.Add(new Separator());
}
var delete = new MenuItem(); var delete = new MenuItem();
delete.Header = App.Text("BranchCM.Delete", branch.Name); delete.Header = App.Text("BranchCM.Delete", branch.Name);
delete.Click += (o, e) => { delete.Click += (o, e) => {
@ -734,12 +744,6 @@ namespace SourceGit.Views.Widgets {
e.Handled = true; 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(delete);
menu.Items.Add(new Separator()); menu.Items.Add(new Separator());
menu.Items.Add(createBranch); menu.Items.Add(createBranch);