예제 #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="workspace"></param>
        /// <param name="force">True when the user manually initiates a ShelveSet via the Team menu or mapped shortcut key.</param>
        private void CreateShelveset(VersionControlServer service, Workspace workspace, bool force)
        {
            // Build event args for notification create shelveset result
            var autoShelveEventArg = new ShelvesetCreatedEventArgs();

            try
            {
                // If there are no pending changes that have changed since the last shelveset then there is nothing to do
                bool isDelta = false;
                var pendingChanges = workspace.GetPendingChanges();
                int numPending = pendingChanges.Count();

                if (!force && numPending > 0)
                {
                    // Compare numPending to numItemsShelved;  Force shelveset if they differ
                    // Otherwise, resort to comparing file HashValues
                    var lastShelveset = GetPastShelvesets(service, workspace).FirstOrDefault();
                    var shelvedChanges = service.QueryShelvedChanges(lastShelveset)[0].PendingChanges;
                    int numItemsShelved = lastShelveset == null ? 0 : shelvedChanges.Count();
                    isDelta = (numPending != numItemsShelved) || pendingChanges.DifferFrom(shelvedChanges);
                }
                autoShelveEventArg.ShelvesetChangeCount = (force || isDelta) ? numPending : 0;
                if (force || isDelta)
                {
                    // Build a new, valid shelve set name
                    var setname = string.Format(ShelvesetName, workspace.Name, workspace.OwnerName, DateTime.Now, workspace.OwnerName.GetDomain(), workspace.OwnerName.GetLogin());
                    setname = CleanShelvesetName(setname);

                    // Actually create a new Shelveset 
                    var shelveset = new Shelveset(service, setname, workspace.OwnerName);
                    autoShelveEventArg.ShelvesetName = setname;
                    shelveset.Comment = string.Format("Shelved by {0}. {1} items", _extensionName, numPending);
                    workspace.Shelve(shelveset, pendingChanges, ShelvingOptions.Replace);

                    // Clean up past Shelvesets
                    if (MaximumShelvesets > 0)
                    {
                        foreach (var set in GetPastShelvesets(service, workspace).Skip(MaximumShelvesets))
                        {
                            service.DeleteShelveset(set);
                            autoShelveEventArg.ShelvesetsPurgeCount++;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _tfsExt = null; // Force re-init on next attempt
                autoShelveEventArg.ExecutionException = ex;
            }
            // Fire event for each VS instance to report results
            if (OnShelvesetCreated != null)
            {
                OnShelvesetCreated(this, autoShelveEventArg);
            }
        }