Skip to content

Commit 3d281f2

Browse files
authored
feature: add new graph-highlighting option "Selected Commits (only first-parent)" (#2646)
* Refactor: move relevant code to new method Histories.RecalculateMergeState() * Reduce redundant code in RecalculateMergeState() * Reduce redundant highlighting-code by modifying if-conditions * Highlight first parent separately for "extra" commits * The remaining parents are covered further down (under condition '!firstParentOnlyEnabled'). * This is made in prep for upcoming work on adding a new highlighting option. * Add new graph-highlight option "Selected Commits (only first-parent)"
1 parent f9ec08b commit 3d281f2

4 files changed

Lines changed: 50 additions & 42 deletions

File tree

src/Models/CommitGraph.cs

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum CommitGraphHighlighting
1313
All = 0,
1414
CurrentBranchOnly,
1515
SelectedCommitsOnly,
16+
SelectedCommitsOnlyFirstParent,
1617
CurrentBranchAndSelectedCommits,
1718
}
1819

@@ -70,7 +71,7 @@ public class Dot
7071
public List<Link> Links { get; } = [];
7172
public List<Dot> Dots { get; } = [];
7273

73-
public static CommitGraph Generate(List<Commit> commits, bool recalculateMergeState, bool firstParentOnlyEnabled, CommitGraphHighlighting highlighting, HashSet<string> highlightExtraCommits)
74+
public static CommitGraph Generate(List<Commit> commits, bool firstParentOnlyEnabled, CommitGraphHighlighting highlighting, HashSet<string> highlightExtraCommits)
7475
{
7576
const double unitWidth = 12;
7677
const double halfWidth = 6;
@@ -82,29 +83,11 @@ public static CommitGraph Generate(List<Commit> commits, bool recalculateMergeSt
8283
var ended = new List<PathHelper>();
8384
var offsetY = -halfHeight;
8485
var colorPicker = new ColorPicker();
85-
var merged = new HashSet<string>();
8686

8787
foreach (var commit in commits)
8888
{
8989
PathHelper major = null;
9090

91-
// Update merge state of this commit.
92-
if (recalculateMergeState)
93-
{
94-
if (commit.IsMerged)
95-
{
96-
merged.Remove(commit.SHA);
97-
foreach (var p in commit.Parents)
98-
merged.Add(p);
99-
}
100-
else if (merged.Remove(commit.SHA))
101-
{
102-
commit.IsMerged = true;
103-
foreach (var p in commit.Parents)
104-
merged.Add(p);
105-
}
106-
}
107-
10891
// Update current y offset
10992
offsetY += unitHeight;
11093

@@ -164,31 +147,23 @@ public static CommitGraph Generate(List<Commit> commits, bool recalculateMergeSt
164147
{
165148
isHighlighted = true;
166149
}
167-
else if (highlighting == CommitGraphHighlighting.CurrentBranchOnly)
150+
151+
if (!isHighlighted &&
152+
(highlighting == CommitGraphHighlighting.CurrentBranchOnly ||
153+
highlighting == CommitGraphHighlighting.CurrentBranchAndSelectedCommits))
168154
{
169155
isHighlighted = commit.IsMerged;
170156
}
171-
else if (highlighting == CommitGraphHighlighting.SelectedCommitsOnly)
157+
158+
if (!isHighlighted &&
159+
(highlighting == CommitGraphHighlighting.SelectedCommitsOnly ||
160+
highlighting == CommitGraphHighlighting.SelectedCommitsOnlyFirstParent ||
161+
highlighting == CommitGraphHighlighting.CurrentBranchAndSelectedCommits))
172162
{
173163
isHighlighted = highlightExtraCommits.Remove(commit.SHA);
174-
if (isHighlighted)
175-
{
176-
foreach (var p in commit.Parents)
177-
highlightExtraCommits.Add(p);
178-
}
179-
}
180-
else
181-
{
182-
if (commit.IsMerged)
183-
{
184-
isHighlighted = true;
185-
}
186-
else if (highlightExtraCommits.Remove(commit.SHA))
187-
{
188-
isHighlighted = true;
189-
foreach (var p in commit.Parents)
190-
highlightExtraCommits.Add(p);
191-
}
164+
// Highlight first parent, other parents are dealt with later
165+
if (isHighlighted && commit.Parents.Count > 0)
166+
highlightExtraCommits.Add(commit.Parents[0]);
192167
}
193168
}
194169
commit.IsHighlightedInGraph = isHighlighted;
@@ -227,6 +202,9 @@ public static CommitGraph Generate(List<Commit> commits, bool recalculateMergeSt
227202
// Deal with other parents (the first parent has been processed)
228203
if (!firstParentOnlyEnabled)
229204
{
205+
if (highlighting == CommitGraphHighlighting.SelectedCommitsOnlyFirstParent)
206+
isHighlighted = false;
207+
230208
for (int j = 1; j < commit.Parents.Count; j++)
231209
{
232210
var parentHash = commit.Parents[j];

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@
518518
<x:String x:Key="Text.Histories.HighlightsInGraph.CurrentBranchOnly" xml:space="preserve">Current Branch Only</x:String>
519519
<x:String x:Key="Text.Histories.HighlightsInGraph.CurrentBranchAndSelectedCommits" xml:space="preserve">Current Branch &amp; Selected Commits</x:String>
520520
<x:String x:Key="Text.Histories.HighlightsInGraph.SelectedCommitsOnly" xml:space="preserve">Selected Commits Only</x:String>
521+
<x:String x:Key="Text.Histories.HighlightsInGraph.SelectedCommitsOnlyFirstParent" xml:space="preserve">Selected Commits (only first-parent)</x:String>
521522
<x:String x:Key="Text.Histories.Selected" xml:space="preserve">SELECTED {0} COMMITS</x:String>
522523
<x:String x:Key="Text.Histories.ShowColumns" xml:space="preserve">SHOW COLUMNS</x:String>
523524
<x:String x:Key="Text.Histories.Tips" xml:space="preserve">Hold 'Ctrl' or 'Shift' to select multiple commits.</x:String>

src/ViewModels/Histories.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public List<Models.Commit> Commits
7575
get => _commits;
7676
set
7777
{
78-
GenerateGraph(value, true);
78+
RecalculateMergeState(value);
79+
GenerateGraph(value);
7980
if (SetProperty(ref _commits, value))
8081
PostCommitsChanged();
8182
}
@@ -510,7 +511,24 @@ private void PostSelectedCommitsChanged()
510511
GenerateGraph(_commits);
511512
}
512513

513-
private void GenerateGraph(List<Models.Commit> commits, bool commitsChanged = false)
514+
private void RecalculateMergeState(List<Models.Commit> commits)
515+
{
516+
var merged = new HashSet<string>();
517+
518+
foreach (var commit in commits)
519+
{
520+
if (merged.Remove(commit.SHA))
521+
commit.IsMerged = true;
522+
523+
if (commit.IsMerged)
524+
{
525+
foreach (var p in commit.Parents)
526+
merged.Add(p);
527+
}
528+
}
529+
}
530+
531+
private void GenerateGraph(List<Models.Commit> commits)
514532
{
515533
var firstParentOnly = _repo.UIStates.HistoryShowFlags.HasFlag(Models.HistoryShowFlags.FirstParentOnly);
516534
var highlighting = _repo.UIStates.GraphHighlighting;
@@ -522,7 +540,7 @@ private void GenerateGraph(List<Models.Commit> commits, bool commitsChanged = fa
522540
extraHeads.Add(c.SHA);
523541
}
524542

525-
Graph = Models.CommitGraph.Generate(commits, commitsChanged, firstParentOnly, highlighting, extraHeads);
543+
Graph = Models.CommitGraph.Generate(commits, firstParentOnly, highlighting, extraHeads);
526544
}
527545

528546
private Repository _repo = null;

src/Views/Repository.axaml.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,16 @@ private void OnOpenAdvancedHistoriesOption(object sender, RoutedEventArgs e)
487487
ev.Handled = true;
488488
};
489489

490+
var selectedCommitsOnlyFirstParent = new MenuItem();
491+
selectedCommitsOnlyFirstParent.Header = App.Text("Histories.HighlightsInGraph.SelectedCommitsOnlyFirstParent");
492+
if (histories.GraphHighlighting == Models.CommitGraphHighlighting.SelectedCommitsOnlyFirstParent)
493+
selectedCommitsOnlyFirstParent.Icon = this.CreateMenuIcon("Icons.Check");
494+
selectedCommitsOnlyFirstParent.Click += (_, ev) =>
495+
{
496+
histories.GraphHighlighting = Models.CommitGraphHighlighting.SelectedCommitsOnlyFirstParent;
497+
ev.Handled = true;
498+
};
499+
490500
var currentBranchAndSelectedCommits = new MenuItem();
491501
currentBranchAndSelectedCommits.Header = App.Text("Histories.HighlightsInGraph.CurrentBranchAndSelectedCommits");
492502
if (histories.GraphHighlighting == Models.CommitGraphHighlighting.CurrentBranchAndSelectedCommits)
@@ -516,6 +526,7 @@ private void OnOpenAdvancedHistoriesOption(object sender, RoutedEventArgs e)
516526
menu.Items.Add(all);
517527
menu.Items.Add(currentBranchOnly);
518528
menu.Items.Add(selectedCommitsOnly);
529+
menu.Items.Add(selectedCommitsOnlyFirstParent);
519530
menu.Items.Add(currentBranchAndSelectedCommits);
520531
menu.Open(button);
521532
}

0 commit comments

Comments
 (0)