Пример #1
0
        /// <summary>
        /// Undo changes to unchanged files in the workspace
        /// </summary>
        /// <param name="workspace">Workspace</param>
        public static void UndoUnchanged(Workspace workspace)
        {
            //Get pending changes
            PendingChange[] pendingChanges = workspace.GetPendingChanges();
            if (pendingChanges.Length == 0)
            {
                return;
            }

            var spec = new ChangesetVersionSpec(workspace.VersionControlServer.GetLatestChangesetId());

            var sourceArray         = ItemSpec.FromPendingChanges(pendingChanges);
            var pendingChangesItems = new ItemSet[0];

            if (sourceArray.Length > 0)
            {
                pendingChangesItems = workspace.VersionControlServer.GetItems(sourceArray, spec, DeletedState.Any, ItemType.Any);
            }

            //Build redundant change list
            var itemsToUndo = new List <ItemSpec>();
            var itemsToGet  = new List <string>();

            for (int j = 0; j < pendingChanges.Length; j++)
            {
                var pendingChange = pendingChanges[j];
                if (pendingChange.IsAdd || pendingChange.IsDelete || pendingChange.IsMerge || (pendingChange.ChangeType & ~ChangeType.Lock) == ChangeType.Edit)
                {
                    Item item = null;
                    foreach (Item currentItem in pendingChangesItems[j].Items)
                    {
                        if (pendingChange.ItemType == currentItem.ItemType && (pendingChange.IsDelete || currentItem.DeletionId == 0))
                        {
                            if (pendingChange.IsDelete && currentItem.DeletionId == 0)
                            {
                                item = null;
                                break;
                            }
                            if (!pendingChange.IsDelete || item == null || currentItem.DeletionId >= item.DeletionId)
                            {
                                item = currentItem;
                            }
                        }
                    }

                    if (item != null)
                    {
                        bool additemToUndo = false;
                        bool addItemToGet  = false;
                        if (pendingChange.ItemType == ItemType.File)
                        {
                            if (pendingChange.IsDelete)
                            {
                                additemToUndo = true;
                                addItemToGet  = true;
                                TrackMessage(string.Format("{0}: {1}", pendingChange.ChangeTypeName, pendingChange.LocalItem));
                            }
                            else if (IsMatchesContent(pendingChange.LocalItem, item.HashValue))
                            {
                                additemToUndo = true;
                                if (pendingChange.IsAdd || pendingChange.IsDelete)
                                {
                                    addItemToGet = true;
                                }
                                TrackMessage(string.Format("{0} (contents match): {1}", pendingChange.ChangeTypeName, pendingChange.LocalItem));
                            }
                        }
                        else if (pendingChange.IsAdd || pendingChange.IsDelete)
                        {
                            additemToUndo = true;
                            addItemToGet  = true;
                            TrackMessage(string.Format("{0}: {1}", pendingChange.ChangeTypeName, pendingChange.LocalItem));
                        }

                        if (additemToUndo)
                        {
                            itemsToUndo.Add(new ItemSpec(pendingChange));
                        }
                        if (addItemToGet)
                        {
                            itemsToGet.Add(pendingChange.LocalItem);
                        }
                    }
                }
            }

            if (itemsToUndo.Count == 0)
            {
                //No redundant pending changes
                TrackMessage("There are no redundant pending changes.");
                return;
            }

            //Undo redundant pending changes
            TrackMessage("Undoing redundant changes...");
            workspace.Undo(itemsToUndo.ToArray());

            if (itemsToGet.Count > 0)
            {
                //Force get undone adds
                TrackMessage("Forcing a get for undone adds and deletes...");
                workspace.Get(itemsToGet.ToArray(), spec, RecursionType.None, GetOptions.GetAll | GetOptions.Overwrite);
            }
        }