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

Commit 7f26b87

Browse files
authored
Merge pull request #585 from github-for-unity/fixes/fix-download-location
Download portable git zip files to a known location
2 parents 46a2230 + eb3c65c commit 7f26b87

File tree

4 files changed

+53
-27
lines changed

4 files changed

+53
-27
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public void Run(bool firstRun)
7272
}
7373
});
7474

75-
var applicationDataPath = Environment.GetSpecialFolder(System.Environment.SpecialFolder.LocalApplicationData).ToNPath();
76-
var installDetails = new GitInstallDetails(applicationDataPath, true);
75+
var installDetails = new GitInstallDetails(Environment.UserCachePath, true);
7776
var gitInstaller = new GitInstaller(Environment, CancellationToken, installDetails);
7877

7978
// if successful, continue with environment initialization, otherwise try to find an existing git installation

src/GitHub.Api/Installer/GitInstaller.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@ class GitInstallDetails
2525

2626
private readonly bool onWindows;
2727

28-
public GitInstallDetails(NPath targetInstallPath, bool onWindows)
28+
public GitInstallDetails(NPath pluginDataPath, bool onWindows)
2929
{
3030
this.onWindows = onWindows;
31-
var gitInstallPath = targetInstallPath.Combine(ApplicationInfo.ApplicationName, PackageNameWithVersion);
31+
32+
PluginDataPath = pluginDataPath;
33+
34+
var gitInstallPath = PluginDataPath.Combine(PackageNameWithVersion);
3235
GitInstallationPath = gitInstallPath;
3336

3437
if (onWindows)
@@ -56,6 +59,7 @@ public NPath GetGitLfsExecutablePath(NPath gitInstallRoot)
5659
: gitInstallRoot.Combine("libexec", "git-core", GitLfsExecutable);
5760
}
5861

62+
public NPath PluginDataPath { get; }
5963
public NPath GitInstallationPath { get; }
6064
public string GitExecutable { get; }
6165
public NPath GitExecutablePath { get; }
@@ -178,21 +182,20 @@ private NPath MoveGitAndLfs(NPath gitExtractPath, NPath gitLfsExtractPath, NPath
178182

179183
private ITask CreateDownloadTask()
180184
{
181-
var tempZipPath = NPath.CreateTempDirectory("git_zip_paths");
182-
gitArchiveFilePath = tempZipPath.Combine("git.zip");
183-
gitLfsArchivePath = tempZipPath.Combine("git-lfs.zip");
185+
gitArchiveFilePath = installDetails.PluginDataPath.Combine("git.zip");
186+
gitLfsArchivePath = installDetails.PluginDataPath.Combine("git-lfs.zip");
184187

185188
var downloadGitMd5Task = new DownloadTextTask(TaskManager.Instance.Token, environment.FileSystem,
186-
installDetails.GitZipMd5Url, tempZipPath);
189+
installDetails.GitZipMd5Url, installDetails.PluginDataPath);
187190

188191
var downloadGitTask = new DownloadTask(TaskManager.Instance.Token, environment.FileSystem,
189-
installDetails.GitZipUrl, tempZipPath);
192+
installDetails.GitZipUrl, installDetails.PluginDataPath);
190193

191194
var downloadGitLfsMd5Task = new DownloadTextTask(TaskManager.Instance.Token, environment.FileSystem,
192-
installDetails.GitLfsZipMd5Url, tempZipPath);
195+
installDetails.GitLfsZipMd5Url, installDetails.PluginDataPath);
193196

194197
var downloadGitLfsTask = new DownloadTask(TaskManager.Instance.Token, environment.FileSystem,
195-
installDetails.GitLfsZipUrl, tempZipPath);
198+
installDetails.GitLfsZipUrl, installDetails.PluginDataPath);
196199

197200
return
198201
downloadGitMd5Task.Then((b, s) => { downloadGitTask.ValidationHash = s; })

src/GitHub.Api/Tasks/DownloadTask.cs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,30 +99,53 @@ protected virtual string RunDownload(bool success)
9999
{
100100
Logger.Trace($"Download of {Url} to {Destination} Attempt {attempts + 1} of {RetryCount + 1}");
101101

102-
using (var destinationStream = fileSystem.OpenWrite(Destination, FileMode.Append))
102+
var fileExistsAndValid = false;
103+
if (Destination.FileExists())
103104
{
104-
result = Utils.Download(Logger, Url, destinationStream,
105-
(value, total) =>
105+
if (ValidationHash == null)
106+
{
107+
Destination.Delete();
108+
}
109+
else
110+
{
111+
var md5 = fileSystem.CalculateFileMD5(Destination);
112+
result = md5.Equals(ValidationHash, StringComparison.CurrentCultureIgnoreCase);
113+
114+
if (result)
106115
{
107-
UpdateProgress(value, total);
108-
return !Token.IsCancellationRequested;
109-
});
116+
Logger.Trace($"Download previously exists & confirmed {md5}");
117+
fileExistsAndValid = true;
118+
}
119+
}
110120
}
111121

112-
if (result && ValidationHash != null)
122+
if (!fileExistsAndValid)
113123
{
114-
var md5 = fileSystem.CalculateFileMD5(TargetDirectory);
115-
result = md5.Equals(ValidationHash, StringComparison.CurrentCultureIgnoreCase);
116-
117-
if (!result)
124+
using (var destinationStream = fileSystem.OpenWrite(Destination, FileMode.Append))
118125
{
119-
Logger.Warning($"Downloaded MD5 {md5} does not match {ValidationHash}. Deleting {TargetDirectory}.");
120-
fileSystem.FileDelete(TargetDirectory);
126+
result = Utils.Download(Logger, Url, destinationStream,
127+
(value, total) =>
128+
{
129+
UpdateProgress(value, total);
130+
return !Token.IsCancellationRequested;
131+
});
121132
}
122-
else
133+
134+
if (result && ValidationHash != null)
123135
{
124-
Logger.Trace($"Download confirmed {md5}");
125-
break;
136+
var md5 = fileSystem.CalculateFileMD5(Destination);
137+
result = md5.Equals(ValidationHash, StringComparison.CurrentCultureIgnoreCase);
138+
139+
if (!result)
140+
{
141+
Logger.Warning($"Downloaded MD5 {md5} does not match {ValidationHash}. Deleting {Destination}.");
142+
fileSystem.FileDelete(TargetDirectory);
143+
}
144+
else
145+
{
146+
Logger.Trace($"Download confirmed {md5}");
147+
break;
148+
}
126149
}
127150
}
128151
}

src/tests/IntegrationTests/Installer/GitInstallerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public override void TestFixtureTearDown()
3535
}
3636

3737
[Test]
38+
[Category("DoNotRunOnAppVeyor")]
3839
public void GitInstallTest()
3940
{
4041
var gitInstallationPath = TestBasePath.Combine("GitInstall").CreateDirectory();

0 commit comments

Comments
 (0)