forked from microsoft/PullRequestQuantifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitCreateDiff.cs
More file actions
115 lines (100 loc) · 3.63 KB
/
GitCreateDiff.cs
File metadata and controls
115 lines (100 loc) · 3.63 KB
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
namespace PullRequestQuantifier.GitEngine.Helpers.Diff
{
using System;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using LibGit2Sharp;
using PullRequestQuantifier.Abstractions.Git;
public sealed class GitCreateDiff : IDisposable
{
private readonly IGitEngine gitEngine;
private string repoPath;
private FileSystem fileSystem;
public GitCreateDiff()
{
gitEngine = new GitEngine();
CreateGitRepo();
}
/// <summary>
/// Create a diff git patch format out of the contents,
/// one was the file content before modification and the other the modified content.
/// </summary>
/// <param name="baseContent">Base file content, the unmodified one.</param>
/// <param name="compareContent">The modified file content.</param>
/// <returns>returns a GitFilePatch.</returns>
public GitFilePatch CreateDiff(
string baseContent,
string compareContent)
{
string fileName = Guid.NewGuid().ToString();
fileSystem.File.WriteAllText(
fileSystem.Path.Combine(repoPath, fileName),
baseContent);
Commit();
fileSystem.File.WriteAllText(
fileSystem.Path.Combine(repoPath, fileName),
compareContent);
return gitEngine.GetGitChanges(repoPath).FirstOrDefault(c =>
c.FilePath.Equals(fileName, StringComparison.InvariantCultureIgnoreCase));
}
public void Dispose()
{
DeleteRepoDirectory();
}
private void CreateGitRepo()
{
// Setup directory for git
fileSystem = new FileSystem();
repoPath = fileSystem.Path.Combine(
fileSystem.Path.GetTempPath(),
Guid.NewGuid().ToString());
fileSystem = gitEngine.CreateRepository(repoPath);
}
private void Commit()
{
var repository = new Repository(repoPath);
Commands.Stage(repository, "*");
var author = new Signature("user", "email", DateTimeOffset.Now);
repository.Commit("Adding files", author, author);
}
// This implementation of delete directory is based on the stack overflow
// answer https://stackoverflow.com/questions/1701457/directory-delete-doesnt-work-access-denied-error-but-under-windows-explorer-it.
// Otherwise this runs into access issues during direct deletion sometimes.
private void DeleteRepoDirectory()
{
#pragma warning disable CS0618 // Type or member is obsolete
var dirInfo = fileSystem.DirectoryInfo.New(repoPath);
#pragma warning restore CS0618 // Type or member is obsolete
if (dirInfo.Exists)
{
try
{
SetNormalAttribute(dirInfo);
dirInfo.Delete(true);
}
catch
{
// ignored
}
}
}
private void SetNormalAttribute(IDirectoryInfo dirInfo)
{
if (!dirInfo.Exists)
{
return;
}
var directories = dirInfo.GetDirectories().ToArray();
foreach (var subDir in directories)
{
SetNormalAttribute(subDir);
}
var files = dirInfo.GetFiles().ToArray();
foreach (var file in files)
{
file.Attributes = FileAttributes.Normal;
}
}
}
}