public void AddItem(SyncFolder folder) { if (this.Items.Any(f => f.Name.Equals(folder.Name, StringComparison.CurrentCultureIgnoreCase))) { throw new DuplicateSyncFolderException(folder.Name); } // add config file for the sync folder to the configuration directory using (var workingDirectory = new TemporaryWorkingDirectory(GitGroup.Repository.Info.Path, RepositoryInitHelper.ConfigurationBranchName.ToString())) { var syncFoldersPath = Path.Combine(workingDirectory.Location, s_SyncFolders); if (NativeDirectory.Exists(syncFoldersPath) == false) { NativeDirectory.CreateDirectory(syncFoldersPath); } var filePath = Path.Combine(syncFoldersPath, $"{folder.Name}.{s_Json}"); using (var stream = NativeFile.Open(filePath, FileMode.CreateNew, FileAccess.Write)) { folder.WriteTo(stream); } workingDirectory.Commit($"Added SyncFolder '{folder.Name}'"); workingDirectory.Push(); } }
public void UpdateItem(SyncFolder folder) { if (folder == null) { throw new ArgumentNullException(nameof(folder)); } if (!ItemExists(folder.Name)) { throw new SyncFolderNotFoundException($"A sync folder named '{folder.Name}' could not be found"); } using (var workingDirectory = new TemporaryWorkingDirectory(GitGroup.Repository.Info.Path, RepositoryInitHelper.ConfigurationBranchName.ToString())) { var syncFoldersPath = Path.Combine(workingDirectory.Location, s_SyncFolders); var filePath = Path.Combine(syncFoldersPath, $"{folder.Name}.{s_Json}"); using (var stream = NativeFile.Open(filePath, FileMode.Open, FileAccess.Write)) { folder.WriteTo(stream); } if (workingDirectory.HasChanges) { try { workingDirectory.Commit($"Updated SyncFolder '{folder.Name}'"); workingDirectory.Push(); } catch (EmptyCommitException) { // no changes after all (HasChanges does not seem to be a 100% accurate) // => ignore exception } } } }