|
4 | 4 | using System.Collections.Generic; |
5 | 5 | using System.IO; |
6 | 6 | using System.Linq; |
| 7 | + using System.Runtime.InteropServices; |
7 | 8 | using System.Text; |
8 | | - using System.Threading.Tasks; |
9 | 9 | using LibGit2Sharp; |
10 | 10 | using Validation; |
11 | 11 | using Version = System.Version; |
|
15 | 15 | /// </summary> |
16 | 16 | public static class GitExtensions |
17 | 17 | { |
| 18 | + /// <summary> |
| 19 | + /// An AppDomain-wide variable used on |
| 20 | + /// </summary> |
| 21 | + private static bool libgit2PathInitialized; |
| 22 | + |
18 | 23 | /// <summary> |
19 | 24 | /// The 0.0 version. |
20 | 25 | /// </summary> |
@@ -272,6 +277,61 @@ public static IEnumerable<Commit> GetCommitsFromVersion(this Repository repo, Ve |
272 | 277 | return possibleCommits; |
273 | 278 | } |
274 | 279 |
|
| 280 | + /// <summary> |
| 281 | + /// Assists the operating system in finding the appropriate native libgit2 module. |
| 282 | + /// </summary> |
| 283 | + /// <param name="basePath">The path to the directory that contains the lib folder.</param> |
| 284 | + /// <exception cref="ArgumentException">Thrown if the provided path does not lead to an existing directory.</exception> |
| 285 | + public static void HelpFindLibGit2NativeBinaries(string basePath) |
| 286 | + { |
| 287 | + if (!TryHelpFindLibGit2NativeBinaries(basePath, out string attemptedDirectory)) |
| 288 | + { |
| 289 | + throw new ArgumentException($"Unable to find native binaries under directory: \"{attemptedDirectory}\"."); |
| 290 | + } |
| 291 | + } |
| 292 | + |
| 293 | + /// <summary> |
| 294 | + /// Assists the operating system in finding the appropriate native libgit2 module. |
| 295 | + /// </summary> |
| 296 | + /// <param name="basePath">The path to the directory that contains the lib folder.</param> |
| 297 | + /// <returns><c>true</c> if the libgit2 native binaries have been found; <c>false</c> otherwise.</returns> |
| 298 | + public static bool TryHelpFindLibGit2NativeBinaries(string basePath) |
| 299 | + { |
| 300 | + return TryHelpFindLibGit2NativeBinaries(basePath, out string attemptedDirectory); |
| 301 | + } |
| 302 | + |
| 303 | + /// <summary> |
| 304 | + /// Assists the operating system in finding the appropriate native libgit2 module. |
| 305 | + /// </summary> |
| 306 | + /// <param name="basePath">The path to the directory that contains the lib folder.</param> |
| 307 | + /// <param name="attemptedDirectory">Receives the directory that native binaries are expected.</param> |
| 308 | + /// <returns><c>true</c> if the libgit2 native binaries have been found; <c>false</c> otherwise.</returns> |
| 309 | + public static bool TryHelpFindLibGit2NativeBinaries(string basePath, out string attemptedDirectory) |
| 310 | + { |
| 311 | + attemptedDirectory = null; |
| 312 | + if (!libgit2PathInitialized) |
| 313 | + { |
| 314 | +#if !NET45 |
| 315 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 316 | +#endif |
| 317 | + { |
| 318 | + attemptedDirectory = Path.Combine(basePath, "lib", "win32", IntPtr.Size == 4 ? "x86" : "x64"); |
| 319 | + if (Directory.Exists(attemptedDirectory)) |
| 320 | + { |
| 321 | + Environment.SetEnvironmentVariable("PATH", Environment.GetEnvironmentVariable("PATH") + Path.PathSeparator + attemptedDirectory); |
| 322 | + } |
| 323 | + else |
| 324 | + { |
| 325 | + return false; |
| 326 | + } |
| 327 | + } |
| 328 | + |
| 329 | + libgit2PathInitialized = true; |
| 330 | + } |
| 331 | + |
| 332 | + return libgit2PathInitialized; |
| 333 | + } |
| 334 | + |
275 | 335 | /// <summary> |
276 | 336 | /// Tests whether a commit is of a specified version, comparing major and minor components |
277 | 337 | /// with the version.txt file defined by that commit. |
@@ -438,7 +498,7 @@ private static Version GetIdAsVersionHelper(Commit commit, VersionOptions versio |
438 | 498 | : 0; |
439 | 499 | } |
440 | 500 |
|
441 | | - int build = versionHeight.Value == 0 ? 0 : versionHeight.Value + (versionOptions?.BuildNumberOffset ?? 0); |
| 501 | + int build = versionHeight.Value == 0 ? 0 : versionHeight.Value + (versionOptions?.BuildNumberOffset ?? 0); |
442 | 502 | Verify.Operation(build <= MaximumBuildNumberOrRevisionComponent, "Git height is {0}, which is greater than the maximum allowed {0}.", build, MaximumBuildNumberOrRevisionComponent); |
443 | 503 | int revision = commit != null |
444 | 504 | ? Math.Min(MaximumBuildNumberOrRevisionComponent, commit.GetTruncatedCommitIdAsUInt16()) |
|
0 commit comments