|
2 | 2 | {
|
3 | 3 | using System;
|
4 | 4 | using System.Collections.Generic;
|
| 5 | + using System.IO; |
5 | 6 | using System.Linq;
|
6 | 7 | using LibGit2Sharp;
|
| 8 | + using Logging; |
7 | 9 |
|
8 | 10 | public static class LibGitExtensions
|
9 | 11 | {
|
| 12 | + private static readonly ILog Log = LogProvider.GetCurrentClassLogger(); |
| 13 | + |
| 14 | + public static DateTimeOffset When(this Commit commit) |
| 15 | + { |
| 16 | + return commit.Committer.When; |
| 17 | + } |
| 18 | + |
| 19 | + public static Branch FindBranch(this IRepository repository, string branchName) |
| 20 | + { |
| 21 | + var exact = repository.Branches.FirstOrDefault(x => x.Name == branchName); |
| 22 | + if (exact != null) |
| 23 | + { |
| 24 | + return exact; |
| 25 | + } |
| 26 | + |
| 27 | + return repository.Branches.FirstOrDefault(x => x.Name == "origin/" + branchName); |
| 28 | + } |
| 29 | + |
10 | 30 | public static bool IsDetachedHead(this Branch branch)
|
11 | 31 | {
|
12 | 32 | return branch.CanonicalName.Equals("(no branch)", StringComparison.OrdinalIgnoreCase);
|
13 | 33 | }
|
14 | 34 |
|
| 35 | + public static string GetRepositoryDirectory(this IRepository repository, bool omitGitPostFix = true) |
| 36 | + { |
| 37 | + var gitDirectory = repository.Info.Path; |
| 38 | + |
| 39 | + gitDirectory = gitDirectory.TrimEnd('\\'); |
| 40 | + |
| 41 | + if (omitGitPostFix && gitDirectory.EndsWith(".git")) |
| 42 | + { |
| 43 | + gitDirectory = gitDirectory.Substring(0, gitDirectory.Length - ".git".Length); |
| 44 | + gitDirectory = gitDirectory.TrimEnd('\\'); |
| 45 | + } |
| 46 | + |
| 47 | + return gitDirectory; |
| 48 | + } |
| 49 | + |
| 50 | + public static void CheckoutFilesIfExist(this IRepository repository, params string[] fileNames) |
| 51 | + { |
| 52 | + if (fileNames == null || fileNames.Length == 0) |
| 53 | + { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + Log.Info("Checking out files that might be needed later in dynamic repository"); |
| 58 | + |
| 59 | + foreach (var fileName in fileNames) |
| 60 | + { |
| 61 | + try |
| 62 | + { |
| 63 | + Log.Info(" Trying to check out '{0}'", fileName); |
| 64 | + |
| 65 | + var headBranch = repository.Head; |
| 66 | + var tip = headBranch.Tip; |
| 67 | + |
| 68 | + var treeEntry = tip[fileName]; |
| 69 | + if (treeEntry == null) |
| 70 | + { |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + var fullPath = Path.Combine(repository.GetRepositoryDirectory(), fileName); |
| 75 | + using (var stream = ((Blob) treeEntry.Target).GetContentStream()) |
| 76 | + { |
| 77 | + using (var streamReader = new BinaryReader(stream)) |
| 78 | + { |
| 79 | + File.WriteAllBytes(fullPath, streamReader.ReadBytes((int)stream.Length)); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + catch (Exception ex) |
| 84 | + { |
| 85 | + Log.Warning(" An error occurred while checking out '{0}': '{1}'", fileName, ex.Message); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
15 | 90 | public static IEnumerable<Branch> GetBranchesContainingCommit(this IRepository repository, string commitSha)
|
16 | 91 | {
|
17 | 92 | var directBranchHasBeenFound = false;
|
|
0 commit comments