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

Commit 06fac2e

Browse files
Rebuilding the GitInstaller
1 parent c9784c7 commit 06fac2e

File tree

9 files changed

+147
-293
lines changed

9 files changed

+147
-293
lines changed

src/GitHub.Api/Application/ApplicationManagerBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void Run(bool firstRun)
9494
Logger.Trace("afterPathDetermined");
9595

9696
var applicationDataPath = Environment.GetSpecialFolder(System.Environment.SpecialFolder.LocalApplicationData).ToNPath();
97-
var installDetails = new PortableGitInstallDetails(applicationDataPath, true);
97+
var installDetails = new GitInstallDetails(applicationDataPath, true);
9898

9999
var gitInstaller = new GitInstaller(Environment, CancellationToken, installDetails);
100100
gitInstaller.SetupGitIfNeeded(new ActionTask<NPath>(CancellationToken, (b, s) => {
@@ -166,7 +166,7 @@ private TaskBase<NPath> BuildDetermineGitPathTask()
166166
if (environmentIsWindows)
167167
{
168168
var applicationDataPath = Environment.GetSpecialFolder(System.Environment.SpecialFolder.LocalApplicationData).ToNPath();
169-
var installDetails = new PortableGitInstallDetails(applicationDataPath, true);
169+
var installDetails = new GitInstallDetails(applicationDataPath, true);
170170
var installTask = new PortableGitInstallTask(CancellationToken, Environment, installDetails);
171171

172172
determinePath = determinePath.Then(new ShortCircuitTask<NPath>(CancellationToken, installTask));

src/GitHub.Api/GitHub.Api.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@
118118
<Compile Include="Helpers\Constants.cs" />
119119
<Compile Include="Helpers\Validation.cs" />
120120
<Compile Include="Installer\GitInstaller.cs" />
121-
<Compile Include="Installer\PortableGitInstallTask.cs" />
122121
<Compile Include="Installer\ShortCircuitTask.cs" />
123122
<Compile Include="Installer\UnzipTask.cs" />
124123
<Compile Include="OutputProcessors\GitAheadBehindStatusOutputProcessor.cs" />

src/GitHub.Api/Installer/GitInstaller.cs

Lines changed: 105 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,70 @@
33

44
namespace GitHub.Unity
55
{
6+
class GitInstallDetails
7+
{
8+
public NPath GitInstallPath { get; }
9+
public string GitExec { get; }
10+
public NPath GitExecPath { get; }
11+
public string GitLfsExec { get; }
12+
public NPath GitLfsExecPath { get; }
13+
14+
public const string ExtractedMD5 = "65fd0575d3b47d8207b9e19d02faca4f";
15+
public const string FileListMD5 = "a152a216b2e76f6c127053251187a278";
16+
17+
private const string PackageVersion = "f02737a78695063deace08e96d5042710d3e32db";
18+
private const string PackageName = "PortableGit";
19+
private const string PackageNameWithVersion = PackageName + "_" + PackageVersion;
20+
21+
private readonly bool onWindows;
22+
23+
public GitInstallDetails(NPath targetInstallPath, bool onWindows)
24+
{
25+
this.onWindows = onWindows;
26+
var gitInstallPath = targetInstallPath.Combine(ApplicationInfo.ApplicationName, PackageNameWithVersion);
27+
GitInstallPath = gitInstallPath;
28+
29+
if (onWindows)
30+
{
31+
GitExec += "git.exe";
32+
GitLfsExec += "git-lfs.exe";
33+
34+
GitExecPath = gitInstallPath.Combine("cmd", GitExec);
35+
}
36+
else
37+
{
38+
GitExec = "git";
39+
GitLfsExec = "git-lfs";
40+
41+
GitExecPath = gitInstallPath.Combine("bin", GitExec);
42+
}
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);
52+
}
53+
}
54+
655
class GitInstaller
756
{
857
private static ILogging Logger = Logging.GetLogger<GitInstaller>();
958

1059
private readonly IEnvironment environment;
1160
private readonly IZipHelper sharpZipLibHelper;
1261
private readonly CancellationToken cancellationToken;
13-
private readonly PortableGitInstallDetails installDetails;
62+
private readonly GitInstallDetails installDetails;
1463

15-
public GitInstaller(IEnvironment environment, CancellationToken cancellationToken, PortableGitInstallDetails installDetails)
16-
: this(environment, null, cancellationToken, installDetails)
64+
public GitInstaller(IEnvironment environment, CancellationToken cancellationToken, GitInstallDetails installDetails)
65+
: this(environment, ZipHelper.Instance, cancellationToken, installDetails)
1766
{
1867
}
1968

20-
public GitInstaller(IEnvironment environment, IZipHelper sharpZipLibHelper, CancellationToken cancellationToken, PortableGitInstallDetails installDetails)
69+
public GitInstaller(IEnvironment environment, IZipHelper sharpZipLibHelper, CancellationToken cancellationToken, GitInstallDetails installDetails)
2170
{
2271
this.environment = environment;
2372
this.sharpZipLibHelper = sharpZipLibHelper;
@@ -34,38 +83,73 @@ public void SetupGitIfNeeded(ActionTask<NPath> onSuccess, ITask onFailure)
3483
onFailure.Start();
3584
}
3685

37-
new FuncTask<bool>(cancellationToken, IsPortableGitExtracted)
86+
new FuncTask<bool>(cancellationToken, IsGitExtracted)
3887
.Then((success, isPortableGitExtracted) => {
3988

4089
Logger.Trace("IsPortableGitExtracted: {0}", isPortableGitExtracted);
4190

4291
if (isPortableGitExtracted)
4392
{
93+
Logger.Trace("SetupGitIfNeeded: Skipped");
94+
4495
new FuncTask<NPath>(cancellationToken, () => installDetails.GitExecPath)
4596
.Then(onSuccess)
4697
.Start();
4798
}
4899
else
49100
{
50-
new PortableGitInstallTask(cancellationToken, environment, installDetails).Then((b, path) => {
51-
if (b && path != null)
52-
{
53-
new FuncTask<NPath>(cancellationToken, () => path)
54-
.Then(onSuccess)
55-
.Start();
56-
}
57-
else
58-
{
59-
onFailure.Start();
60-
}
61-
}).Start();
62-
101+
var tempZipPath = NPath.CreateTempDirectory("git_zip_paths");
102+
var gitArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git.zip", tempZipPath, environment);
103+
var gitLfsArchivePath = AssemblyResources.ToFile(ResourceType.Platform, "git-lfs.zip", tempZipPath, environment);
104+
105+
var gitExtractPath = tempZipPath.Combine("git").CreateDirectory();
106+
var gitLfsExtractPath = tempZipPath.Combine("git-lfs").CreateDirectory();
107+
108+
new UnzipTask(cancellationToken, gitArchivePath, gitExtractPath, sharpZipLibHelper)
109+
.Then(new UnzipTask(cancellationToken, gitLfsArchivePath, gitLfsExtractPath, sharpZipLibHelper))
110+
.Then(() => {
111+
var targetGitLfsExecPath = installDetails.GetGitLfsExecPath(gitExtractPath);
112+
var extractGitLfsExePath = gitLfsExtractPath.Combine(installDetails.GitLfsExec);
113+
extractGitLfsExePath.Move(targetGitLfsExecPath);
114+
115+
var extractedMD5 = environment.FileSystem.CalculateFolderMD5(gitExtractPath);
116+
if (!extractedMD5.Equals(GitInstallDetails.ExtractedMD5, StringComparison.InvariantCultureIgnoreCase))
117+
{
118+
Logger.Warning("MD5 {0} does not match expected {1}", extractedMD5, GitInstallDetails.ExtractedMD5);
119+
Logger.Warning("Failed PortableGitInstallTask");
120+
throw new Exception();
121+
}
122+
123+
Logger.Trace("Moving tempDirectory:\"{0}\" to extractTarget:\"{1}\"", gitExtractPath,
124+
installDetails.GitInstallPath);
125+
126+
gitExtractPath.Move(installDetails.GitInstallPath);
127+
128+
Logger.Trace("Deleting tempZipPath:\"{0}\"", tempZipPath);
129+
tempZipPath.DeleteIfExists();
130+
131+
}).Finally((b, exception) => {
132+
if (b)
133+
{
134+
Logger.Trace("SetupGitIfNeeded: Success");
135+
136+
new FuncTask<NPath>(cancellationToken, () => installDetails.GitExecPath)
137+
.Then(onSuccess)
138+
.Start();
139+
}
140+
else
141+
{
142+
Logger.Trace("SetupGitIfNeeded: Failed");
143+
144+
onFailure.Start();
145+
}
146+
}).Start();
63147
}
64148

65149
}).Start();
66150
}
67151

68-
private bool IsPortableGitExtracted()
152+
private bool IsGitExtracted()
69153
{
70154
if (!installDetails.GitInstallPath.DirectoryExists())
71155
{
@@ -74,9 +158,9 @@ private bool IsPortableGitExtracted()
74158
}
75159

76160
var fileListMD5 = environment.FileSystem.CalculateFolderMD5(installDetails.GitInstallPath, false);
77-
if (!fileListMD5.Equals(PortableGitInstallDetails.FileListMD5, StringComparison.InvariantCultureIgnoreCase))
161+
if (!fileListMD5.Equals(GitInstallDetails.FileListMD5, StringComparison.InvariantCultureIgnoreCase))
78162
{
79-
Logger.Trace("MD5 {0} does not match expected {1}", fileListMD5, PortableGitInstallDetails.FileListMD5);
163+
Logger.Trace("MD5 {0} does not match expected {1}", fileListMD5, GitInstallDetails.FileListMD5);
80164
return false;
81165
}
82166

0 commit comments

Comments
 (0)