Added container accessor to Castle.Core
[castle.git] / Tools / SubversionHooks / Hooks / DefaultRepository.cs
blob5c286bdb502aeba3ca232441f6eb9c8743c6905a
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.SvnHooks
17 using System;
18 using System.Collections;
19 using System.IO;
20 using System.Diagnostics;
22 /// <summary>
23 /// Summary description for DefaultRepository.
24 /// </summary>
25 public class DefaultRepository : IRepository
27 private DefaultRepository(String svnLookPath, String repositoryPath)
29 this.SvnLook = new SvnLook(svnLookPath, repositoryPath);
31 public DefaultRepository(String svnLookPath, String repositoryPath, int revision)
32 : this(svnLookPath, repositoryPath)
34 this.RevisionOrTransaction = revision;
36 public DefaultRepository(String svnLookPath, String repositoryPath, Transaction transaction)
37 : this(svnLookPath, repositoryPath)
39 this.RevisionOrTransaction = transaction;
43 #region IRepository Members
45 public virtual System.IO.Stream GetFileContents(String path)
47 using(Process proc = SvnGetFileContents(path))
49 MemoryStream stream = new MemoryStream();
50 using(StreamWriter writer = new StreamWriter(stream))
52 String line;
53 while((line = proc.StandardOutput.ReadLine()) != null)
55 writer.WriteLine(line);
59 return new MemoryStream(stream.ToArray(), false);
63 public virtual String[] GetProperty(String path, String property)
65 ArrayList properties = new ArrayList();
66 using(Process proc = SvnGetProperty(path, property))
68 String line;
69 while((line = proc.StandardOutput.ReadLine()) != null)
71 // Any line starting with svnlook: is an error message.
72 // Technically you might get that from a log message,
73 // but thats not considered in the current implementation.
74 if (line.StartsWith("svnlook:"))
76 Console.Error.WriteLine(line);
77 return null;
80 properties.Add(line);
84 // If we have no properties in the list
85 // then return null, otherwise return the
86 // properties in an array.
87 if (properties.Count > 0)
88 return (String[]) properties.ToArray(typeof(String));
89 return null;
93 #endregion
95 protected Process SvnGetProperty(String path, String property)
97 String arguments = String.Concat(property, " \"", path, "\"");
99 return SwitchedExecuteSvnLook(SvnLookCommand.PropertyGet, arguments);
101 protected Process SvnGetFileContents(String path)
103 return SwitchedExecuteSvnLook(SvnLookCommand.Cat, String.Concat('"', path, '"'));
107 private Process SwitchedExecuteSvnLook(SvnLookCommand command, String arguments)
109 if(RevisionOrTransaction is int)
111 return SvnLook.Execute(command, (int)RevisionOrTransaction, arguments);
113 else if(RevisionOrTransaction is Transaction)
115 return SvnLook.Execute(command, (Transaction)RevisionOrTransaction, arguments);
117 // Due to constructor logic we can never actually reach this point
118 throw new InvalidOperationException("RevisionOrTransaction is not a valid type");
122 private readonly ValueType RevisionOrTransaction;
123 private readonly SvnLook SvnLook;