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

Commit 4971e3e

Browse files
Merge branch 'enhancements/async-git-setup' into enhancements/window-loading-view
2 parents f627aa5 + b508a6d commit 4971e3e

File tree

3 files changed

+106
-60
lines changed

3 files changed

+106
-60
lines changed

src/GitHub.Api/Extensions/FileSystemExtensions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static string CalculateFileMD5(this IFileSystem fileSystem, string file)
3737
return BitConverter.ToString(computeHash).Replace("-", string.Empty).ToLower();
3838
}
3939

40-
public static string CalculateFolderMD5(this IFileSystem fileSystem, string path)
40+
public static string CalculateFolderMD5(this IFileSystem fileSystem, string path, bool includeContents = true)
4141
{
4242
//https://stackoverflow.com/questions/3625658/creating-hash-for-folder
4343

@@ -56,9 +56,12 @@ public static string CalculateFolderMD5(this IFileSystem fileSystem, string path
5656
var pathBytes = Encoding.UTF8.GetBytes(relativeFilePath);
5757
md5.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);
5858

59-
// hash contents
60-
var contentBytes = File.ReadAllBytes(filePath);
61-
md5.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes, 0);
59+
if (includeContents)
60+
{
61+
// hash contents
62+
var contentBytes = File.ReadAllBytes(filePath);
63+
md5.TransformBlock(contentBytes, 0, contentBytes.Length, contentBytes, 0);
64+
}
6265
}
6366

6467
//Handles empty filePaths case

src/GitHub.Api/Installer/PortableGitInstallTask.cs

Lines changed: 97 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ class PortableGitInstallDetails
1212
public NPath GitLfsExecPath { get; }
1313

1414
public const string ExtractedMD5 = "65fd0575d3b47d8207b9e19d02faca4f";
15+
public const string FileListMD5 = "a152a216b2e76f6c127053251187a278";
1516

16-
private const string ExpectedVersion = "f02737a78695063deace08e96d5042710d3e32db";
17+
private const string PackageVersion = "f02737a78695063deace08e96d5042710d3e32db";
1718
private const string PackageName = "PortableGit";
18-
private const string PackageNameWithVersion = PackageName + "_" + ExpectedVersion;
19+
private const string PackageNameWithVersion = PackageName + "_" + PackageVersion;
20+
21+
private readonly bool onWindows;
1922

2023
public PortableGitInstallDetails(NPath targetInstallPath, bool onWindows)
2124
{
25+
this.onWindows = onWindows;
2226
var gitInstallPath = targetInstallPath.Combine(ApplicationInfo.ApplicationName, PackageNameWithVersion);
2327
GitInstallPath = gitInstallPath;
2428

@@ -28,16 +32,23 @@ public PortableGitInstallDetails(NPath targetInstallPath, bool onWindows)
2832
GitLfsExec += "git-lfs.exe";
2933

3034
GitExecPath = gitInstallPath.Combine("cmd", GitExec);
31-
GitLfsExecPath = gitInstallPath.Combine("mingw32", "libexec", "git-core", GitLfsExec);
3235
}
3336
else
3437
{
3538
GitExec = "git";
3639
GitLfsExec = "git-lfs";
3740

3841
GitExecPath = gitInstallPath.Combine("bin", GitExec);
39-
GitLfsExecPath = gitInstallPath.Combine("libexec", "git-core", GitLfsExec);
4042
}
43+
44+
GitLfsExecPath = GetGitLfsExecPath(gitInstallPath);
45+
}
46+
47+
public NPath GetGitLfsExecPath(NPath gitInstallRoot)
48+
{
49+
return onWindows
50+
? gitInstallRoot.Combine("mingw32", "libexec", "git-core", GitLfsExec)
51+
: gitInstallRoot.Combine("libexec", "git-core", GitLfsExec);
4152
}
4253
}
4354

@@ -64,20 +75,74 @@ protected override NPath RunWithReturn(bool success)
6475
return installDetails.GitExecPath;
6576
}
6677

67-
var installGit = InstallGit();
68-
if (installGit)
78+
Token.ThrowIfCancellationRequested();
79+
80+
installDetails.GitInstallPath.DeleteIfExists();
81+
installDetails.GitInstallPath.EnsureParentDirectoryExists();
82+
83+
Token.ThrowIfCancellationRequested();
84+
85+
var extractTarget = NPath.CreateTempDirectory("git_install_task");
86+
var installGit = InstallGit(extractTarget);
87+
if (!installGit)
88+
{
89+
Logger.Warning("Failed PortableGitInstallTask");
90+
return null;
91+
}
92+
93+
Token.ThrowIfCancellationRequested();
94+
95+
var installGitLfs = InstallGitLfs(extractTarget);
96+
if (!installGitLfs)
97+
{
98+
Logger.Warning("Failed PortableGitInstallTask");
99+
return null;
100+
}
101+
102+
Token.ThrowIfCancellationRequested();
103+
104+
var extractedMD5 = environment.FileSystem.CalculateFolderMD5(extractTarget);
105+
if (!extractedMD5.Equals(PortableGitInstallDetails.ExtractedMD5, StringComparison.InvariantCultureIgnoreCase))
69106
{
70-
var installGitLfs = InstallGitLfs();
71-
if (installGitLfs)
72-
{
73-
Logger.Trace("Completed PortableGitInstallTask");
74-
return installDetails.GitExecPath;
75-
}
107+
Logger.Warning("MD5 {0} does not match expected {1}", extractedMD5, PortableGitInstallDetails.ExtractedMD5);
108+
Logger.Warning("Failed PortableGitInstallTask");
109+
return null;
76110
}
77111

78-
Logger.Warning("Unsuccessful PortableGitInstallTask");
112+
var moveSuccessful = MoveExtractTarget(extractTarget);
113+
if (!moveSuccessful)
114+
{
115+
Logger.Warning("Failed PortableGitInstallTask");
116+
return null;
117+
}
79118

80-
return null;
119+
Logger.Trace("Completed PortableGitInstallTask");
120+
return installDetails.GitExecPath;
121+
}
122+
123+
private bool MoveExtractTarget(NPath extractTarget)
124+
{
125+
try
126+
{
127+
Logger.Trace("Moving tempDirectory:\"{0}\" to extractTarget:\"{1}\"", extractTarget,
128+
installDetails.GitInstallPath);
129+
130+
extractTarget.Move(installDetails.GitInstallPath);
131+
132+
Logger.Trace("Deleting extractTarget:\"{0}\"", extractTarget);
133+
extractTarget.DeleteIfExists();
134+
135+
Logger.Trace("Completed PortableGitInstallTask");
136+
}
137+
catch (Exception ex)
138+
{
139+
Logger.Warning(ex, "Error Moving tempDirectory:\"{0}\" to extractTarget:\"{1}\"", extractTarget,
140+
installDetails.GitInstallPath);
141+
142+
return false;
143+
}
144+
145+
return true;
81146
}
82147

83148
private bool IsPortableGitExtracted()
@@ -88,22 +153,22 @@ private bool IsPortableGitExtracted()
88153
return false;
89154
}
90155

91-
var installMD5 = environment.FileSystem.CalculateFolderMD5(installDetails.GitInstallPath);
92-
if (!installMD5.Equals(PortableGitInstallDetails.ExtractedMD5, StringComparison.InvariantCultureIgnoreCase))
156+
var fileListMD5 = environment.FileSystem.CalculateFolderMD5(installDetails.GitInstallPath, false);
157+
if (!fileListMD5.Equals(PortableGitInstallDetails.FileListMD5, StringComparison.InvariantCultureIgnoreCase))
93158
{
94-
Logger.Trace("MD5 {0} does not match expected {1}", installMD5, PortableGitInstallDetails.ExtractedMD5);
159+
Logger.Trace("MD5 {0} does not match expected {1}", fileListMD5, PortableGitInstallDetails.FileListMD5);
95160
return false;
96161
}
97162

98163
Logger.Trace("Git Present");
99164
return true;
100165
}
101166

102-
private bool InstallGit()
167+
private bool InstallGit(NPath targetPath)
103168
{
104169
Logger.Trace("InstallGit");
105170

106-
var tempPath = NPath.GetTempFilename();
171+
var tempPath = NPath.CreateTempDirectory("git_zip_path");
107172
var gitArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", tempPath, environment);
108173

109174
if (!environment.FileSystem.FileExists(gitArchivePath))
@@ -115,54 +180,31 @@ private bool InstallGit()
115180

116181
Token.ThrowIfCancellationRequested();
117182

118-
var tempDirectory = NPath.CreateTempDirectory("git_install_task");
119-
120183
try
121184
{
122-
Logger.Trace("Extracting gitArchivePath:\"{0}\" tempDirectory:\"{1}\"",
123-
gitArchivePath, tempDirectory);
185+
Logger.Trace("Extracting gitArchivePath:\"{0}\" targetPath:\"{1}\"",
186+
gitArchivePath, targetPath);
124187

125-
ZipHelper.ExtractZipFile(gitArchivePath, tempDirectory, Token);
188+
ZipHelper.ExtractZipFile(gitArchivePath, targetPath, Token);
126189
}
127190
catch (Exception ex)
128191
{
129192
Logger.Warning(ex, "Error Extracting gitArchivePath:\"{0}\" tempDirectory:\"{1}\"",
130-
gitArchivePath, tempDirectory);
193+
gitArchivePath, targetPath);
131194

132195
return false;
133196
}
134197

135-
Token.ThrowIfCancellationRequested();
136-
137-
try
138-
{
139-
installDetails.GitInstallPath.DeleteIfExists();
140-
installDetails.GitInstallPath.EnsureParentDirectoryExists();
141-
142-
Logger.Trace("Moving tempDirectory:\"{0}\" to gitInstallPath:\"{1}\"",
143-
tempDirectory, installDetails.GitInstallPath);
144-
145-
tempDirectory.Move(installDetails.GitInstallPath);
146-
}
147-
catch (Exception ex)
148-
{
149-
Logger.Warning(ex, "Error Moving tempDirectory:\"{0}\" to gitInstallPath:\"{1}\"",
150-
tempDirectory, installDetails.GitInstallPath);
151-
152-
return false;
153-
}
154-
155-
Logger.Trace("Deleting tempDirectory:\"{0}\"", tempDirectory);
156-
tempDirectory.DeleteIfExists();
198+
tempPath.DeleteIfExists();
157199

158200
return true;
159201
}
160202

161-
private bool InstallGitLfs()
203+
private bool InstallGitLfs(NPath targetPath)
162204
{
163205
Logger.Trace("InstallGitLfs");
164206

165-
var tempPath = NPath.GetTempFilename();
207+
var tempPath = NPath.CreateTempDirectory("git_lfs_zip_path");
166208
var gitLfsArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git-lfs.zip", tempPath, environment);
167209

168210
if (!environment.FileSystem.FileExists(gitLfsArchivePath))
@@ -173,7 +215,7 @@ private bool InstallGitLfs()
173215

174216
Token.ThrowIfCancellationRequested();
175217

176-
var tempDirectory = NPath.CreateTempDirectory("git_install_task");
218+
var tempDirectory = NPath.CreateTempDirectory("git_lfs_extract_path");
177219

178220
try
179221
{
@@ -182,22 +224,23 @@ private bool InstallGitLfs()
182224
}
183225
catch (Exception ex)
184226
{
185-
Logger.Warning($"Error Extracting gitLfsArchivePath:\"{gitLfsArchivePath}\" tempDirectory:\"{tempDirectory}\"", ex);
227+
Logger.Warning(ex, $"Error Extracting gitLfsArchivePath:\"{gitLfsArchivePath}\" tempDirectory:\"{tempDirectory}\"");
186228
return false;
187229
}
188230

189231
Token.ThrowIfCancellationRequested();
190232

191233
var tempDirectoryGitLfsExec = tempDirectory.Combine(installDetails.GitLfsExec);
192234

235+
var targetLfsExecPath = installDetails.GetGitLfsExecPath(targetPath);
193236
try
194237
{
195-
Logger.Trace("Moving tempDirectoryGitLfsExec:\"{0}\" to gitLfsExecFullPath:\"{1}\"", tempDirectoryGitLfsExec, installDetails.GitLfsExecPath);
196-
tempDirectoryGitLfsExec.Move(installDetails.GitLfsExecPath);
238+
Logger.Trace("Moving tempDirectoryGitLfsExec:\"{0}\" to targetLfsExecPath:\"{1}\"", tempDirectoryGitLfsExec, targetLfsExecPath);
239+
tempDirectoryGitLfsExec.Move(targetLfsExecPath);
197240
}
198241
catch (Exception ex)
199242
{
200-
Logger.Warning($"Error Moving tempDirectoryGitLfsExec:\"{tempDirectoryGitLfsExec}\" to gitLfsExecFullPath:\"{installDetails.GitLfsExecPath}\"", ex);
243+
Logger.Warning(ex, $"Error Moving tempDirectoryGitLfsExec:\"{tempDirectoryGitLfsExec}\" to targetLfsExecPath:\"{targetLfsExecPath}\"");
201244
return false;
202245
}
203246

src/tests/IntegrationTests/Installer/PortableGitInstallTaskTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public void GitInstallTest()
2424

2525
gitInstallTask.Start().Wait();
2626

27-
var calculateFolderMd5 = Environment.FileSystem.CalculateFolderMD5(gitInstallDetails.GitInstallPath);
28-
calculateFolderMd5.Should().Be(PortableGitInstallDetails.ExtractedMD5);
27+
Environment.FileSystem.CalculateFolderMD5(gitInstallDetails.GitInstallPath).Should().Be(PortableGitInstallDetails.ExtractedMD5);
28+
Environment.FileSystem.CalculateFolderMD5(gitInstallDetails.GitInstallPath, false).Should().Be(PortableGitInstallDetails.FileListMD5);
2929

3030
new PortableGitInstallTask(CancellationToken.None, Environment, gitInstallDetails)
3131
.Then(new PortableGitInstallTask(CancellationToken.None, Environment, gitInstallDetails))

0 commit comments

Comments
 (0)