feature: add CurrentBranch property to ViewModels.Repository

This commit is contained in:
leo 2024-07-24 15:36:26 +08:00
parent f8caeceade
commit 6f317039ab
No known key found for this signature in database
4 changed files with 63 additions and 64 deletions

View file

@ -147,7 +147,7 @@ namespace SourceGit.ViewModels
if (datagrid.SelectedItems.Count != 1)
return null;
var current = _repo.Branches.Find(x => x.IsCurrent);
var current = _repo.CurrentBranch;
if (current == null)
return null;

View file

@ -68,7 +68,7 @@ namespace SourceGit.ViewModels
public Pull(Repository repo, Models.Branch specifiedRemoteBranch)
{
_repo = repo;
_current = repo.Branches.Find(x => x.IsCurrent);
_current = repo.CurrentBranch;
if (specifiedRemoteBranch != null)
{

View file

@ -99,6 +99,12 @@ namespace SourceGit.ViewModels
private set => SetProperty(ref _branches, value);
}
public Models.Branch CurrentBranch
{
get => _currentBranch;
private set => SetProperty(ref _currentBranch, value);
}
public List<BranchTreeNode> LocalBranchTrees
{
get => _localBranchTrees;
@ -364,7 +370,7 @@ namespace SourceGit.ViewModels
if (!PopupHost.CanCreatePopup())
return;
if (Remotes.Count == 0)
if (_remotes.Count == 0)
{
App.RaiseException(_fullpath, "No remotes added to this repository!!!");
return;
@ -378,7 +384,7 @@ namespace SourceGit.ViewModels
if (!PopupHost.CanCreatePopup())
return;
if (Remotes.Count == 0)
if (_remotes.Count == 0)
{
App.RaiseException(_fullpath, "No remotes added to this repository!!!");
return;
@ -392,13 +398,13 @@ namespace SourceGit.ViewModels
if (!PopupHost.CanCreatePopup())
return;
if (Remotes.Count == 0)
if (_remotes.Count == 0)
{
App.RaiseException(_fullpath, "No remotes added to this repository!!!");
return;
}
if (Branches.Find(x => x.IsCurrent) == null)
if (_currentBranch == null)
{
App.RaiseException(_fullpath, "Can NOT found current branch!!!");
return;
@ -478,10 +484,10 @@ namespace SourceGit.ViewModels
break;
case 2:
visible = new Commands.QueryCommits(FullPath, 1000, _searchCommitFilter, false).Result();
visible = new Commands.QueryCommits(_fullpath, 1000, _searchCommitFilter, false).Result();
break;
case 3:
visible = new Commands.QueryCommits(FullPath, 1000, _searchCommitFilter, true).Result();
visible = new Commands.QueryCommits(_fullpath, 1000, _searchCommitFilter, true).Result();
break;
}
@ -527,9 +533,8 @@ namespace SourceGit.ViewModels
public void NavigateToCurrentHead()
{
var cur = Branches.Find(x => x.IsCurrent);
if (cur != null)
NavigateToCommit(cur.Head);
if (_currentBranch != null)
NavigateToCommit(_currentBranch.Head);
}
public void UpdateFilter(string filter, bool toggle)
@ -607,22 +612,20 @@ namespace SourceGit.ViewModels
public void RefreshBranches()
{
var branches = new Commands.QueryBranches(FullPath).Result();
var remotes = new Commands.QueryRemotes(FullPath).Result();
var branches = new Commands.QueryBranches(_fullpath).Result();
var remotes = new Commands.QueryRemotes(_fullpath).Result();
var builder = BuildBranchTree(branches, remotes);
Dispatcher.UIThread.Invoke(() =>
{
Remotes = remotes;
Branches = branches;
CurrentBranch = branches.Find(x => x.IsCurrent);
LocalBranchTrees = builder.Locals;
RemoteBranchTrees = builder.Remotes;
if (_workingCopy != null)
{
var cur = Branches.Find(x => x.IsCurrent);
_workingCopy.CanCommitWithPush = cur != null && !string.IsNullOrEmpty(cur.Upstream);
}
_workingCopy.CanCommitWithPush = _currentBranch != null && !string.IsNullOrEmpty(_currentBranch.Upstream);
});
}
@ -647,7 +650,7 @@ namespace SourceGit.ViewModels
public void RefreshTags()
{
var tags = new Commands.QueryTags(FullPath).Result();
var tags = new Commands.QueryTags(_fullpath).Result();
foreach (var tag in tags)
tag.IsFiltered = _settings.Filters.Contains(tag.Name);
@ -698,7 +701,7 @@ namespace SourceGit.ViewModels
var canPushCommits = new HashSet<string>();
var canPullCommits = new HashSet<string>();
var currentBranch = Branches.Find(x => x.IsCurrent);
var currentBranch = _branches.Find(x => x.IsCurrent);
if (currentBranch != null)
{
foreach (var sha in currentBranch.TrackStatus.Ahead)
@ -724,13 +727,13 @@ namespace SourceGit.ViewModels
public void RefreshSubmodules()
{
var submodules = new Commands.QuerySubmodules(FullPath).Result();
var submodules = new Commands.QuerySubmodules(_fullpath).Result();
Dispatcher.UIThread.Invoke(() => Submodules = submodules);
}
public void RefreshWorkingCopyChanges()
{
var changes = new Commands.QueryLocalChanges(FullPath, _includeUntracked).Result();
var changes = new Commands.QueryLocalChanges(_fullpath, _includeUntracked).Result();
if (_workingCopy == null)
return;
@ -774,7 +777,7 @@ namespace SourceGit.ViewModels
public void RefreshStashes()
{
var stashes = new Commands.QueryStashes(FullPath).Result();
var stashes = new Commands.QueryStashes(_fullpath).Result();
Dispatcher.UIThread.Invoke(() =>
{
if (_stashesPage != null)
@ -785,15 +788,14 @@ namespace SourceGit.ViewModels
public void CreateNewBranch()
{
var current = Branches.Find(x => x.IsCurrent);
if (current == null)
if (_currentBranch == null)
{
App.RaiseException(_fullpath, "Git do not hold any branch until you do first commit.");
return;
}
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new CreateBranch(this, current));
PopupHost.ShowPopup(new CreateBranch(this, _currentBranch));
}
public void CheckoutBranch(Models.Branch branch)
@ -820,7 +822,7 @@ namespace SourceGit.ViewModels
}
else
{
foreach (var b in Branches)
foreach (var b in _branches)
{
if (b.IsLocal && b.Upstream == branch.FullName)
{
@ -843,15 +845,14 @@ namespace SourceGit.ViewModels
public void CreateNewTag()
{
var current = Branches.Find(x => x.IsCurrent);
if (current == null)
if (_currentBranch == null)
{
App.RaiseException(_fullpath, "Git do not hold any branch until you do first commit.");
return;
}
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new CreateTag(this, current));
PopupHost.ShowPopup(new CreateTag(this, _currentBranch));
}
public void AddRemote()
@ -1007,12 +1008,12 @@ namespace SourceGit.ViewModels
var fetch = new MenuItem();
fetch.Header = App.Text("GitLFS.Fetch");
fetch.Icon = App.CreateMenuIcon("Icons.Fetch");
fetch.IsEnabled = Remotes.Count > 0;
fetch.IsEnabled = _remotes.Count > 0;
fetch.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
{
if (Remotes.Count == 1)
if (_remotes.Count == 1)
PopupHost.ShowAndStartPopup(new LFSFetch(this));
else
PopupHost.ShowPopup(new LFSFetch(this));
@ -1025,12 +1026,12 @@ namespace SourceGit.ViewModels
var pull = new MenuItem();
pull.Header = App.Text("GitLFS.Pull");
pull.Icon = App.CreateMenuIcon("Icons.Pull");
pull.IsEnabled = Remotes.Count > 0;
pull.IsEnabled = _remotes.Count > 0;
pull.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
{
if (Remotes.Count == 1)
if (_remotes.Count == 1)
PopupHost.ShowAndStartPopup(new LFSPull(this));
else
PopupHost.ShowPopup(new LFSPull(this));
@ -1043,12 +1044,12 @@ namespace SourceGit.ViewModels
var push = new MenuItem();
push.Header = App.Text("GitLFS.Push");
push.Icon = App.CreateMenuIcon("Icons.Push");
push.IsEnabled = Remotes.Count > 0;
push.IsEnabled = _remotes.Count > 0;
push.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
{
if (Remotes.Count == 1)
if (_remotes.Count == 1)
PopupHost.ShowAndStartPopup(new LFSPush(this));
else
PopupHost.ShowPopup(new LFSPush(this));
@ -1074,8 +1075,8 @@ namespace SourceGit.ViewModels
var locks = new MenuItem();
locks.Header = App.Text("GitLFS.Locks");
locks.Icon = App.CreateMenuIcon("Icons.Lock");
locks.IsEnabled = Remotes.Count > 0;
if (Remotes.Count == 1)
locks.IsEnabled = _remotes.Count > 0;
if (_remotes.Count == 1)
{
locks.Click += (_, e) =>
{
@ -1083,14 +1084,14 @@ namespace SourceGit.ViewModels
if (topLevel == null)
return;
var dialog = new Views.LFSLocks() { DataContext = new LFSLocks(_fullpath, Remotes[0].Name) };
var dialog = new Views.LFSLocks() { DataContext = new LFSLocks(_fullpath, _remotes[0].Name) };
dialog.Show(topLevel);
e.Handled = true;
};
}
else
{
foreach (var remote in Remotes)
foreach (var remote in _remotes)
{
var remoteName = remote.Name;
var lockRemote = new MenuItem();
@ -1138,7 +1139,7 @@ namespace SourceGit.ViewModels
var push = new MenuItem();
push.Header = new Views.NameHighlightedTextBlock("BranchCM.Push", branch.Name);
push.Icon = App.CreateMenuIcon("Icons.Push");
push.IsEnabled = Remotes.Count > 0;
push.IsEnabled = _remotes.Count > 0;
push.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
@ -1201,8 +1202,6 @@ namespace SourceGit.ViewModels
}
else
{
var current = Branches.Find(x => x.IsCurrent);
var checkout = new MenuItem();
checkout.Header = new Views.NameHighlightedTextBlock("BranchCM.Checkout", branch.Name);
checkout.Icon = App.CreateMenuIcon("Icons.Check");
@ -1213,7 +1212,7 @@ namespace SourceGit.ViewModels
};
menu.Items.Add(checkout);
var upstream = Branches.Find(x => x.FullName == branch.Upstream);
var upstream = _branches.Find(x => x.FullName == branch.Upstream);
if (upstream != null)
{
var fastForward = new MenuItem();
@ -1235,22 +1234,22 @@ namespace SourceGit.ViewModels
menu.Items.Add(push);
var merge = new MenuItem();
merge.Header = new Views.NameHighlightedTextBlock("BranchCM.Merge", branch.Name, current.Name);
merge.Header = new Views.NameHighlightedTextBlock("BranchCM.Merge", branch.Name, _currentBranch.Name);
merge.Icon = App.CreateMenuIcon("Icons.Merge");
merge.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new Merge(this, branch.Name, current.Name));
PopupHost.ShowPopup(new Merge(this, branch.Name, _currentBranch.Name));
e.Handled = true;
};
var rebase = new MenuItem();
rebase.Header = new Views.NameHighlightedTextBlock("BranchCM.Rebase", current.Name, branch.Name);
rebase.Header = new Views.NameHighlightedTextBlock("BranchCM.Rebase", _currentBranch.Name, branch.Name);
rebase.Icon = App.CreateMenuIcon("Icons.Rebase");
rebase.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new Rebase(this, current, branch));
PopupHost.ShowPopup(new Rebase(this, _currentBranch, branch));
e.Handled = true;
};
@ -1268,9 +1267,9 @@ namespace SourceGit.ViewModels
if (_histories != null)
{
var target = new Commands.QuerySingleCommit(FullPath, branch.Head).Result();
var target = new Commands.QuerySingleCommit(_fullpath, branch.Head).Result();
_histories.AutoSelectedCommit = null;
_histories.DetailContext = new RevisionCompare(FullPath, target, null);
_histories.DetailContext = new RevisionCompare(_fullpath, target, null);
}
};
menu.Items.Add(new MenuItem() { Header = "-" });
@ -1353,7 +1352,7 @@ namespace SourceGit.ViewModels
menu.Items.Add(new MenuItem() { Header = "-" });
var remoteBranches = new List<Models.Branch>();
foreach (var b in Branches)
foreach (var b in _branches)
{
if (!b.IsLocal)
remoteBranches.Add(b);
@ -1505,7 +1504,6 @@ namespace SourceGit.ViewModels
public ContextMenu CreateContextMenuForRemoteBranch(Models.Branch branch)
{
var menu = new ContextMenu();
var current = Branches.Find(x => x.IsCurrent);
var name = branch.FriendlyName;
var checkout = new MenuItem();
@ -1519,10 +1517,10 @@ namespace SourceGit.ViewModels
menu.Items.Add(checkout);
menu.Items.Add(new MenuItem() { Header = "-" });
if (current != null)
if (_currentBranch != null)
{
var pull = new MenuItem();
pull.Header = new Views.NameHighlightedTextBlock("BranchCM.PullInto", name, current.Name);
pull.Header = new Views.NameHighlightedTextBlock("BranchCM.PullInto", name, _currentBranch.Name);
pull.Icon = App.CreateMenuIcon("Icons.Pull");
pull.Click += (_, e) =>
{
@ -1532,22 +1530,22 @@ namespace SourceGit.ViewModels
};
var merge = new MenuItem();
merge.Header = new Views.NameHighlightedTextBlock("BranchCM.Merge", name, current.Name);
merge.Header = new Views.NameHighlightedTextBlock("BranchCM.Merge", name, _currentBranch.Name);
merge.Icon = App.CreateMenuIcon("Icons.Merge");
merge.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new Merge(this, name, current.Name));
PopupHost.ShowPopup(new Merge(this, name, _currentBranch.Name));
e.Handled = true;
};
var rebase = new MenuItem();
rebase.Header = new Views.NameHighlightedTextBlock("BranchCM.Rebase", current.Name, name);
rebase.Header = new Views.NameHighlightedTextBlock("BranchCM.Rebase", _currentBranch.Name, name);
rebase.Icon = App.CreateMenuIcon("Icons.Rebase");
rebase.Click += (_, e) =>
{
if (PopupHost.CanCreatePopup())
PopupHost.ShowPopup(new Rebase(this, current, branch));
PopupHost.ShowPopup(new Rebase(this, _currentBranch, branch));
e.Handled = true;
};
@ -1569,9 +1567,9 @@ namespace SourceGit.ViewModels
if (_histories != null)
{
var target = new Commands.QuerySingleCommit(FullPath, branch.Head).Result();
var target = new Commands.QuerySingleCommit(_fullpath, branch.Head).Result();
_histories.AutoSelectedCommit = null;
_histories.DetailContext = new RevisionCompare(FullPath, target, null);
_histories.DetailContext = new RevisionCompare(_fullpath, target, null);
}
};
menu.Items.Add(compareWithWorktree);
@ -1663,7 +1661,7 @@ namespace SourceGit.ViewModels
var pushTag = new MenuItem();
pushTag.Header = new Views.NameHighlightedTextBlock("TagCM.Push", tag.Name);
pushTag.Icon = App.CreateMenuIcon("Icons.Push");
pushTag.IsEnabled = Remotes.Count > 0;
pushTag.IsEnabled = _remotes.Count > 0;
pushTag.Click += (_, ev) =>
{
if (PopupHost.CanCreatePopup())
@ -1813,14 +1811,14 @@ namespace SourceGit.ViewModels
private MenuItem CreateMenuItemToCompareBranches(Models.Branch branch)
{
if (Branches.Count == 1)
if (_branches.Count == 1)
return null;
var compare = new MenuItem();
compare.Header = App.Text("BranchCM.CompareWithBranch");
compare.Icon = App.CreateMenuIcon("Icons.Compare");
foreach (var b in Branches)
foreach (var b in _branches)
{
if (b.FullName != branch.FullName)
{
@ -1836,7 +1834,7 @@ namespace SourceGit.ViewModels
var wnd = new Views.BranchCompare()
{
DataContext = new BranchCompare(FullPath, branch, dup)
DataContext = new BranchCompare(_fullpath, branch, dup)
};
wnd.Show(topLevel);
@ -1922,6 +1920,7 @@ namespace SourceGit.ViewModels
private List<Models.Remote> _remotes = new List<Models.Remote>();
private List<Models.Branch> _branches = new List<Models.Branch>();
private Models.Branch _currentBranch = null;
private List<BranchTreeNode> _localBranchTrees = new List<BranchTreeNode>();
private List<BranchTreeNode> _remoteBranchTrees = new List<BranchTreeNode>();
private List<Models.Worktree> _worktrees = new List<Models.Worktree>();

View file

@ -90,7 +90,7 @@ namespace SourceGit.ViewModels
{
if (SetProperty(ref _useAmend, value) && value)
{
var currentBranch = _repo.Branches.Find(x => x.IsCurrent);
var currentBranch = _repo.CurrentBranch;
if (currentBranch == null)
{
App.RaiseException(_repo.FullPath, "No commits to amend!!!");