-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathDiffInfo.cs
91 lines (76 loc) · 2.59 KB
/
DiffInfo.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
//-----------------------------------------------------------------------
// <copyright file="DiffInfo.cs" company="(none)">
// Copyright © 2013 John Gietzen and the WebGit .NET Authors. All rights reserved.
// </copyright>
// <author>John Gietzen</author>
//-----------------------------------------------------------------------
namespace WebGitNet
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class DiffInfo
{
private readonly bool added;
private readonly bool copied;
private readonly bool deleted;
private readonly string destinationFile;
private readonly IList<string> headers;
private readonly IList<string> lines;
private readonly bool renamed;
private readonly string sourceFile;
public DiffInfo(IList<string> lines)
{
if (lines == null)
{
throw new ArgumentNullException("lines");
}
Func<string, bool> isHeader = l => !l.StartsWith("@") && !l.StartsWith("Binary files");
this.headers = lines.TakeWhile(isHeader).ToList().AsReadOnly();
this.lines = lines.Skip(this.headers.Count).ToList().AsReadOnly();
var match = Regex.Match(this.headers[0], "^diff --git a:/(?<src>.*?) b:/(?<dst>.*)$");
if (match.Success)
{
this.sourceFile = match.Groups["src"].Value;
this.destinationFile = match.Groups["dst"].Value;
}
this.renamed = this.headers.Any(h => h.StartsWith("rename from"));
this.copied = this.headers.Any(h => h.StartsWith("copy from"));
this.added = this.headers.Any(h => h.StartsWith("new file"));
this.deleted = this.headers.Any(h => h.StartsWith("deleted file"));
}
public bool Added
{
get { return this.added; }
}
public bool Copied
{
get { return this.copied; }
}
public bool Deleted
{
get { return this.deleted; }
}
public string DestinationFile
{
get { return this.destinationFile; }
}
public IList<string> Headers
{
get { return this.headers; }
}
public IList<string> Lines
{
get { return this.lines; }
}
public bool Renamed
{
get { return this.renamed; }
}
public string SourceFile
{
get { return this.sourceFile; }
}
}
}