/// <summary> /// One of the most important methods in a source control provider, is called by projects that are under source control when they are first opened to register project settings /// </summary> public int RegisterSccProject([InAttribute] IVsSccProject2 pscp2Project, [InAttribute] string pszSccProjectName, [InAttribute] string pszSccAuxPath, [InAttribute] string pszSccLocalPath, [InAttribute] string pszProvider) { if (pszProvider.CompareTo(_sccProvider.ProviderName)!=0) { // If the provider name controlling this project is not our provider, the user may be adding to a // solution controlled by this provider an existing project controlled by some other provider. // We'll deny the registration with scc in such case. return VSConstants.E_FAIL; } if (pscp2Project == null) { // Manual registration with source control of the solution, from OnAfterOpenSolution Trace.WriteLine(String.Format(CultureInfo.CurrentUICulture, "Solution {0} is registering with source control - {1}, {2}, {3}, {4}", _sccProvider.GetSolutionFileName(), pszSccProjectName, pszSccAuxPath, pszSccLocalPath, pszProvider)); IVsHierarchy solHier = (IVsHierarchy)_sccProvider.GetService(typeof(SVsSolution)); string solutionFile = _sccProvider.GetSolutionFileName(); SccProviderStorage storage = new SccProviderStorage(solutionFile); _controlledProjects[solHier] = storage; } else { Trace.WriteLine(String.Format(CultureInfo.CurrentUICulture, "Project {0} is registering with source control - {1}, {2}, {3}, {4}", _sccProvider.GetProjectFileName(pscp2Project), pszSccProjectName, pszSccAuxPath, pszSccLocalPath, pszProvider)); // Add the project to the list of controlled projects IVsHierarchy hierProject = (IVsHierarchy)pscp2Project; SccProviderStorage storage = new SccProviderStorage(_sccProvider.GetProjectFileName(pscp2Project)); _controlledProjects[hierProject] = storage; } return VSConstants.S_OK; }
/// <summary> /// Adds the specified projects and solution to source control /// </summary> public void AddProjectsToSourceControl(ref Hashtable hashUncontrolledProjects, bool addSolutionToSourceControl) { // A real source control provider will ask the user for a location where the projects will be controlled // From the user input it should create up to 4 strings that will pass them to the projects to persist, // so next time the project is open from disk, it will callback source control package, and the package // could use the 4 binding strings to identify the correct database location of the project files. foreach (IVsHierarchy pHier in hashUncontrolledProjects.Keys) { IVsSccProject2 sccProject2 = (IVsSccProject2)pHier; sccProject2.SetSccLocation("<Project Location In Database>", "<Source Control Database>", "<Local Binding Root of Project>", _sccProvider.ProviderName); // Add the newly controlled projects now to the list of controlled projects in this solution _controlledProjects[pHier] = null; } // Also, if the solution was selected to be added to scc, write in the solution properties the controlled status if (addSolutionToSourceControl) { IVsHierarchy solHier = (IVsHierarchy)_sccProvider.GetService(typeof(SVsSolution)); _controlledProjects[solHier] = null; _sccProvider.SolutionHasDirtyProps = true; } // Now save all the modified files IVsSolution sol = (IVsSolution)_sccProvider.GetService(typeof(SVsSolution)); sol.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_SaveIfDirty, null, 0); // Add now the solution and project files to the source control database // which in our case means creating a text file containing the list of controlled files foreach (IVsHierarchy pHier in hashUncontrolledProjects.Keys) { IVsSccProject2 sccProject2 = (IVsSccProject2)pHier; IList<string> files = _sccProvider.GetProjectFiles(sccProject2); SccProviderStorage storage = new SccProviderStorage(_sccProvider.GetProjectFileName(sccProject2)); storage.AddFilesToStorage(files); _controlledProjects[pHier] = storage; } // If adding solution to source control, create a storage for the solution, too if (addSolutionToSourceControl) { IVsHierarchy solHier = (IVsHierarchy)_sccProvider.GetService(typeof(SVsSolution)); IList<string> files = new List<string>(); string solutionFile = _sccProvider.GetSolutionFileName(); files.Add(solutionFile); SccProviderStorage storage = new SccProviderStorage(solutionFile); storage.AddFilesToStorage(files); _controlledProjects[solHier] = storage; } // For all the projects added to source control, refresh their source control glyphs IList<VSITEMSELECTION> nodes = new List<VSITEMSELECTION>(); foreach (IVsHierarchy pHier in hashUncontrolledProjects.Keys) { VSITEMSELECTION vsItem; vsItem.itemid = VSConstants.VSITEMID_ROOT; vsItem.pHier = pHier; nodes.Add(vsItem); } // Also, add the solution if necessary to the list of glyphs to refresh if (addSolutionToSourceControl) { VSITEMSELECTION vsItem; vsItem.itemid = VSConstants.VSITEMID_ROOT; vsItem.pHier = null; nodes.Add(vsItem); } _sccProvider.RefreshNodesGlyphs(nodes); }