public void Convert_detection_of_file_properties_files_is_case_invariant() { var file1 = new EmptyFile(s_File1); var filePropertiesFile = FilePropertiesFile.ForFile(null, file1); var mock = new Mock<IReadableFile>(MockBehavior.Strict); mock.Setup(f => f.Name).Returns(file1.Name + FilePropertiesFile.FileNameSuffix.ToUpper()); mock.Setup(f => f.OpenRead()).Returns(filePropertiesFile.OpenRead()); var directoryCreator = new LocalItemCreator(); using (var temporaryDirectory = directoryCreator.CreateTemporaryDirectory( new Directory(Path.GetRandomFileName()) { dir => { mock.Setup(d => d.Parent).Returns(dir); return mock.Object; } })) { var metaFs = m_Instance.Convert(temporaryDirectory.Directory); Assert.Empty(metaFs.Directories); Assert.True(metaFs.GetFile(s_File1 + FilePropertiesFile.FileNameSuffix) is FilePropertiesFile); } }
public GitDirectoryTest() { m_DirectoryCreator = new LocalItemCreator(); m_Repository = m_DirectoryCreator.CreateTemporaryDirectory(); RepositoryInitHelper.InitializeRepository(m_Repository.Directory.Location); }
public void Convert_replaces_files_with_DirectoryPropertiesFile_instances() { var input = new Directory(Path.GetRandomFileName()); input.Add(_ => DirectoryPropertiesFile.ForDirectory(null, input)); var directoryCreator = new LocalItemCreator(); using (var temporaryDirectory = directoryCreator.CreateTemporaryDirectory(input)) { var metaFs = m_Instance.Convert(temporaryDirectory.Directory); Assert.Empty(metaFs.Directories); Assert.True(metaFs.GetFile(DirectoryPropertiesFile.FileName) is DirectoryPropertiesFile); } }
public void Convert_replaces_files_with_FilePropertiesFile_instances() { var input = new Directory(Path.GetRandomFileName()) { dir => FilePropertiesFile.ForFile(dir, new EmptyFile(s_File1)) }; var directoryCreator = new LocalItemCreator(); using (var temporaryDirectory = directoryCreator.CreateTemporaryDirectory(input)) { var metaFs = m_Instance.Convert(temporaryDirectory.Directory); Assert.Empty(metaFs.Directories); Assert.True(metaFs.GetFile(s_File1 + FilePropertiesFile.FileNameSuffix) is FilePropertiesFile); } }
public static GitBasedMultiFileSystemSnapshot Create(Repository repository, BranchName branchName, IHistoryService historyService) { var directoryCreator = new LocalItemCreator(); var branch = repository.GetBranch(branchName); string commitId; using (var workingRepository = new TemporaryWorkingDirectory(repository.Info.Path, branch.FriendlyName)) { var snapshotDirectory = new Directory(null, s_SnapshotDirectoryName); foreach (var fileSystemHistory in historyService.Items) { var fileName = fileSystemHistory.Name + s_FileNameSuffix; var content = fileSystemHistory.LatestFileSystemSnapshot?.Id ?? ""; snapshotDirectory.Add(d => new TextFile(d, fileName, content)); } var snapshotDirectoryPath = Path.Combine(workingRepository.Location, s_SnapshotDirectoryName); directoryCreator.CreateDirectoryInPlace(snapshotDirectory, snapshotDirectoryPath, true); if (workingRepository.HasChanges) { try { commitId = workingRepository.Commit("Created multi-filesystem snapshot"); workingRepository.Push(); } catch (EmptyCommitException) { // no changes after all (HasChanges does not seem to be a 100% accurate) commitId = repository.GetBranch(branchName).Tip.Sha; } } else { commitId = repository.GetBranch(branchName).Tip.Sha; } } var commit = repository.Lookup<Commit>(commitId); return IsSnapshot(commit) ? new GitBasedMultiFileSystemSnapshot(commit, historyService) : null; }
/// <summary> /// Initializes a new bare repository at the specified location, adds a repository info file to the root directory /// and tags the initial commit with he value of <see cref="InitialCommitTagName"/> /// </summary> public static void InitializeRepository(string location) { // initialize a bare repository Repository.Init(location, true); var directoryCreator = new LocalItemCreator(); // clone the repository, add initial commit and push the changes back to the actual repository using (var tempDirectory = directoryCreator.CreateTemporaryDirectory()) { var clonedRepoPath = Repository.Clone(location, tempDirectory.Directory.Location); var repositoryInfoFile = new RepositoryInfoFile(tempDirectory.Directory); // add a empty file to the repository directoryCreator.CreateFile(repositoryInfoFile, tempDirectory.Directory.Location); // commit and push the file to the bare repository we created using (var clonedRepo = new Repository(clonedRepoPath)) { var signature = SignatureHelper.NewSignature(); clonedRepo.Stage(repositoryInfoFile.Name); clonedRepo.Commit("Initial Commit", signature, signature, new CommitOptions()); clonedRepo.Network.Push(clonedRepo.Network.Remotes["origin"], @"refs/heads/master"); } } //create the configuration branch pointing to the initial commit using (var repository = new Repository(location)) { repository.CreateBranch(ConfigurationBranchName.ToString(), repository.GetAllCommits().Single()); repository.Tags.Add(InitialCommitTagName, repository.GetAllCommits().Single()); } }
public LocalItemCreatorTest() { m_Instance = new LocalItemCreator(); }
public void Convert_detection_of_directory_properties_files_is_case_invariant() { var directory = new Directory(Path.GetRandomFileName()); var directoryPropertiesFile = DirectoryPropertiesFile.ForDirectory(null, directory); var mock = new Mock<IReadableFile>(); mock.Setup(f => f.Name).Returns(DirectoryPropertiesFile.FileName.ToUpper()); mock.Setup(f => f.OpenRead()).Returns(directoryPropertiesFile.OpenRead()); directory.Add(dir => mock.Object); var directoryCreator = new LocalItemCreator(); using (var temporaryDirectory = directoryCreator.CreateTemporaryDirectory(directory)) { var metaFs = m_Instance.Convert(temporaryDirectory.Directory); Assert.Empty(metaFs.Directories); Assert.True(metaFs.GetFile(DirectoryPropertiesFile.FileName) is DirectoryPropertiesFile); } }
public void UpdateItems(IEnumerable<SyncAction> syncActions) { syncActions = syncActions.ToList(); if (!syncActions.Any()) { return; } // make sure all to be updated actions exist (no need to check the state, this property might have changed) AssertSyncActionsExist(syncActions, false); using (var workingDir = new TemporaryWorkingDirectory(GitGroup.Repository.Info.Path, BranchName.ToString())) { var root = new Directory(null, "root"); foreach (var syncAction in syncActions) { PathValidator.EnsureIsRootedPath(syncAction.Path); // remove existing files for this sync action var filesToDelete = Enum.GetValues(typeof (SyncActionState)).Cast<SyncActionState>() .Where(state => state != syncAction.State) .Select(state => GetRelativeSyncActionDirectoryPath(state, syncAction.Path)) .Select(relativeDirectoryPath => Path.Combine(relativeDirectoryPath, SyncActionFile.GetFileName(syncAction))) .Select(relativePath => Path.Combine(workingDir.Location, relativePath)) .Where(System.IO.File.Exists).ToList(); foreach (var file in filesToDelete) { System.IO.File.Delete(file); } // add a new file var directory = DirectoryHelper.GetOrAddDirectory(root, GetRelativeSyncActionDirectoryPath(syncAction)); directory.Add(d => new SyncActionFile(d, syncAction)); } var localItemCreator = new LocalItemCreator(); localItemCreator.CreateDirectoryInPlace(root, workingDir.Location); workingDir.Commit($"{nameof(GitSyncActionService)}: Updated {syncActions.Count()} items"); workingDir.Push(); } }
public void AddItems(IEnumerable<SyncAction> syncActions) { syncActions = syncActions.ToArray(); if (!syncActions.Any()) { return; } // make sure, the action does not already exist foreach (var action in syncActions) { var exisitingActions = this[action.State, action.Path].Where(a => a.Id == action.Id); if (exisitingActions.Any()) { throw new DuplicateSyncActionException($"A sync action with id {action.Id} already exists"); } } // create the branch if it does not already exist EnsureBranchExists(); // create a file system tree for the actions var root = new Directory(null, "root"); foreach (var syncAction in syncActions) { PathValidator.EnsureIsRootedPath(syncAction.Path); var directory = DirectoryHelper.GetOrAddDirectory(root, GetRelativeSyncActionDirectoryPath(syncAction)); directory.Add(d => new SyncActionFile(d, syncAction)); } // store the actions using (var workingDir = new TemporaryWorkingDirectory(GitGroup.Repository.Info.Path, BranchName.ToString())) { // create the actions on disk var localItemCreator = new LocalItemCreator(); localItemCreator.CreateDirectoryInPlace(root, workingDir.Location); // commit (no check if there are pending changes, because there will always be changes (we made sure that syncActions is not empty and action do not yet exist)) workingDir.Commit($"{nameof(GitSyncActionService)}: Added {syncActions.Count()} items"); workingDir.Push(); } }
public void AddItem(ISyncPoint state) { if (state == null) { throw new ArgumentNullException(nameof(state)); } if (ItemExists(state.Id)) { throw new DuplicateSyncPointException(state.Id); } // create synchronization state branch if necessary EnsureBranchExists(); var directory = new Directory(null, s_DirectoryName) { d => new SyncPointStateFile(d, state) }; using (var workingDirectory = new TemporaryWorkingDirectory(GitGroup.Repository.Info.Path, BranchName.ToString())) { var localItemCreator = new LocalItemCreator(); localItemCreator.CreateDirectory(directory, workingDirectory.Location); workingDirectory.Commit($"{nameof(GitSyncPointService)}: Added SyncPoint {state.Id}"); workingDirectory.Push(); } }
public void AddItems(IEnumerable<ConflictInfo> conflicts) { if (conflicts == null) { throw new ArgumentNullException(nameof(conflicts)); } conflicts = conflicts.ToArray(); if(!conflicts.Any()) { return; } EnsureBranchExists(); var exisitngRoot = new GitDirectory(null, "root", GitGroup.Repository.GetLocalBranch(BranchName).Tip); var createdRoot = new Directory(null, "root"); // verify conflicts foreach (var conflict in conflicts) { var relativePath = GetRelativeConflictInfoFilePath(conflict.FilePath); if (exisitngRoot.FileExists(relativePath)) { throw new DuplicateItemException($"A ConflictInfo for '{conflict.FilePath}' already exists"); } var directory = DirectoryHelper.GetOrAddDirectory(createdRoot, PathParser.GetDirectoryName(relativePath)); directory.Add(f => new ConflictInfoFile(f, conflict)); } using (var workingDirectory = new TemporaryWorkingDirectory(GitGroup.Repository.Info.Path, BranchName.ToString())) { var localItemCreator = new LocalItemCreator(); localItemCreator.CreateDirectoryInPlace(createdRoot, workingDirectory.Location); workingDirectory.Commit($"{nameof(GitConflictService)}: Added {conflicts.Count()} items"); workingDirectory.Push(); } }