Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 17 additions & 39 deletions src/Models/CommitGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum CommitGraphHighlighting
All = 0,
CurrentBranchOnly,
SelectedCommitsOnly,
SelectedCommitsOnlyFirstParent,
CurrentBranchAndSelectedCommits,
}

Expand Down Expand Up @@ -70,7 +71,7 @@ public class Dot
public List<Link> Links { get; } = [];
public List<Dot> Dots { get; } = [];

public static CommitGraph Generate(List<Commit> commits, bool recalculateMergeState, bool firstParentOnlyEnabled, CommitGraphHighlighting highlighting, HashSet<string> highlightExtraCommits)
public static CommitGraph Generate(List<Commit> commits, bool firstParentOnlyEnabled, CommitGraphHighlighting highlighting, HashSet<string> highlightExtraCommits)
{
const double unitWidth = 12;
const double halfWidth = 6;
Expand All @@ -82,29 +83,11 @@ public static CommitGraph Generate(List<Commit> commits, bool recalculateMergeSt
var ended = new List<PathHelper>();
var offsetY = -halfHeight;
var colorPicker = new ColorPicker();
var merged = new HashSet<string>();

foreach (var commit in commits)
{
PathHelper major = null;

// Update merge state of this commit.
if (recalculateMergeState)
{
if (commit.IsMerged)
{
merged.Remove(commit.SHA);
foreach (var p in commit.Parents)
merged.Add(p);
}
else if (merged.Remove(commit.SHA))
{
commit.IsMerged = true;
foreach (var p in commit.Parents)
merged.Add(p);
}
}

// Update current y offset
offsetY += unitHeight;

Expand Down Expand Up @@ -164,31 +147,23 @@ public static CommitGraph Generate(List<Commit> commits, bool recalculateMergeSt
{
isHighlighted = true;
}
else if (highlighting == CommitGraphHighlighting.CurrentBranchOnly)

if (!isHighlighted &&
(highlighting == CommitGraphHighlighting.CurrentBranchOnly ||
highlighting == CommitGraphHighlighting.CurrentBranchAndSelectedCommits))
{
isHighlighted = commit.IsMerged;
}
else if (highlighting == CommitGraphHighlighting.SelectedCommitsOnly)

if (!isHighlighted &&
(highlighting == CommitGraphHighlighting.SelectedCommitsOnly ||
highlighting == CommitGraphHighlighting.SelectedCommitsOnlyFirstParent ||
highlighting == CommitGraphHighlighting.CurrentBranchAndSelectedCommits))
{
isHighlighted = highlightExtraCommits.Remove(commit.SHA);
if (isHighlighted)
{
foreach (var p in commit.Parents)
highlightExtraCommits.Add(p);
}
}
else
{
if (commit.IsMerged)
{
isHighlighted = true;
}
else if (highlightExtraCommits.Remove(commit.SHA))
{
isHighlighted = true;
foreach (var p in commit.Parents)
highlightExtraCommits.Add(p);
}
// Highlight first parent, other parents are dealt with later
if (isHighlighted && commit.Parents.Count > 0)
highlightExtraCommits.Add(commit.Parents[0]);
}
}
commit.IsHighlightedInGraph = isHighlighted;
Expand Down Expand Up @@ -227,6 +202,9 @@ public static CommitGraph Generate(List<Commit> commits, bool recalculateMergeSt
// Deal with other parents (the first parent has been processed)
if (!firstParentOnlyEnabled)
{
if (highlighting == CommitGraphHighlighting.SelectedCommitsOnlyFirstParent)
isHighlighted = false;

for (int j = 1; j < commit.Parents.Count; j++)
{
var parentHash = commit.Parents[j];
Expand Down
1 change: 1 addition & 0 deletions src/Resources/Locales/en_US.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@
<x:String x:Key="Text.Histories.HighlightsInGraph.CurrentBranchOnly" xml:space="preserve">Current Branch Only</x:String>
<x:String x:Key="Text.Histories.HighlightsInGraph.CurrentBranchAndSelectedCommits" xml:space="preserve">Current Branch &amp; Selected Commits</x:String>
<x:String x:Key="Text.Histories.HighlightsInGraph.SelectedCommitsOnly" xml:space="preserve">Selected Commits Only</x:String>
<x:String x:Key="Text.Histories.HighlightsInGraph.SelectedCommitsOnlyFirstParent" xml:space="preserve">Selected Commits (only first-parent)</x:String>
<x:String x:Key="Text.Histories.Selected" xml:space="preserve">SELECTED {0} COMMITS</x:String>
<x:String x:Key="Text.Histories.ShowColumns" xml:space="preserve">SHOW COLUMNS</x:String>
<x:String x:Key="Text.Histories.Tips" xml:space="preserve">Hold 'Ctrl' or 'Shift' to select multiple commits.</x:String>
Expand Down
24 changes: 21 additions & 3 deletions src/ViewModels/Histories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public List<Models.Commit> Commits
get => _commits;
set
{
GenerateGraph(value, true);
RecalculateMergeState(value);
GenerateGraph(value);
if (SetProperty(ref _commits, value))
PostCommitsChanged();
}
Expand Down Expand Up @@ -510,7 +511,24 @@ private void PostSelectedCommitsChanged()
GenerateGraph(_commits);
}

private void GenerateGraph(List<Models.Commit> commits, bool commitsChanged = false)
private void RecalculateMergeState(List<Models.Commit> commits)
{
var merged = new HashSet<string>();

foreach (var commit in commits)
{
if (merged.Remove(commit.SHA))
commit.IsMerged = true;

if (commit.IsMerged)
{
foreach (var p in commit.Parents)
merged.Add(p);
}
}
}

private void GenerateGraph(List<Models.Commit> commits)
{
var firstParentOnly = _repo.UIStates.HistoryShowFlags.HasFlag(Models.HistoryShowFlags.FirstParentOnly);
var highlighting = _repo.UIStates.GraphHighlighting;
Expand All @@ -522,7 +540,7 @@ private void GenerateGraph(List<Models.Commit> commits, bool commitsChanged = fa
extraHeads.Add(c.SHA);
}

Graph = Models.CommitGraph.Generate(commits, commitsChanged, firstParentOnly, highlighting, extraHeads);
Graph = Models.CommitGraph.Generate(commits, firstParentOnly, highlighting, extraHeads);
}

private Repository _repo = null;
Expand Down
11 changes: 11 additions & 0 deletions src/Views/Repository.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,16 @@ private void OnOpenAdvancedHistoriesOption(object sender, RoutedEventArgs e)
ev.Handled = true;
};

var selectedCommitsOnlyFirstParent = new MenuItem();
selectedCommitsOnlyFirstParent.Header = App.Text("Histories.HighlightsInGraph.SelectedCommitsOnlyFirstParent");
if (histories.GraphHighlighting == Models.CommitGraphHighlighting.SelectedCommitsOnlyFirstParent)
selectedCommitsOnlyFirstParent.Icon = this.CreateMenuIcon("Icons.Check");
selectedCommitsOnlyFirstParent.Click += (_, ev) =>
{
histories.GraphHighlighting = Models.CommitGraphHighlighting.SelectedCommitsOnlyFirstParent;
ev.Handled = true;
};

var currentBranchAndSelectedCommits = new MenuItem();
currentBranchAndSelectedCommits.Header = App.Text("Histories.HighlightsInGraph.CurrentBranchAndSelectedCommits");
if (histories.GraphHighlighting == Models.CommitGraphHighlighting.CurrentBranchAndSelectedCommits)
Expand Down Expand Up @@ -516,6 +526,7 @@ private void OnOpenAdvancedHistoriesOption(object sender, RoutedEventArgs e)
menu.Items.Add(all);
menu.Items.Add(currentBranchOnly);
menu.Items.Add(selectedCommitsOnly);
menu.Items.Add(selectedCommitsOnlyFirstParent);
menu.Items.Add(currentBranchAndSelectedCommits);
menu.Open(button);
}
Expand Down