/// <summary> /// Moves the specified folder and all of its content, by creating a ItemsDeletedChange and a ItemsCreatedChange. /// The two changes is returned inside of a ICollection. /// </summary> /// <param name="from">The relative path to copy from</param> /// <param name="to">The relative path to copy to</param> /// <returns>Returns the changes that makes the move possible</returns> protected ICollection<AbstractChange> MoveFolder(string from, string to) { ICollection<AbstractChange> returnCollection = new List<AbstractChange>(); ItemsCreatedChange itemsCreation = new ItemsCreatedChange(); SortedSet<String> entriesFullPaths = VersionSystemFacade.GetEntryPaths(from); itemsCreation.CreateItem(new FolderCreation(to)); foreach (var entry in entriesFullPaths) { string newPath = to + "\\" + entry; newPath = newPath.Replace("\\\\", "\\"); string oldPath = from + "\\" + entry; oldPath = oldPath.Replace("\\\\", "\\"); if (VersionSystemFacade.GetVersionControlSystem(oldPath).IsDirectory(oldPath)) { itemsCreation.CreateItem(new FolderCreation(newPath + (newPath.EndsWith("\\") ? string.Empty : "\\"))); } else { string[] content = VersionSystemFacade.GetFileText(oldPath); itemsCreation.CreateItem(new FileCreation(newPath, content)); } } returnCollection.Add(itemsCreation); returnCollection.Add(DeleteFolder(from)); return returnCollection; }
/// <summary> /// Moves the specified file, by creating a ItemsDeletedChange (deleting the old file) and a ItemsCreatedChange (creating the file at the new path, with the content). /// The two changes is returned inside of a ICollection. /// </summary> /// <param name="from">The relative path to copy from</param> /// <param name="to">The relative path to copy to</param> /// <param name="content">The content of the file</param> /// <returns>Returns the changes that makes the move possible</returns> protected ICollection<AbstractChange> MoveFile(string from, string to, string content) { ICollection<AbstractChange> returnCollection = new List<AbstractChange>(); ICreation creation = new FileCreation((to), content.Split('\n')); ItemsCreatedChange itemCreation = new ItemsCreatedChange(); itemCreation.CreateItem(creation); ItemsDeletedChange itemDeletion = new ItemsDeletedChange(); itemDeletion.DeleteItem(from); returnCollection.Add(itemCreation); returnCollection.Add(itemDeletion); return returnCollection; }
/// <summary> /// Is called when an the add-item button is clicked. Make sure that the new item is added. /// </summary> protected void Add_Item(object sender, EventArgs e) { // You cant add an item to something that aint a folder! if (!_isDir) return; string newItemName = ItemNameTextBox.Text; string pathToNewItem = _path.Substring(3) + "\\" + newItemName; pathToNewItem = pathToNewItem.StartsWith("\\") ? pathToNewItem.Remove(0, 1) : pathToNewItem; string selectedItemType = ItemTypeDropDown.SelectedItem.ToString().ToLower(); VersionControlSystem vcs = VersionSystemFacade.GetVersionControlSystem(pathToNewItem); ItemsCreatedChange itemsCreation = new ItemsCreatedChange(); if (selectedItemType.Equals("folder")) { itemsCreation.CreateItem(new FolderCreation(pathToNewItem + (pathToNewItem.EndsWith("\\") ? string.Empty : "\\"))); } else { // Make sure that a file has an extension! if (!pathToNewItem.Contains(".")) { pathToNewItem += ".txt"; newItemName += ".txt"; } itemsCreation.CreateItem(new FileCreation(pathToNewItem, new string[] {""})); } if (vcs.Save(itemsCreation) == null) { Response.Redirect("Info.aspx?path=" + _path + "\\" + newItemName); } else { base.ShowWarning(); } }
/// <summary> /// Parses the changes into change objects. /// </summary> /// <param name="text">The text to parse change objects from.</param> /// <returns>A list of change objects. NOTE: If no items have been deleted, index = 0 will be null. If no items have been created, index = 1 will be null</returns> public static IList<AbstractChange> ParseChanges(String[] text) { if (text.Any(s => s == null)) throw new ArgumentNullException("text"); if (!text.Any()) throw new ArgumentException("Argument was empty"); IList<AbstractChange> parsedChanges = new List<AbstractChange>(); ItemsCreatedChange itemsCreated = null; ItemsDeletedChange itemsDeleted = null; parsedChanges.Add(itemsDeleted); parsedChanges.Add(itemsCreated); int pointer = 0; while (pointer < text.Length) { if (text[pointer].StartsWith(AbstractChange.ItemCreationMark)) { // An item has been created if (parsedChanges[1] == null) { parsedChanges[1] = new ItemsCreatedChange(); } // The item is a folder if (text[pointer].EndsWith(FileSystem.PathSeparator)) { if (parsedChanges[1] is ItemsCreatedChange) { ((ItemsCreatedChange)parsedChanges[1]).CreateItem(new FolderCreation(text[pointer].Substring(AbstractChange.ItemCreationMark.Length))); } pointer++; } else { IList<String> fileContents = new List<string>(); String createdFilePath = text[pointer].Substring(AbstractChange.ItemCreationMark.Length); pointer++; while (pointer < text.Length) { // No more modifications for this file if (text[pointer].Equals(AbstractChange.BlockSeparatorMark)) break; // Line insertion if (text[pointer].StartsWith(AbstractChange.LineInsertionMark)) { String lineNumber = text[pointer].Substring(AbstractChange.LineInsertionMark.Length); int tmp = lineNumber.IndexOf(AbstractChange.LineInsertionMark); String line = lineNumber.Substring(tmp + AbstractChange.LineInsertionMark.Length); fileContents.Add(line); pointer++; } } if (parsedChanges[1] is ItemsCreatedChange) { ((ItemsCreatedChange)parsedChanges[1]).CreateItem(new FileCreation(createdFilePath, fileContents.ToArray())); } } } else if (text[pointer].StartsWith(AbstractChange.ItemDeletionMark)) { // An item has been deleted String path = text[pointer].Substring(AbstractChange.ItemDeletionMark.Length); if (parsedChanges[0] == null) { parsedChanges[0] = new ItemsDeletedChange(); } if (parsedChanges[0] is ItemsDeletedChange) { ((ItemsDeletedChange)parsedChanges[0]).DeleteItem(path); } pointer++; } else if (text[pointer].StartsWith(AbstractChange.ItemModificationMark)) { // A file has been modified String path = text[pointer].Substring(AbstractChange.ItemModificationMark.Length); FileModifiedChange modified = new FileModifiedChange(path); pointer++; // Iterate through the file modifications until there are no more (end of file or linebreak) while (pointer < text.Length) { // No more modifications for this file if (text[pointer].Equals(AbstractChange.BlockSeparatorMark)) break; // Line insertion if (text[pointer].StartsWith(AbstractChange.LineInsertionMark)) { String lineNumber = text[pointer].Substring(AbstractChange.LineInsertionMark.Length); int tmp = lineNumber.IndexOf(AbstractChange.LineInsertionMark); int number = int.Parse(lineNumber.Substring(0, tmp)); String line = lineNumber.Substring(tmp + AbstractChange.LineInsertionMark.Length); modified.AddInsertion(number, line); pointer++; } // Line deletion if (text[pointer].StartsWith(AbstractChange.LineDeletionMark)) { String lineNumber = text[pointer].Substring(AbstractChange.LineDeletionMark.Length); int tmp = lineNumber.IndexOf(AbstractChange.LineDeletionMark); int number = int.Parse(lineNumber.Substring(0, tmp)); String line = lineNumber.Substring(tmp + AbstractChange.LineDeletionMark.Length); modified.AddDeletion(number, line); pointer++; } } parsedChanges.Add(modified); } if (pointer < text.Length && text[pointer].Equals(AbstractChange.BlockSeparatorMark)) { // Current line is empty pointer++; } } return parsedChanges; }
/// <summary> /// Listens to the renaming of files in the file system. /// </summary> /// <param name="oldPath">The path to the old file.</param> /// <param name="newPath">The path to the new file.</param> public void OnRenamed(String oldPath, String newPath) { // Create the changes var created = new ItemsCreatedChange(); var deleted = new ItemsDeletedChange(); // Delete the item in the change deleted.DeleteItem(oldPath); // Create the item if (IsDirectory(newPath)) { created.CreateItem(new FolderCreation(newPath)); } else { // Read the new path on purpose, since the old path no longer exist (the file has been deleted) created.CreateItem(new FileCreation(newPath, ReadAllLines(newPath))); } // Make the cache up to date _cache.Delete(oldPath); _cache.Update(newPath); // Commit Commit(new List<AbstractChange> { deleted, created }); }
/// <summary> /// Listens to creations of files in the file system. /// </summary> /// <param name="path">The path to the item that has been created</param> public void OnCreated(String path) { // Create the change ItemsCreatedChange change = new ItemsCreatedChange(); if (IsDirectory(path)) { change.CreateItem(new FolderCreation(path)); } else { change.CreateItem(new FileCreation(path, ReadAllLines(path))); } // Keep the track up to date _cache.Update(path); Commit(new List<AbstractChange> { change }); }