1 #ifndef _SvnLib_h_
2 #define _SvnLib_h_
3 
4 #include <Core/Core.h>
5 
6 NAMESPACE_UPP
7 
8 //////////////////////////////////////////////////////////////////////////////////////////////
9 // svn interface class
10 // encapsulates svn operation using command line tool 'svn'
11 class Svn
12 {
13 	public:
14 		// types of svn nodes
15 		enum NodeTypes
16 		{
17 			SvnUnknown,
18 			SvnDir
19 		};
20 
21 		// errors returned by various svn functions
22 		enum Errors
23 		{
24 			Ok			= 0,
25 			UnknownError,
26 			NoSvnApp,
27 			NotARepo,
28 			NotConnected,
29 			FileNotFound,
30 			PathNotFound,
31 			FileExists,
32 			DirectoryExists,
33 			PathExists,
34 			DirectoryNotEmpty,
35 			NotADirectory,
36 			CantRemoveDirectory,
37 			CantWriteDirectory,
38 			PathNotInRepo,
39 			NotAnURL,
40 			BadXml,
41 			BadLogin
42 		};
43 
44 		// Human-readable error messages
45 		static const char *ErrorMessages[];
46 
47 		// Item status
48 		enum ItemStatus
49 		{
50 			unknown,
51 			added,
52 			missing,
53 			incomplete,
54 			replaced,
55 			modified,
56 			merged,
57 			conflicted,
58 			obstructed,
59 			ignored,
60 			external,
61 			unversioned,
62 
63 		}; // END enum ItemStatus
64 
65 		// Human-readable status strings
66 		static const char *StatusStrings[];
67 		static const int NumStatus;
68 
69 		// from status string, gets status
70 		static ItemStatus String2Status(String const &s);
71 
72 		// from status, gets human-readable string
73 		static String Status2String(ItemStatus s);
74 
75 		// svn entry info
76 		struct EntryInfo
77 		{
78 			String	kind;
79 			String	path;
80 			long	revision;
81 			String	url;
82 			String	repositoryRoot;
83 			String	repositoryUUID;
84 			long	commitRevision;
85 			String	commitAuthor;
86 			String	commitDate;
87 
88 		}; // END struct EntryInfo;
89 
90 		// svn log info
91 		struct LogInfo
92 		{
93 			long	revision;
94 			String	author;
95 			String	message;
96 			String	date;
97 
98 		}; // END struct LogInfo
99 
100 		// svn status info
101 		struct StatusInfo
102 		{
103 			String		path;
104 			String		props;
105 			ItemStatus	status;
106 			long		revision;
107 			long		commitRevision;
108 			String		commitAuthor;
109 			String		commitDate;
110 
111 		}; // END struct StatusInfo
112 
113 	private:
114 		// log-in stuffs
115 		String		FUserName;
116 		String		FPassword;
117 		bool		FAnonymous;
118 
119 		// svn repository info stuffs
120 		String		FLocalPath;
121 		String		FRepositoryRoot;
122 		String		FRepositoryUUID;
123 
124 		String		FCheckedRevisionAuthor;
125 		long		FCheckedRevision;
126 		String		FCheckedRevisionDate;
127 
128 		String		FHeadRevisionAuthor;
129 		long		FHeadRevision;
130 		String		FHeadRevisionDate;
131 
132 		bool		FConnected;
133 		Errors		FLastError;
134 
135 		String		FLastCommandOutput;
136 		String		FLastCommandError;
137 
138 		// executes svn, passing cmdline as argument, gathering output in Output
139 		// in xml format (where available) if xml is true
140 		Svn::Errors ExecSvn(String const &CmdLine, String &OutStr, String &ErrStr, bool xml = false);
141 
142 		// parses output of 'svn info' command and gathers its output
143 		Errors ParseInfo(String const &str, EntryInfo &info);
144 
145 		//  parses xml output of 'svn log' command
146 		Errors ParseLog(String const &str, Array<LogInfo> &info);
147 
148 		// parses xml output of 'svn status' command
149 		Errors ParseStatus(String const &str, Array<StatusInfo> &info);
150 
151 		// builds username/password string
152 		String BuildUserPassword(void);
153 
154 		// builds revision string
155 		String BuildRevision(String const &Revision);
156 
157 		// checks whether a string is a remote URL or a local path
158 		bool IsUrl(String const &path);
159 
160 		// checks whether a path is inside local repository
161 		bool IsLocalPath(String const &path);
162 
163 		// checks whether a path is inside remote repository
164 		bool IsRemotePath(String const &path);
165 
166 		// checks whether a path is inside repository, remote or local side
167 		bool IsRepositoryPath(String const &path);
168 
169 		// initializer... used by constructor and on disconnection
170 		void Init(void);
171 
172 	public:
173 
174 		// constructor
175 		Svn();
176 
177 		// destructor
178 		~Svn();
179 
180 		// sets error condition
SetError(Errors e)181 		Errors SetError(Errors e) { FLastError = e; return e; }
182 
183 		// checks wether a path is a svn
184 		bool IsSvn(String const &LocalPath);
185 
186 		// checks wether the repository is connected
IsConnected(void)187 		bool IsConnected(void) { return FConnected; }
188 
189 		// gets local path of repository
GetLocalPath()190 		String		GetLocalPath()						{ return FLocalPath; }
GetRepositoryRoot()191 		String		GetRepositoryRoot()					{ return FRepositoryRoot; }
GetRepositoryUUID()192 		String		GetRepositoryUUID()					{ return FRepositoryUUID; }
GetCheckedRevisionAuthor()193 		String		GetCheckedRevisionAuthor()			{ return FCheckedRevisionAuthor; }
GetCheckedRevision()194 		long		GetCheckedRevision()				{ return FCheckedRevision; }
GetCkeckedRevisionDate()195 		String		GetCkeckedRevisionDate()			{ return FCheckedRevisionDate; }
GetHeadRevisionAuthor()196 		String		GetHeadRevisionAuthor()				{ return FHeadRevisionAuthor; }
GetHeadRevision()197 		long		GetHeadRevision()					{ return FHeadRevision; }
GetHeadRevisionDate()198 		String		GetHeadRevisionDate()				{ return FHeadRevisionDate; }
199 
GetLastError()200 		Errors		GetLastError()						{ return FLastError; }
GetErrorMessage(Svn::Errors err)201 		String		GetErrorMessage(Svn::Errors err)	{ return ErrorMessages[err]; }
GetErrorMessage(void)202 		String		GetErrorMessage(void)				{ return ErrorMessages[FLastError]; }
203 
GetLastCommandOutput()204 		String		GetLastCommandOutput()				{ return FLastCommandOutput; }
GetLastCommandError()205 		String		GetLastCommandError()				{ return FLastCommandError; }
206 
207 		// sets user authentication
208 		void SetUser(String const &UserName, String const &Password);
209 		void SetUser(void);
210 
211 		// connects to the local copy
212 		Errors Connect(String const &LocalPath);
213 
214 		// disconnects from local copy
215 		Errors Disconnect(void);
216 
217 		// checks out from server
218 		Errors Checkout(String const &Url, String const &LocalPath, String const &Revision = "");
219 
220 		// updates current copy from server
221 		Errors Update(String const &Revision = "");
222 
223 		// commits to server
224 		Errors Commit(String const &Message);
225 
226 		// adds file(s) to current repository
227 		Errors Add(String const &FileName);
228 		Errors Add(Array<String> const &FileNames);
229 
230 		// removes file(s) from current repository
231 		Errors Delete(String const &FileName);
232 		Errors Delete(Array<String> const &FileNames);
233 
234 		// copy a file inside current repository
235 		Errors Copy(String const &Src, String const &Dest);
236 
237 		// moves a file inside current repository
238 		Errors Move(String const &Src, String const &Dest);
239 
240 		// gets log from remote
241 		Errors GetLog(Array<Svn::LogInfo>&logArray);
242 
243 		// gets local repository status
244 		Errors GetStatus(Array<Svn::StatusInfo>&statusArray);
245 
246 		// updates info from current repository
247 		Errors UpdateInfo(void);
248 
249 }; // END Class Svn
250 
251 /*
252 	blame
253 	cat
254 	cleanup
255 	diff
256 	export
257 	import
258 	list
259 	lock
260 	log
261 	merge
262 	mkdir
263 	propdel (pdel, pd)
264 	propedit (pedit, pe)
265 	propget (pget, pg)
266 	proplist (plist, pl)
267 	propset (pset, ps)
268 	resolved
269 	revert
270 	status (stat, st)
271 	switch (sw)
272 	unlock
273 */
274 
275 END_UPP_NAMESPACE
276 
277 #endif
278