File: gitg-diff-view-commit-details.vala

package info (click to toggle)
gitg 41-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,876 kB
  • sloc: ansic: 1,636; ruby: 1,445; sh: 314; python: 261; xml: 57; makefile: 15
file content (409 lines) | stat: -rw-r--r-- 9,215 bytes parent folder | download
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
/*
 * This file is part of gitg
 *
 * Copyright (C) 2015 - Jesse van den Kieboom
 *
 * gitg 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.
 *
 * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
 */

[GtkTemplate (ui = "/org/gnome/gitg/ui/gitg-diff-view-commit-details.ui")]
class Gitg.DiffViewCommitDetails : Gtk.Grid
{
	[GtkChild( name = "image_avatar" )]
	private unowned Gtk.Image d_image_avatar;

	[GtkChild( name = "label_author" )]
	private unowned Gtk.Label d_label_author;

	[GtkChild( name = "label_author_date" )]
	private unowned Gtk.Label d_label_author_date;

	[GtkChild( name = "label_committer" )]
	private unowned Gtk.Label d_label_committer;

	[GtkChild( name = "label_committer_date" )]
	private unowned Gtk.Label d_label_committer_date;

	[GtkChild( name = "label_subject" )]
	private unowned Gtk.Label d_label_subject;

	[GtkChild( name = "label_sha1" )]
	private unowned Gtk.Label d_label_sha1;

	[GtkChild( name = "grid_parents_container" )]
	private unowned Gtk.Grid d_grid_parents_container;

	[GtkChild( name = "grid_parents" )]
	private unowned Gtk.Grid d_grid_parents;

	[GtkChild( name = "expander_files" )]
	private unowned Gtk.Expander d_expander_files;

	[GtkChild( name = "label_expand_collapse_files" )]
	private unowned Gtk.Label d_label_expand_collapse_files;

	public bool expanded
	{
		get { return d_expander_files.expanded; }
		set
		{
			if (d_expander_files.expanded != value)
			{
				d_expander_files.expanded = value;
			}
		}
	}

	public bool expander_visible
	{
		get { return d_expander_files.visible; }

		set
		{
			d_expander_files.visible = value;
			d_label_expand_collapse_files.visible = value;
		}
	}

	private Cancellable? d_avatar_cancel;

	private Ggit.Commit? d_commit;

	public Ggit.Commit? commit
	{
		get { return d_commit; }
		construct set
		{
			if (d_commit != value)
			{
				d_commit = value;
				update();
			}
		}
	}

	private Ggit.Commit d_parent_commit;

	public Ggit.Commit parent_commit
	{
		get { return d_parent_commit; }
		set
		{
			if (d_parent_commit != value)
			{
				d_parent_commit = value;

				if (value != null)
				{
					var button = d_parents_map[value.get_id()];

					if (button != null)
					{
						button.active = true;
					}
				}
			}
		}
	}

	private bool d_use_gravatar;

	public bool use_gravatar
	{
		get { return d_use_gravatar; }
		construct set
		{
			d_use_gravatar = value;
			update_avatar();
		}
	}

	public Gitg.Repository? repository {get; set; }

	private Gee.HashMap<Ggit.OId, Gtk.RadioButton> d_parents_map;

	private GLib.Regex regex_url = /\w+:(\/?\/?)[^\s]+/;
	private Ggit.Config config {get; set;}
	private GLib.Regex regex_custom_links = /gitg\.custom-link\.(.+)\.regex/;

	construct
	{
		d_expander_files.notify["expanded"].connect(() => {
			if (d_expander_files.expanded)
			{
				d_label_expand_collapse_files.label = _("Collapse all");
			}
			else
			{
				d_label_expand_collapse_files.label = _("Expand all");
			}

			notify_property("expanded");
		});

		use_gravatar = true;
	}

	protected override void dispose()
	{
		if (d_avatar_cancel != null)
		{
			d_avatar_cancel.cancel();
		}

		base.dispose();
	}

	private string author_to_markup(Ggit.Signature author)
	{
		var name = Markup.escape_text(author.get_name());
		var email = Markup.escape_text(author.get_email());

		return "%s &lt;<a href=\"mailto:%s\">%s</a>&gt;".printf(name, email, email);
	}

	private void update()
	{
		d_parents_map = new Gee.HashMap<Ggit.OId, Gtk.RadioButton>((oid) => oid.hash(), (o1, o2) => o1.equal(o2));

		foreach (var child in d_grid_parents.get_children())
		{
			child.destroy();
		}

		if (commit == null)
		{
			return;
		}

		d_label_subject.set_markup(subject_to_markup(commit.get_subject()));
		d_label_sha1.label = commit.get_id().to_string();

		var author = commit.get_author();

		d_label_author.label = author_to_markup(author);
		d_label_author_date.label = author.get_time().to_timezone(author.get_time_zone()).format("%x %X %z");

		var committer = commit.get_committer();

		if (committer.get_name() != author.get_name() ||
		    committer.get_email() != author.get_email() ||
		    committer.get_time().compare(author.get_time()) != 0)
		{
			d_label_committer.label = _("Committed by %s").printf(author_to_markup(committer));
			d_label_committer_date.label = committer.get_time().to_timezone(committer.get_time_zone()).format("%x %X %z");
		}
		else
		{
			d_label_committer.label = "";
			d_label_committer_date.label = "";
		}

		var parents = commit.get_parents();
		var first_parent = parents.size == 0 ? null : parents.get(0);

		parent_commit = first_parent;

		if (parents.size > 1)
		{
			d_grid_parents_container.show();
			var grp = new SList<Gtk.RadioButton>();

			Gtk.RadioButton? first = null;

			foreach (var parent in parents)
			{
				var pid = parent.get_id().to_string().substring(0, 6);
				var psubj = parent.get_subject();

				var button = new Gtk.RadioButton.with_label(grp, @"$pid: $psubj");

				if (first == null)
				{
					first = button;
				}

				button.group = first;

				d_parents_map[parent.get_id()] = button;

				button.show();
				d_grid_parents.add(button);

				var par = parent;

				button.toggled.connect(() => {
					if (button.active) {
						parent_commit = par;
					}
				});
			}
		}
		else
		{
			d_grid_parents_container.hide();
		}

		update_avatar();
	}

	private string subject_to_markup(string subject_text)
	{
		return parse_links_on_subject(subject_text);
	}

	private string parse_links_on_subject(string subject_text)
	{
		string result = subject_text.dup();
		try
		{
			GLib.MatchInfo matchInfo;
			regex_url.match (subject_text, 0, out matchInfo);

			while (matchInfo.matches ())
			{
				string text = matchInfo.fetch(0);
				result = result.replace(text, "<a href=\"%s\">%s</a>".printf(text, text));
				matchInfo.next();
			}

			result = parse_smart_text(result);
		}
		catch(Error e)
		{
		}
		return result;
	}

	private string parse_smart_text(string subject_text)
	{
		string result = subject_text;
		if (repository != null)
		{
			result = subject_text.dup();
			try
			{
				var conf = repository.get_config().snapshot();
				conf.match_foreach(regex_custom_links, (match_info, value) => {
					string group = match_info.fetch(1);
					debug ("found custom-link group: %s", group);
					debug (value == null ? "es nulo": "es vacio");
					string custom_link_regexp = value;
					string replacement_key = "gitg.custom-link.%s.replacement".printf(group);
					try
					{
						string custom_link_replacement = conf.get_string(replacement_key);

						var custom_regex = new Regex (custom_link_regexp);
						try
						{
							GLib.MatchInfo matchInfo;

							custom_regex.match (subject_text, 0, out matchInfo);

							while (matchInfo.matches ())
							{
								string text = matchInfo.fetch(0);
								string link = text.dup();
								debug ("found: %s", link);
								if (custom_link_replacement != null)
								{
									link = custom_regex.replace(link, text.length, 0, custom_link_replacement);
								}
								result = result.replace(text, "<a href=\"%s\" title=\"%s\">%s</a>".printf(link, link, text));

								matchInfo.next();
							}
						}
						catch(Error e)
						{
						}
					} catch (Error e)
					{
						warning ("Cannot read git config: %s", e.message);
					}
					return 0;
				});
			}
			catch(Error e)
			{
				warning ("Cannot read git config: %s", e.message);
			}
		}
		return result;
	}

	private void update_avatar()
	{
		if (commit == null)
		{
			return;
		}

		if (d_use_gravatar)
		{
			if (d_avatar_cancel != null)
			{
				d_avatar_cancel.cancel();
			}

			d_avatar_cancel = new Cancellable();
			var cancel = d_avatar_cancel;

			var cache = AvatarCache.default();

			cache.load.begin(commit.get_author().get_email(), d_image_avatar.pixel_size, cancel, (obj, res) => {
				if (!cancel.is_cancelled())
				{
					var pixbuf = cache.load.end(res);

					if (pixbuf != null)
					{
						d_image_avatar.pixbuf = pixbuf;
						d_image_avatar.get_style_context().remove_class("dim-label");
					}
					else
					{
						d_image_avatar.icon_name = "avatar-default-symbolic";
						d_image_avatar.get_style_context().add_class("dim-label");
					}
				}

				if (cancel == d_avatar_cancel)
				{
					d_avatar_cancel = null;
				}
			});
		}
		else
		{
			d_image_avatar.icon_name = "avatar-default-symbolic";
			d_image_avatar.get_style_context().add_class("dim-label");
		}
	}

	[GtkCallback]
	private bool button_press_on_event_box_expand_collapse(Gdk.EventButton event)
	{
		if (event.button == Gdk.BUTTON_PRIMARY)
		{
			d_expander_files.expanded = !d_expander_files.expanded;
			return true;
		}

		return false;
	}
}

// ex:ts=4 noet