-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatus.cs
More file actions
96 lines (86 loc) · 3.36 KB
/
Copy pathStatus.cs
File metadata and controls
96 lines (86 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using LibGit2Sharp;
namespace EyePatch
{
internal class Status : Command
{
public override void Execute(Settings settings, string? arg = null)
{
var repo = FindRepository();
ExecuteWithRepo(settings, repo);
}
internal void ExecuteWithRepo(Settings settings, IRepository repo)
{
var statusOptions = new StatusOptions
{
IncludeIgnored = false,
IncludeUnaltered = false
};
var repoStatus = repo.RetrieveStatus(statusOptions);
if (repoStatus == null)
{
throw new EyePatchException("No status on repo");
}
TreeChanges? remoteChanges = null;
var mainBranch = repo.Branches["main"];
if (mainBranch?.Tip != null && repo.Head.Tip != null)
{
try
{
remoteChanges = repo.Diff.Compare<TreeChanges>(
repo.Head.Tip.Tree,
mainBranch.Tip.Tree
);
}
catch
{
// ignore diff issues
}
}
// Iterate each file entry in the working status.
foreach (var entry in repoStatus)
{
var filePath = entry.FilePath;
var color = ConsoleColor.Yellow;
var isConflict = false;
// Check if the file is already marked as conflicted.
if (entry.State.HasFlag(FileStatus.Conflicted))
{
color = ConsoleColor.Magenta;
isConflict = true;
}
else
{
// Determine color based on working directory change.
if (entry.State.HasFlag(FileStatus.NewInWorkdir))
{
color = ConsoleColor.Green;
}
else if (entry.State.HasFlag(FileStatus.ModifiedInWorkdir))
{
color = ConsoleColor.Yellow;
}
else if (entry.State.HasFlag(FileStatus.DeletedFromWorkdir))
{
color = ConsoleColor.Red;
}
}
// If there are remote changes (committed changes past current) for the same file,
// and the local change is a modification or deletion, mark as conflict.
if (!isConflict && remoteChanges != null)
{
var remoteChange = remoteChanges.Any(change => change.Path == filePath);
if (remoteChange &&
(entry.State.HasFlag(FileStatus.ModifiedInIndex) ||
entry.State.HasFlag(FileStatus.DeletedFromIndex)))
{
color = ConsoleColor.Magenta;
isConflict = true;
}
}
Console.ForegroundColor = color;
Console.WriteLine($"{filePath} {(isConflict ? "(CONFLICT)" : string.Empty)}");
}
Console.ResetColor();
}
}
}