~monodevelop-bzr/monodevelop-bzr/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright (C) 2007 by Jelmer Vernooij <jelmer@samba.org>
//		
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//	
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//   
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Threading;

using MonoDevelop.Core;
using MonoDevelop.VersionControl;

namespace MonoDevelop.VersionControl.Bazaar
{
	public class BazaarRepository : UrlBasedRepository
	{
		private Dictionary<string,string> tempfiles;
		private Dictionary<FilePath,VersionInfo> statusCache;
		
		public BazaarRepository ()
		{
			Init ();
		}
		
		public BazaarRepository (BazaarVersionControl vcs, string url) : base (vcs)
		{
			Init ();
			Url = url;
		}

		private void Init ()
		{
			Method = BazaarVersionControl.protocols[0];
			tempfiles = new Dictionary<string,string> ();
			statusCache = new Dictionary<FilePath, VersionInfo> ();
		}// Init

		/// <summary>
		/// Remove temp files
		/// </summary>
		~BazaarRepository ()
		{
			foreach (string tmpfile in tempfiles.Values) {
				if (File.Exists (tmpfile)){ File.Delete (tmpfile); }
			}
		}// finalizer
		
		public override bool HasChildRepositories { 
			get { return true; /* TODO */ }
		}
		
		public BazaarVersionControl Bazaar {
			get { return (BazaarVersionControl) VersionControlSystem; }
		}

		public override IEnumerable<Repository> ChildRepositories {
			get {
				List<Repository> repos = new List<Repository> ();

				// System.Console.WriteLine ("Getting children for {0}", Url);
				
				foreach (string directory in Bazaar.List (Url, true, ListKind.Directory)) {
					int lastSep = directory.LastIndexOf ("/");
					if (0 < lastSep && Url.EndsWith (directory.Substring (0, lastSep))) {
						repos.Add (new BazaarRepository (Bazaar, directory));
					}// if the directory is a direct child of Url
				}// for each directory

				// foreach (Repository repo in repos){ System.Console.WriteLine ("Child repo {0}", ((UrlBasedRepository)repo).Url); }

				return repos;
			}// get
		}// ChildRepositories

		/// <value>
		/// The path where .bzr is located
		/// </value>
		public string LocalBasePath {
			get {
				return localBasePath;
			}// get
			set {
				localBasePath = value;
			}// set
		}// LocalBasePath
		private string localBasePath;

		/// <remarks>
		/// Since bzr doesn't store a text-base like svn,
		/// we're catting the baseline text to a temp file
		/// </remarks>
		public override string GetPathToBaseText (FilePath localFilePath)
		{
			string localFile = localFilePath.FullPath;

			try {
				string   tmpfile = tempfiles.ContainsKey (localFile)? tempfiles[localFile]: Path.GetTempFileName ();
				File.WriteAllText (tmpfile, Bazaar.GetTextAtRevision (localFile, new BazaarRevision (this, BazaarRevision.HEAD)));
				tempfiles[localFile] = tmpfile;
				return tmpfile;
			} catch { }

			return localFile;
		}

		public override Revision[] GetHistory (FilePath localFilePath, Revision since)
		{
			if (null == LocalBasePath) {
				// System.Console.WriteLine ("Getting local base path for {0}", localFile);
				LocalBasePath = GetLocalBasePath (localFilePath.FullPath);
				// System.Console.WriteLine ("Got base path {0}", LocalBasePath);
			}
			return Bazaar.GetHistory (this, localFilePath.FullPath, since);
		}
		
		public override VersionInfo GetVersionInfo (FilePath localPath, bool getRemoteStatus)
		{
			return statusCache[localPath] = Bazaar.GetVersionInfo (this, localPath.FullPath, getRemoteStatus);
		}
		
		/// <summary>
		/// Prefer cached version info for non-essential operations (menu display)
		/// </summary>
		private VersionInfo GetCachedVersionInfo (FilePath localPath, bool getRemoteStatus)
		{
			VersionInfo status = null;
			if (statusCache.ContainsKey (localPath)) {
				status = statusCache[localPath];
			} else {
				status = GetVersionInfo (localPath, getRemoteStatus);
			}
			return status;
		}

		public override VersionInfo[] GetDirectoryVersionInfo (FilePath localDirectory, bool getRemoteStatus, bool recursive)
		{
			VersionInfo[] versions = Bazaar.GetDirectoryVersionInfo (this, localDirectory.FullPath, getRemoteStatus, recursive);
			if (null != versions) {
				foreach (VersionInfo version in versions) {
					statusCache[version.LocalPath] = version;
				}
			}
			return versions;
		}
		
		// Deprecated
		public override Repository Publish (string serverPath, FilePath localPath, FilePath[] files, string message, IProgressMonitor monitor)
		{
			serverPath = string.Format ("{0}{1}{2}", Url, Url.EndsWith ("/")? string.Empty: "/", serverPath);
			// System.Console.WriteLine ("Got publish {0} {1}", serverPath, localPath);
			Bazaar.StoreCredentials (serverPath);
			Bazaar.Push (serverPath, localPath.FullPath, false, false, false, monitor);
			return new BazaarRepository (Bazaar, serverPath);
		}
		
		public virtual void Push (string pushLocation, FilePath localPath, bool remember, bool overwrite, bool omitHistory, IProgressMonitor monitor) {
			Bazaar.StoreCredentials (pushLocation);
			Bazaar.Push (pushLocation, localPath.FullPath, remember, overwrite, omitHistory, monitor);
		}// Push

		public virtual void Pull (string pullLocation, FilePath localPath, bool remember, bool overwrite, IProgressMonitor monitor) {
			Bazaar.StoreCredentials (pullLocation);
			Bazaar.Pull (pullLocation, localPath.FullPath, remember, overwrite, monitor);
		}// Pull

		public virtual void Merge (string mergeLocation, FilePath localPath, bool remember, bool overwrite, IProgressMonitor monitor) {
			Bazaar.StoreCredentials (mergeLocation);
			Bazaar.Merge (mergeLocation, localPath.FullPath, remember, overwrite, new BazaarRevision (this, BazaarRevision.NONE), new BazaarRevision (this, BazaarRevision.NONE), monitor);
		}// Merge
		
		public override void Update (FilePath[] localPaths, bool recurse, IProgressMonitor monitor)
		{
			foreach (FilePath localPath in localPaths)
				Bazaar.Update (localPath.FullPath, recurse, monitor);
		}

		public override void Commit (ChangeSet changeSet, IProgressMonitor monitor)
		{
			Bazaar.Commit (changeSet, monitor);
		}

		public override void Checkout (FilePath targetLocalPath, Revision rev, bool recurse, IProgressMonitor monitor)
		{
			Bazaar.StoreCredentials (Url);
			BazaarRevision brev = (null == rev)? new BazaarRevision (this, BazaarRevision.HEAD): (BazaarRevision)rev;
			Bazaar.Checkout (Url, targetLocalPath.FullPath, brev, recurse, monitor);
		}

		public override void Revert (FilePath[] localPaths, bool recurse, IProgressMonitor monitor)
		{
			foreach (FilePath localPath in localPaths)
				Bazaar.Revert (localPath.FullPath, recurse, monitor, new BazaarRevision (this, BazaarRevision.HEAD));
		}
		
		public override void Add (FilePath[] localPaths, bool recurse, IProgressMonitor monitor)
		{
			foreach (FilePath localPath in localPaths)
				Bazaar.Add (localPath.FullPath, recurse, monitor);
		}

		public override string GetTextAtRevision (FilePath repositoryPath, Revision revision)
		{
			// System.Console.WriteLine ("Got GetTextAtRevision for {0}", repositoryPath);
			BazaarRevision brev = (null == revision)? new BazaarRevision (this, BazaarRevision.HEAD): (BazaarRevision)revision;
			return Bazaar.GetTextAtRevision (repositoryPath.FullPath, brev);
		}

		public override void RevertToRevision (FilePath localPath, Revision revision, IProgressMonitor monitor)
		{
			BazaarRevision brev = (null == revision)? new BazaarRevision (this, BazaarRevision.HEAD): (BazaarRevision)revision;
			Bazaar.Revert (localPath.FullPath, true, monitor, brev);
		}

		public override void RevertRevision (FilePath localPath, Revision revision, IProgressMonitor monitor)
		{
			BazaarRevision brev = (BazaarRevision)revision;
			string localPathStr = localPath.FullPath;
			Bazaar.Merge (localPathStr, localPathStr, false, false, brev, (BazaarRevision)(brev.GetPrevious ()), monitor);
		}

		public override bool IsVersioned (FilePath localPath)
		{
			if (string.IsNullOrEmpty (GetLocalBasePath (localPath.FullPath))) {
				return false;
			} 
			
			VersionInfo info = GetCachedVersionInfo (localPath, false);
			return (null != info && info.IsVersioned);
		}

		public override bool IsModified (FilePath localFile)
		{
			if (string.IsNullOrEmpty (GetLocalBasePath (localFile.FullPath))) {
				return false;
			}
			
			VersionInfo info = GetCachedVersionInfo (localFile, false);
			return (null != info && info.IsVersioned && info.HasLocalChanges);
		}

		public virtual bool IsConflicted (FilePath localFile)
		{
			if (string.IsNullOrEmpty (GetLocalBasePath (localFile.FullPath))) {
				return false;
			}
			
			VersionInfo info = GetCachedVersionInfo (localFile, false);
			return (null != info && info.IsVersioned && (0 != (info.Status & VersionStatus.Conflicted)));
		}

		public override bool CanRevert (FilePath localPath)
		{
			return IsModified (localPath) || IsConflicted (localPath);
		}

		public override bool CanCommit (FilePath localPath)
		{
			bool rv = IsModified (localPath);
			return rv;
		}

		public override bool CanAdd (FilePath localPath)
		{
			return !IsVersioned (localPath);
		}

		public virtual bool CanResolve (FilePath localPath)
		{
			return IsConflicted (localPath);
		}

		public virtual bool CanPull (FilePath localPath)
		{
			return Directory.Exists (localPath.FullPath) && CanUpdate (localPath);
		}// CanPull

		public virtual bool CanMerge (FilePath localPath)
		{
			return Directory.Exists (localPath.FullPath) && CanUpdate (localPath);
		}// CanMerge
		
		public virtual bool CanBind (FilePath localPath)
		{
			return Directory.Exists (localPath.FullPath) && !IsBound (localPath);
		}// CanBind
		
		public virtual bool CanUnbind (FilePath localPath)
		{
			return Directory.Exists (localPath.FullPath) && IsBound (localPath);
		}// CanUnbind
		
		public virtual bool CanUncommit (FilePath localPath)
		{
			return Directory.Exists (localPath.FullPath) && IsVersioned (localPath);
		}// CanUncommit
		
		public override bool CanGetAnnotations (MonoDevelop.Core.FilePath localPath)
		{
		    return IsVersioned (localPath);
		}// CanGetAnnotations

		/// <summary>
		/// Finds the bzr branch root for a path
		/// </summary>
		/// <param name="localPath">
		/// A <see cref="System.String"/>: A path somewhere in a bzr branch
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>: The path to the branch root,
		/// or string.Empty
		/// </returns>
		public static string GetLocalBasePath (string localPath) {
			if (null == localPath){ return string.Empty; }
			if (Directory.Exists (Path.Combine (localPath, ".bzr"))){ return localPath; }

			return GetLocalBasePath (Path.GetDirectoryName (localPath));
		}// GetLocalBasePath

		public override DiffInfo[] PathDiff (FilePath baseLocalPath, FilePath[] localPaths, bool remoteDiff)
		{
			string[] localFiles = new string[null == localPaths? 0: localPaths.Length];
			for(int i=0; i<localFiles.Length; ++i){ 
				localFiles[i] = localPaths[i].ToRelative (baseLocalPath.FullPath);
			}
			
			return Bazaar.Diff (baseLocalPath.FullPath, localFiles);
		}// PathDiff

		public override void DeleteFiles (FilePath[] localPaths, bool force, IProgressMonitor monitor)
		{
			foreach (FilePath localPath in localPaths)
				Bazaar.Remove (localPath.FullPath, force, monitor);
		}// DeleteFiles

		public override void DeleteDirectories (FilePath[] localPaths, bool force, IProgressMonitor monitor)
		{
			foreach (FilePath localPath in localPaths)
				Bazaar.Remove (localPath.FullPath, force, monitor);
		}// DeleteDirectories

		public virtual void Resolve (FilePath[] localPaths, bool recurse, IProgressMonitor monitor)
		{
			foreach (FilePath localPath in localPaths)
				Bazaar.Resolve (localPath.FullPath, recurse, monitor);
		}// Resolve

		public virtual Dictionary<string, BranchType> GetKnownBranches (FilePath localPath)
		{
			return Bazaar.GetKnownBranches (localPath.FullPath);
		}// GetKnownBranches
		
		public virtual void Ignore (FilePath localPath)
		{
			Bazaar.Ignore (localPath.FullPath);
		}// Ignore
		
		public virtual bool IsBound (FilePath localPath)
		{
			return Bazaar.IsBound (localPath.FullPath);
		}// IsBound
		
		public virtual string GetBoundBranch (FilePath localPath)
		{
			return Bazaar.GetBoundBranch (localPath.FullPath);
		}// GetBoundBranch
		
		public virtual void Bind (string branchUrl, FilePath localPath, IProgressMonitor monitor)
		{
			Bazaar.Bind (branchUrl, localPath.FullPath, monitor);
		}// Bind
		
		public virtual void Unbind (FilePath localPath, IProgressMonitor monitor)
		{
			Bazaar.Unbind (localPath.FullPath, monitor);
		}// Unbind
		
		public virtual void Uncommit (FilePath localPath, IProgressMonitor monitor)
		{
			Bazaar.Uncommit (localPath.FullPath, monitor);
		}// Uncommit
		
		public override string[] GetAnnotations (FilePath localPath)
		{
			return Bazaar.GetAnnotations (localPath.FullPath);
		}// GetAnnotations
		
		/// <summary>
		/// Export a (portion of a) local tree.
		/// </summary>
		/// <param name="localPath">
		/// A <see cref="FilePath"/>: The path to be exported.
		/// </param>
		/// <param name="exportPath">
		/// A <see cref="FilePath"/>: The output path.
		/// </param>
		/// <param name="monitor">
		/// A <see cref="IProgressMonitor"/>
		/// </param>
		public virtual void Export (FilePath localPath, FilePath exportLocation, IProgressMonitor monitor)
		{
			Bazaar.Export (localPath.FullPath, exportLocation.FullPath, monitor);
		}// Export
		
		public virtual bool CanRebase ()
		{
			return Bazaar.CanRebase ();
		}// CanRebase
	}// BazaarRepository
}