Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 81a4071

Browse files
Merge branch 'enhancements/md5-folder' into enhancements/async-git-setup
2 parents dde152e + 934d2c2 commit 81a4071

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

GitHub.Unity.sln.DotSettings

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@
335335
</TypePattern>
336336
&lt;/Patterns&gt;</s:String>
337337
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=ID/@EntryIndexedValue">ID</s:String>
338+
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=MD/@EntryIndexedValue">ME</s:String>
338339
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=SSH/@EntryIndexedValue">SSH</s:String>
339340
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
340341
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>

src/GitHub.Api/Extensions/FileSystemExtensions.cs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
using System;
2+
using System.IO;
3+
using System.Linq;
24
using System.Security.Cryptography;
35
using System.Text;
46

57
namespace GitHub.Unity
68
{
79
static class FileSystemExtensions
810
{
9-
public static string CalculateMD5(this IFileSystem fileSystem, string file)
11+
public static string CalculateMD5(this IFileSystem fileSystem, string path)
12+
{
13+
if (fileSystem.DirectoryExists(path))
14+
{
15+
return fileSystem.CalculateFolderMD5(path);
16+
}
17+
18+
if (fileSystem.FileExists(path))
19+
{
20+
return fileSystem.CalculateFileMD5(path);
21+
}
22+
23+
throw new ArgumentException($@"Path does not exist: ""{path}""");
24+
}
25+
26+
public static string CalculateFileMD5(this IFileSystem fileSystem, string file)
1027
{
1128
byte[] computeHash;
1229
using (var md5 = MD5.Create())
@@ -17,7 +34,36 @@ public static string CalculateMD5(this IFileSystem fileSystem, string file)
1734
}
1835
}
1936
20-
return BitConverter.ToString(computeHash).Replace("-", string.Empty);
37+
return BitConverter.ToString(computeHash).Replace("-", string.Empty).ToLower();
38+
}
39+
40+
public static string CalculateFolderMD5(this IFileSystem fileSystem, string path)
41+
{
42+
//https://stackoverflow.com/questions/3625658/creating-hash-for-folder
43+
44+
var filePaths = fileSystem.GetFiles(path, "*", SearchOption.AllDirectories)
45+
.OrderBy(p => p)
46+
.ToArray();
47+
48+
using (var md5 = MD5.Create())
49+
{
50+
foreach (var filePath in filePaths)
51+
{
52+
// hash path
53+
var relativeFilePath = filePath.Substring(path.Length + 1);
54+
var pathBytes = Encoding.UTF8.GetBytes(relativeFilePath);
55+
md5.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);
56+
57+
// hash contents
58+
var contentBytes = File.ReadAllBytes(filePath);
59+
md5.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes, 0);
60+
}
61+
62+
//Handles empty filePaths case
63+
md5.TransformFinalBlock(new byte[0], 0, 0);
64+
65+
return BitConverter.ToString(md5.Hash).Replace("-", "").ToLower();
66+
}
2167
}
2268
}
2369
}

src/GitHub.Api/IO/FileSystem.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ public void WriteAllText(string path, string contents, Encoding encoding)
167167
File.WriteAllText(path, contents, encoding);
168168
}
169169

170+
public byte[] ReadAllBytes(string path)
171+
{
172+
return File.ReadAllBytes(path);
173+
}
174+
170175
public string ReadAllText(string path)
171176
{
172177
return File.ReadAllText(path);

src/GitHub.Api/IO/IFileSystem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public interface IFileSystem
3434
void WriteAllText(string path, string contents);
3535
void WriteAllText(string path, string contents, Encoding encoding);
3636
void WriteAllLines(string path, string[] contents);
37+
byte[] ReadAllBytes(string path);
3738
string ReadAllText(string path);
3839
string ReadAllText(string path, Encoding encoding);
3940
Stream OpenRead(string path);

0 commit comments

Comments
 (0)