public DirectReference GetRemoteReference(GitBranchNameInfo branchNameInfo) { var targetBranchName = branchNameInfo.GetCanonicalBranchName(); var remoteReferences = repository.Network.ListReferences(repoUrl); return(remoteReferences.FirstOrDefault(remoteRef => string.Equals(remoteRef.CanonicalName, targetBranchName))); }
public void CanGetRemoteRepoContextAndCheckoutReleaseNotesIfExists(string branchName, bool shouldHaveReleaseNotesFile) { // Arrange // Create a local repo to serve as the origin for our clone, - with a release notes file. var currentDir = Environment.CurrentDirectory; var originRepoDir = TestGitRepoUtils.GetUniqueTempFolder("testOriginGitRepo"); using (var testOriginRepo = TestGitRepoUtils.CreateRepoWithBranch(originRepoDir, branchName)) { string releaseNotesFileName = Guid.NewGuid().ToString(); // If a release notes file should be added, switch to the branch and add one. if (shouldHaveReleaseNotesFile) { // switch head to the branch. var branchInfo = new GitBranchNameInfo(branchName); var targetBranchName = branchInfo.GetCanonicalBranchName(); var newHead = testOriginRepo.Refs.FirstOrDefault(localRef => string.Equals(localRef.CanonicalName, targetBranchName)); testOriginRepo.Refs.UpdateTarget(testOriginRepo.Refs.Head, newHead); // commit a releasenotes file to the branch. var releaseNotesFilePath = Path.Combine(testOriginRepo.Info.WorkingDirectory, releaseNotesFileName); File.WriteAllText(releaseNotesFilePath, @"Some customised release notes contents..."); TestGitRepoUtils.CommitFile(testOriginRepo, releaseNotesFilePath, "Added test release notes file to repo"); } // Construct the arguments necessary for cloning the origin repo. var remoteArgs = new GitRemoteRepositoryContextFactory.RemoteRepoArgs(); var creds = new DefaultCredentials(); remoteArgs.Credentials = creds; var desinationDirForClone = TestGitRepoUtils.GetUniqueTempFolder("testClonedGitRepo"); // Path.Combine(currentDir, "testClonedGitRepo", Guid.NewGuid().ToString("N")); remoteArgs.DestinationPath = desinationDirForClone; remoteArgs.Url = testOriginRepo.Info.Path; // This could be the Url of the git repo, but as this is a unit test, we are using a local file path. var remoteRepoContextFactory = new GitRemoteRepositoryContextFactory(remoteArgs); using (var repoContext = remoteRepoContextFactory.GetRepositoryContext()) { repoContext.PrepareRemoteRepoForUse(branchName); // Act. repoContext.CheckoutFilesIfExist(releaseNotesFileName); // Assert. var releaseNotesFilePath = Path.Combine(desinationDirForClone,releaseNotesFileName); File.Exists(releaseNotesFilePath).ShouldBe(shouldHaveReleaseNotesFile); } } }
/// <summary> /// Prepares the git repository for first use. /// </summary> /// <param name="repo"></param> /// <param name="credentials"></param> /// <param name="branchName"></param> /// <param name="url"></param> public void PrepareRemoteRepoForUse(string branchName) { // Normalize (download branches) before using the branch this.NormalizeGitDirectory(); string targetBranch = branchName; if (string.IsNullOrWhiteSpace(branchName)) { targetBranch = Repository.Head.Name; } var branchNameInfo = new GitBranchNameInfo(targetBranch); Reference newHead = null; var localReference = GetLocalReference(branchNameInfo); if (localReference != null) { newHead = localReference; } if (newHead == null) { var remoteReference = GetRemoteReference(branchNameInfo); if (remoteReference != null) { Repository.Network.Fetch(repoUrl, new[] { string.Format("{0}:{1}", remoteReference.CanonicalName, targetBranch) }); newHead = Repository.Refs[string.Format("refs/heads/{0}", targetBranch)]; } } if (newHead != null) { Log.WriteLine("Switching to branch '{0}'", targetBranch); Repository.Refs.UpdateTarget(Repository.Refs.Head, newHead); } }
public static IRepository CreateRepoWithBranch(string path, string branchName) { Repository.Init(path); Log.WriteLine("Created git repository at '{0}'", path); var repo = new Repository(path); // Let's move the HEAD to this branch to be created var branchInfo = new GitBranchNameInfo(branchName); repo.Refs.UpdateTarget("HEAD", branchInfo.GetCanonicalBranchName()); // Create a commit against HEAD var c = GenerateCommit(repo); var branch = repo.Branches[branchName]; if (branch == null) { Log.WriteLine("Branch was NULL!"); } return repo; }
public Reference GetLocalReference(GitBranchNameInfo branchNameInfo) { var targetBranchName = branchNameInfo.GetCanonicalBranchName(); return(repository.Refs.FirstOrDefault(localRef => string.Equals(localRef.CanonicalName, targetBranchName))); }
public DirectReference GetRemoteReference(GitBranchNameInfo branchNameInfo) { var targetBranchName = branchNameInfo.GetCanonicalBranchName(); var remoteReferences = repository.Network.ListReferences(repoUrl); return remoteReferences.FirstOrDefault(remoteRef => string.Equals(remoteRef.CanonicalName, targetBranchName)); }
public Reference GetLocalReference(GitBranchNameInfo branchNameInfo) { var targetBranchName = branchNameInfo.GetCanonicalBranchName(); return repository.Refs.FirstOrDefault(localRef => string.Equals(localRef.CanonicalName, targetBranchName)); }