5 // Joel Reed (joelwreed@gmail.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System
.Reflection
;
31 using System
.Collections
.Generic
;
32 using System
.Collections
.Specialized
;
36 using System
.Security
.Cryptography
.X509Certificates
;
37 using Mono
.GetOptions
;
38 using Microsoft
.TeamFoundation
;
39 using Microsoft
.TeamFoundation
.Client
;
40 using Microsoft
.TeamFoundation
.VersionControl
.Client
;
41 using Microsoft
.TeamFoundation
.Server
;
43 [assembly
: AssemblyTitle ("tf.exe")]
44 [assembly
: AssemblyVersion ("1.0")]
45 [assembly
: AssemblyDescription ("Team Foundation Source Control Tool")]
46 [assembly
: AssemblyCopyright ("(c) Joel W. Reed")]
48 [assembly
: Mono
.UsageComplement ("")]
50 [assembly
: Mono
.About("Team Foundation Source Control Tool")]
51 [assembly
: Mono
.Author ("Joel W. Reed")]
53 partial class Driver
: ICertificatePolicy
55 private TeamFoundationServer _tfs
;
56 private VersionControlServer _versionControl
;
57 private DriverOptions Options
= new DriverOptions();
58 private string[] Commands
;
59 public string[] Arguments
;
61 private string domain
;
62 private string username
;
66 get { return domain; }
69 public string Username
71 get { return username; }
74 public Driver(string[] args
)
77 Options
.ProcessArgs(args
);
78 Commands
= Options
.RemainingArguments
;
81 static void ConflictEventHandler(Object sender
, ConflictEventArgs e
)
83 Console
.Error
.WriteLine(e
.Message
);
86 static void ExceptionEventHandler(Object sender
, ExceptionEventArgs e
)
88 // sometimes e.Failure is null, not sure why yet
89 if (e
.Failure
!= null)
90 Console
.Error
.WriteLine(e
.Failure
.Message
);
93 public VersionControlServer
ConnectToServer()
95 if (null != _versionControl
) return _versionControl
;
96 string server
= GetServerUrl();
97 string login
= GetLogin(server
);
102 int comma
= login
.IndexOf(",");
105 userinfo
= login
.Substring(0, comma
);
106 pwd
= login
.Substring(comma
+1);
111 Console
.Write("Password: ");
112 pwd
= Console
.ReadLine();
115 int slash
= userinfo
.IndexOf('\\');
116 if (-1 == slash
) username
= userinfo
;
119 domain
= userinfo
.Substring(0, slash
);
120 username
= userinfo
.Substring(slash
+1);
123 _tfs
= new TeamFoundationServer(server
, new NetworkCredential(username
, pwd
, domain
));
124 _versionControl
= (VersionControlServer
) _tfs
.GetService(typeof(VersionControlServer
));
126 _versionControl
.Conflict
+= ConflictEventHandler
;
127 _versionControl
.NonFatalError
+= ExceptionEventHandler
;
129 // save credentials if passed
130 bool saveSetting
= Settings
.Current
.GetAsBool("Credentials.Save");
131 if (saveSetting
&& !String
.IsNullOrEmpty(Options
.Login
))
132 TfsKeyring
.SetCredentials(server
, Domain
, Username
, pwd
);
134 return _versionControl
;
137 static public void ShowHelp()
139 Console
.WriteLine("usage: tf [subcommand] [arguments]");
141 Console
.WriteLine("Available subcommands:");
144 CommandRegistry
.ShowHelp();
147 public void ProcessAllCommands()
149 // some commands don't need to connect to TFS server
150 string firstCmd
= Commands
[0].ToLower();
156 cmd
= new CacheCommand(this);
160 cmd
= new ConfigureCommand(this);
174 VersionControlServer vcs
= ConnectToServer();
176 for (int i
=0; i
< Commands
.Length
; i
++)
178 string scmd
= Commands
[i
].ToLower();
179 List
<string> cmdArgs
= new List
<string>();
183 for (j
=i
+1; j
< Commands
.Length
; j
++)
185 string arg
= Commands
[j
];
186 if (arg
== "%") break;
190 ProcessCommand(vcs
, scmd
, cmdArgs
.ToArray());
195 public void ProcessCommand(VersionControlServer vcs
, string cmd
,
198 Type commandType
= CommandRegistry
.GetCommandType(cmd
);
199 if (commandType
== null)
201 Console
.WriteLine("Unknown command: " + cmd
);
205 Command command
= (Command
) Activator
.CreateInstance(commandType
, new object[]{ this }
);
209 public string GetServerUrl()
211 if (!String
.IsNullOrEmpty(Options
.Server
))
213 if (Options
.Server
.StartsWith("http://") || Options
.Server
.StartsWith("https://"))
214 return Options
.Server
;
216 return String
.Format("http://{0}:8080/", Options
.Server
);
219 WorkspaceInfo info
= Workstation
.Current
.GetLocalWorkspaceInfo(Environment
.CurrentDirectory
);
222 Console
.WriteLine("Unable to determine the team foundation server");
223 Console
.WriteLine(" hint: try adding /server:<ip|name>");
227 return info
.ServerUri
.ToString();
230 public string GetLogin(string server
)
232 if (!String
.IsNullOrEmpty(Options
.Login
))
233 return Options
.Login
;
236 string login
= TfsKeyring
.GetCredentials(server
);
237 if (!String
.IsNullOrEmpty(login
)) return login
;
240 return PromptForLogin();
243 public VersionControlServer VersionControlServer
245 get { return _versionControl; }
248 // ignoring certificate errors
249 public bool CheckValidationResult (ServicePoint sp
,
250 X509Certificate certificate
, WebRequest request
, int error
)
255 public static void Main(string[] args
)
257 if (args
.Length
== 0) {
262 Driver driver
= new Driver(args
);
263 ServicePointManager
.CertificatePolicy
= driver
;
265 // basic auth doesn't seem to work well on mono when POSTing
266 // large data sets via a webservice.
267 AuthenticationManager
.Unregister("Basic");
271 driver
.ProcessAllCommands();
273 catch (TeamFoundationServerException e
)
275 Console
.Error
.WriteLine(e
.Message
);
276 // Console.WriteLine(Domain + "\\" + Username);