|
6 | 6 | using LibGit2Sharp;
|
7 | 7 | using Logging;
|
8 | 8 |
|
9 |
| - public static class GitRepositoryFactory |
| 9 | + public static class DynamicRepositories |
10 | 10 | {
|
11 | 11 | static readonly ILog Log = LogProvider.GetCurrentClassLogger();
|
12 | 12 |
|
13 | 13 | /// <summary>
|
14 |
| - /// Creates the repository based on the repository info. If the <see cref="RepositoryInfo.Directory"/> points |
15 |
| - /// to a valid directory, it will be used as the source for the git repository. Otherwise this method will create |
16 |
| - /// a dynamic repository based on the url and authentication info. |
| 14 | + /// Creates a dynamic repository based on the repository info |
17 | 15 | /// </summary>
|
18 |
| - /// <param name="repositoryInfo">The repository information.</param> |
| 16 | + /// <param name="repositoryInfo">The source repository information.</param> |
| 17 | + /// <param name="dynamicRepsitoryPath">The path to create the dynamic repository, NOT thread safe.</param> |
| 18 | + /// <param name="targetBranch"></param> |
| 19 | + /// <param name="targetCommit"></param> |
19 | 20 | /// <param name="noFetch">If set to <c>true</c>, don't fetch anything.</param>
|
20 | 21 | /// <returns>The git repository.</returns>
|
21 |
| - public static GitRepository CreateRepository(RepositoryInfo repositoryInfo, bool noFetch = false) |
| 22 | + public static Repository CreateOrOpen(RepositoryInfo repositoryInfo, string dynamicRepsitoryPath, string targetBranch, string targetCommit, bool noFetch = false) |
22 | 23 | {
|
23 |
| - bool isDynamicRepository = false; |
24 |
| - string repositoryDirectory = null; |
25 |
| - |
26 |
| - // TODO: find a better way to check for existing repositories |
27 |
| - if (!string.IsNullOrWhiteSpace(repositoryInfo.Directory)) |
28 |
| - { |
29 |
| - var expectedDirectory = Path.Combine(repositoryInfo.Directory, ".git"); |
30 |
| - if (Directory.Exists(expectedDirectory)) |
31 |
| - { |
32 |
| - repositoryDirectory = expectedDirectory; |
33 |
| - } |
34 |
| - } |
35 |
| - |
36 |
| - if (string.IsNullOrWhiteSpace(repositoryDirectory)) |
37 |
| - { |
38 |
| - isDynamicRepository = true; |
| 24 | + if (string.IsNullOrWhiteSpace(targetBranch)) |
| 25 | + throw new GitToolsException("Dynamic Git repositories must have a target branch"); |
| 26 | + if (string.IsNullOrWhiteSpace(targetCommit)) |
| 27 | + throw new GitToolsException("Dynamic Git repositories must have a target commit"); |
39 | 28 |
|
40 |
| - var tempRepositoryPath = CalculateTemporaryRepositoryPath(repositoryInfo.Url, repositoryDirectory); |
41 |
| - repositoryDirectory = CreateDynamicRepository(tempRepositoryPath, repositoryInfo.Authentication, |
42 |
| - repositoryInfo.Url, repositoryInfo.Branch, noFetch); |
43 |
| - } |
| 29 | + var tempRepositoryPath = CalculateTemporaryRepositoryPath(repositoryInfo.Url, dynamicRepsitoryPath); |
| 30 | + var dynamicRepositoryPath = CreateDynamicRepository(tempRepositoryPath, repositoryInfo, noFetch, targetBranch, targetCommit); |
44 | 31 |
|
45 |
| - if (string.IsNullOrWhiteSpace(repositoryDirectory)) |
46 |
| - { |
47 |
| - Log.Warn("Could not create a repository, not enough information was specified"); |
48 |
| - return null; |
49 |
| - } |
50 |
| - |
51 |
| - // TODO: Should we do something with fetch for existing repositoriess? |
52 |
| - var repository = new Repository(repositoryDirectory); |
53 |
| - return new GitRepository(repository, isDynamicRepository); |
| 32 | + return new Repository(dynamicRepositoryPath); |
54 | 33 | }
|
55 | 34 |
|
56 | 35 | static string CalculateTemporaryRepositoryPath(string targetUrl, string dynamicRepositoryLocation)
|
@@ -98,33 +77,39 @@ static bool GitRepoHasMatchingRemote(string possiblePath, string targetUrl)
|
98 | 77 | }
|
99 | 78 | }
|
100 | 79 |
|
101 |
| - static string CreateDynamicRepository(string targetPath, AuthenticationInfo authentication, string repositoryUrl, string targetBranch, bool noFetch) |
| 80 | + static string CreateDynamicRepository(string targetPath, RepositoryInfo repositoryInfo, bool noFetch, string targetBranch, string targetCommit) |
102 | 81 | {
|
103 |
| - if (string.IsNullOrWhiteSpace(targetBranch)) |
104 |
| - { |
105 |
| - throw new GitToolsException("Dynamic Git repositories must have a target branch (/b)"); |
106 |
| - } |
107 |
| - |
108 | 82 | Log.Info(string.Format("Creating dynamic repository at '{0}'", targetPath));
|
109 | 83 |
|
110 | 84 | var gitDirectory = Path.Combine(targetPath, ".git");
|
111 | 85 | if (Directory.Exists(targetPath))
|
112 | 86 | {
|
113 | 87 | Log.Info("Git repository already exists");
|
114 |
| - GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch); |
| 88 | + CheckoutCommit(targetCommit, gitDirectory); |
| 89 | + GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, repositoryInfo.Authentication, noFetch, targetBranch); |
115 | 90 |
|
116 | 91 | return gitDirectory;
|
117 | 92 | }
|
118 | 93 |
|
119 |
| - CloneRepository(repositoryUrl, gitDirectory, authentication); |
| 94 | + CloneRepository(repositoryInfo.Url, gitDirectory, repositoryInfo.Authentication); |
| 95 | + CheckoutCommit(targetCommit, gitDirectory); |
120 | 96 |
|
121 | 97 | // Normalize (download branches) before using the branch
|
122 |
| - GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, authentication, noFetch, targetBranch); |
| 98 | + GitRepositoryHelper.NormalizeGitDirectory(gitDirectory, repositoryInfo.Authentication, noFetch, targetBranch); |
123 | 99 |
|
124 | 100 | return gitDirectory;
|
125 | 101 | }
|
126 | 102 |
|
127 |
| - private static void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo authentication) |
| 103 | + static void CheckoutCommit(string targetCommit, string gitDirectory) |
| 104 | + { |
| 105 | + using (var repo = new Repository(gitDirectory)) |
| 106 | + { |
| 107 | + Log.Info(string.Format("Checking out {0}", targetCommit)); |
| 108 | + repo.Checkout(targetCommit); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + static void CloneRepository(string repositoryUrl, string gitDirectory, AuthenticationInfo authentication) |
128 | 113 | {
|
129 | 114 | Credentials credentials = null;
|
130 | 115 | if (!string.IsNullOrWhiteSpace(authentication.Username) && !string.IsNullOrWhiteSpace(authentication.Password))
|
|
0 commit comments