~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
// 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.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

using Gtk;
using MonoDevelop.Core;
using MonoDevelop.VersionControl;

namespace MonoDevelop.VersionControl.Bazaar
{
	public class BazaarVersionControl : VersionControlSystem
	{
		private BazaarClient client;
		
		// TODO: Obtain this list from bzr.
		/// <value>
		/// Protocols supported by this addin
		/// </value>
		public static readonly string[] protocols = {
			"http", "https", "bzr", "bzr+ssh", "sftp", 
			"ftp", "file" 
		};

		public override string Name {
			get { return "Bazaar"; }
		}// Name
		
		public override bool IsInstalled {
			get {
				if (!installChecked) {
					installed = Client.CheckInstalled ();
					installChecked = true;
				}
				return installed;
			}
		}// IsInstalled
		private bool installed;
		private bool installChecked;

		/// <value>
		/// Bazaar client
		/// </value>
		public BazaarClient Client {
			get {
				if (null == client) { client = new BazaarCLibClient (); }
				return client;
			}
		}// Client
		
		public override Widget CreateRepositoryEditor (Repository repo)
		{
			return new UrlBasedRepositoryEditor ((BazaarRepository)repo, protocols);
		}// CreateRepositoryEditor

		protected override Repository OnCreateRepositoryInstance ()
		{
			return new BazaarRepository ();
		}// OnCreateRepositoryInstance

		public IList<string> List (string path, bool recurse, ListKind kind) {
			return Client.List (path, recurse, kind);
		}// List

		/// <summary>
		/// Gets the status of a version-controlled file
		/// </summary>
		/// <param name="repo">
		/// A <see cref="Repository"/> to which the file belongs
		/// </param>
		/// <param name="sourcefile">
		/// A <see cref="System.String"/>: The filename
		/// </param>
		/// <param name="getRemoteStatus">
		/// A <see cref="System.Boolean"/>: unused
		/// </param>
		/// <returns>
		/// A <see cref="VersionInfo"/> representing the file status
		/// </returns>
		private VersionInfo GetFileStatus (Repository repo, string sourcefile, bool getRemoteStatus)
		{
			IList<LocalStatus> statuses = Client.Status (sourcefile, null);
			
			if (null == statuses || statuses.Count == 0)
				throw new ArgumentException ("Path '" + sourcefile + "' does not exist in the repository.");
			
			return CreateNode (statuses[0], repo);
		}// GetFileStatus

		/// <summary>
		/// Create a VersionInfo from a LocalStatus
		/// </summary>
		private VersionInfo CreateNode (LocalStatus status, Repository repo) 
		{
			VersionStatus rs = VersionStatus.Unversioned;
			Revision rr = null;
			
			VersionStatus vstatus = ConvertStatus (status.Status);
			// System.Console.WriteLine ("Converted {0} to {1} for {2}", status.Status, vstatus, status.Filename);

			VersionInfo ret = new VersionInfo (status.Filename, Path.GetFullPath (status.Filename), Directory.Exists (status.Filename),
			                                   vstatus, new BazaarRevision (repo, status.Revision),
			                                   rs, rr);
			return ret;
		}// CreateNode

		/// <summary>
		/// Create a VersionInfo[] from an IList<LocalStatus>
		/// </summary>
		private VersionInfo[] CreateNodes (Repository repo, IList<LocalStatus> statuses) {
			List<VersionInfo> nodes = new List<VersionInfo> (statuses.Count);

			foreach (LocalStatus status in statuses) {
				nodes.Add (CreateNode (status, repo));
			}

			return nodes.ToArray ();
		}// CreateNodes
		

		/// <summary>
		/// Convert an ItemStatus to a VersionStatus
		/// </summary>
		private VersionStatus ConvertStatus (ItemStatus status) {
			switch (status) {
			case ItemStatus.Added:
				return VersionStatus.Versioned | VersionStatus.ScheduledAdd;
			case ItemStatus.Conflicted:
				return VersionStatus.Versioned | VersionStatus.Conflicted;
			case ItemStatus.Deleted:
				return VersionStatus.Versioned | VersionStatus.ScheduledDelete;
			case ItemStatus.Ignored:
				return VersionStatus.Versioned | VersionStatus.Ignored;
			case ItemStatus.Modified:
				return VersionStatus.Versioned | VersionStatus.Modified;
			case ItemStatus.Replaced:
				return VersionStatus.Versioned | VersionStatus.ScheduledReplace;
			case ItemStatus.Unchanged:
				return VersionStatus.Versioned;
			}

			return VersionStatus.Unversioned;
		}// ConvertStatus

		public override Repository GetRepositoryReference (FilePath path, string id)
		{
			// System.Console.WriteLine ("Requested repository reference for {0}", path);
			try {
				if (string.IsNullOrEmpty (BazaarRepository.GetLocalBasePath (path.FullPath)))
					return null;
				string url = Client.GetPathUrl (path.FullPath);
				// System.Console.WriteLine ("Got {0} for {1}", url, path);
				return new BazaarRepository (this, url);
			} catch (Exception ex) {
				// No bzr
				LoggingService.LogError (ex.ToString ());
				return null;
			}
		}// GetRepositoryReference

		public VersionInfo GetVersionInfo (Repository repo, string localPath, bool getRemoteStatus)
		{
			return GetFileStatus (repo, localPath, getRemoteStatus);
		}

		public VersionInfo[] GetDirectoryVersionInfo (Repository repo, string sourcepath, bool getRemoteStatus, bool recursive) {
			IList<LocalStatus> statuses = Client.Status (sourcepath, null);
			return CreateNodes (repo, statuses);
		}
		

		public void Update (string localPath, bool recurse, IProgressMonitor monitor) {
			Client.Update (localPath, recurse, monitor);
		}// Update

		public bool IsVersioned (string localPath)
		{
			return ((string.Empty != BazaarRepository.GetLocalBasePath (localPath)) && Client.IsVersioned (localPath));
		}

		public IList<LocalStatus> Status (string localPath, BazaarRevision revision) {
			return Client.Status (localPath, null);
		}// Status

		public void Revert (string localPath, bool recurse, IProgressMonitor monitor, BazaarRevision toRevision) {
			Client.Revert (localPath, recurse, monitor, toRevision);
		}// Revert

		public void Add (string localPath, bool recurse, IProgressMonitor monitor) {
			Client.Add (localPath, recurse, monitor);
			FileService.NotifyFileChanged (localPath);
		}// Add

		public void Checkout (string url, string targetLocalPath, BazaarRevision rev, bool recurse, IProgressMonitor monitor) {
			Client.Checkout (url, targetLocalPath, rev, recurse, monitor);
		}// Checkout

		public void Branch (string branchLocation, string localPath, IProgressMonitor monitor)
		{
			StoreCredentials (branchLocation);
			Client.Branch (branchLocation, localPath, monitor);
		}// Branch
		
		public string GetTextAtRevision (string path, BazaarRevision revision) {
			return Client.GetTextAtRevision (path, revision);
		}// GetTextAtRevision

		public Revision[] GetHistory (BazaarRepository repo, string localFile, Revision since) {
			BazaarRevision brev = (null == since)? new BazaarRevision (repo, BazaarRevision.FIRST): (BazaarRevision)since;
			return Client.GetHistory (repo, localFile, brev);
		}// GetHistory

		public void Merge (string mergeLocation, string localPath, bool remember, bool overwrite, BazaarRevision start, BazaarRevision end, IProgressMonitor monitor) {
			Client.Merge (mergeLocation, localPath, remember, overwrite, start, end, monitor);
		}// Merge

		public void Push (string pushLocation, string localPath, bool remember, bool overwrite, bool omitHistory, IProgressMonitor monitor) {
			if (omitHistory) {
				Client.DPush (pushLocation, localPath, remember, monitor);
			} else {
				Client.Push (pushLocation, localPath, remember, overwrite, monitor);
			}
		}// Push

		public void Pull (string pullLocation, string localPath, bool remember, bool overwrite, IProgressMonitor monitor) {
			Client.Pull (pullLocation, localPath, remember, overwrite, monitor);
		}// Pull
		
		public void Commit (ChangeSet changeSet, IProgressMonitor monitor) {
			if (Client.IsMergePending (changeSet.BaseLocalPath.FullPath)) {
				int result = (int)ResponseType.Cancel;
				MonoDevelop.Core.Gui.DispatchService.GuiSyncDispatch(delegate{ 
					MessageDialog warningDialog = new MessageDialog (null, DialogFlags.Modal, MessageType.Warning, ButtonsType.OkCancel, 
					                   GettextCatalog.GetString ("Because there are merges pending, all pending changes must be committed together. Proceed?"));
					result = warningDialog.Run ();
					warningDialog.Destroy ();
				});// Warn user, see if she wants to proceed
				if ((int)ResponseType.Ok == result) {
					ChangeSet newChangeSet = changeSet.Repository.CreateChangeSet (changeSet.BaseLocalPath);
					newChangeSet.GlobalComment = changeSet.GlobalComment;
					changeSet = newChangeSet;
				} else {
					monitor.Log.WriteLine (GettextCatalog.GetString ("Aborted by user."));
					return;
				}
			}// if there are merges pending commit
			
			Client.Commit (changeSet, monitor);
			foreach (ChangeSetItem csi in changeSet.Items) {
				FileService.NotifyFileChanged (csi.LocalPath);
			}// Refresh file status
		}// Commit

		public DiffInfo[] Diff (string basePath, string[] files) {
			return Client.Diff (basePath, files);
		}// Diff

		public void Remove (string path, bool force, IProgressMonitor monitor) {
			Client.Remove (path, force, monitor);
		}// Remove

		public void Resolve (string path, bool recurse, IProgressMonitor monitor)
		{
			Client.Resolve (path, recurse, monitor);
			FileService.NotifyFileChanged (path);
		}// Resolve

		public Dictionary<string, BranchType> GetKnownBranches (string path)
		{
			return Client.GetKnownBranches (path);
		}// GetKnownBranches
		
		public void StoreCredentials (string url)
		{
			Client.StoreCredentials (url);
		}// StoreCredentials
		
		/// <summary>
		/// Initialize a new Bazaar repo
		/// </summary>
		/// <param name="newRepoPath">
		/// A <see cref="System.String"/>: The path at which to initialize a new repo
		/// </param>
		public void Init (string newRepoPath)
		{
			Client.Init (newRepoPath);
		}// Init
		
		public void Ignore (string path)
		{
			Client.Ignore (path);
		}// Ignore
		
		public bool IsBound (string path)
		{
			return Client.IsBound (path);
		}// IsBound
		
		public string GetBoundBranch (string path)
		{
			return Client.GetBoundBranch (path);
		}// GetBoundBranch
		
		public void Bind (string branchUrl, string localPath, IProgressMonitor monitor)
		{
			Client.Bind (branchUrl, localPath, monitor);
		}// Bind
		
		public void Unbind (string localPath, IProgressMonitor monitor)
		{
			Client.Unbind (localPath, monitor);
		}// Unbind
		
		public void Uncommit (string localPath, IProgressMonitor monitor)
		{
			Client.Uncommit (localPath, monitor);
		}// Uncommit
		
		public string[] GetAnnotations (string localPath)
		{
			return Client.GetAnnotations (localPath);
		}// GetAnnotations
		
		public void Export (string localPath, string exportPath, IProgressMonitor monitor)
		{
			Client.Export (localPath, exportPath, monitor);
		}// Export
		
		public bool CanRebase ()
		{
			return Client.CanRebase ();
		}// CanRebase
	}// BazaarVersionControl
}