Skip to content

Commit

Permalink
Apply user-defined branch sorting
Browse files Browse the repository at this point in the history
The user can specify whether branches are sorted in A-Z or by date order.
The left panel now uses the same ordering techniques.

Fixes gitextensions#4830
Relates to gitextensions#4814
  • Loading branch information
RussKie committed Jun 3, 2018
1 parent aa27c54 commit 656b641
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
13 changes: 0 additions & 13 deletions GitCommands/Git/GitModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ public struct ConflictData
public sealed class GitModule : IGitModule
{
private const char LineSeparator = '\n';
public static readonly char ActiveBranchIndicator = '*';

private static readonly Regex AnsiCodePattern = new Regex(@"\u001B[\u0040-\u005F].*?[\u0040-\u007E]", RegexOptions.Compiled);
private static readonly Regex CpEncodingPattern = new Regex("cp\\d+", RegexOptions.Compiled);
Expand Down Expand Up @@ -2970,18 +2969,6 @@ public IReadOnlyList<IGitRef> ParseRefs([NotNull] string refList)
return gitRefs;
}

/// <summary>Gets the branch names, with the active branch, if applicable, listed first.
/// The active branch will be indicated by a "*", so ensure to Trim before processing.</summary>
public IEnumerable<string> GetBranchNames()
{
return RunGitCmd("branch", SystemEncoding)
.Split(LineSeparator)
.Where(branch => !string.IsNullOrWhiteSpace(branch)) // first is ""
.OrderByDescending(branch => branch.Contains(ActiveBranchIndicator)) // * for current branch
.ThenBy(r => r)
.Select(line => line.Trim());
}

/// <summary>
/// Gets branches which contain the given commit.
/// If both local and remote branches are requested, remote branches are prefixed with "remotes/"
Expand Down
21 changes: 15 additions & 6 deletions GitUI/BranchTreePanel/RepoObjectsTree.Nodes.Branches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ protected void SelectRevision()

private sealed class LocalBranchNode : BaseBranchNode
{
public LocalBranchNode(Tree tree, string fullPath)
: base(tree, fullPath.TrimStart(GitModule.ActiveBranchIndicator))
public LocalBranchNode(Tree tree, string fullPath, bool isCurrent)
: base(tree, fullPath)
{
IsActive = fullPath.StartsWith(GitModule.ActiveBranchIndicator.ToString());
IsActive = isCurrent;
}

public bool IsActive { get; }
Expand Down Expand Up @@ -231,7 +231,14 @@ protected override async Task LoadNodesAsync(CancellationToken token)
{
await TaskScheduler.Default;
token.ThrowIfCancellationRequested();
FillBranchTree(Module.GetBranchNames(), token);

var branchNames = Module.GetRefs(false).Select(b => b.Name);
if (AppSettings.BranchOrderingCriteria == BranchOrdering.Alphabetically)
{
branchNames = branchNames.OrderBy(b => b);
}

FillBranchTree(branchNames, token);
}

private void FillBranchTree(IEnumerable<string> branches, CancellationToken token)
Expand Down Expand Up @@ -265,12 +272,14 @@ private void FillBranchTree(IEnumerable<string> branches, CancellationToken toke
// 0 master

#endregion ex

var currentBranch = Module.GetSelectedBranch();
var nodes = new Dictionary<string, BaseBranchNode>();
var branchFullPaths = new List<string>();
foreach (var branch in branches)
{
token.ThrowIfCancellationRequested();
var localBranchNode = new LocalBranchNode(this, branch);
var localBranchNode = new LocalBranchNode(this, branch, branch == currentBranch);
var parent = localBranchNode.CreateRootNode(nodes,
(tree, parentPath) => new BranchPathNode(tree, parentPath));
if (parent != null)
Expand All @@ -295,7 +304,7 @@ protected override void FillTreeViewNode()
}
}
}
#endregion private classes
#endregion private classes

[Pure]
public static GitRef CreateBranchRef(GitModule module, string name)
Expand Down

0 comments on commit 656b641

Please sign in to comment.