/// <summary> /// 'Pends' the add of a new folder and file and then checks it into the /// repository. /// </summary> /// <param name="workspace">Version control workspace to use when /// adding the folder and file.</param> /// <param name="newFilename">Full path to the file to add (the path /// of the folder will be derived from the file's path.</param> /// <exception cref="SecurityException">If the user doesn't have /// check-in permission for the specified <paramref name="workspace"/>.</exception> /// <exception cref="IOException">If there's a problem creating the file.</exception> /// <exception cref="VersionControlException">If </exception> private static void AddNewFile(Workspace workspace, String newFilename) { Debug.Assert(workspace != null); Debug.Assert(!String.IsNullOrEmpty(newFilename)); Debug.Assert(!File.Exists(newFilename)); if (!workspace.HasCheckInPermission) { throw new SecurityException( String.Format("{0} does not have check-in permission for workspace: {1}", workspace.VersionControlServer.AuthenticatedUser, workspace.DisplayName)); } try { // create the new file using (var streamWriter = new StreamWriter(newFilename)) { streamWriter.WriteLine("Revision 1"); } // Now pend the add of our new folder and file workspace.PendAdd(Path.GetDirectoryName(newFilename), true); // Show our pending changes var pendingAdds = new List <PendingChange>((IEnumerable <PendingChange>) workspace.GetPendingChanges()); pendingAdds.ForEach(delegate(PendingChange add) { Console.WriteLine("\t{1}: {0}", add.LocalItem, PendingChange.GetLocalizedStringForChangeType(add.ChangeType)); }); // Checkin the items we added int changesetForAdd = workspace.CheckIn(pendingAdds.ToArray(), "Initial check-in"); Console.WriteLine("Checked in changeset {0}", changesetForAdd); } catch (IOException ex) { Console.Error.WriteLine("Error writing {1}: {0}", ex.Message, newFilename); throw; } catch (VersionControlException ex) { Console.Error.WriteLine("Error adding file: {0}", ex.Message); throw; } }
/// <summary> /// Convert the passed changeset to an array of modifcations. /// </summary> private Modification[] toModifcations(Changeset changeset) { List <Modification> modifications = new List <Modification>(); string userName = changeset.Committer; string comment = changeset.Comment; int changeNumber = changeset.ChangesetId; // In VSTS, the version of the file is the same as the changeset number it was checked in with. string version = Convert.ToString(changeNumber); DateTime modifedTime = this.TFS.TimeZone.ToLocalTime(changeset.CreationDate); foreach (Change change in changeset.Changes) { Modification modification = new Modification(); modification.UserName = userName; modification.Comment = comment; modification.ChangeNumber = changeNumber; modification.ModifiedTime = modifedTime; modification.Version = version; modification.Type = PendingChange.GetLocalizedStringForChangeType(change.ChangeType); // Populate fields from change item Item item = change.Item; if (item.ItemType == ItemType.File) { // split into foldername and filename int lastSlash = item.ServerItem.LastIndexOf('/'); modification.FileName = item.ServerItem.Substring(lastSlash + 1); // patch to the following line submitted by Ralf Kretzschmar. modification.FolderName = item.ServerItem.Substring(0, lastSlash); } else { // TODO - what should filename be if dir?? Empty string or null? modification.FileName = string.Empty; modification.FolderName = item.ServerItem; } Log.Debug(modification.ToString()); modifications.Add(modification); } return(modifications.ToArray()); }
// Displays change and path information from the ExtendedItem. static void DisplayExtendedItem(ExtendedItem item, Workspace workspace) { Console.Write(" "); // Indicate whether someone else has a pending change on this file or folder. The // ExtendedItem doesn't contain the list of users with pending changes on this file. // For that information, we'd need to call QueryPendingSets() in addition to // GetExtendedItems() and join the two together via the item ID. if (item.HasOtherPendingChange) { Console.Write("^ "); } // Show the lock information if someone has locked the file or folder. if (item.LockStatus != LockLevel.None) { Console.Write("[{0},{1}] ", PendingChange.GetLocalizedStringForLockLevel(item.LockStatus), item.LockOwner); } // If there is a change pending on the item in the current workspace, display it. if (item.ChangeType != ChangeType.None) { Console.Write("({0}) ", PendingChange.GetLocalizedStringForChangeType(item.ChangeType)); } // Display the local path. if (!item.IsInWorkspace) { // Get the mapping so that we can determine its local path or that it is cloaked. WorkingFolder wf = workspace.TryGetWorkingFolderForServerItem(item.TargetServerItem); // Let's skip cloaked items, since the user doesn't want them. if (!wf.IsCloaked) { Console.WriteLine(wf.LocalItem); } } else { Console.WriteLine(item.LocalItem); } }
internal static void OnNewPendingChange(Object sender, PendingChangeEventArgs e) { Console.WriteLine(" Pending " + PendingChange.GetLocalizedStringForChangeType(e.PendingChange.ChangeType) + " on " + e.PendingChange.LocalItem); }
static void Main(string[] args) { string teamCollection = "https://pruebas88.visualstudio.com/DefaultCollection"; string teamProject = "$/PruebasTfsVersionControl/sub"; NetworkCredential netCred = new NetworkCredential( "*****", "****"); BasicAuthCredential basicCred = new BasicAuthCredential(netCred); TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred); tfsCred.AllowInteractive = false; // Get a reference to our Team Foundation Server. TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(teamCollection), tfsCred); tpc.Authenticate(); // Get a reference to Version Control. VersionControlServer versionControl = tpc.GetService <VersionControlServer>(); // Listen for the Source Control events. versionControl.NonFatalError += Example.OnNonFatalError; versionControl.Getting += Example.OnGetting; versionControl.BeforeCheckinPendingChange += Example.OnBeforeCheckinPendingChange; versionControl.NewPendingChange += Example.OnNewPendingChange; // Create a workspace. Workspace workspace = versionControl.CreateWorkspace("BasicSccExample2", versionControl.AuthorizedUser); String topDir = null; try { String localDir = @"c:\temp\BasicSccExample"; Console.WriteLine("\r\n— Create a mapping: {0} -> {1}", teamProject, localDir); workspace.Map(teamProject, localDir); Console.WriteLine("\r\n— Get the files from the repository.\r\n"); workspace.Get(); Console.WriteLine("\r\n— Create a file."); topDir = workspace.Folders[0].LocalItem; //Path.Combine(workspace.Folders[0].LocalItem, "sub"); String fileName = Path.Combine(topDir, "basic.cs"); using (StreamWriter sw = new StreamWriter(fileName)) { sw.WriteLine("revision 16 of basic.cs"); } Console.WriteLine("\r\n— Now add everything.\r\n"); workspace.PendAdd(topDir, true); Console.WriteLine("\r\n— Show our pending changes.\r\n"); PendingChange[] pendingChanges = workspace.GetPendingChanges(); Console.WriteLine(" Your current pending changes:"); foreach (PendingChange pendingChange in pendingChanges) { Console.WriteLine(" path: " + pendingChange.LocalItem + ", change: " + PendingChange.GetLocalizedStringForChangeType(pendingChange.ChangeType)); } Console.WriteLine("\r\n— Checkin the items we added.\r\n"); int changesetNumber = 0; try { changesetNumber = workspace.CheckIn(pendingChanges, "Sample changes"); } catch (Exception ee) { if (ee.Message.Contains("conflict")) { Conflict[] conflicts = workspace.QueryConflicts(new string[] { teamProject }, true); foreach (Conflict conflict in conflicts) { conflict.Resolution = Resolution.AcceptYours; workspace.ResolveConflict(conflict); if (conflict.IsResolved) { workspace.PendEdit(conflict.TargetLocalItem); } } } else { throw ee; } } Console.WriteLine(" Checked in changeset " + changesetNumber); Console.WriteLine("\r\n— Checkout and modify the file.\r\n"); workspace.PendEdit(fileName); using (StreamWriter sw = new StreamWriter(fileName)) { sw.WriteLine("revision 27 of basic.cs"); } Console.WriteLine("\r\n— Get the pending change and check in the new revision.\r\n"); pendingChanges = workspace.GetPendingChanges(); changesetNumber = workspace.CheckIn(pendingChanges, "Modified basic.cs"); Console.WriteLine(" Checked in changeset " + changesetNumber); } finally { if (topDir != null) { //Console.WriteLine("\r\n— Delete all of the items under the test project.\r\n"); //workspace.PendDelete(topDir, RecursionType.Full); //PendingChange[] pendingChanges = workspace.GetPendingChanges(); //if (pendingChanges.Length > 0) //{ // workspace.CheckIn(pendingChanges, "Clean up!"); //} //Console.WriteLine("\r\n— Delete the workspace."); workspace.Delete(); } } }
internal static string FormatDisplayChangeType(PendingChange pendingChange, bool useServerPath) { var str = string.Empty; if (pendingChange != null) { var changeType = pendingChange.ChangeType; if ((changeType & ChangeType.None) != 0) { str = string.Empty; } else if (changeType == ChangeType.Edit) { str = string.Empty; } else if (changeType == ChangeType.Delete) { str = string.Empty; } else if (changeType == ChangeType.Merge) { str = string.Format(CultureInfo.CurrentCulture, "[{0}]", Resources.Get("ChangeTypeMerge")); } else if (changeType == ChangeType.Branch) { str = string.Format(CultureInfo.CurrentCulture, "[{0}]", Resources.Get("ChangeTypeBranch")); } else if (changeType == ChangeType.Undelete) { str = string.Format(CultureInfo.CurrentCulture, "[{0}]", Resources.Get("ChangeTypeUndelete")); } else if (changeType == ChangeType.Rollback) { str = string.Format(CultureInfo.CurrentCulture, "[{0}]", Resources.Get("ChangeTypeRollback")); } else if (changeType == ChangeType.Encoding) { str = string.Format(CultureInfo.CurrentCulture, "[{0}]", Resources.Get("ChangeTypeFileType")); } else if (pendingChange.IsRename) { var originalName = GetOriginalName(pendingChange, useServerPath); var flag = !string.IsNullOrEmpty(originalName); if (changeType == ChangeType.Rename && flag) { str = string.Format(CultureInfo.CurrentCulture, "[{0}]", originalName); } else if (flag) { str = string.Format(CultureInfo.CurrentCulture, "[{0}][{1}]", originalName, PendingChange.GetLocalizedStringForChangeType(changeType, true)); } else { str = string.Format(CultureInfo.CurrentCulture, "[{0}]", PendingChange.GetLocalizedStringForChangeType(changeType, true)); } } else { str = string.Format(CultureInfo.CurrentCulture, "[{0}]", PendingChange.GetLocalizedStringForChangeType(changeType, true)); } } return(str); }
private static List <string> SearchSetupFiles(string directory, string WixSource, string[] projectNames = null) { int projectcount = 0; string searchDirectory = string.Empty; string SolutionError = string.Empty; List <string> ProcessedProjects = new List <string>(); DirectoryInfo WixSourceDirectory = new DirectoryInfo(WixSource); var WixSetup = WixSourceDirectory.GetFiles(); try { string msg = string.Empty; List <string> SolutionNames = new List <string>(); List <string> SolutionAppNames = new List <string>(); if (projectNames != null) { SolutionAppNames = projectNames.ToList(); } foreach (var solution in SolutionAppNames) { TFSGetLatest(solution.Substring(0, solution.LastIndexOf(".")), solution.Split('.').Last()); SolutionNames.Add(solution.Substring(0, solution.LastIndexOf("."))); } List <string> folders = new List <string>(); DirectoryInfo MainDirectory = new DirectoryInfo(directory); var Applications = MainDirectory.GetDirectories(); List <DirectoryInfo> Projects = new List <DirectoryInfo>(); int i = 0; foreach (var application in Applications) { if (!application.FullName.Contains("$tf") || !application.Name.Contains(".vs")) { var files = application.GetDirectories(); foreach (var file in files) { Projects.Add(file); } } } foreach (var item in Projects) { SolutionError = item.Name; if (SolutionNames != null & SolutionNames.Exists(a => a.Equals(item.Name))) { Console.WriteLine("Processing Solution: " + item.Name + " at the location: " + item.FullName); LogHelper.WriteGeneralMessage("Processing Solution: " + item.Name + " at the location: " + item.FullName); searchDirectory = item.FullName; DirectoryInfo ProjectFolder = new DirectoryInfo(searchDirectory); var projectFiles = ProjectFolder.GetDirectories(); DirectoryInfo[] projectContents = new DirectoryInfo[1000]; if (projectFiles != null) { foreach (var projectFile in projectFiles) { if (projectFile != null) { if (projectFile.Extension.Contains("Setup")) { ProcessedProjects.Add(projectFile.Parent.Name); projectcount++; XmlDocument xmldoc = new XmlDocument(); FileInfo xmlFile = new FileInfo(projectFile.FullName); string path = "\\"; string xmlPath = @xmlFile.FullName + path + xmlFile.Name + ".wixproj"; xmldoc.Load(xmlPath); XmlNamespaceManager mgr = new XmlNamespaceManager(xmldoc.NameTable); mgr.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003"); bool IsNodeExist = false; foreach (XmlNode item1 in xmldoc.SelectNodes("//x:Project", mgr)) { foreach (XmlNode item2 in item1.SelectNodes("//x:ItemGroup", mgr)) { if (item2.FirstChild.Name.Equals("Content")) { foreach (XmlNode innerItem2 in item2.SelectNodes("//x:Content", mgr)) { foreach (XmlAttribute attribute in innerItem2.Attributes) { if (attribute.Value.Equals("CustomActions.CA.dll")) { IsNodeExist = true; LogHelper.WriteGeneralMessage("Node Already Exist"); Console.WriteLine("Node Already Exist"); break; } } if (IsNodeExist) { break; } } if (!IsNodeExist) { //Create the node name Content XmlNode Content = xmldoc.CreateNode(XmlNodeType.Element, "Content", "http://schemas.microsoft.com/developer/msbuild/2003"); //Insert Include XmlAttribute Include = xmldoc.CreateAttribute("Include"); Include.Value = "CustomActions.CA.dll"; Content.Attributes.Append(Include); item2.AppendChild(Content); } } } } var removeXMLReadOnly = new DirectoryInfo(xmlPath); removeXMLReadOnly.Attributes &= ~FileAttributes.ReadOnly; xmldoc.Save(xmlPath); foreach (var wix in WixSetup) { FileInfo file = new FileInfo(wix.FullName); if (wix.Name.Contains("Product")) { var removeFileReadOnly = new DirectoryInfo(@projectFile.FullName + "/" + wix.Name); removeFileReadOnly.Attributes &= ~FileAttributes.ReadOnly; } file.CopyTo(@projectFile.FullName + "/" + wix.Name, true); } // Get a reference to our Team Foundation Server. TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri("http://vistfs01:8080/tfs/cgi/")); // Get a reference to Version Control. VersionControlServer versionControl = tpc.GetService <VersionControlServer>(); // Listen for the Source Control events. versionControl.NonFatalError += Program.OnNonFatalError; versionControl.Getting += Program.OnGetting; versionControl.BeforeCheckinPendingChange += Program.OnBeforeCheckinPendingChange; versionControl.NewPendingChange += Program.OnNewPendingChange; // Create a workspace. // versionControl.DeleteWorkspace("TEST3", versionControl.AuthorizedUser); Workspace workspace = versionControl.GetWorkspace("TEST3", versionControl.AuthorizedUser); Console.WriteLine("\r\n--- Create a file."); String fileName = projectFile.FullName + "\\CustomActions.CA.dll"; String fileName2 = projectFile.FullName + "\\" + projectFile.Name + ".wixproj"; String fileName3 = projectFile.FullName + "\\Product.wxs"; Console.WriteLine("\r\n--- Now add everything.\r\n"); workspace.PendAdd(fileName, true); workspace.PendEdit(fileName2); workspace.PendEdit(fileName3); Console.WriteLine("\r\n--- Show our pending changes.\r\n"); PendingChange[] pendingChanges = workspace.GetPendingChanges(); Console.WriteLine(" Your current pending changes:"); foreach (PendingChange pendingChange in pendingChanges) { Console.WriteLine("------------------------------------------------------------"); Console.WriteLine("\r\npath: " + pendingChange.LocalItem + ", \r\nFileName: " + pendingChange.FileName + ", \r\nchange: " + PendingChange.GetLocalizedStringForChangeType(pendingChange.ChangeType)); } Console.WriteLine("------------------------------------------------------------"); Console.WriteLine("------------------------------------------------------------"); } } } } else { Console.WriteLine("Project File Empty " + SolutionError); LogHelper.WriteErrorMessage("Project File Empty " + SolutionError); } // break; } } return(ProcessedProjects); } catch (Exception ex) { logging.LogMessage(LogLevel.Error, SolutionError); LogHelper.WriteErrorMessage(" FAILED at the Solution " + SolutionError); Console.WriteLine(" FAILED at the Solution " + SolutionError); throw ex; } }
public static void uploadFileToTFS() { string uploadUrl = Program.uploadUrl; // Get a reference to our Team Foundation Server. TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(uploadUrl)); // Get a reference to Version Control. VersionControlServer versionControl = tpc.GetService <VersionControlServer>(); // Create a workspace. Workspace workspace = versionControl.CreateWorkspace("tempWorkSpace", versionControl.AuthorizedUser); String topDir = null; try { String localDir = "../.."; Console.WriteLine("Create a mapping."); workspace.Map(uploadUrl, localDir); Console.WriteLine("Get the files from the repository."); workspace.Get(); Console.WriteLine("Create a file."); topDir = Path.Combine(workspace.Folders[0].LocalItem, "sub"); Directory.CreateDirectory(topDir); String fileName = Path.Combine(topDir, "basic.cs"); using (StreamWriter sw = new StreamWriter(fileName)) { sw.WriteLine("revision 1 of basic.cs"); } Console.WriteLine("Now add everything."); workspace.PendAdd(topDir, true); Console.WriteLine("Show our pending changes."); PendingChange[] pendingChanges = workspace.GetPendingChanges(); Console.WriteLine("Your current pending changes:"); foreach (PendingChange pendingChange in pendingChanges) { Console.WriteLine("path: " + pendingChange.LocalItem + ", change: " + PendingChange.GetLocalizedStringForChangeType(pendingChange.ChangeType)); } Console.WriteLine("Checkin the items we added."); int changesetNumber = workspace.CheckIn(pendingChanges, "Sample changes"); Console.WriteLine(" Checked in changeset " + changesetNumber); Console.WriteLine("Checkout and modify the file."); workspace.PendEdit(fileName); using (StreamWriter sw = new StreamWriter(fileName)) { sw.WriteLine("revision 2 of basic.cs"); } Console.WriteLine("Get the pending change and check in the new revision."); pendingChanges = workspace.GetPendingChanges(); changesetNumber = workspace.CheckIn(pendingChanges, "Modified basic.cs"); Console.WriteLine(" Checked in changeset " + changesetNumber); } finally { if (topDir != null) { Console.WriteLine("Delete all of the items under the test project."); workspace.PendDelete(topDir, RecursionType.Full); PendingChange[] pendingChanges = workspace.GetPendingChanges(); if (pendingChanges.Length > 0) { workspace.CheckIn(pendingChanges, "Clean up!"); } Console.WriteLine("Delete the workspace."); workspace.Delete(); } } }