forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommitData.cs
426 lines (348 loc) · 16.7 KB
/
CommitData.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Net;
using System.Diagnostics;
namespace GitCommands
{
public class CommitData
{
private const int COMMITHEADER_STRING_LENGTH = 16;
/// <summary>
/// Private constructor
/// </summary>
private CommitData(string guid,
string treeGuid, ReadOnlyCollection<string> parentGuids,
string author, DateTimeOffset authorDate,
string committer, DateTimeOffset commitDate,
string body)
{
Guid = guid;
TreeGuid = treeGuid;
ParentGuids = parentGuids;
Author = author;
AuthorDate = authorDate;
Committer = committer;
CommitDate = commitDate;
Body = body;
}
public string Guid { get; private set; }
public string TreeGuid { get; private set; }
public ReadOnlyCollection<string> ParentGuids { get; private set; }
public List<string> ChildrenGuids { get; set; }
public string Author { get; private set; }
public DateTimeOffset AuthorDate { get; private set; }
public string Committer { get; private set; }
public DateTimeOffset CommitDate { get; private set; }
public string Body { get; private set; }
/// <summary>
/// Generate header.
/// </summary>
/// <returns></returns>
public string GetHeader()
{
StringBuilder header = new StringBuilder();
string authorEmail = GetEmail(Author);
header.AppendLine(FillToLength(WebUtility.HtmlEncode(Strings.GetAuthorText()) + ":", COMMITHEADER_STRING_LENGTH) +
"<a href='mailto:" + WebUtility.HtmlEncode(authorEmail) + "'>" + WebUtility.HtmlEncode(Author) + "</a>");
header.AppendLine(FillToLength(WebUtility.HtmlEncode(Strings.GetAuthorDateText()) + ":", COMMITHEADER_STRING_LENGTH) +
WebUtility.HtmlEncode(GitCommandHelpers.GetRelativeDateString(DateTime.UtcNow, AuthorDate.UtcDateTime) + " (" + AuthorDate.LocalDateTime.ToString("ddd MMM dd HH':'mm':'ss yyyy")) + ")");
string committerEmail = GetEmail(Committer);
header.AppendLine(FillToLength(WebUtility.HtmlEncode(Strings.GetCommitterText()) + ":", COMMITHEADER_STRING_LENGTH) +
"<a href='mailto:" + WebUtility.HtmlEncode(committerEmail) + "'>" + WebUtility.HtmlEncode(Committer) + "</a>");
header.AppendLine(FillToLength(WebUtility.HtmlEncode(Strings.GetCommitDateText()) + ":", COMMITHEADER_STRING_LENGTH) +
WebUtility.HtmlEncode(GitCommandHelpers.GetRelativeDateString(DateTime.UtcNow, CommitDate.UtcDateTime) + " (" + CommitDate.LocalDateTime.ToString("ddd MMM dd HH':'mm':'ss yyyy")) + ")");
header.Append(FillToLength(WebUtility.HtmlEncode(Strings.GetCommitHashText()) + ":", COMMITHEADER_STRING_LENGTH) +
WebUtility.HtmlEncode(Guid));
if (ChildrenGuids != null && ChildrenGuids.Count != 0)
{
header.AppendLine();
var commitsString = ChildrenGuids.Select(LinkFactory.CreateCommitLink).Join(" ");
header.Append(FillToLength(WebUtility.HtmlEncode(Strings.GetChildrenText()) + ":",
COMMITHEADER_STRING_LENGTH) + commitsString);
}
var parentGuids = ParentGuids.Where(s => !string.IsNullOrEmpty(s));
if (parentGuids.Any())
{
header.AppendLine();
var commitsString = parentGuids.Select(LinkFactory.CreateCommitLink).Join(" ");
header.Append(FillToLength(WebUtility.HtmlEncode(Strings.GetParentsText()) + ":",
COMMITHEADER_STRING_LENGTH) + commitsString);
}
return RemoveRedundancies(header.ToString());
}
/// <summary>
/// Returns an array of strings containg titles of fields field returned by GetHeader.
/// Used to calculate layout in advance
/// </summary>
/// <returns></returns>
public static string[] GetPossibleHeaders()
{
return new string[] {Strings.GetAuthorText(), Strings.GetAuthorDateText(), Strings.GetCommitterText(),
Strings.GetCommitDateText(), Strings.GetCommitHashText(), Strings.GetChildrenText(),
Strings.GetParentsText()};
}
/// <summary>
/// Generate header.
/// </summary>
/// <returns></returns>
public string GetHeaderPlain()
{
StringBuilder header = new StringBuilder();
header.AppendLine(FillToLength(Strings.GetAuthorText() + ":", COMMITHEADER_STRING_LENGTH) + Author);
header.AppendLine(FillToLength(Strings.GetAuthorDateText() + ":", COMMITHEADER_STRING_LENGTH) +
GitCommandHelpers.GetRelativeDateString(DateTime.UtcNow, AuthorDate.UtcDateTime) + " (" + AuthorDate.LocalDateTime.ToString("ddd MMM dd HH':'mm':'ss yyyy") + ")");
header.AppendLine(FillToLength(Strings.GetCommitterText() + ":", COMMITHEADER_STRING_LENGTH) +
Committer);
header.AppendLine(FillToLength(Strings.GetCommitDateText() + ":", COMMITHEADER_STRING_LENGTH) +
GitCommandHelpers.GetRelativeDateString(DateTime.UtcNow, CommitDate.UtcDateTime) + " (" + CommitDate.LocalDateTime.ToString("ddd MMM dd HH':'mm':'ss yyyy") + ")");
header.Append(FillToLength(Strings.GetCommitHashText() + ":", COMMITHEADER_STRING_LENGTH) +
Guid);
return RemoveRedundancies(header.ToString());
}
private static string GetEmail(string author)
{
if (String.IsNullOrEmpty(author))
return "";
int ind = author.IndexOf("<") + 1;
if (ind == -1)
return "";
return author.Substring(ind, author.LastIndexOf(">") - ind);
}
/// <summary>
/// Gets the commit info for submodule.
/// </summary>
public static void UpdateCommitMessage(CommitData data, GitModule module, string sha1, ref string error)
{
if (module == null)
throw new ArgumentNullException("module");
if (sha1 == null)
throw new ArgumentNullException("sha1");
//Do not cache this command, since notes can be added
string arguments = string.Format(CultureInfo.InvariantCulture,
"log -1 --pretty=\"format:" + ShortLogFormat + "\" {0}", sha1);
var info =
module.RunCmd(
Settings.GitCommand,
arguments,
GitModule.LosslessEncoding
);
if (info.Trim().StartsWith("fatal"))
{
error = "Cannot find commit " + sha1;
return;
}
int index = info.IndexOf(sha1) + sha1.Length;
if (index < 0)
{
error = "Cannot find commit " + sha1;
return;
}
if (index >= info.Length)
{
error = info;
return;
}
UpdateBodyInCommitData(data, info, module);
}
/// <summary>
/// Gets the commit info for submodule.
/// </summary>
public static CommitData GetCommitData(GitModule module, string sha1, ref string error)
{
if (module == null)
throw new ArgumentNullException("module");
if (sha1 == null)
throw new ArgumentNullException("sha1");
//Do not cache this command, since notes can be added
string arguments = string.Format(CultureInfo.InvariantCulture,
"log -1 --pretty=\"format:"+LogFormat+"\" {0}", sha1);
var info =
module.RunCmd(
Settings.GitCommand,
arguments,
GitModule.LosslessEncoding
);
if (info.Trim().StartsWith("fatal"))
{
error = "Cannot find commit " + sha1;
return null;
}
int index = info.IndexOf(sha1) + sha1.Length;
if (index < 0)
{
error = "Cannot find commit " + sha1;
return null;
}
if (index >= info.Length)
{
error = info;
return null;
}
CommitData commitInformation = CreateFromFormatedData(info, module);
return commitInformation;
}
public const string LogFormat = "%H%n%T%n%P%n%aN <%aE>%n%at%n%cN <%cE>%n%ct%n%e%n%B%nNotes:%n%-N";
/// <summary>
/// Creates a CommitData object from formated commit info data from git. The string passed in should be
/// exact output of a log or show command using --format=LogFormat.
/// </summary>
/// <param name="data">Formated commit data from git.</param>
/// <returns>CommitData object populated with parsed info from git string.</returns>
public static CommitData CreateFromFormatedData(string data, GitModule aModule)
{
if (data == null)
throw new ArgumentNullException("Data");
var lines = data.Split('\n');
var guid = lines[0];
// TODO: we can use this to add more relationship info like gitk does if wanted
var treeGuid = lines[1];
// TODO: we can use this to add more relationship info like gitk does if wanted
string[] parentLines = lines[2].Split(new char[]{' '});
ReadOnlyCollection<string> parentGuids = parentLines.ToList().AsReadOnly();
var author = aModule.ReEncodeStringFromLossless(lines[3]);
var authorDate = DateTimeUtils.ParseUnixTime(lines[4]);
var committer = aModule.ReEncodeStringFromLossless(lines[5]);
var commitDate = DateTimeUtils.ParseUnixTime(lines[6]);
string commitEncoding = lines[7];
const int startIndex = 8;
string message = ProccessDiffNotes(startIndex, lines);
//commit message is not reencoded by git when format is given
var body = aModule.ReEncodeCommitMessage(message, commitEncoding);
var commitInformation = new CommitData(guid, treeGuid, parentGuids, author, authorDate,
committer, commitDate, body);
return commitInformation;
}
public const string ShortLogFormat = "%H%n%e%n%B%nNotes:%n%-N";
/// <summary>
/// Creates a CommitData object from formated commit info data from git. The string passed in should be
/// exact output of a log or show command using --format=LogFormat.
/// </summary>
/// <param name="data">Formated commit data from git.</param>
/// <returns>CommitData object populated with parsed info from git string.</returns>
public static void UpdateBodyInCommitData(CommitData commitData, string data, GitModule aModule)
{
if (data == null)
throw new ArgumentNullException("Data");
var lines = data.Split('\n');
var guid = lines[0];
string commitEncoding = lines[1];
const int startIndex = 2;
string message = ProccessDiffNotes(startIndex, lines);
//commit message is not reencoded by git when format is given
Debug.Assert(commitData.Guid == guid);
commitData.Body = aModule.ReEncodeCommitMessage(message, commitEncoding);
}
private static string ProccessDiffNotes(int startIndex, string[] lines)
{
int endIndex = lines.Length - 1;
if (lines[endIndex] == "Notes:")
endIndex--;
var message = new StringBuilder();
bool bNotesStart = false;
for (int i = startIndex; i <= endIndex; i++)
{
string line = lines[i];
if (bNotesStart)
line = " " + line;
message.AppendLine(line);
if (lines[i] == "Notes:")
bNotesStart = true;
}
return message.ToString();
}
/// <summary>
/// Creates a CommitData object from Git revision.
/// </summary>
/// <param name="revision">Git commit.</param>
/// <returns>CommitData object populated with parsed info from git string.</returns>
public static CommitData CreateFromRevision(GitRevision revision)
{
if (revision == null)
throw new ArgumentNullException("revision");
CommitData data = new CommitData(revision.Guid, revision.TreeGuid, revision.ParentGuids.ToList().AsReadOnly(),
String.Format("{0} <{1}>", revision.Author, revision.AuthorEmail), revision.AuthorDate,
String.Format("{0} <{1}>", revision.Committer, revision.CommitterEmail), revision.CommitDate,
revision.Message);
return data;
}
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 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(
FillToLength(Strings.GetAuthorDateText() + ":", COMMITHEADER_STRING_LENGTH), FillToLength(Strings.GetDateText() + ":", COMMITHEADER_STRING_LENGTH));
}
return info;
}
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;
}
}
}