|
1 | 1 | using System;
|
2 |
| -using System.Collections.Generic; |
3 | 2 | using System.IO;
|
4 | 3 | using System.Linq;
|
5 | 4 | using GitVersion.Extensions;
|
@@ -209,9 +208,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
|
209 | 208 |
|
210 | 209 | try
|
211 | 210 | {
|
212 |
| - var remote = EnsureOnlyOneRemoteIsDefined(repository, log); |
213 |
| - |
214 |
| - AddMissingRefSpecs(repository, log, remote); |
| 211 | + var remote = repository.EnsureOnlyOneRemoteIsDefined(log); |
215 | 212 |
|
216 | 213 | //If noFetch is enabled, then GitVersion will assume that the git repository is normalized before execution, so that fetching from remotes is not required.
|
217 | 214 | if (noFetch)
|
@@ -242,7 +239,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
|
242 | 239 |
|
243 | 240 | var headSha = repository.Refs.Head.TargetIdentifier;
|
244 | 241 |
|
245 |
| - if (!repository.Info.IsHeadDetached) |
| 242 | + if (!repository.IsHeadDetached) |
246 | 243 | {
|
247 | 244 | log.Info($"HEAD points at branch '{headSha}'.");
|
248 | 245 | return;
|
@@ -295,7 +292,7 @@ private void NormalizeGitDirectory(string gitDirectory, bool noFetch, string cur
|
295 | 292 | else if (localBranchesWhereCommitShaIsHead.Count == 0)
|
296 | 293 | {
|
297 | 294 | log.Info($"No local branch pointing at the commit '{headSha}'. Fake branch needs to be created.");
|
298 |
| - CreateFakeBranchPointingAtThePullRequestTip(repository, log, authentication); |
| 295 | + repository.CreateFakeBranchPointingAtThePullRequestTip(log, authentication); |
299 | 296 | }
|
300 | 297 | else
|
301 | 298 | {
|
@@ -410,103 +407,5 @@ private static void EnsureLocalBranchExistsForCurrentBranch(IGitRepository repo,
|
410 | 407 |
|
411 | 408 | repo.Commands.Checkout(localCanonicalName);
|
412 | 409 | }
|
413 |
| - |
414 |
| - private static Remote EnsureOnlyOneRemoteIsDefined(IGitRepository repo, ILog log) |
415 |
| - { |
416 |
| - var remotes = repo.Network.Remotes; |
417 |
| - var howMany = remotes.Count(); |
418 |
| - |
419 |
| - if (howMany == 1) |
420 |
| - { |
421 |
| - var remote = remotes.Single(); |
422 |
| - log.Info($"One remote found ({remote.Name} -> '{remote.Url}')."); |
423 |
| - return remote; |
424 |
| - } |
425 |
| - |
426 |
| - var message = $"{howMany} remote(s) have been detected. When being run on a build server, the Git repository is expected to bear one (and no more than one) remote."; |
427 |
| - throw new WarningException(message); |
428 |
| - } |
429 |
| - |
430 |
| - private static void AddMissingRefSpecs(IGitRepository repo, ILog log, Remote remote) |
431 |
| - { |
432 |
| - if (remote.FetchRefSpecs.Any(r => r.Source == "refs/heads/*")) |
433 |
| - return; |
434 |
| - |
435 |
| - var allBranchesFetchRefSpec = $"+refs/heads/*:refs/remotes/{remote.Name}/*"; |
436 |
| - |
437 |
| - log.Info($"Adding refspec: {allBranchesFetchRefSpec}"); |
438 |
| - |
439 |
| - repo.Network.Remotes.Update(remote.Name, |
440 |
| - r => r.FetchRefSpecs.Add(allBranchesFetchRefSpec)); |
441 |
| - } |
442 |
| - |
443 |
| - private static void CreateFakeBranchPointingAtThePullRequestTip(IGitRepository repo, ILog log, AuthenticationInfo authentication) |
444 |
| - { |
445 |
| - var remote = repo.Network.Remotes.Single(); |
446 |
| - |
447 |
| - log.Info("Fetching remote refs to see if there is a pull request ref"); |
448 |
| - var remoteTips = (string.IsNullOrEmpty(authentication.Username) ? |
449 |
| - GetRemoteTipsForAnonymousUser(repo, remote) : |
450 |
| - GetRemoteTipsUsingUsernamePasswordCredentials(repo, remote, authentication.Username, authentication.Password)) |
451 |
| - .ToList(); |
452 |
| - |
453 |
| - log.Info($"Remote Refs:{System.Environment.NewLine}" + string.Join(System.Environment.NewLine, remoteTips.Select(r => r.CanonicalName))); |
454 |
| - |
455 |
| - var headTipSha = repo.Head.Tip.Sha; |
456 |
| - |
457 |
| - var refs = remoteTips.Where(r => r.TargetIdentifier == headTipSha).ToList(); |
458 |
| - |
459 |
| - if (refs.Count == 0) |
460 |
| - { |
461 |
| - var message = $"Couldn't find any remote tips from remote '{remote.Url}' pointing at the commit '{headTipSha}'."; |
462 |
| - throw new WarningException(message); |
463 |
| - } |
464 |
| - |
465 |
| - if (refs.Count > 1) |
466 |
| - { |
467 |
| - var names = string.Join(", ", refs.Select(r => r.CanonicalName)); |
468 |
| - var message = $"Found more than one remote tip from remote '{remote.Url}' pointing at the commit '{headTipSha}'. Unable to determine which one to use ({names})."; |
469 |
| - throw new WarningException(message); |
470 |
| - } |
471 |
| - |
472 |
| - var reference = refs[0]; |
473 |
| - var canonicalName = reference.CanonicalName; |
474 |
| - log.Info($"Found remote tip '{canonicalName}' pointing at the commit '{headTipSha}'."); |
475 |
| - |
476 |
| - if (canonicalName.StartsWith("refs/tags")) |
477 |
| - { |
478 |
| - log.Info($"Checking out tag '{canonicalName}'"); |
479 |
| - repo.Commands.Checkout(reference.Target.Sha); |
480 |
| - return; |
481 |
| - } |
482 |
| - |
483 |
| - if (!canonicalName.StartsWith("refs/pull/") && !canonicalName.StartsWith("refs/pull-requests/")) |
484 |
| - { |
485 |
| - var message = $"Remote tip '{canonicalName}' from remote '{remote.Url}' doesn't look like a valid pull request."; |
486 |
| - throw new WarningException(message); |
487 |
| - } |
488 |
| - |
489 |
| - var fakeBranchName = canonicalName.Replace("refs/pull/", "refs/heads/pull/").Replace("refs/pull-requests/", "refs/heads/pull-requests/"); |
490 |
| - |
491 |
| - log.Info($"Creating fake local branch '{fakeBranchName}'."); |
492 |
| - repo.Refs.Add(fakeBranchName, new ObjectId(headTipSha)); |
493 |
| - |
494 |
| - log.Info($"Checking local branch '{fakeBranchName}' out."); |
495 |
| - repo.Commands.Checkout(fakeBranchName); |
496 |
| - } |
497 |
| - |
498 |
| - private static IEnumerable<DirectReference> GetRemoteTipsUsingUsernamePasswordCredentials(IGitRepository repository, Remote remote, string username, string password) |
499 |
| - { |
500 |
| - return repository.Network.ListReferences(remote, (url, fromUrl, types) => new UsernamePasswordCredentials |
501 |
| - { |
502 |
| - Username = username, |
503 |
| - Password = password ?? string.Empty |
504 |
| - }).Select(r => r.ResolveToDirectReference()); |
505 |
| - } |
506 |
| - |
507 |
| - private static IEnumerable<DirectReference> GetRemoteTipsForAnonymousUser(IGitRepository repository, Remote remote) |
508 |
| - { |
509 |
| - return repository.Network.ListReferences(remote).Select(r => r.ResolveToDirectReference()); |
510 |
| - } |
511 | 410 | }
|
512 | 411 | }
|
0 commit comments