|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using CommunityToolkit.Mvvm.ComponentModel; |
| 5 | + |
| 6 | +namespace SourceGit.ViewModels |
| 7 | +{ |
| 8 | + public class Worktree : ObservableObject |
| 9 | + { |
| 10 | + public Models.Worktree Backend { get; private set; } |
| 11 | + public bool IsMain { get; private set; } |
| 12 | + public bool IsCurrent { get; private set; } |
| 13 | + public string DisplayPath { get; private set; } |
| 14 | + public string Name { get; private set; } |
| 15 | + public string Branch { get; private set; } |
| 16 | + public int Depth { get; private set; } |
| 17 | + |
| 18 | + public bool IsLocked |
| 19 | + { |
| 20 | + get => _isLocked; |
| 21 | + set => SetProperty(ref _isLocked, value); |
| 22 | + } |
| 23 | + |
| 24 | + public string FullPath => Backend.FullPath; |
| 25 | + public string Head => Backend.Head; |
| 26 | + |
| 27 | + public Worktree(DirectoryInfo repo, Models.Worktree wt, bool isMain) |
| 28 | + { |
| 29 | + Backend = wt; |
| 30 | + IsMain = isMain; |
| 31 | + IsCurrent = IsCurrentWorktree(repo, wt); |
| 32 | + DisplayPath = IsCurrent ? string.Empty : Path.GetRelativePath(repo.FullName, wt.FullPath); |
| 33 | + Name = GenerateName(); |
| 34 | + Branch = GenerateBranchName(); |
| 35 | + Depth = isMain ? 0 : 1; |
| 36 | + IsLocked = wt.IsLocked; |
| 37 | + } |
| 38 | + |
| 39 | + public static List<Worktree> Build(string repo, List<Models.Worktree> worktrees) |
| 40 | + { |
| 41 | + if (worktrees is not { Count: > 1 }) |
| 42 | + return []; |
| 43 | + |
| 44 | + var repoDir = new DirectoryInfo(repo); |
| 45 | + var nodes = new List<Worktree>(); |
| 46 | + nodes.Add(new(repoDir, worktrees[0], true)); |
| 47 | + for (int i = 1; i < worktrees.Count; i++) |
| 48 | + nodes.Add(new(repoDir, worktrees[i], false)); |
| 49 | + |
| 50 | + return nodes; |
| 51 | + } |
| 52 | + |
| 53 | + public bool IsAttachedTo(Models.Branch branch) |
| 54 | + { |
| 55 | + if (string.IsNullOrEmpty(branch.WorktreePath)) |
| 56 | + return false; |
| 57 | + |
| 58 | + var wtDir = new DirectoryInfo(Backend.FullPath); |
| 59 | + var test = new DirectoryInfo(branch.WorktreePath); |
| 60 | + return test.FullName.Equals(wtDir.FullName, StringComparison.Ordinal); |
| 61 | + } |
| 62 | + |
| 63 | + private bool IsCurrentWorktree(DirectoryInfo repo, Models.Worktree wt) |
| 64 | + { |
| 65 | + var wtDir = new DirectoryInfo(wt.FullPath); |
| 66 | + return wtDir.FullName.Equals(repo.FullName, StringComparison.Ordinal); |
| 67 | + } |
| 68 | + |
| 69 | + private string GenerateName() |
| 70 | + { |
| 71 | + if (IsMain) |
| 72 | + return Path.GetFileName(Backend.FullPath); |
| 73 | + |
| 74 | + if (Backend.IsDetached) |
| 75 | + return $"detached HEAD at {Backend.Head.AsSpan(10)}"; |
| 76 | + |
| 77 | + var b = Backend.Branch; |
| 78 | + |
| 79 | + if (b.StartsWith("refs/heads/", StringComparison.Ordinal)) |
| 80 | + return b.Substring(11); |
| 81 | + |
| 82 | + if (b.StartsWith("refs/remotes/", StringComparison.Ordinal)) |
| 83 | + return b.Substring(13); |
| 84 | + |
| 85 | + return b; |
| 86 | + } |
| 87 | + |
| 88 | + private string GenerateBranchName() |
| 89 | + { |
| 90 | + if (Backend.IsBare) |
| 91 | + return "-- (default)"; |
| 92 | + |
| 93 | + if (Backend.IsDetached) |
| 94 | + return "-- (detached)"; |
| 95 | + |
| 96 | + if (string.IsNullOrEmpty(Backend.Branch)) |
| 97 | + return "-- (unknown)"; |
| 98 | + |
| 99 | + var b = Backend.Branch; |
| 100 | + |
| 101 | + if (b.StartsWith("refs/heads/", StringComparison.Ordinal)) |
| 102 | + return b.Substring(11); |
| 103 | + |
| 104 | + if (b.StartsWith("refs/remotes/", StringComparison.Ordinal)) |
| 105 | + return b.Substring(13); |
| 106 | + |
| 107 | + return b; |
| 108 | + } |
| 109 | + |
| 110 | + private bool _isLocked = false; |
| 111 | + } |
| 112 | +} |
0 commit comments