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;
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
if (isEnd) Add(new Point(x, y));
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else if (x < LastX)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
if (y > LastY + halfHeight) Add(new Point(LastX, LastY + halfHeight));
|
|
|
|
|
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-03-17 18:37:06 -07:00
|
|
|
|
public static CommitGraph Parse(List<Commit> commits, double rowHeight, int colorCount)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
double UNIT_WIDTH = 12;
|
|
|
|
|
double HALF_WIDTH = 6;
|
|
|
|
|
double UNIT_HEIGHT = rowHeight;
|
|
|
|
|
double HALF_HEIGHT = rowHeight / 2;
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
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-02-05 23:08:37 -08:00
|
|
|
|
if (!mapUnsolved.ContainsKey(l.Next)) mapUnsolved.Add(l.Next, l);
|
|
|
|
|
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);
|
|
|
|
|
colorIdx = (colorIdx + 1) % colorCount;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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-17 18:37:06 -07:00
|
|
|
|
if (mapUnsolved.ContainsKey(parent))
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var l = mapUnsolved[parent];
|
|
|
|
|
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);
|
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);
|
|
|
|
|
colorIdx = (colorIdx + 1) % colorCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
|
|
if (path.Path.Points.Count == 1 && path.Path.Points[0].Y == endY) continue;
|
|
|
|
|
path.Add((i + 0.5) * UNIT_WIDTH, endY + HALF_HEIGHT, HALF_HEIGHT, true);
|
|
|
|
|
}
|
|
|
|
|
unsolved.Clear();
|
|
|
|
|
|
|
|
|
|
return temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|