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

CommitDialog.cs « MonoDevelop.VersionControl.Dialogs « MonoDevelop.VersionControl « VersionControl « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bc56415478a7c6803bad88332ba50cfb3d8a5ab1 (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
//
// CommitDialog.cs
//
// Authors:
//       Thiago Becker    (GSoC)
//       Rafael Giorgetti (GSoC)
//       Lluis Sanchez Gual <lluis@novell.com>
//       Michael Hutchinson  <mhutchinson@novell.com>
//       Ungureanu Marius <teromario@yahoo.com>
//
// Copyright (c) 2006-2011 Novell, Inc (http://www.novell.com)
// Copyright (c) 2013 Xamarin (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using Gtk;
using MonoDevelop.Core;
using Mono.Addins;
using MonoDevelop.Projects;
using MonoDevelop.Components;
using System.Collections.Generic;
using System.Linq;

namespace MonoDevelop.VersionControl.Dialogs
{
	partial class CommitDialog : Gtk.Dialog
	{
		ListStore store;
		HashSet<FilePath> selected = new HashSet<FilePath> ();
		List<CommitDialogExtension> extensions = new List<CommitDialogExtension> ();
		ChangeSet changeSet;
		string oldMessage;
		bool responseSensitive;

		public CommitDialog (ChangeSet changeSet)
		{
			Build ();

			store = new ListStore(typeof (Xwt.Drawing.Image), typeof (string), typeof (string), typeof(bool), typeof(object));
			fileList.Model = store;
			fileList.SearchColumn = -1; // disable the interactive search
			this.changeSet = changeSet;
			oldMessage = changeSet.GlobalComment;

			CellRendererText crt = new CellRendererText ();
			var crp = new CellRendererImage ();
			TreeViewColumn colStatus = new TreeViewColumn ();
			colStatus.Title = GettextCatalog.GetString ("Status");
			colStatus.PackStart (crp, false);
			colStatus.PackStart (crt, true);
			colStatus.Spacing = 2;
			colStatus.AddAttribute (crp, "image", 0);
			colStatus.AddAttribute (crt, "text", 1);
			CellRendererToggle cellToggle = new CellRendererToggle();
			cellToggle.Toggled += new ToggledHandler(OnCommitToggledHandler);
			TreeViewColumn colCommit = new TreeViewColumn ("", cellToggle, "active", 3);
			TreeViewColumn colFile = new TreeViewColumn (GettextCatalog.GetString ("File"), new CellRendererText(), "text", 2);
			
			fileList.AppendColumn(colCommit);
			fileList.AppendColumn(colStatus);
			fileList.AppendColumn(colFile);
			
			colCommit.Visible = false;
			
			object[] exts = AddinManager.GetExtensionObjects ("/MonoDevelop/VersionControl/CommitDialogExtensions", false);
			bool separatorRequired = false;
			
			foreach (object ob in exts) {
				CommitDialogExtension ext = ob as CommitDialogExtension;
				if (ext == null) {
					LoggingService.LogError ("Commit extension type " + ob.GetType() + " must be a subclass of CommitDialogExtension");
					continue;
				}
				if (ext.Initialize (changeSet)) {
					ext.CommitDialog = this;
					var newTitle = ext.FormatDialogTitle (changeSet, Title);
					if (newTitle != null)
						Title = newTitle;

					ext.CommitMessageTextViewHook (textview);
					if (separatorRequired) {
						HSeparator sep = new HSeparator ();
						sep.Show ();
						vboxExtensions.PackEnd (sep, false, false, 0);
					}
					vboxExtensions.PackEnd (ext, false, false, 0);
					extensions.Add (ext);
					ext.AllowCommitChanged += HandleAllowCommitChanged;
					separatorRequired = true;
				} else
					ext.Destroy ();
			}
			HandleAllowCommitChanged (null, null);

			LoadChangeset (changeSet.Items);

			if (string.IsNullOrEmpty (changeSet.GlobalComment)) {
				AuthorInformation aInfo;
				CommitMessageFormat fmt = VersionControlService.GetCommitMessageFormat (changeSet, out aInfo);
				Message = changeSet.GenerateGlobalComment (fmt, aInfo);
			}
			else
				Message = changeSet.GlobalComment;
				
			textview.Buffer.Changed += OnTextChanged;
			responseSensitive = !string.IsNullOrEmpty (Message);
			
			// Focus the text view and move the insert point to the beginning. Makes it easier to insert
			// a comment header.
			textview.Buffer.MoveMark (textview.Buffer.InsertMark, textview.Buffer.StartIter);
			textview.Buffer.MoveMark (textview.Buffer.SelectionBound, textview.Buffer.StartIter);
			textview.GrabFocus ();
			textview.Buffer.MarkSet += OnMarkSet;

			SetResponseSensitive (ResponseType.Ok, responseSensitive);

			VersionControlService.FileStatusChanged += OnFileStatusChanged;
		}

		void OnFileStatusChanged (object sender, FileUpdateEventArgs args)
		{
			foreach (FileUpdateEventInfo f in args) {
				OnFileStatusChanged (f);
			}
		}

		void OnFileStatusChanged (FileUpdateEventInfo args)
		{
			VersionInfo newInfo = null;
			try {
				// Reuse remote status from old version info
				newInfo = changeSet.Repository.GetVersionInfo (args.FilePath);
			} catch (Exception ex) {
				LoggingService.LogError (ex.ToString ());
			}
			AddFile (newInfo);
		}

		void AddFile (VersionInfo vinfo)
		{
			if (vinfo != null && (vinfo.HasLocalChanges || vinfo.HasRemoteChanges)) {
				changeSet.AddFile (vinfo.LocalPath);
				bool added = selected.Add (vinfo.LocalPath);
				if (added)
					AppendFileInfo (vinfo);
			}
		}

		TreeIter AppendFileInfo (VersionInfo info)
		{
			Xwt.Drawing.Image statusicon = VersionControlService.LoadIconForStatus (info.Status);
			string lstatus = VersionControlService.GetStatusLabel (info.Status);
			string localpath;

			if (info.IsDirectory)
				localpath = (!info.LocalPath.IsChildPathOf (changeSet.BaseLocalPath) ?
								"." :
								(string)info.LocalPath.ToRelative (changeSet.BaseLocalPath));
			else
				localpath = System.IO.Path.GetFileName ((string)info.LocalPath);

			if (localpath.Length > 0 && localpath [0] == System.IO.Path.DirectorySeparatorChar) localpath = localpath.Substring (1);
			if (localpath == "") { localpath = "."; } 

			TreeIter it = store.AppendValues (statusicon, lstatus, localpath, true, info);

			return it;
		}

		void HandleAllowCommitChanged (object sender, EventArgs e)
		{
			bool allowCommit = responseSensitive;
			foreach (CommitDialogExtension ext in extensions)
				allowCommit &= ext.AllowCommit;
			SetResponseSensitive (Gtk.ResponseType.Ok, allowCommit);
		}

		protected override void OnResponse (Gtk.ResponseType type)
		{
			if (type != Gtk.ResponseType.Ok) {
				changeSet.GlobalComment = oldMessage;
			} else if (!ButtonCommitClicked ())
				return;

			base.OnResponse (type);
		}

		protected override void OnDestroyed ()
		{
			VersionControlService.FileStatusChanged -= OnFileStatusChanged;

			foreach (var ob in extensions) {
				var ext = ob as CommitDialogExtension;
				if (ext != null)
					ext.Destroy ();
			}
			base.OnDestroyed ();
		}

		void LoadChangeset (IEnumerable<ChangeSetItem> items)
		{
			fileList.Model = null;
			store.Clear ();

			foreach (ChangeSetItem info in items) {
				Xwt.Drawing.Image statusicon = VersionControlService.LoadIconForStatus (info.Status);
				string lstatus = VersionControlService.GetStatusLabel (info.Status);
				string localpath;

				if (info.IsDirectory)
					localpath = (!info.LocalPath.IsChildPathOf (changeSet.BaseLocalPath) ?
									"." :
									(string)info.LocalPath.ToRelative (changeSet.BaseLocalPath));
				else
					localpath = System.IO.Path.GetFileName ((string)info.LocalPath);

				if (localpath.Length > 0 && localpath [0] == System.IO.Path.DirectorySeparatorChar) localpath = localpath.Substring (1);
				if (localpath == "") { localpath = "."; } // not sure if this happens

				store.AppendValues (statusicon, lstatus, localpath, true, info);
				selected.Add (info.LocalPath);
			}

			fileList.Model = store;
		}

		bool ButtonCommitClicked ()
		{
			changeSet.GlobalComment = Message;
			
			// Perform the commit
			int n;
			for (n=0; n<extensions.Count; n++) {
				CommitDialogExtension ext = extensions [n];
				bool res;
				try {
					res = ext.OnBeginCommit (changeSet);
				} catch (Exception ex) {
					LoggingService.LogInternalError (ex);
					res = false;
				}
				if (!res) {
					// Commit failed. Rollback the previous extensions
					for (int m=0; m<n; m++) {
						ext = extensions [m];
						try {
							ext.OnEndCommit (changeSet, false);
						} catch (Exception ex) {
							LoggingService.LogInternalError ("Commit operation failed.", ex);
						}
					}
					return false;
				}
			}
			return true;
		}

		void UpdatePositionLabel (TextIter iter)
		{
			int row = iter.Line + 1;
			int col = iter.LineOffset + 1;
			label3.Text = String.Format ("{0}/{1}", row, col);
		}

		void OnMarkSet (object o, MarkSetArgs e)
		{
			if (e.Mark != textview.Buffer.InsertMark)
				return;

			UpdatePositionLabel (e.Location);
		}
		
		void OnTextChanged (object s, EventArgs args)
		{
			changeSet.GlobalComment = Message;
			responseSensitive = !string.IsNullOrEmpty (Message);
			SetResponseSensitive (ResponseType.Ok, responseSensitive);
			UpdatePositionLabel (textview.Buffer.GetIterAtOffset (textview.Buffer.CursorPosition));
		}
		
		public void EndCommit (bool success)
		{
			foreach (CommitDialogExtension ext in extensions) {
				try {
					ext.OnEndCommit (changeSet, success);
				} catch {}
			}
		}
		
		public string[] GetFilesToCommit ()
		{
			return selected.ToPathStrings ().ToArray ();
		}
		
		public string Message {
			get { return textview.Buffer.Text; }
			set { textview.Buffer.Text = value; }
		}
		
		void OnCommitToggledHandler(object o, ToggledArgs args)
		{
			TreeIter pos;
			if (!store.GetIterFromString (out pos, args.Path))
				return;
			bool active = !(bool) store.GetValue (pos, 3);
			ChangeSetItem vinfo = (ChangeSetItem) store.GetValue (pos, 4);
			store.SetValue (pos, 3, active);
			if (active)
				selected.Add (vinfo.LocalPath);
			else
				selected.Remove (vinfo.LocalPath);
		}
	}
}