static int Main(string[] args) { try { 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(); var pat = Console.ReadLine(); TfsClientCredentials credentials; if (!String.IsNullOrEmpty(pat)) { credentials = new TfsClientCredentials(new BasicAuthCredential(new NetworkCredential("", pat))); } else 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) { try { 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)); } } catch (Exception e) { FailOnFile(e.Message); } } Console.Out.Flush(); } return(0); } catch (Exception e) { FailOnProject(e.Message); return(1); } }
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; }
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; }
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); }