-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
114 lines (94 loc) · 3.89 KB
/
Program.cs
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.IO;
using System.Linq;
using System.Threading.Channels;
using System.Threading.Tasks;
using LibGit2Sharp;
using static System.Console;
using static System.String;
namespace GitClearBranches
{
internal static class Program
{
private static ConsoleColor _defaultConsoleColor;
private static Task<int> Main(string[] whitelisted)
{
_defaultConsoleColor = ForegroundColor;
if (whitelisted.Length == 0)
{
whitelisted = new[] { "master", "main", "dev", "develop", "prod" };
}
var repositoryPath = ResolveRepositoryPath();
if (IsNullOrEmpty(repositoryPath))
{
WriteLine("fatal: Not a git repository");
return Task.FromResult(-1);
}
var pathToGit = ResolveGitPath();
if (IsNullOrEmpty(pathToGit))
{
WriteLine("fatal: git is not in your path");
return Task.FromResult(-2);
}
using var repository = new Repository(repositoryPath);
var headRef = repository.Refs.Where(r => r.CanonicalName == repository.Head.CanonicalName);
var notTrackedMergedLocalBranches = repository.Branches.Where(
b =>
// !b.IsTracking &&
!b.IsRemote &&
(!b.IsTracking || b.TrackingDetails.CommonAncestor is null) &&
b.FriendlyName != repository.Head.FriendlyName &&
repository.Refs.ReachableFrom(headRef, new[] { b.Tip }).Any() && // merged
!whitelisted.Contains(b.FriendlyName)
).ToList();
if (!notTrackedMergedLocalBranches.Any())
return Task.FromResult(0);
if (whitelisted.Contains("--yes") || whitelisted.Contains("-y"))
{
notTrackedMergedLocalBranches.ForEach(repository.Branches.Remove);
return Task.FromResult(0);
}
WriteLine("Branches to be removed: ");
WriteLine();
var indexer = 0;
notTrackedMergedLocalBranches.ForEach((b) =>
{
Write(b.FriendlyName);
Write("\t");
if(indexer++ % 5 == 0) WriteLine();
});
WriteLine("--------------------------------------------");
WriteLine();
ForegroundColor = ConsoleColor.Cyan;
Write("Are you sure you wanna delete those branches (Y/n) ? ");
var answer = ReadLine();
ForegroundColor = _defaultConsoleColor;
if (IsNullOrEmpty(answer) || answer == "Y")
notTrackedMergedLocalBranches.ForEach(repository.Branches.Remove);
return Task.FromResult(0);
}
private static string ResolveRepositoryPath()
{
var currentDirectory = Directory.GetCurrentDirectory();
var repositoryPath = Repository.Discover(currentDirectory);
if (IsNullOrEmpty(repositoryPath))
return Empty;
var index = repositoryPath.LastIndexOf("/.git/", StringComparison.Ordinal);
if(index == -1)
index = repositoryPath.LastIndexOf("\\.git\\", StringComparison.Ordinal);
return Repository.IsValid(repositoryPath)
? repositoryPath[..index]
: Empty;
}
private static string ResolveGitPath()
{
var path = Environment.GetEnvironmentVariable("PATH");
if (IsNullOrEmpty(path))
return Empty;
var paths = path.Split(Path.PathSeparator);
var fileNames = new[] { "git.exe", "git" };
var searchPaths = paths.SelectMany(p => fileNames.Select(f => Path.Combine(p, f)));
return searchPaths.FirstOrDefault(File.Exists) ?? Empty;
}
}
}