public override void Run() { string path = Environment.CurrentDirectory; if (Arguments.Length > 0) path = Path.GetFullPath(Arguments[0]); char[] charsToTrim = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}; string fxdPath = path.TrimEnd(charsToTrim); string itemPath = Path.Combine(path, "*"); Workspace workspace = GetWorkspaceFromCache(); workspace.RefreshMappings(); string serverPath = workspace.GetServerItemForLocalItem(fxdPath); // pull item list based on WorkspaceVersion. otherwise might get // new items on server that haven't been pulled yet in the list returned WorkspaceVersionSpec version = new WorkspaceVersionSpec(workspace); // process command options ItemSpec itemSpec = new ItemSpec(itemPath, RecursionType.Full); ItemSet itemSet = VersionControlServer.GetItems(itemSpec, version, DeletedState.NonDeleted, ItemType.Any, true); Item[] items = itemSet.Items; SortedList<string, bool > itemList = new SortedList<string, bool >(PathComparer); foreach (Item item in items) { string serverItem = item.ServerItem.Remove(0, serverPath.Length+1); string fname = Path.Combine(path, serverItem); //Console.WriteLine(serverItem + " : " + fname); itemList.Add(fname, true); } DirectoryInfo dir = new DirectoryInfo(path); foreach (FileInfo file in dir.GetFiles("*", SearchOption.AllDirectories)) { if (!itemList.ContainsKey(file.FullName)) { if (OptionPreview) Console.WriteLine(file.FullName); else DeleteReadOnlyFile(file.FullName); } } }
/// <summary> /// Generates a version part which matches the current change set of the working copy being patched /// </summary> /// <param name="currentPartValue">The current value of the version part - this value is not used by this /// generator</param> /// <param name="ctx">The version part generation context</param> /// <returns>The TFS change set number for the current working copy being patched</returns> public int Generate(int currentPartValue, VersionPartPatchingContext ctx) { // The workspace info for the provided path var wsInfo = Workstation.Current.GetLocalWorkspaceInfo(ctx.SolutionRootPath); if(wsInfo == null) throw new InvalidOperationException(string.Format("The target solution and project must be a TFS working copy - {0}", ctx)); // Get the TeamProjectCollection and VersionControl server associated with the // WorkspaceInfo var tpc = new TfsTeamProjectCollection(wsInfo.ServerUri); var vcServer = tpc.GetService<VersionControlServer>(); // Now get the actual Workspace OM object var ws = vcServer.GetWorkspace(wsInfo); // We are interested in the current version of the workspace var versionSpec = new WorkspaceVersionSpec(ws); var historyParams = new QueryHistoryParameters(ctx.SolutionRootPath, RecursionType.Full) { ItemVersion = versionSpec, VersionEnd = versionSpec, MaxResults = 1 }; // return changeset var changeset = vcServer.QueryHistory(historyParams).FirstOrDefault(); if (changeset != null) return changeset.ChangesetId; // no changeset found. throw new InvalidOperationException(string.Format("Could not find changeset values for solution path \"{0}\"", ctx.SolutionRootPath)); }
static int Main(string[] args) { Console.InputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8; if (args.Length != 0) { Console.Error.WriteLine("This program is only expected to be called by the SonarQube TFS SCM plugin."); return 1; } Console.WriteLine("Enter your credentials"); Console.Out.Flush(); var username = Console.ReadLine(); var password = Console.ReadLine(); TfsClientCredentials credentials; if (!String.IsNullOrEmpty(username) || !String.IsNullOrEmpty(password)) { credentials = new TfsClientCredentials(new WindowsCredential(new NetworkCredential(username, password))); } else { credentials = new TfsClientCredentials(true); } credentials.AllowInteractive = false; Console.WriteLine("Enter the Collection URI"); Console.Out.Flush(); var serverUriString = Console.ReadLine(); if (!string.IsNullOrEmpty(serverUriString)) { if (!SetServerUri(serverUriString)) { return 1; } } using (var cache = new TfsCache(credentials)) { if (serverUri != null) { if (!UpdateCache(cache, serverUri)) { return 1; } } Console.Out.WriteLine("Enter the paths to annotate"); Console.Out.Flush(); string path; while ((path = Console.ReadLine()) != null) { Console.Out.Flush(); Console.WriteLine(path); if (!File.Exists(path)) { FailOnFile("does not exist: " + path); continue; } if (!Workstation.Current.IsMapped(path)) { FailOnFile("is not in a mapped TFS workspace: " + path); continue; } WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(path); WorkspaceVersionSpec version = new WorkspaceVersionSpec(workspaceInfo); if (serverUri == null || workspaceInfo.ServerUri.AbsoluteUri != serverUri.AbsoluteUri) { serverUri = workspaceInfo.ServerUri; if (!UpdateCache(cache, serverUri)) { return 1; } } var versionControlServer = cache.GetVersionControlServer(serverUri); IAnnotatedFile annotatedFile = new FileAnnotator(versionControlServer).Annotate(path, version); if (annotatedFile == null) { FailOnFile("is not yet checked-in: " + path); continue; } if (annotatedFile.IsBinary()) { FailOnFile("is a binary one: " + path); continue; } bool failed = false; for (int i = 0; !failed && i < annotatedFile.Lines(); i++) { var state = annotatedFile.State(i); if (state != AnnotationState.COMMITTED) { FailOnFile("line " + (i + 1) + " has not yet been checked-in (" + state + "): " + path); failed = true; } } if (failed) { continue; } Console.WriteLine(annotatedFile.Lines()); for (int i = 0; i < annotatedFile.Lines(); i++) { Changeset changeset = annotatedFile.Changeset(i); Console.Write(changeset.ChangesetId); Console.Write('\t'); Console.Write(cache.GetEmailOrAccountName(serverUri, changeset.Owner)); Console.Write('\t'); Console.Write(ToUnixTimestampInMs(changeset.CreationDate)); Console.Write('\t'); Console.WriteLine(annotatedFile.Data(i)); } } Console.Out.Flush(); } return 0; }
public void WorkspaceVersionSpec() { VersionSpec spec = new WorkspaceVersionSpec("name", "owner"); Assert.AreEqual("Wname;owner", spec.DisplayString); }
static int Main(string[] args) { if (args.Length == 0) { Console.Error.WriteLine("Expected exactly one argument, the file to annotate. " + args.Length + " given."); return 1; } String path = args[args.Length - 1]; if (!File.Exists(path)) { Console.Error.WriteLine("The given file does not exist: " + path); return 2; } if (!Workstation.Current.IsMapped(path)) { Console.Error.WriteLine("The given file is not in a mapped TFS workspace: " + path); return 3; } WorkspaceInfo workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(path); Uri serverUri = workspaceInfo.ServerUri; WorkspaceVersionSpec version = new WorkspaceVersionSpec(workspaceInfo); TfsClientCredentials credentials = new TfsClientCredentials(true); using (TfsTeamProjectCollection collection = new TfsTeamProjectCollection(serverUri, credentials)) { VersionControlServer server = collection.GetService<VersionControlServer>(); IAnnotatedFile annotatedFile = new FileAnnotator(server).Annotate(path, version); if (annotatedFile == null) { Console.Error.WriteLine("The given file has not yet been checked-in: " + path); return 4; } if (annotatedFile.IsBinary()) { Console.Error.WriteLine("The given file is a binary one: " + path); return 5; } for (int i = 0; i < annotatedFile.Lines(); i++) { switch (annotatedFile.State(i)) { case AnnotationState.UNKNOWN: Console.Write("unknown "); break; case AnnotationState.LOCAL: Console.Write("local "); break; case AnnotationState.COMMITTED: Changeset changeset = annotatedFile.Changeset(i); Console.Write(changeset.ChangesetId); Console.Write(' '); Console.Write(changeset.Owner); Console.Write(' '); Console.Write(changeset.CreationDate.ToString("MM/dd/yyyy")); Console.Write(' '); break; default: throw new InvalidOperationException("Unsupported annotation state: " + annotatedFile.State(i)); } Console.WriteLine(annotatedFile.Data(i)); } } return 0; }
private static Changeset GetChangeset(IBuildDetail buildDetail, Workspace workspace, IBuildAgent buildAgent) { var workspaceSourcePath = Path.Combine(buildAgent.GetExpandedBuildDirectory(buildDetail.BuildDefinition), "Sources"); var versionSpec = new WorkspaceVersionSpec(workspace); var historyParams = new QueryHistoryParameters(workspaceSourcePath, RecursionType.Full) { ItemVersion = versionSpec, VersionEnd = versionSpec, MaxResults = 1 }; var changeset = workspace.VersionControlServer.QueryHistory(historyParams).FirstOrDefault(); return changeset; }
private void ProcessFileList(SortedList<string, bool> files) { List<ItemSpec> itemSpecs = new List<ItemSpec>(); foreach (string file in files.Keys) itemSpecs.Add(new ItemSpec(file, RecursionType.None)); // pull item list based on WorkspaceVersion. otherwise might get // new items on server that haven't been pulled yet in the list returned WorkspaceVersionSpec version = new WorkspaceVersionSpec(workspace); SortedList<string, byte[] > itemList = new SortedList<string, byte[] >(PathComparer); // get item list from TFS server ItemSet[] itemSets = VersionControlServer.GetItems(itemSpecs.ToArray(), version, DeletedState.NonDeleted, ItemType.File, true); foreach (ItemSet itemSet in itemSets) { foreach (Item item in itemSet.Items) { string localItem = workspace.GetLocalItemForServerItem(item.ServerItem); itemList.Add(localItem, item.HashValue); } } // process adds and edits foreach (string file in files.Keys) { // skip files we're not interested in here if (IsExcludedFile(file)) continue; if (!File.Exists(file)) { if (OptionDeleted && itemList.ContainsKey(file)) { Console.WriteLine("Deleted: " + file); deletedFiles.Add(file); } continue; } ProcessFile(itemList, file); } }
private void ProcessDirectory(string path) { char[] charsToTrim = { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}; string itemPath = path.TrimEnd(charsToTrim); string serverPath = workspace.GetServerItemForLocalItem(itemPath); // pull item list based on WorkspaceVersion. otherwise might get // new items on server that haven't been pulled yet in the list returned WorkspaceVersionSpec version = new WorkspaceVersionSpec(workspace); // process recursion settings RecursionType rtype = OptionRecursive ? RecursionType.Full : RecursionType.OneLevel; bool recursionSetting = Settings.Current.GetAsBool("Online.Recursive"); if (recursionSetting) rtype = RecursionType.Full; SearchOption searchType = (rtype == RecursionType.Full) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly; // process command options ItemSpec itemSpec = new ItemSpec(itemPath, rtype); ItemSet itemSet = VersionControlServer.GetItems(itemSpec, version, DeletedState.NonDeleted, ItemType.Any, true); // get item list from TFS server Item[] items = itemSet.Items; SortedList<string, byte[] > itemList = new SortedList<string, byte[] >(PathComparer); foreach (Item item in items) { if (item.ServerItem.Length == serverPath.Length) continue; string serverItem = item.ServerItem.Remove(0, serverPath.Length+1); // server item paths are separated with '/', but on windows the file list below has '\' separated paths if (Path.DirectorySeparatorChar != '/') serverItem = serverItem.Replace('/', Path.DirectorySeparatorChar); string fname = Path.Combine(itemPath, serverItem); //Console.WriteLine(serverItem + " : " + fname); itemList.Add(fname, item.HashValue); } DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] localFiles = dir.GetFiles("*", searchType); SortedList<string, bool> dirList = new SortedList<string, bool>(); foreach (FileInfo file in localFiles) { // skip files we're not interested in if (IsExcludedFile(file.FullName)) continue; dirList.Add(file.FullName, true); ProcessFile(itemList, file.FullName); } foreach (DirectoryInfo di in dir.GetDirectories("*", SearchOption.AllDirectories)) { dirList.Add(di.FullName, true); } if (!OptionDeleted) return; foreach (string key in itemList.Keys) { // skip files that exist or we're not interested in if (dirList.ContainsKey(key)) continue; if (IsExcludedFile(key)) continue; Console.WriteLine("Deleted: " + key); deletedFiles.Add(key); } }