root/trunk/SubversionSharp/test/src/CmdBase.cs

Revision 78, 15.7 kB (checked in by olivierd, 3 years ago)

[SubversionSharp]
Creating Win32 wrapper dll for subversion libraries

  • Property svn:eol-style set to native
Line 
1 //  SvnTest, a client program used to test SubversionSharp library
2 #region Copyright (C) 2004 SOFTEC sa.
3 //
4 //  SvnTest, a client program used to test SubversionSharp library
5 //  Copyright 2004 by SOFTEC sa
6 //
7 //  This program is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU General Public License as
9 //  published by the Free Software Foundation; either version 2 of
10 // the License, or (at your option) any later version.
11 //
12 //  This program is distributed in the hope that it will be useful,
13 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 //  See the GNU General Public License for more details.
16 //
17 //  You should have received a copy of the GNU General Public
18 //  License along with this program; if not, write to the Free Software
19 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 //
21 //  Sources, support options and lastest version of the complete library
22 //  is available from:
23 //              http://www.softec.st/SubversionSharp
24 //              Support@softec.st
25 //
26 //  Initial authors :
27 //              Denis Gervalle
28 //              Olivier Desaive
29 #endregion
30 //
31 using System;
32 using System.Collections;
33 using Mono.GetOptions;
34 using Softec.AprSharp;
35 using Softec.SubversionSharp;
36
37 namespace Softec.SubversionSharp.Test {
38
39         abstract class CmdBase : Options
40         {
41                 protected Application.SubCommand mSubCmd;
42                
43                 protected SvnClient client;
44                
45                 #region Options
46                
47                 [Option("print debug information", 'd', "debug")]
48                 public bool oDebug = false;
49                
50                 [Option("print as little as possible", 'q', "quiet")]
51                 public bool oQuiet = false;
52
53                 [Option("read user configuration files from {DIR}", "config-dir")]
54                 public string oConfigDir = null;
55                
56                 public bool oInteractive = true;
57                 #endregion
58                
59                 protected SvnRevision StringToRevision(string value)
60                 {
61                         try {
62                                 return new SvnRevision(int.Parse(value));
63                         }
64                         catch {}
65                        
66                         switch(value.ToUpper())
67                         {
68                                 case "HEAD":
69                                         return new SvnRevision(Svn.Revision.Head);
70                                 case "BASE":
71                                         return new SvnRevision(Svn.Revision.Base);
72                                 case "COMMITED":
73                                         return new SvnRevision(Svn.Revision.Committed);
74                                 case "PREV":
75                                         return new SvnRevision(Svn.Revision.Previous);
76                                        
77                                 default:
78                                         try
79                                         {
80                                                 return new SvnRevision(AprTime.FromDateTime(DateTime.Parse(value)));
81                                         }
82                                         catch( Exception e )
83                                         {
84                                                 if( oDebug )
85                                                         Console.WriteLine(e);
86                                                 else
87                                                         Console.WriteLine(e.Message);
88                                                 System.Environment.Exit(1);
89                                                 return(-1);
90                                         }
91                         }
92                 }
93                
94                 protected abstract int Execute();
95        
96                 public virtual int Run(Application.SubCommand sc, string[] args)
97                 {
98                         int res;
99                        
100                         mSubCmd = sc;
101                         BreakSingleDashManyLettersIntoManyOptions = true;
102                         ProcessArgs(args);
103
104                         try {
105                                 AprPool p = Svn.PoolCreate();
106                         SvnClientContext ctx = SvnClientContext.Create(p);
107                         if( oConfigDir != null )
108                                 ctx.Config = SvnConfig.GetConfig(new SvnPath(oConfigDir,p), p);
109                                 else
110                                         ctx.Config = SvnConfig.GetConfig(p);
111                                
112                                 client = new SvnClient(ctx, p);
113                                
114                                 client.AddSimpleProvider();
115                         client.AddUsernameProvider();
116                         client.AddSslServerTrustFileProvider();
117                         client.AddSslClientCertFileProvider();
118                         client.AddSslClientCertPwFileProvider();
119                        
120                         if( oInteractive )
121                         {
122                                 client.AddPromptProvider(
123                                                                 new SvnAuthProviderObject.SimplePrompt(SimpleAuth),
124                                                                 IntPtr.Zero, 2);
125                                 client.AddPromptProvider(
126                                                                 new SvnAuthProviderObject.UsernamePrompt(UsernameAuth),
127                                                                 IntPtr.Zero, 2);
128                                 client.AddPromptProvider(
129                                                                 new SvnAuthProviderObject.SslServerTrustPrompt(SslServerTrustAuth),
130                                                                 IntPtr.Zero);
131                             }
132                         client.OpenAuth();
133
134                                 if( !oQuiet )
135                                 client.Context.NotifyFunc = new SvnDelegate(new SvnWcNotify.Func(NotifyCallback));
136                                
137                         client.Context.LogMsgFunc = new SvnDelegate(new SvnClient.GetCommitLog(GetCommitLogCallback));
138                         client.Context.CancelFunc = new SvnDelegate(new Svn.CancelFunc(CancelCallback));
139                                
140                                 res = Execute();
141                         }
142                         catch( Exception e )
143                         {
144                                 if( oDebug )
145                                         Console.WriteLine(e);
146                                 else
147                                         Console.WriteLine(e.Message);
148                                 res = -1;
149                         }
150                         finally
151                         {                       
152                                 client.Pool.Destroy();
153                         }
154                         return(res);       
155                 }
156                
157                 private WhatToDoNext UsageAppend(WhatToDoNext ret)
158                 {
159                         if(mSubCmd.Description != string.Empty)
160                                 Console.WriteLine("\n{0} {1}\n",mSubCmd.LongName,mSubCmd.Description);
161                         return(ret);
162                 }
163                        
164                 public override WhatToDoNext DoUsage()
165                 {
166                         return UsageAppend(base.DoUsage());
167                 }
168
169                 public override WhatToDoNext DoHelp()
170                 {
171                         return UsageAppend(base.DoHelp());
172                 }
173                
174                
175                 private bool mInExternal = false;
176                 private bool mChanged = false;
177                 private bool mTxDelta = false;
178                
179                 public void NotifyCallback(IntPtr baton, SvnPath Path, 
180                                                    SvnWcNotify.Action action, Svn.NodeKind kind,
181                                                            AprString mimeType, SvnWcNotify.State contentState,
182                                                            SvnWcNotify.State propState, int revNum)
183             {
184                 switch(action)
185                 {
186                         case SvnWcNotify.Action.Add:
187                                         if (!mimeType.IsNull && !mimeType.ToString().StartsWith("text/"))
188                                                 Console.WriteLine("A  (bin)  {0}", Path);
189                                         else
190                                                 Console.WriteLine("A         {0}", Path);
191                                         mChanged = true;
192                                 break;
193                        
194                         case SvnWcNotify.Action.BlameRevision:
195                                 break;
196                        
197                         case SvnWcNotify.Action.CommitAdded:
198                                         if (!mimeType.IsNull && !mimeType.ToString().StartsWith("text/"))
199                                         Console.WriteLine("Adding  (bin)  {0}", Path);
200                                         else
201                                         Console.WriteLine("Adding         {0}", Path);
202                                 break;
203                        
204                         case SvnWcNotify.Action.CommitDeleted:
205                                         Console.WriteLine("Deleting       {0}", Path);
206                                 break;
207                        
208                         case SvnWcNotify.Action.CommitModified:
209                                 Console.WriteLine("Sending        {0}", Path);
210                                 break;
211                        
212                         case SvnWcNotify.Action.CommitReplaced:
213                                         Console.WriteLine("Replacing      {0}", Path);
214                                 break;
215                        
216                         case SvnWcNotify.Action.Copy:
217                                 break;
218                        
219                         case SvnWcNotify.Action.Delete:
220                                 Console.WriteLine("D         {0}", Path);
221                                 mChanged = true;
222                                 break;
223                        
224                         case SvnWcNotify.Action.FailedRevert:
225                                 Console.WriteLine("Failed to revert '{0}' -- try updating instead.", Path);
226                                 break;
227                        
228                         case SvnWcNotify.Action.PostfixTxdelta:
229                                 if( !mTxDelta )
230                                 {
231                                         Console.Write("Transmitting file data ");
232                                         mTxDelta = true;
233                                 }
234                                 Console.Write(".");
235                                 break;
236                        
237                         case SvnWcNotify.Action.Resolved:
238                                 Console.WriteLine("Resolved conflicted state of '{0}'", Path);
239                                 break;
240                        
241                         case SvnWcNotify.Action.Restore:
242                                 Console.WriteLine("Restored '{0}'", Path);
243                                         break;
244                        
245                         case SvnWcNotify.Action.Revert:
246                                 Console.WriteLine("Reverted '{0}'", Path);
247                                 break;
248                                
249                         case SvnWcNotify.Action.Skip:
250                         if (contentState == SvnWcNotify.State.Missing)
251                                         Console.WriteLine("Skipped missing target: '{0}'", Path);
252                                 else
253                                         Console.WriteLine("Skipped '{0}'", Path);
254                                         break;
255                        
256                         case SvnWcNotify.Action.StatusCompleted:
257                         if( revNum >= 0 )
258                                         Console.WriteLine("Status against revision: {0}", revNum);
259                                 break;
260                        
261                         case SvnWcNotify.Action.StatusExternal:
262                                         Console.WriteLine("\nPerforming status on external item at '{0}'", Path);
263                                 break;
264                        
265                         case SvnWcNotify.Action.UpdateAdd:
266                                 Console.WriteLine("A {0}", Path);
267                                 mChanged = true;
268                                 break;
269                        
270                         case SvnWcNotify.Action.UpdateCompleted:
271                         if( revNum >= 0 )
272                         {
273                                                 if( mSubCmd.LongName == "export" )
274                                                         Console.WriteLine("Exported {0}revision {1}.",
275                                                                                                 (mInExternal) ? "external at " : "",
276                                                                                                 revNum);
277                                                 else if( mSubCmd.LongName == "checkout" )
278                                                         Console.WriteLine("Checked out {0}revision {1}.",
279                                                                                                 (mInExternal) ? "external at " : "",
280                                                                                                 revNum);
281                                                 else
282                                                 {
283                                                         if( mChanged )
284                                         Console.WriteLine("Updated {0}to revision {1}.",
285                                                                                                         (mInExternal) ? "external at " : "",
286                                                                                                         revNum);
287                                                         else
288                                         Console.WriteLine("{0}t revision {1}.",
289                                                                                                         (mInExternal) ? "External a" : "A",
290                                                                                                         revNum);
291                                                 }
292                                         }
293                                         else  /* no revision */
294                                         {
295                                                 if( mSubCmd.LongName == "export" )
296                                         Console.WriteLine("{0}xport complete.",
297                                                                                                 (mInExternal) ? "External e" : "E");
298                                 else if( mSubCmd.LongName == "checkout" )
299                                         Console.WriteLine("{0}heckout complete.\n",
300                                                                                                 (mInExternal) ? "External c" : "C");
301                                 else
302                                         Console.WriteLine("{0}pdate complete.\n",
303                                                                                                 (mInExternal) ? "External u" : "U");
304                                         }
305                                         if( mInExternal )
306                                                 Console.WriteLine();
307                                         mInExternal = false;
308                                         mChanged = false;
309                                 break;
310                        
311                         case SvnWcNotify.Action.UpdateDelete:
312                                 Console.WriteLine("D {0}", Path);
313                                 mChanged = true;
314                                 break;
315                        
316                         case SvnWcNotify.Action.UpdateExternal:
317                                 Console.WriteLine("\nFetching external item into '{0}'", Path);
318                                 mInExternal = true;
319                                 break;
320                        
321                         case SvnWcNotify.Action.UpdateUpdate:
322                                 string s1 = " ";
323                                 string s2 = " ";
324                                         if (! ((kind == Svn.NodeKind.Dir)
325                                         && ((propState == SvnWcNotify.State.Inapplicable)
326                                          || (propState == SvnWcNotify.State.Unknown)
327                                                          || (propState == SvnWcNotify.State.Unchanged))))
328                                         {
329                                                 mChanged = true;
330                                                 if (kind == Svn.NodeKind.File)
331                                                 {
332                                                         if (contentState == SvnWcNotify.State.Conflicted)
333                                                                 s1 = "C";
334                                                         else if (contentState == SvnWcNotify.State.Merged)
335                                                                 s1 = "G";
336                                                         else if (contentState == SvnWcNotify.State.Changed)
337                                                                 s1 = "U";
338                                 }
339            
340                                                 if (propState == SvnWcNotify.State.Conflicted)
341                                                         s2 = "C";
342                                                 else if (propState == SvnWcNotify.State.Merged)
343                                                         s2 = "G";
344                                                 else if (propState == SvnWcNotify.State.Changed)
345                                                         s2 = "U";
346
347                                                 if (! ((contentState == SvnWcNotify.State.Unchanged
348                                                                 || contentState == SvnWcNotify.State.Unknown)
349                                                         && (propState == SvnWcNotify.State.Unchanged
350                                                                 || propState == SvnWcNotify.State.Unknown)))
351                                                         Console.WriteLine("{0}{1} {2}", s1, s2, Path);
352                                         }                               
353                                 break;
354                 }
355             }
356                
357                 public SvnError GetCommitLogCallback(out AprString logMessage, out SvnPath tmpFile,
358                                                                                          AprArray commitItems, IntPtr baton,
359                                                                                  AprPool pool)
360                 {
361                         if (!commitItems.IsNull)
362                         {
363                                 foreach (SvnClientCommitItem item in commitItems)
364                                 {
365                                         Console.WriteLine("C1: {1} ({2}) r{3}",
366                                                 item.Path, item.Kind, item.Revision);
367                                         Console.WriteLine("C2: {1} -> {2}",
368                                                 item.Url,
369                                                 item.CopyFromUrl);
370                                 }
371                                 Console.WriteLine();
372                         }
373                        
374                         Console.Write("Enter log message: ");
375                         logMessage = new AprString(Console.ReadLine(), pool);
376                         tmpFile = new SvnPath(pool);
377                        
378                         return(SvnError.NoError);
379                 }
380
381                
382                 public SvnError CancelCallback(IntPtr baton)
383                 {
384                         return(SvnError.NoError);               
385                 }
386                
387                 public SvnError SimpleAuth(out SvnAuthCredSimple cred, IntPtr baton,
388                                                            AprString realm, AprString username,
389                                                            bool maySave, AprPool pool)
390                 {
391                         Console.WriteLine("Simple Authentication");
392                         Console.WriteLine("---------------------");
393                         Console.WriteLine("Realm: {0}", realm);
394                         Console.WriteLine("");
395                        
396                         bool valid = false;
397                         string line = "";
398                        
399                         while(!valid)
400                         {
401                                 if (!username.IsNull)
402                                         Console.Write("Enter Username ({0}): ", username);
403                                 else
404                                         Console.Write("Enter Username: ");
405
406                                 line = Console.ReadLine();
407
408                                 if (line.Trim().Length == 0 && !username.IsNull)
409                                 {
410                                         line = username.ToString();
411                                         valid = true;
412                                 }
413                                 else if (line.Trim().Length > 0)
414                                 {
415                                         valid = true;
416                                 }
417                         }
418                        
419                         cred = SvnAuthCredSimple.Alloc(pool);
420                         cred.Username = new AprString(line, pool);
421                         Console.Write("Enter Password: ");
422                         cred.Password = new AprString(Console.ReadLine(), pool);
423                         cred.MaySave = maySave;
424                         return(SvnError.NoError);
425                 }
426                
427                 public SvnError UsernameAuth(out SvnAuthCredUsername cred, IntPtr baton,
428                                                                          AprString realm, bool maySave, AprPool pool)
429                 {
430                         Console.WriteLine("Username Authentication:");
431                         Console.WriteLine("------------------------");
432                         Console.WriteLine("Realm: {0}", realm);
433                         Console.WriteLine("");
434
435                         bool valid = false;
436                         string line = "";
437                        
438                         while(!valid)
439                         {
440                                 Console.Write("Enter Username: ");
441
442                                 line = Console.ReadLine();
443
444                                 if (line.Trim().Length > 0)
445                                 {
446                                         valid = true;
447                                 }
448                         }
449                        
450                         cred = SvnAuthCredUsername.Alloc(pool);
451                         cred.Username = new AprString(line, pool);
452                         cred.MaySave = maySave;
453                         return(SvnError.NoError);
454                 }
455                
456             public SvnError SslServerTrustAuth(out SvnAuthCredSslServerTrust cred,
457                                                                            IntPtr baton, AprString realm,
458                                                                                    SvnAuthCredSslServerTrust.CertFailures failures,
459                                                                                    SvnAuthSslServerCertInfo certInfo,
460                                                                                    bool maySave, IntPtr pool)
461                 {
462                         Console.WriteLine("Ssl Server Trust Prompt:");
463                         Console.WriteLine("------------------------");
464                         Console.WriteLine("");
465                        
466                         Console.WriteLine("Error validating server certificate for '{0}':", realm);
467                         if ((failures & SvnAuthCredSslServerTrust.CertFailures.UnknownCA) > 0)
468                                 Console.WriteLine(" - The certificate is not issued by a trusted authority");
469                         if ((failures & SvnAuthCredSslServerTrust.CertFailures.CNMismatch) > 0)
470                                 Console.WriteLine(" - The certificate hostname does not match");
471                         if ((failures & SvnAuthCredSslServerTrust.CertFailures.NotYetValid) > 0)
472                                 Console.WriteLine(" - The certificate is not yet valid");
473                         if ((failures & SvnAuthCredSslServerTrust.CertFailures.Expired) > 0)
474                                 Console.WriteLine(" - The certificate has expired");
475                         if ((failures & SvnAuthCredSslServerTrust.CertFailures.Other) > 0)
476                                 Console.WriteLine(" - The certificate has an unknown error");
477                
478                         Console.WriteLine("Certificate informations:");
479                         Console.WriteLine("\tHostName:    " + certInfo.Hostname);
480                         Console.WriteLine("\tIssuer:      " + certInfo.IssuerDName);
481                         Console.WriteLine("\tValid From:  " + certInfo.ValidFrom);
482                         Console.WriteLine("\tValid Until: " + certInfo.ValidUntil);
483                         Console.WriteLine("\tFingerprint: " + certInfo.Fingerprint);
484                
485                         cred = SvnAuthCredSslServerTrust.Alloc(pool);
486                         bool valid = false;
487                         while (!valid)
488                         {
489                                 if (maySave)
490                                         Console.WriteLine("(R)eject, accept (t)emporarily or accept (p)ermanently? ");
491                                 else
492                                         Console.WriteLine("(R)eject or accept (t)emporarily? ");
493                                        
494                                 string line = Console.ReadLine();
495                                 if (line.Length > 0)
496                                 {
497                                         char choice = line.ToLower()[0];
498                                         if (choice == 'r')
499                                         {
500                                                 cred.AcceptedFailures = 0;
501                                                 cred.MaySave=false;
502                                                 valid = true;
503                                         }
504                                         else if (choice == 't')
505                                         {
506                                                 cred.AcceptedFailures = failures;
507                                                 cred.MaySave=false;
508                                                 valid = true;
509                                         }
510                                         else if (choice == 'p')
511                                         {
512                                                 cred.AcceptedFailures = failures;
513                                                 cred.MaySave=true;
514                                                 valid = true;
515                                         }
516                                 }
517                         }
518                         return(SvnError.NoError);
519                 }
520                        
521                 }
522
523
524                 abstract class CmdBaseWithAuth : CmdBase
525                 {
526                         [Option("authentify using username {USER}", "username")]
527                         public string oUsername = null;
528                        
529                         [Option("authentify using password {PASS}", "password")]
530                         public string oPassword = null;
531                        
532                         [Option("do not cache authentication tokens", "no-auth-cache")]
533                         public bool oMayNotSave {
534                                 set { oMaySave = !value; }
535                         }
536                         public bool oMaySave = true;
537                        
538                         [Option("do no interactive prompting", "non-interactive")]
539                         public bool oNotInteractive {
540                                 set { oInteractive = !value; }
541                         }
542                 }       
543 }
Note: See TracBrowser for help on using the browser.