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

Commit c132224

Browse files
Merge pull request #550 from github-for-unity/enhancements/md5-folder
Adding functionality to md5 an entire folder
2 parents 434d1d1 + 3cf8660 commit c132224

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

src/GitHub.Api/Extensions/FileSystemExtensions.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
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 CalculateFileMD5(this IFileSystem fileSystem, string file)
1012
{
1113
byte[] computeHash;
1214
using (var md5 = MD5.Create())
@@ -17,7 +19,39 @@ public static string CalculateMD5(this IFileSystem fileSystem, string file)
1719
}
1820
}
1921

20-
return BitConverter.ToString(computeHash).Replace("-", string.Empty);
22+
return BitConverter.ToString(computeHash).Replace("-", string.Empty).ToLower();
23+
}
24+
25+
public static string CalculateFolderMD5(this IFileSystem fileSystem, string path, bool includeContents = true)
26+
{
27+
//https://stackoverflow.com/questions/3625658/creating-hash-for-folder
28+
29+
var filePaths = fileSystem.GetFiles(path, "*", SearchOption.AllDirectories)
30+
.OrderBy(p => p)
31+
.ToArray();
32+
33+
using (var md5 = MD5.Create())
34+
{
35+
foreach (var filePath in filePaths)
36+
{
37+
// hash path
38+
var relativeFilePath = filePath.Substring(path.Length + 1);
39+
var pathBytes = Encoding.UTF8.GetBytes(relativeFilePath);
40+
md5.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);
41+
42+
if (includeContents)
43+
{
44+
// hash contents
45+
var contentBytes = File.ReadAllBytes(filePath);
46+
md5.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes, 0);
47+
}
48+
}
49+
50+
//Handles empty filePaths case
51+
md5.TransformFinalBlock(new byte[0], 0, 0);
52+
53+
return BitConverter.ToString(md5.Hash).Replace("-", "").ToLower();
54+
}
2155
}
2256
}
2357
}

src/GitHub.Api/IO/IFileSystem.cs

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

src/GitHub.Api/Installer/GitInstaller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public bool IsGitLfsExtracted()
9090
return false;
9191
}
9292

93-
var calculateMd5 = environment.FileSystem.CalculateMD5(GitLfsExecutablePath);
93+
var calculateMd5 = environment.FileSystem.CalculateFileMD5(GitLfsExecutablePath);
9494
logger.Trace("GitLFS MD5: {0}", calculateMd5);
9595
var md5 = environment.IsWindows ? WindowsGitLfsExecutableMD5 : MacGitLfsExecutableMD5;
9696
if (String.Compare(calculateMd5, md5, true) != 0)

src/tests/IntegrationTests/Git/GitSetupTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task InstallGit()
4141
gitLfsDestinationPath = gitLfsDestinationPath.Combine("libexec", "git-core", "git-lfs.exe");
4242
gitLfsDestinationPath.FileExists().Should().BeTrue();
4343

44-
var calculateMd5 = NPath.FileSystem.CalculateMD5(gitLfsDestinationPath);
44+
var calculateMd5 = NPath.FileSystem.CalculateFileMD5(gitLfsDestinationPath);
4545
Assert.IsTrue(string.Compare(calculateMd5, GitInstaller.WindowsGitLfsExecutableMD5, true) == 0);
4646

4747
setupDone = await gitSetup.SetupIfNeeded(new Progress<float>(x => percent = x));
@@ -83,7 +83,7 @@ public void VerifyWindowsGitLfsBundle()
8383

8484
gitLfsPath.Exists().Should().BeTrue();
8585

86-
var calculateMd5 = NPath.FileSystem.CalculateMD5(gitLfsPath);
86+
var calculateMd5 = NPath.FileSystem.CalculateFileMD5(gitLfsPath);
8787
calculateMd5.ToLower().Should().Be(GitInstaller.WindowsGitLfsExecutableMD5.ToLower());
8888
}
8989

@@ -103,7 +103,7 @@ public void VerifyMacGitLfsBundle()
103103

104104
gitLfsPath.Exists().Should().BeTrue();
105105

106-
var calculateMd5 = NPath.FileSystem.CalculateMD5(gitLfsPath);
106+
var calculateMd5 = NPath.FileSystem.CalculateFileMD5(gitLfsPath);
107107
calculateMd5.ToLower().Should().Be(GitInstaller.MacGitLfsExecutableMD5.ToLower());
108108
}
109109
}

0 commit comments

Comments
 (0)