forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommitDataExt.cs
214 lines (179 loc) · 8.48 KB
/
CommitDataExt.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Linq;
using System.Net;
using System.Text;
using GitCommands;
namespace ResourceManager
{
public static class CommitDataExt
{
private const int COMMITHEADER_STRING_LENGTH = 16;
private static string GetEmail(string author)
{
if (String.IsNullOrEmpty(author))
return "";
int ind = author.IndexOf("<");
if (ind == -1)
return "";
++ind;
return author.Substring(ind, author.LastIndexOf(">") - ind);
}
/// <summary>
/// Generate header.
/// </summary>
/// <param name="commitData"></param>
/// <returns></returns>
public static string GetHeaderPlain(this CommitData commitData)
{
StringBuilder header = new StringBuilder();
header.AppendLine(FillToLength(Strings.GetAuthorText() + ":", COMMITHEADER_STRING_LENGTH) +
commitData.Author);
header.AppendLine(FillToLength(Strings.GetAuthorDateText() + ":", COMMITHEADER_STRING_LENGTH) +
LocalizationHelpers.GetRelativeDateString(DateTime.UtcNow, commitData.AuthorDate.UtcDateTime) +
" (" + LocalizationHelpers.GetFullDateString(commitData.AuthorDate) + ")");
header.AppendLine(FillToLength(Strings.GetCommitterText() + ":", COMMITHEADER_STRING_LENGTH) +
commitData.Committer);
header.AppendLine(FillToLength(Strings.GetCommitDateText() + ":", COMMITHEADER_STRING_LENGTH) +
LocalizationHelpers.GetRelativeDateString(DateTime.UtcNow, commitData.CommitDate.UtcDateTime) +
" (" + LocalizationHelpers.GetFullDateString(commitData.CommitDate) + ")");
header.Append(FillToLength(Strings.GetCommitHashText() + ":", COMMITHEADER_STRING_LENGTH) +
commitData.Guid);
return RemoveRedundancies(header.ToString());
}
/// <summary>
/// Generate header.
/// </summary>
/// <returns></returns>
public static string GetHeader(this CommitData commitData, bool showRevisionsAsLinks)
{
StringBuilder header = new StringBuilder();
string authorEmail = GetEmail(commitData.Author);
header.AppendLine(
FillToLength(WebUtility.HtmlEncode(Strings.GetAuthorText()) + ":", COMMITHEADER_STRING_LENGTH) +
"<a href='mailto:" + WebUtility.HtmlEncode(authorEmail) + "'>" +
WebUtility.HtmlEncode(commitData.Author) + "</a>");
header.AppendLine(
FillToLength(WebUtility.HtmlEncode(Strings.GetAuthorDateText()) + ":",
COMMITHEADER_STRING_LENGTH) +
WebUtility.HtmlEncode(
LocalizationHelpers.GetRelativeDateString(DateTime.UtcNow, commitData.AuthorDate.UtcDateTime) + " (" +
LocalizationHelpers.GetFullDateString(commitData.AuthorDate) + ")"));
string committerEmail = GetEmail(commitData.Committer);
header.AppendLine(
FillToLength(WebUtility.HtmlEncode(Strings.GetCommitterText()) + ":",
COMMITHEADER_STRING_LENGTH) +
"<a href='mailto:" + WebUtility.HtmlEncode(committerEmail) + "'>" +
WebUtility.HtmlEncode(commitData.Committer) + "</a>");
header.AppendLine(
FillToLength(WebUtility.HtmlEncode(Strings.GetCommitDateText()) + ":",
COMMITHEADER_STRING_LENGTH) +
WebUtility.HtmlEncode(
LocalizationHelpers.GetRelativeDateString(DateTime.UtcNow, commitData.CommitDate.UtcDateTime) + " (" +
LocalizationHelpers.GetFullDateString(commitData.CommitDate) + ")"));
header.Append(
FillToLength(WebUtility.HtmlEncode(Strings.GetCommitHashText()) + ":",
COMMITHEADER_STRING_LENGTH) +
WebUtility.HtmlEncode(commitData.Guid));
if (commitData.ChildrenGuids != null && commitData.ChildrenGuids.Count != 0)
{
header.AppendLine();
string commitsString;
if (showRevisionsAsLinks)
commitsString = commitData.ChildrenGuids.Select(LinkFactory.CreateCommitLink).Join(" ");
else
commitsString = commitData.ChildrenGuids.Select(guid => guid.Substring(0, 10)).Join(" ");
header.Append(FillToLength(WebUtility.HtmlEncode(Strings.GetChildrenText()) + ":",
COMMITHEADER_STRING_LENGTH) + commitsString);
}
var parentGuids = commitData.ParentGuids.Where(s => !String.IsNullOrEmpty(s));
if (parentGuids.Any())
{
header.AppendLine();
string commitsString;
if (showRevisionsAsLinks)
commitsString = parentGuids.Select(LinkFactory.CreateCommitLink).Join(" ");
else
commitsString = parentGuids.Select(guid => guid.Substring(0, 10)).Join(" ");
header.Append(FillToLength(WebUtility.HtmlEncode(Strings.GetParentsText()) + ":",
COMMITHEADER_STRING_LENGTH) + commitsString);
}
return RemoveRedundancies(header.ToString());
}
private static string RemoveRedundancies(string info)
{
string author = GetField(info, Strings.GetAuthorText() + ":");
string committer = GetField(info, Strings.GetCommitterText() + ":");
if (String.Equals(author, committer, StringComparison.CurrentCulture))
{
info = RemoveField(info, Strings.GetCommitterText() + ":");
}
string authorDate = GetField(info, Strings.GetAuthorDateText() + ":");
string commitDate = GetField(info, Strings.GetCommitDateText() + ":");
if (String.Equals(authorDate, commitDate, StringComparison.CurrentCulture))
{
info =
RemoveField(info, Strings.GetCommitDateText() + ":").Replace(
(string) FillToLength(Strings.GetAuthorDateText() + ":", COMMITHEADER_STRING_LENGTH),
FillToLength(Strings.GetDateText() + ":", COMMITHEADER_STRING_LENGTH));
}
return info;
}
private static string FillToLength(string input, int length)
{
return FillToLength(input, length, 0);
}
private static string FillToLength(string input, int length, int skip)
{
// length
const int tabsize = 8;
if ((input.Length - skip) < length)
{
int l = length - (input.Length - skip);
return input + new string('\t', (l / tabsize) + ((l % tabsize) == 0 ? 0 : 1));
}
return input;
}
private static string RemoveField(string data, string header)
{
int headerIndex = data.IndexOf(header);
if (headerIndex == -1)
return data;
int endIndex = data.IndexOf('\n', headerIndex);
if (endIndex == -1)
endIndex = data.Length - 1;
int length = endIndex - headerIndex + 1;
return data.Remove(headerIndex, length);
}
private static string GetField(string data, string header)
{
int valueIndex = IndexOfValue(data, header);
if (valueIndex == -1)
return null;
int length = LengthOfValue(data, valueIndex);
return data.Substring(valueIndex, length);
}
private static int LengthOfValue(string data, int valueIndex)
{
if (valueIndex == -1)
return 0;
int endIndex = data.IndexOf('\n', valueIndex);
if (endIndex == -1)
endIndex = data.Length - 1;
return endIndex - valueIndex;
}
private static int IndexOfValue(string data, string header)
{
int headerIndex = data.IndexOf(header);
if (headerIndex == -1)
return -1;
int valueIndex = headerIndex + header.Length;
while (data[valueIndex] == '\t')
{
valueIndex++;
if (valueIndex == data.Length)
return -1;
}
return valueIndex;
}
}
}