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-03-17 18:37:06 -07:00
|
|
|
|
public class Dot
|
|
|
|
|
{
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CommitGraph Parse(List<Commit> commits)
|
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-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.
|
|
|
|
|
double offsetX = -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-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-03-17 18:37:06 -07:00
|
|
|
|
if (major == null && commit.Parents.Count > 0)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
offsetX += UNIT_WIDTH;
|
|
|
|
|
major = new PathHelper(commit.Parents[0], isMerged, colorIdx, new Point(offsetX, offsetY));
|
|
|
|
|
unsolved.Add(major);
|
|
|
|
|
temp.Paths.Add(major.Path);
|
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-03-17 18:37:06 -07:00
|
|
|
|
if (major != null)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
major.IsMerged = isMerged;
|
|
|
|
|
position = new Point(major.LastX, offsetY);
|
|
|
|
|
temp.Dots.Add(new Dot() { Center = position, Color = major.Path.Color });
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
temp.Dots.Add(new Dot() { Center = position, Color = 0 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Deal with parents
|
2024-03-17 18:37:06 -07:00
|
|
|
|
for (int j = 1; j < commit.Parents.Count; j++)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var parent = commit.Parents[j];
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (mapUnsolved.TryGetValue(parent, out var value))
|
2024-03-17 18:37:06 -07:00
|
|
|
|
{
|
2024-07-10 19:55:48 -07:00
|
|
|
|
// Try to change the merge state of linked graph
|
2024-03-31 01:54:29 -07:00
|
|
|
|
var l = value;
|
2024-07-10 19:55:48 -07:00
|
|
|
|
if (isMerged)
|
|
|
|
|
l.IsMerged = true;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
2024-07-10 19:55:48 -07:00
|
|
|
|
var link = new Link();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
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;
|
2024-07-10 19:55:48 -07:00
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
temp.Links.Add(link);
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
offsetX += UNIT_WIDTH;
|
|
|
|
|
|
|
|
|
|
// Create new curve for parent commit that not includes before
|
|
|
|
|
var l = new PathHelper(commit.Parents[j], isMerged, colorIdx, position, new Point(offsetX, position.Y + HALF_HEIGHT));
|
|
|
|
|
unsolved.Add(l);
|
|
|
|
|
temp.Paths.Add(l.Path);
|
2024-07-02 07:54:26 -07:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Margins & merge state (used by datagrid).
|
|
|
|
|
commit.IsMerged = isMerged;
|
|
|
|
|
commit.Margin = new Thickness(Math.Max(offsetX + HALF_WIDTH, oldCount * UNIT_WIDTH), 0, 0, 0);
|
|
|
|
|
|
|
|
|
|
// 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-03-31 01:54:29 -07:00
|
|
|
|
if (path.Path.Points.Count == 1 && path.Path.Points[0].Y == endY)
|
|
|
|
|
continue;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
path.Add((i + 0.5) * UNIT_WIDTH, endY + HALF_HEIGHT, HALF_HEIGHT, true);
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|