-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathGitRepository.cs
198 lines (169 loc) · 6.96 KB
/
GitRepository.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using OpenOnGitHub.Extensions;
using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using GitReader;
using GitReader.Collections;
using GitReader.Primitive;
namespace OpenOnGitHub
{
public sealed class GitRepository(string targetFullPath) : IDisposable
{
private PrimitiveRepository _innerRepository;
private string _rootDirectory;
public string MainBranchName { get; private set; }
public string DevelopBranchName { get; private set; } // nullable
public bool IsDiscoveredGitRepository => _innerRepository != null;
public bool HasDevelopBranch => DevelopBranchName != null;
public string UrlRoot { get; private set; }
public async Task InitializeAsync()
{
_innerRepository = await Repository.Factory.OpenPrimitiveAsync(targetFullPath);
// https://github.com/user/repo.git
if (!_innerRepository.RemoteUrls.TryGetValue("origin", out var originUrl))
{
throw new InvalidOperationException("OriginUrl can't found");
}
// https://github.com/user/repo
UrlRoot = originUrl.EndsWith(".git", StringComparison.InvariantCultureIgnoreCase)
? originUrl.Substring(0, originUrl.Length - 4) // remove .git
: originUrl;
// [email protected]:user/repo -> http://github.com/user/repo
UrlRoot = Regex.Replace(UrlRoot, "^git@(.+):(.+)/(.+)$",
match => "http://" + string.Join("/", match.Groups.OfType<Group>().Skip(1).Select(group => group.Value)),
RegexOptions.IgnoreCase);
// https://[email protected]/user/repo -> https://github.com/user/repo
UrlRoot = Regex.Replace(UrlRoot, "(?<=^https?://)([^@/]+)@", "");
//https://github.com/user/repo/ -> https://github.com/user/repo
UrlRoot = UrlRoot.TrimEnd('/');
// foo/bar.cs
_rootDirectory = Path.GetDirectoryName(_innerRepository.GitPath);
var mainBranches = new[] { "main", "master", "develop" }; // development is Contains
var branches = await _innerRepository.GetBranchHeadReferencesAsync();
var branchesNames = branches.Select(x => x.Name).ToArray();
MainBranchName = SearchMainBranchName(branchesNames);
DevelopBranchName = branchesNames.FirstOrDefault(b => b.ToLower().Contains("develop"));
if (MainBranchName == DevelopBranchName)
{
DevelopBranchName = null;
}
}
string SearchMainBranchName(string[] branchNames)
{
string main = null;
string master = null;
string develop = null;
foreach (var item in branchNames)
{
if (item.Equals("main", StringComparison.OrdinalIgnoreCase))
{
main = item;
break;
}
if (item.Equals("master", StringComparison.OrdinalIgnoreCase))
{
master = item;
break;
}
if (item.Equals("develop", StringComparison.OrdinalIgnoreCase)) // develop or development...
{
develop = item;
}
}
if (main != null) return main;
if (master != null) return master;
if (develop != null) return develop;
return "main"; // not found, default label
}
public bool IsInsideRepositoryFolder(string filePath)
{
return filePath.IsSubPathOf(_rootDirectory);
}
public string GetFileIndexPath(string fullFilePath)
{
return fullFilePath.Substring(_rootDirectory.Length).Replace('\\', '/');
}
public async Task<string> GetGitHubTargetPathAsync(GitHubUrlType urlType)
{
if (_innerRepository == null)
{
return MainBranchName;
}
var head = await _innerRepository.GetCurrentHeadReferenceAsync();
if (head == null)
{
return MainBranchName;
}
return urlType switch
{
GitHubUrlType.Develop => DevelopBranchName ?? "develop",
GitHubUrlType.CurrentBranch => head.Value.Name.Replace("origin/", ""),
GitHubUrlType.CurrentRevision => ToString(head.Value.Target.HashCode, 8),
GitHubUrlType.CurrentRevisionFull => ToString(head.Value.Target.HashCode, head.Value.Target.HashCode.Length * 2),
_ => MainBranchName
};
}
public string GetInitialGitHubTargetDescription(GitHubUrlType urlType)
{
return urlType switch
{
GitHubUrlType.Develop => "develop",
GitHubUrlType.CurrentBranch => "branch",
GitHubUrlType.CurrentRevision => "revision",
GitHubUrlType.CurrentRevisionFull => "revision full",
_ => "main"
};
}
public async Task<string> GetGitHubTargetDescriptionAsync(GitHubUrlType urlType)
{
if (_innerRepository == null)
{
return MainBranchName;
}
var head = await _innerRepository.GetCurrentHeadReferenceAsync();
if (head == null)
{
return MainBranchName;
}
return urlType switch
{
GitHubUrlType.Develop => DevelopBranchName,
GitHubUrlType.CurrentBranch => $"branch: {head.Value.Name.Replace("origin/", "")}",
GitHubUrlType.CurrentRevision => $"revision: {ToString(head.Value.Target.HashCode, 8)}",
GitHubUrlType.CurrentRevisionFull => $"revision: {ToString(head.Value.Target.HashCode, 8)}... (Full ID)",
_ => MainBranchName
};
}
internal static string ToString(byte[] id, int lengthInNibbles)
{
char[] array = new char[lengthInNibbles];
for (int i = 0; i < (lengthInNibbles & -2); i++)
{
int num = i >> 1;
byte index = (byte)(id[num] >> 4);
array[i++] = "0123456789abcdef"[index];
index = (byte)(id[num] & 0xFu);
array[i] = "0123456789abcdef"[index];
}
if ((lengthInNibbles & 1) == 1)
{
int num2 = lengthInNibbles >> 1;
byte index2 = (byte)(id[num2] >> 4);
array[lengthInNibbles - 1] = "0123456789abcdef"[index2];
}
return new string(array);
}
public void Dispose()
{
_innerRepository?.Dispose();
_innerRepository = null;
GC.SuppressFinalize(this);
}
~GitRepository()
{
_innerRepository?.Dispose();
}
}
}