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); } } }
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); }