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 TfOptions Options
;
58 private string[] Commands
;
60 public Driver(TfOptions options
, string[] commands
)
66 static void ConflictEventHandler(Object sender
, ConflictEventArgs e
)
68 Console
.Error
.WriteLine(e
.Message
);
71 static void ExceptionEventHandler(Object sender
, ExceptionEventArgs e
)
73 // sometimes e.Failure is null, not sure why yet
74 if (e
.Failure
!= null)
75 Console
.Error
.WriteLine(e
.Failure
.Message
);
78 public VersionControlServer
ConnectToServer()
80 if (null != _versionControl
) return _versionControl
;
81 string server
= GetServerUrl();
82 string login
= GetLogin(server
);
87 int comma
= login
.IndexOf(",");
90 userinfo
= login
.Substring(0, comma
);
91 pwd
= login
.Substring(comma
+1);
96 Console
.Write("Password: ");
97 pwd
= Console
.ReadLine();
100 int slash
= userinfo
.IndexOf('\\');
101 if (-1 == slash
) Options
.Username
= userinfo
;
104 Options
.Domain
= userinfo
.Substring(0, slash
);
105 Options
.Username
= userinfo
.Substring(slash
+1);
108 _tfs
= new TeamFoundationServer(server
, new NetworkCredential(Options
.Username
, pwd
, Options
.Domain
));
109 _versionControl
= (VersionControlServer
) _tfs
.GetService(typeof(VersionControlServer
));
111 _versionControl
.Conflict
+= ConflictEventHandler
;
112 _versionControl
.NonFatalError
+= ExceptionEventHandler
;
114 // save credentials if passed
115 bool saveSetting
= Settings
.Current
.GetAsBool("Credentials.Save");
116 if (saveSetting
&& !String
.IsNullOrEmpty(Options
.Login
))
117 TfsKeyring
.SetCredentials(server
, Options
.Domain
, Options
.Username
, pwd
);
119 return _versionControl
;
122 static public void ShowHelp()
124 Console
.WriteLine("usage: tf [subcommand] [arguments]");
126 Console
.WriteLine("Available subcommands:");
129 CommandRegistry
.ShowHelp();
132 public void ProcessBranch() {}
133 public void ProcessBranches() {}
134 public void ProcessDifference() {}
135 public void ProcessLock() {}
136 public void ProcessMerge() {}
137 public void ProcessMerges() {}
138 public void ProcessResolve() {}
139 public void ProcessShelve() {}
140 public void ProcessShelvesets() {}
141 public void ProcessUndelete() {}
142 public void ProcessUnlabel() {}
143 public void ProcessUnshelve() {}
145 public void ProcessAllCommands()
147 // some commands don't need to connect to TFS server
148 string firstCmd
= Commands
[0].ToLower();
154 cmd
= new CacheCommand(Commands
, Options
);
158 cmd
= new ConfigureCommand(Commands
, Options
);
172 VersionControlServer vcs
= ConnectToServer();
174 for (int i
=0; i
< Commands
.Length
; i
++)
176 string scmd
= Commands
[i
].ToLower();
177 List
<string> cmdArgs
= new List
<string>();
181 for (j
=i
+1; j
< Commands
.Length
; j
++)
183 string arg
= Commands
[j
];
184 if (arg
== "%") break;
188 ProcessCommand(vcs
, scmd
, cmdArgs
.ToArray());
193 public void ProcessCommand(VersionControlServer vcs
, string cmd
,
196 Type commandType
= CommandRegistry
.GetCommandType(cmd
);
197 if (commandType
== null)
199 Console
.WriteLine("Unknown command: " + cmd
);
203 Command command
= (Command
) Activator
.CreateInstance(commandType
, new object[]{cmdArgs, Options, vcs}
);
207 public string GetServerUrl()
209 if (!String
.IsNullOrEmpty(Options
.Server
))
211 if (Options
.Server
.StartsWith("http://") || Options
.Server
.StartsWith("https://"))
212 return Options
.Server
;
214 return String
.Format("http://{0}:8080/", Options
.Server
);
217 WorkspaceInfo info
= Workstation
.Current
.GetLocalWorkspaceInfo(Environment
.CurrentDirectory
);
220 Console
.WriteLine("Unable to determine the team foundation server");
221 Console
.WriteLine(" hint: try adding /server:<ip|name>");
225 return info
.ServerUri
.ToString();
228 public string GetLogin(string server
)
230 if (!String
.IsNullOrEmpty(Options
.Login
))
231 return Options
.Login
;
234 string login
= TfsKeyring
.GetCredentials(server
);
235 if (!String
.IsNullOrEmpty(login
)) return login
;
238 return PromptForLogin();
241 public VersionControlServer VersionControlServer
243 get { return _versionControl; }
246 // ignoring certificate errors
247 public bool CheckValidationResult (ServicePoint sp
,
248 X509Certificate certificate
, WebRequest request
, int error
)
253 public static void Main(string[] args
)
255 if (args
.Length
== 0) {
260 TfOptions options
= new TfOptions();
261 options
.ProcessArgs(args
);
263 Driver driver
= new Driver(options
, options
.RemainingArguments
);
264 ServicePointManager
.CertificatePolicy
= driver
;
266 // basic auth doesn't seem to work well on mono when POSTing
267 // large data sets via a webservice.
268 AuthenticationManager
.Unregister("Basic");
272 driver
.ProcessAllCommands();
274 catch (TeamFoundationServerException e
)
276 Console
.Error
.WriteLine(e
.Message
);
277 // Console.WriteLine(options.Domain + "\\" + options.Username);