Welcome to mirror list, hosted at ThFree Co, Russian Federation.

VersionControlService.cs « MonoDevelop.VersionControl « MonoDevelop.VersionControl « VersionControl « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9d204b43093a9012a330e86f71241b21c1f35924 (plain)
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822

using System;
using System.Linq;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;

using MonoDevelop.Ide.Gui;
using MonoDevelop.Core;
using MonoDevelop.Projects;
using MonoDevelop.Projects.Policies;
using MonoDevelop.Core.Serialization;
using Mono.Addins;
using MonoDevelop.Ide;
using MonoDevelop.Core.ProgressMonitoring;

namespace MonoDevelop.VersionControl
{
	public class VersionControlService
	{
		static Xwt.Drawing.Image overlay_modified;
		static Xwt.Drawing.Image overlay_removed;
		static Xwt.Drawing.Image overlay_conflicted;
		static Xwt.Drawing.Image overlay_added;
		internal static Xwt.Drawing.Image overlay_controled;
		static Xwt.Drawing.Image overlay_unversioned;
		static Xwt.Drawing.Image overlay_protected;
		static Xwt.Drawing.Image overlay_locked;
		static Xwt.Drawing.Image overlay_unlocked;
        static Xwt.Drawing.Image overlay_ignored;

		static Xwt.Drawing.Image icon_modified;
		static Xwt.Drawing.Image icon_removed;
		static Xwt.Drawing.Image icon_conflicted;
		static Xwt.Drawing.Image icon_added;
		internal static Xwt.Drawing.Image icon_controled;
		
		static Hashtable comments;
		static object commentsLock = new object ();
		static DateTime nextSave = DateTime.MinValue;
		
		static List<VersionControlSystem> handlers = new List<VersionControlSystem> ();
		static VersionControlConfiguration configuration;
		static DataContext dataContext = new DataContext ();
		
		public static event FileUpdateEventHandler FileStatusChanged;
		public static event CommitEventHandler PrepareCommit;
		public static event CommitEventHandler BeginCommit;
		public static event CommitEventHandler EndCommit;
		
		static VersionControlService ()
		{
			IdeApp.Initialized += delegate {
				try {
					overlay_modified = Xwt.Drawing.Image.FromResource("modified-overlay-light-16.png");
					overlay_removed = Xwt.Drawing.Image.FromResource("removed-overlay-light-16.png");
					overlay_conflicted = Xwt.Drawing.Image.FromResource("conflict-overlay-light-16.png");
					overlay_added = Xwt.Drawing.Image.FromResource("added-overlay-light-16.png");
					overlay_controled = Xwt.Drawing.Image.FromResource("versioned-overlay-light-16.png");
					overlay_unversioned = Xwt.Drawing.Image.FromResource("unversioned-overlay-light-16.png");
					overlay_protected = Xwt.Drawing.Image.FromResource("lock-required-overlay-light-16.png");
					overlay_unlocked = Xwt.Drawing.Image.FromResource("unlocked-overlay-light-16.png");
					overlay_locked = Xwt.Drawing.Image.FromResource("locked-overlay-light-16.png");
					overlay_ignored = Xwt.Drawing.Image.FromResource("ignored-overlay-light-16.png");

					icon_modified = ImageService.GetIcon ("vc-file-modified", Gtk.IconSize.Menu);
					icon_removed = ImageService.GetIcon ("vc-file-removed", Gtk.IconSize.Menu);
					icon_conflicted = ImageService.GetIcon ("vc-file-conflicted", Gtk.IconSize.Menu);
					icon_added = ImageService.GetIcon ("vc-file-added", Gtk.IconSize.Menu);
					icon_controled = Xwt.Drawing.Image.FromResource("versioned-overlay-light-16.png");
				} catch (Exception e) {
					LoggingService.LogError ("Error while loading icons.", e);
				}

				IdeApp.Workspace.SolutionLoaded += (sender, e) => SessionSolutionDisabled |= IsSolutionDisabled (e.Solution);

				IdeApp.Workspace.FileAddedToProject += OnFileAdded;
				//IdeApp.Workspace.FileChangedInProject += OnFileChanged;
				//IdeApp.Workspace.FileRemovedFromProject += OnFileRemoved;
				//IdeApp.Workspace.FileRenamedInProject += OnFileRenamed;

				IdeApp.Workspace.ItemAddedToSolution += OnEntryAdded;
				IdeApp.Exiting += delegate {
					DelayedSaveComments (null);
				};
			};

			AddinManager.AddExtensionNodeHandler ("/MonoDevelop/VersionControl/VersionControlSystems", OnExtensionChanged);
		}

		static void OnExtensionChanged (object s, ExtensionNodeEventArgs args)
		{
			VersionControlSystem vcs = (VersionControlSystem) args.ExtensionObject;
			if (args.Change == ExtensionChange.Add) {
				handlers.Add (vcs);
				try {
					// Include the repository type in the serialization context, so repositories
					// of this type can be deserialized from the configuration file.
					Repository r = vcs.CreateRepositoryInstance ();
					r.AddRef ();
					dataContext.IncludeType (r.GetType ());
					r.Unref ();
				} catch (Exception e) {
					LoggingService.LogError ("Error while adding version control system.", e);
				}
			}
			else {
				handlers.Remove (vcs);
			}
		}
		
		public static Xwt.Drawing.Image LoadOverlayIconForStatus(VersionStatus status)
		{
			if ((status & VersionStatus.Ignored) != 0)
				return overlay_ignored;

			if ((status & VersionStatus.Versioned) == 0)
				return overlay_unversioned;
			
			switch (status & VersionStatus.LocalChangesMask) {
				case VersionStatus.Modified:
				case VersionStatus.ScheduledReplace:
				case VersionStatus.ScheduledIgnore:
					return overlay_modified;
				case VersionStatus.Conflicted:
					return overlay_conflicted;
				case VersionStatus.ScheduledAdd:
					return overlay_added;
				case VersionStatus.Missing:
				case VersionStatus.ScheduledDelete:
					return overlay_removed;
			}
			
			if ((status & VersionStatus.LockOwned) != 0)
				return overlay_unlocked;
			
			if ((status & VersionStatus.Locked) != 0)
				return overlay_locked;
			
			if ((status & VersionStatus.LockRequired) != 0)
				return overlay_protected;
				
			return null;
		}
		
		public static Xwt.Drawing.Image LoadIconForStatus (VersionStatus status)
		{
			switch (status & VersionStatus.LocalChangesMask) {
				case VersionStatus.Modified:
				case VersionStatus.ScheduledReplace:
					return icon_modified;
				case VersionStatus.Conflicted:
					return icon_conflicted;
				case VersionStatus.ScheduledAdd:
					return icon_added;
				case VersionStatus.Missing:
				case VersionStatus.ScheduledDelete:
					return icon_removed;
			}
			return null;
		}

		public static string GetStatusLabel (VersionStatus status)
		{
			if ((status & VersionStatus.Versioned) == 0)
				return GettextCatalog.GetString ("Unversioned");
			
			switch (status & VersionStatus.LocalChangesMask) {
				case VersionStatus.Modified:
				case VersionStatus.ScheduledReplace:
					return GettextCatalog.GetString ("Modified");
				case VersionStatus.Conflicted:
					return GettextCatalog.GetString ("Conflict");
				case VersionStatus.ScheduledAdd:
					return GettextCatalog.GetString ("Add");
				case VersionStatus.ScheduledDelete:
					return GettextCatalog.GetString ("Delete");
				case VersionStatus.Missing:
					return GettextCatalog.GetString ("Missing");
			}
			return String.Empty;
		}

		internal static Dictionary<Repository, InternalRepositoryReference> referenceCache = new Dictionary<Repository, InternalRepositoryReference> ();
		public static Repository GetRepository (IWorkspaceObject entry)
		{
			if (IsGloballyDisabled)
				return null;

			InternalRepositoryReference repoRef = (InternalRepositoryReference) entry.ExtendedProperties [typeof(InternalRepositoryReference)];
			if (repoRef != null)
				return repoRef.Repo;
			
			Repository repo = VersionControlService.GetRepositoryReference (entry.BaseDirectory, entry.Name);
			InternalRepositoryReference rref = null;
			if (repo != null) {
				repo.AddRef ();
				if (!referenceCache.TryGetValue (repo, out rref)) {
					rref = new InternalRepositoryReference (repo);
					referenceCache [repo] = rref;
				}
			}
			entry.ExtendedProperties [typeof(InternalRepositoryReference)] = rref;
			
			return repo;
		}
		
		public static Repository GetRepositoryReference (string path, string id)
		{
			foreach (VersionControlSystem vcs in GetVersionControlSystems ()) {
				Repository repo = vcs.GetRepositoryReference (path, id);
				if (repo != null) {
					repo.VersionControlSystem = vcs;
					return repo;
				}
			}
			return null;
		}
		
		internal static void SetCommitComment (string file, string comment, bool save)
		{
			lock (commentsLock) {
				Hashtable doc = GetCommitComments ();
				if (String.IsNullOrEmpty (comment)) {
					if (doc.ContainsKey (file)) {
						doc.Remove (file);
						if (save) SaveComments ();
					}
				} else {
					CommitComment cm = new CommitComment ();
					cm.Comment = comment;
					cm.Date = DateTime.Now;
					doc [file] = cm;
					if (save) SaveComments ();
				}
			}
		}
		
		internal static string GetCommitComment (string file)
		{
			lock (commentsLock) {
				Hashtable doc = GetCommitComments ();
				CommitComment cm = doc [file] as CommitComment;
				if (cm != null) {
					cm.Date = DateTime.Now;
					return cm.Comment;
				}
				else
					return null;
			}
		}
		
		static string CommitMessagesFile {
			get {
				return UserProfile.Current.CacheDir.Combine ("version-control-commit-msg");
				
			}
		}
		
		static Hashtable GetCommitComments ()
		{
			if (comments != null)
				return comments;

			ResolveEventHandler localResolve = delegate (object s, ResolveEventArgs args) {
				foreach (System.Reflection.Assembly asm in AppDomain.CurrentDomain.GetAssemblies ()) {
					if (asm.GetName ().FullName == args.Name)
						return asm;
				}
				return null;
			};

			string file = CommitMessagesFile;
			if (File.Exists (file)) {
				FileStream stream = null;
				try {
					AppDomain.CurrentDomain.AssemblyResolve += localResolve;

					stream = File.OpenRead (file);
					BinaryFormatter formatter = new BinaryFormatter ();
					comments = (Hashtable) formatter.Deserialize (stream);
				
					// Remove comments for files that don't exists
					// Remove comments more than 60 days old
					
					ArrayList toDelete = new ArrayList ();
					foreach (DictionaryEntry e in comments) {
						if (!File.Exists ((string)e.Key))
							toDelete.Add (e.Key);
						if ((DateTime.Now - ((CommitComment)e.Value).Date).TotalDays > 60)
							toDelete.Add (e.Key);
					}
					foreach (string f in toDelete)
						comments.Remove (f);
						
				} catch (Exception ex) {
					// If there is an error, just discard the file
					LoggingService.LogError (ex.ToString ());
					comments = new Hashtable ();
				} finally {
					AppDomain.CurrentDomain.AssemblyResolve -= localResolve;
					if (stream != null)
						stream.Close ();
				}
			} else {
				comments = new Hashtable ();
			}
			return comments;
		}
		
		static void SaveComments ()
		{
			lock (commentsLock) {
				if (comments == null)
					return;
				if (nextSave == DateTime.MinValue)
					ThreadPool.QueueUserWorkItem (DelayedSaveComments);
				nextSave = DateTime.Now.AddSeconds (3);
			}
		}
		
		static void DelayedSaveComments (object ob)
		{
			lock (commentsLock)
			{
				if (comments == null || nextSave == DateTime.MinValue)
					return;
				
				DateTime tim = DateTime.Now;
				while (tim < nextSave) {
					Monitor.Wait (commentsLock, nextSave - tim);
					tim = DateTime.Now;
				}
				nextSave = DateTime.MinValue;
				
				FileStream stream = null;
				try {
					FilePath file = CommitMessagesFile;
					if (comments.Count == 0) {
						if (File.Exists (file))
							FileService.DeleteFile (file);
						return;
					}
				
					Directory.CreateDirectory (file.ParentDirectory);
					stream = new FileStream (file, FileMode.Create, FileAccess.Write);
					BinaryFormatter formatter = new BinaryFormatter ();
					formatter.Serialize (stream, comments);
				} catch (Exception ex) {
					// If there is an error, just discard the file
					LoggingService.LogError (ex.ToString ());
				} finally {
					if (stream != null)
						stream.Close ();
				}
			}
		}
		
		internal static void NotifyPrepareCommit (Repository repo, ChangeSet changeSet)
		{
			if (!DispatchService.IsGuiThread) {
				Gtk.Application.Invoke (delegate {
					NotifyPrepareCommit (repo, changeSet);
				});
				return;
			}

			if (PrepareCommit != null) {
				try {
					PrepareCommit (null, new CommitEventArgs (repo, changeSet, false));
				} catch (Exception ex) {
					MessageService.ShowException (ex);
				}
			}
		}
		
		internal static void NotifyBeforeCommit (Repository repo, ChangeSet changeSet)
		{
			if (!DispatchService.IsGuiThread) {
				Gtk.Application.Invoke (delegate {
					NotifyBeforeCommit (repo, changeSet);
				});
				return;
			}

			if (BeginCommit != null) {
				try {
					BeginCommit (null, new CommitEventArgs (repo, changeSet, false));
				} catch (Exception ex) {
					MessageService.ShowException (ex);
				}
			}
		}
		
		internal static void NotifyAfterCommit (Repository repo, ChangeSet changeSet, bool success)
		{
			if (!DispatchService.IsGuiThread) {
				Gtk.Application.Invoke (delegate {
					NotifyAfterCommit (repo, changeSet, success);
				});
				return;
			}

			if (EndCommit != null) {
				try {
					EndCommit (null, new CommitEventArgs (repo, changeSet, success));
				} catch (Exception ex) {
					MessageService.ShowException (ex);
					return;
				}
			}
			if (success) {
				foreach (ChangeSetItem it in changeSet.Items)
					SetCommitComment (it.LocalPath, null, false);
				SaveComments ();
			}
		}

		public static void NotifyFileStatusChanged (IEnumerable<VersionControlItem> items) 
		{
			FileUpdateEventArgs vargs = new FileUpdateEventArgs ();
			vargs.AddRange (items.Select (i => new FileUpdateEventInfo (i.Repository, i.Path, i.IsDirectory)));
			NotifyFileStatusChanged (vargs);
		}
		
		public static void NotifyFileStatusChanged (FileUpdateEventArgs args) 
		{
			if (!DispatchService.IsGuiThread)
				Gtk.Application.Invoke (delegate {
					NotifyFileStatusChanged (args);
				});
			else {
				if (FileStatusChanged != null)
					FileStatusChanged (null, args);
			}
		}
		
		//static void OnFileChanged (object s, ProjectFileEventArgs args)
		//{
		//	Repository repo = GetRepository (args.Project);
		//	if (repo != null)
		//		NotifyFileStatusChanged (repo, args.ProjectFile.FilePath, false);
		//}

		static void OnFileAdded (object s, ProjectFileEventArgs e)
		{
			FileUpdateEventArgs vargs = new FileUpdateEventArgs ();
			IProgressMonitor monitor = null;
			try {
				foreach (var repoFiles in e.GroupBy (i => i.Project)) {
					Repository repo = GetRepository (repoFiles.Key);
					if (repo == null)
						continue;
					var versionInfos = repo.GetVersionInfo (repoFiles.Select (f => f.ProjectFile.FilePath), VersionInfoQueryFlags.IgnoreCache);
					FilePath[] paths = versionInfos.Where (i => i.CanAdd).Select (i => i.LocalPath).ToArray ();
					if (paths.Length > 0) {
						if (monitor == null)
							monitor = GetStatusMonitor ();
						repo.Add (paths, false, monitor);
					}
					vargs.AddRange (repoFiles.Select (i => new FileUpdateEventInfo (repo, i.ProjectFile.FilePath, i.ProjectFile.Subtype == Subtype.Directory)));
				}
			}
			finally {
				if (monitor != null)
					monitor.Dispose ();
			}
			if (vargs.Count > 0)
				NotifyFileStatusChanged (vargs);
		}
		
/*		static void OnFileRemoved (object s, ProjectFileEventArgs args)
		{
			string path = args.ProjectFile.FilePath;
			Repository repo = GetRepository (args.Project);
			if (repo != null && repo.IsVersioned (path) && repo.CanRemove (path)) {
				using (IProgressMonitor monitor = GetStatusMonitor ()) {
					repo.DeleteFile (path, true, monitor);
				}
				NotifyFileStatusChanged (repo, path, args.ProjectFile.Subtype == Subtype.Directory);
			}
		}

		static void OnFileRenamed (object s, ProjectFileRenamedEventArgs args)
		{
			string path = args.ProjectFile.FilePath;
			Repository repo = GetRepository (args.Project);
			if (repo.IsVersioned (path) && repo.CanRemove (path)) {
				repo.Remove (path);
				NotifyFileStatusChanged (repo, path, args.ProjectFile.Subtype == Subtype.Directory);
			}
		}
*/
		static void SolutionItemAddFiles (string rootPath, SolutionItem entry, HashSet<string> files)
		{
			if (entry is SolutionEntityItem) {
				foreach (var file in ((SolutionEntityItem)entry).GetItemFiles (false))
					SolutionItemAddFile (rootPath, files, file);
			}
			
			if (entry is Project) {
				foreach (ProjectFile file in ((Project) entry).Files) {
					if (file.Subtype != Subtype.Directory)
						SolutionItemAddFile (rootPath, files, file.FilePath);
				}
			} else if (entry is SolutionFolder) {
				foreach (SolutionItem ent in ((SolutionFolder) entry).Items)
					SolutionItemAddFiles (rootPath, ent, files);
			}
		}
		
		static void SolutionItemAddFile (string rootPath, HashSet<string> files, string file)
		{
			if (!file.StartsWith (rootPath + Path.DirectorySeparatorChar, StringComparison.Ordinal))
			    return;
			if (!File.Exists (file))
				return;
			if (files.Add (file)) {
				string dir = Path.GetDirectoryName (file);
				while (dir != rootPath && files.Add (dir))
					dir = Path.GetDirectoryName (dir);
			}
		}
		
		static void OnEntryAdded (object o, SolutionItemEventArgs args)
		{
			if (args is SolutionItemChangeEventArgs && ((SolutionItemChangeEventArgs) args).Reloading)
				return;

			// handles addition of solutions and projects
			SolutionItem parent = (SolutionItem) args.SolutionItem.ParentFolder;
			
			if (parent == null)
				return;
			
			Repository repo = GetRepository (parent);
			
			if (repo == null)
				return;
			
			SolutionItem entry = args.SolutionItem;
			Repository currentRepo = GetRepository (entry);
			if (currentRepo != null && currentRepo.VersionControlSystem != repo.VersionControlSystem) {
				// If the item is already under version control using a different version control system
				// don't add it to the parent repo.
				return;
			}
			
			string path = entry.BaseDirectory;
			
			// While we /could/ call repo.Add with `recursive = true', we don't
			// necessarily want to add files under the project/solution directory
			// that may not be a part of this project/solution.

			var files = new HashSet<string> { path };
			SolutionItemAddFiles (path, entry, files);
			
			using (IProgressMonitor monitor = GetStatusMonitor ()) {
				var status = repo.GetDirectoryVersionInfo (path, false, true);
				foreach (var v in status) {
					if (!v.IsVersioned && files.Contains (v.LocalPath))
						repo.Add (v.LocalPath, false, monitor);
				}
			}

			if (entry is SolutionFolder && files.Count == 1)
				return;

			NotifyFileStatusChanged (new FileUpdateEventArgs (repo, parent.BaseDirectory, true));
		}
		
		public static IProgressMonitor GetProgressMonitor (string operation)
		{
			return GetProgressMonitor (operation, VersionControlOperationType.Other);
		}
		
		public static IProgressMonitor GetProgressMonitor (string operation, VersionControlOperationType op)
		{
			IconId icon;
			switch (op) {
			case VersionControlOperationType.Pull: icon = Stock.StatusDownload; break;
			case VersionControlOperationType.Push: icon = Stock.StatusUpload; break;
			default: icon = "md-version-control"; break;
			}

			IProgressMonitor monitor = IdeApp.Workbench.ProgressMonitors.GetOutputProgressMonitor ("MonoDevelop.VersionControlOutput", "Version Control", "md-version-control", false, true);
			Pad outPad = IdeApp.Workbench.ProgressMonitors.GetPadForMonitor (monitor);
			
			AggregatedProgressMonitor mon = new AggregatedProgressMonitor (monitor);
			mon.AddSlaveMonitor (IdeApp.Workbench.ProgressMonitors.GetStatusProgressMonitor (operation, icon, true, true, false, outPad));
			return mon;
		}
		
		static IProgressMonitor GetStatusMonitor ()
		{
			return IdeApp.Workbench.ProgressMonitors.GetStatusProgressMonitor (GettextCatalog.GetString ("Updating version control repository"), "vc-remote-status", true);
		}
		
		static string ConfigFile {
			get {
				return UserProfile.Current.ConfigDir.Combine ("VersionControl.config");
			}
		}
		
		static public IEnumerable<VersionControlSystem> GetVersionControlSystems ()
		{
			foreach (VersionControlSystem vs in handlers)
				if (vs.IsInstalled)
					yield return vs;
		}
		
		public static void AddRepository (Repository repo)
		{
			GetConfiguration ().Repositories.Add (repo);
		}
		
		public static void RemoveRepository (Repository repo)
		{
			GetConfiguration ().Repositories.Remove (repo);
		}

		public static bool IsGloballyDisabled {
			get { return ConfigurationGlobalDisabled || SessionSolutionDisabled; }
			set { ConfigurationGlobalDisabled = value; }
		}

		public static bool IsSolutionDisabled (Solution it)
		{
			return it.UserProperties.HasValue ("VersionControlDisabled");
		}

		public static void SetSolutionDisabled (Solution it, bool value)
		{
			if (value)
				it.UserProperties.SetValue ("VersionControlDisabled", true);
			else
				it.UserProperties.RemoveValue ("VersionControlDisabled");
			SessionSolutionDisabled = value;
		}

		internal static bool ConfigurationGlobalDisabled {
			get { return GetConfiguration ().Disabled; }
			set { GetConfiguration ().Disabled = value; }
		}

		internal static bool SessionSolutionDisabled {
			get;
			private set;
		}
		
		static public IEnumerable<Repository> GetRepositories ()
		{
			return GetConfiguration ().Repositories;
		}
		
		static VersionControlConfiguration GetConfiguration ()
		{
			if (configuration == null) {
				if (File.Exists (ConfigFile)) {
					try {
						XmlDataSerializer ser = new XmlDataSerializer (dataContext);
						using (var reader = File.OpenText (ConfigFile))
							configuration = (VersionControlConfiguration) ser.Deserialize (reader, typeof (VersionControlConfiguration));
					} catch {
						((FilePath) ConfigFile).Delete ();
					}
				}
				if (configuration == null)
					configuration = new VersionControlConfiguration ();
			}
			return configuration;
		}
		
		public static void SaveConfiguration ()
		{
			if (configuration != null) {
				XmlDataSerializer ser = new XmlDataSerializer (dataContext);
				using (var tw = new XmlTextWriter (File.CreateText (ConfigFile))) {
					tw.Formatting = Formatting.Indented;
					ser.Serialize (tw, configuration, typeof (VersionControlConfiguration));
				}
			}
		}
		
		public static void ResetConfiguration ()
		{
			configuration = null;
		}
		
		public static void StoreRepositoryReference (Repository repo, string path, string id)
		{
			repo.VersionControlSystem.StoreRepositoryReference (repo, path, id);
		}
		
		public static bool CheckVersionControlInstalled ()
		{
			if (IsGloballyDisabled)
				return false;

			foreach (VersionControlSystem vcs in GetVersionControlSystems ()) {
				if (vcs.IsInstalled)
					return true;
			}

			return false;
		}
		
		internal static Repository InternalGetRepositoryReference (string path, string id)
		{
			string file = Path.Combine (path, id) + ".mdvcs";
			if (!File.Exists (file))
				return null;
			
			XmlDataSerializer ser = new XmlDataSerializer (dataContext);
			XmlTextReader reader = new XmlTextReader (new StreamReader (file));
			try {
				return (Repository) ser.Deserialize (reader, typeof(Repository));
			} finally {
				reader.Close ();
			}
		}
		
		internal static void InternalStoreRepositoryReference (Repository repo, string path, string id)
		{
			string file = Path.Combine (path, id) + ".mdvcs";
			
			XmlDataSerializer ser = new XmlDataSerializer (dataContext);
			XmlTextWriter tw = new XmlTextWriter (new StreamWriter (file));
			try {
				ser.Serialize (tw, repo, typeof(Repository));
			} finally {
				tw.Close ();
			}
		}
		
		public static CommitMessageFormat GetCommitMessageFormat (SolutionItem item)
		{
			CommitMessageFormat format = new CommitMessageFormat ();
			format.Style = item.Policies.Get<VersionControlPolicy> ().CommitMessageStyle;
			format.ShowFilesForSingleComment = false;
			return format;
		}
		
		public static CommitMessageFormat GetCommitMessageFormat (ChangeSet cset, out AuthorInformation authorInfo)
		{
			// If all files belong to a project, use that project's policy. If not, use the solution policy
			Project project = null;
			bool sameProject = true;
			foreach (ChangeSetItem item in cset.Items) {
				if (project != null) {
					if (project.Files.GetFile (item.LocalPath) == null) {
						// Not all files belong to the same project
						sameProject = false;
						break;
					}
				} else {
					project = IdeApp.Workspace.GetProjectContainingFile (item.LocalPath);
				}
			}
			CommitMessageStyle style;
			
			if (project != null) {
				VersionControlPolicy policy;
				if (sameProject)
					policy = project.Policies.Get<VersionControlPolicy> ();
				else
					policy = project.ParentSolution.Policies.Get<VersionControlPolicy> ();
				style = policy.CommitMessageStyle;
			}
			else {
				style = PolicyService.GetDefaultPolicy<CommitMessageStyle> ();
			}
			
			authorInfo = project != null ? project.AuthorInformation : AuthorInformation.Default;
			
			CommitMessageFormat format = new CommitMessageFormat ();
			format.Style = style;
			format.ShowFilesForSingleComment = false;
			
			return format;
		}
	}
	
	[Serializable]
	class CommitComment
	{
		public string Comment;
		public DateTime Date;
	}
	
	class InternalRepositoryReference: IDisposable
	{
		Repository repo;
		
		public InternalRepositoryReference (Repository repo)
		{
			this.repo = repo;
		}

		public Repository Repo {
			get {
				return repo;
			}
		}
		
		public void Dispose ()
		{
			VersionControlService.referenceCache.Remove (repo);
			repo.Unref ();
		}
	}

	public enum VersionControlOperationType
	{
		Pull,
		Push,
		Other
	}
}