2024-03-17 18:37:06 -07:00
|
|
|
|
using System;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
using Avalonia;
|
2024-06-06 05:25:16 -07:00
|
|
|
|
using Avalonia.Media;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
|
|
|
|
|
namespace SourceGit.Models
|
|
|
|
|
{
|
|
|
|
|
public class CommitGraph
|
|
|
|
|
{
|
|
|
|
|
public class Path
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public List<Point> Points = new List<Point>();
|
|
|
|
|
public int Color = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public class PathHelper
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public string Next;
|
|
|
|
|
public bool IsMerged;
|
|
|
|
|
public double LastX;
|
|
|
|
|
public double LastY;
|
|
|
|
|
public double EndY;
|
|
|
|
|
public Path Path;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public PathHelper(string next, bool isMerged, int color, Point start)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Next = next;
|
|
|
|
|
IsMerged = isMerged;
|
|
|
|
|
LastX = start.X;
|
|
|
|
|
LastY = start.Y;
|
|
|
|
|
EndY = LastY;
|
|
|
|
|
|
|
|
|
|
Path = new Path();
|
|
|
|
|
Path.Color = color;
|
|
|
|
|
Path.Points.Add(start);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public PathHelper(string next, bool isMerged, int color, Point start, Point to)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Next = next;
|
|
|
|
|
IsMerged = isMerged;
|
|
|
|
|
LastX = to.X;
|
|
|
|
|
LastY = to.Y;
|
|
|
|
|
EndY = LastY;
|
|
|
|
|
|
|
|
|
|
Path = new Path();
|
|
|
|
|
Path.Color = color;
|
|
|
|
|
Path.Points.Add(start);
|
|
|
|
|
Path.Points.Add(to);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public void Add(double x, double y, double halfHeight, bool isEnd = false)
|
|
|
|
|
{
|
|
|
|
|
if (x > LastX)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Add(new Point(LastX, LastY));
|
|
|
|
|
Add(new Point(x, y - halfHeight));
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (isEnd)
|
|
|
|
|
Add(new Point(x, y));
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (x < LastX)
|
|
|
|
|
{
|
2024-07-08 03:10:26 -07:00
|
|
|
|
var testY = LastY + halfHeight;
|
|
|
|
|
if (y > testY)
|
|
|
|
|
Add(new Point(LastX, testY));
|
|
|
|
|
|
|
|
|
|
if (!isEnd)
|
|
|
|
|
y += halfHeight;
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Add(new Point(x, y));
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (isEnd)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Add(new Point(x, y));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LastX = x;
|
|
|
|
|
LastY = y;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private void Add(Point p)
|
|
|
|
|
{
|
|
|
|
|
if (EndY < p.Y)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
Path.Points.Add(p);
|
|
|
|
|
EndY = p.Y;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public class Link
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public Point Start;
|
|
|
|
|
public Point Control;
|
|
|
|
|
public Point End;
|
|
|
|
|
public int Color;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-22 19:22:53 -07:00
|
|
|
|
public enum DotType
|
|
|
|
|
{
|
|
|
|
|
Default,
|
|
|
|
|
Head,
|
|
|
|
|
Merge,
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public class Dot
|
|
|
|
|
{
|
2024-08-22 19:22:53 -07:00
|
|
|
|
public DotType Type;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public Point Center;
|
|
|
|
|
public int Color;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<Path> Paths { get; set; } = new List<Path>();
|
|
|
|
|
public List<Link> Links { get; set; } = new List<Link>();
|
|
|
|
|
public List<Dot> Dots { get; set; } = new List<Dot>();
|
|
|
|
|
|
2024-07-02 07:54:26 -07:00
|
|
|
|
public static List<Pen> Pens
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
private set;
|
|
|
|
|
} = new List<Pen>();
|
|
|
|
|
|
2024-07-08 22:04:39 -07:00
|
|
|
|
public static void SetDefaultPens(double thickness = 2)
|
2024-07-02 07:54:26 -07:00
|
|
|
|
{
|
2024-07-08 01:21:57 -07:00
|
|
|
|
SetPens(_defaultPenColors, thickness);
|
2024-07-02 07:54:26 -07:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-08 01:21:57 -07:00
|
|
|
|
public static void SetPens(List<Color> colors, double thickness)
|
2024-07-02 07:54:26 -07:00
|
|
|
|
{
|
|
|
|
|
Pens.Clear();
|
|
|
|
|
|
|
|
|
|
foreach (var c in colors)
|
2024-07-08 01:21:57 -07:00
|
|
|
|
Pens.Add(new Pen(c.ToUInt32(), thickness));
|
2024-07-02 07:54:26 -07:00
|
|
|
|
|
|
|
|
|
_penCount = colors.Count;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-21 04:45:32 -07:00
|
|
|
|
public static CommitGraph Parse(List<Commit> commits, bool firstParentOnlyEnabled)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
double UNIT_WIDTH = 12;
|
|
|
|
|
double HALF_WIDTH = 6;
|
2024-06-06 05:25:16 -07:00
|
|
|
|
double UNIT_HEIGHT = 28;
|
|
|
|
|
double HALF_HEIGHT = 14;
|
2024-08-22 19:22:53 -07:00
|
|
|
|
double H_MARGIN = 2;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
|
|
var temp = new CommitGraph();
|
|
|
|
|
var unsolved = new List<PathHelper>();
|
|
|
|
|
var mapUnsolved = new Dictionary<string, PathHelper>();
|
|
|
|
|
var ended = new List<PathHelper>();
|
|
|
|
|
var offsetY = -HALF_HEIGHT;
|
|
|
|
|
var colorIdx = 0;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var commit in commits)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var major = null as PathHelper;
|
|
|
|
|
var isMerged = commit.IsMerged;
|
|
|
|
|
var oldCount = unsolved.Count;
|
|
|
|
|
|
|
|
|
|
// Update current y offset
|
|
|
|
|
offsetY += UNIT_HEIGHT;
|
|
|
|
|
|
|
|
|
|
// Find first curves that links to this commit and marks others that links to this commit ended.
|
2024-08-22 19:22:53 -07:00
|
|
|
|
double offsetX = H_MARGIN - HALF_WIDTH;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var l in unsolved)
|
|
|
|
|
{
|
|
|
|
|
if (l.Next == commit.SHA)
|
|
|
|
|
{
|
|
|
|
|
if (major == null)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
offsetX += UNIT_WIDTH;
|
|
|
|
|
major = l;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (commit.Parents.Count > 0)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
major.Next = commit.Parents[0];
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!mapUnsolved.ContainsKey(major.Next))
|
|
|
|
|
mapUnsolved.Add(major.Next, major);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
major.Next = "ENDED";
|
|
|
|
|
ended.Add(l);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
major.Add(offsetX, offsetY, HALF_HEIGHT);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
ended.Add(l);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isMerged = isMerged || l.IsMerged;
|
2024-08-26 02:26:02 -07:00
|
|
|
|
major.IsMerged = isMerged;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!mapUnsolved.ContainsKey(l.Next))
|
|
|
|
|
mapUnsolved.Add(l.Next, l);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
offsetX += UNIT_WIDTH;
|
|
|
|
|
l.Add(offsetX, offsetY, HALF_HEIGHT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create new curve for branch head
|
2024-08-26 02:26:02 -07:00
|
|
|
|
if (major == null)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
offsetX += UNIT_WIDTH;
|
2024-08-26 02:26:02 -07:00
|
|
|
|
|
|
|
|
|
if (commit.Parents.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
major = new PathHelper(commit.Parents[0], isMerged, colorIdx, new Point(offsetX, offsetY));
|
|
|
|
|
unsolved.Add(major);
|
|
|
|
|
temp.Paths.Add(major.Path);
|
|
|
|
|
}
|
2024-08-27 22:42:25 -07:00
|
|
|
|
|
2024-07-02 07:54:26 -07:00
|
|
|
|
colorIdx = (colorIdx + 1) % _penCount;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Calculate link position of this commit.
|
|
|
|
|
Point position = new Point(offsetX, offsetY);
|
2024-08-22 19:22:53 -07:00
|
|
|
|
int dotColor = 0;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (major != null)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
position = new Point(major.LastX, offsetY);
|
2024-08-22 19:22:53 -07:00
|
|
|
|
dotColor = major.Path.Color;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
2024-08-22 19:22:53 -07:00
|
|
|
|
Dot anchor = new Dot() { Center = position, Color = dotColor };
|
|
|
|
|
if (commit.IsCurrentHead)
|
|
|
|
|
anchor.Type = DotType.Head;
|
|
|
|
|
else if (commit.Parents.Count > 1)
|
|
|
|
|
anchor.Type = DotType.Merge;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
else
|
2024-08-22 19:22:53 -07:00
|
|
|
|
anchor.Type = DotType.Default;
|
|
|
|
|
temp.Dots.Add(anchor);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-08-21 04:45:32 -07:00
|
|
|
|
// Deal with other parents (the first parent has been processed)
|
|
|
|
|
if (!firstParentOnlyEnabled)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-08-21 04:45:32 -07:00
|
|
|
|
for (int j = 1; j < commit.Parents.Count; j++)
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-08-21 04:45:32 -07:00
|
|
|
|
var parent = commit.Parents[j];
|
|
|
|
|
if (mapUnsolved.TryGetValue(parent, out var value))
|
|
|
|
|
{
|
|
|
|
|
// Try to change the merge state of linked graph
|
|
|
|
|
var l = value;
|
|
|
|
|
if (isMerged)
|
|
|
|
|
l.IsMerged = true;
|
|
|
|
|
|
|
|
|
|
var link = new Link();
|
|
|
|
|
link.Start = position;
|
|
|
|
|
link.End = new Point(l.LastX, offsetY + HALF_HEIGHT);
|
|
|
|
|
link.Control = new Point(link.End.X, link.Start.Y);
|
|
|
|
|
link.Color = l.Path.Color;
|
|
|
|
|
|
|
|
|
|
temp.Links.Add(link);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
offsetX += UNIT_WIDTH;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-08-21 04:45:32 -07:00
|
|
|
|
// Create new curve for parent commit that not includes before
|
|
|
|
|
var l = new PathHelper(parent, isMerged, colorIdx, position, new Point(offsetX, position.Y + HALF_HEIGHT));
|
|
|
|
|
unsolved.Add(l);
|
|
|
|
|
temp.Paths.Add(l.Path);
|
|
|
|
|
colorIdx = (colorIdx + 1) % _penCount;
|
|
|
|
|
}
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remove ended curves from unsolved
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var l in ended)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
l.Add(position.X, position.Y, HALF_HEIGHT, true);
|
|
|
|
|
unsolved.Remove(l);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-27 06:28:48 -07:00
|
|
|
|
// Margins & merge state (used by Views.Histories).
|
2024-02-05 23:08:37 -08:00
|
|
|
|
commit.IsMerged = isMerged;
|
2024-08-22 19:22:53 -07:00
|
|
|
|
commit.Margin = new Thickness(Math.Max(offsetX + HALF_WIDTH, oldCount * UNIT_WIDTH + H_MARGIN) + H_MARGIN, 0, 0, 0);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
|
ended.Clear();
|
|
|
|
|
mapUnsolved.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Deal with curves haven't ended yet.
|
2024-03-17 18:37:06 -07:00
|
|
|
|
for (int i = 0; i < unsolved.Count; i++)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var path = unsolved[i];
|
|
|
|
|
var endY = (commits.Count - 0.5) * UNIT_HEIGHT;
|
|
|
|
|
|
2024-07-14 09:30:31 -07:00
|
|
|
|
if (path.Path.Points.Count == 1 && Math.Abs(path.Path.Points[0].Y - endY) < 0.0001)
|
2024-03-31 01:54:29 -07:00
|
|
|
|
continue;
|
2024-07-14 09:30:31 -07:00
|
|
|
|
|
2024-08-22 19:22:53 -07:00
|
|
|
|
path.Add((i + 0.5) * UNIT_WIDTH + H_MARGIN, endY + HALF_HEIGHT, HALF_HEIGHT, true);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
|
|
|
|
unsolved.Clear();
|
|
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
2024-07-02 07:54:26 -07:00
|
|
|
|
|
|
|
|
|
private static int _penCount = 0;
|
|
|
|
|
private static readonly List<Color> _defaultPenColors = [
|
|
|
|
|
Colors.Orange,
|
|
|
|
|
Colors.ForestGreen,
|
|
|
|
|
Colors.Gold,
|
|
|
|
|
Colors.Magenta,
|
|
|
|
|
Colors.Red,
|
|
|
|
|
Colors.Gray,
|
|
|
|
|
Colors.Turquoise,
|
|
|
|
|
Colors.Olive,
|
|
|
|
|
Colors.Khaki,
|
|
|
|
|
Colors.Lime,
|
|
|
|
|
];
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|