This repository was archived by the owner on Feb 3, 2023. It is now read-only.
forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathFileExportFilter.cs
More file actions
97 lines (80 loc) · 2.93 KB
/
FileExportFilter.cs
File metadata and controls
97 lines (80 loc) · 2.93 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
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace LibGit2Sharp.Tests.TestHelpers
{
class FileExportFilter : Filter
{
public int CleanCalledCount = 0;
public int CompleteCalledCount = 0;
public int SmudgeCalledCount = 0;
public readonly HashSet<string> FilesFiltered;
private bool clean;
public FileExportFilter(string name, IEnumerable<FilterAttributeEntry> attributes)
: base(name, attributes)
{
FilesFiltered = new HashSet<string>();
}
protected override void Create(string path, string root, FilterMode mode)
{
if (mode == FilterMode.Clean)
{
string filename = Path.GetFileName(path);
string cachePath = Path.Combine(root, ".git", filename);
if (File.Exists(cachePath))
{
File.Delete(cachePath);
}
}
}
protected override void Clean(string path, string root, Stream input, Stream output)
{
CleanCalledCount++;
string filename = Path.GetFileName(path);
string cachePath = Path.Combine(root, ".git", filename);
using (var file = File.Exists(cachePath) ? File.Open(cachePath, FileMode.Append, FileAccess.Write, FileShare.None) : File.Create(cachePath))
{
input.CopyTo(file);
}
clean = true;
}
protected override void Complete(string path, string root, Stream output)
{
CompleteCalledCount++;
string filename = Path.GetFileName(path);
string cachePath = Path.Combine(root, ".git", filename);
if (clean)
{
byte[] bytes = Encoding.UTF8.GetBytes(path);
output.Write(bytes, 0, bytes.Length);
FilesFiltered.Add(path);
}
else
{
if (File.Exists(cachePath))
{
using (var file = File.Open(cachePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None))
{
file.CopyTo(output);
}
}
}
}
protected override void Smudge(string path, string root, Stream input, Stream output)
{
SmudgeCalledCount++;
string filename = Path.GetFileName(path);
StringBuilder text = new StringBuilder();
byte[] buffer = new byte[64 * 1024];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
string decoded = Encoding.UTF8.GetString(buffer, 0, read);
text.Append(decoded);
}
if (!FilesFiltered.Contains(text.ToString()))
throw new FileNotFoundException();
clean = false;
}
}
}