File: gitg-lanes.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 (594 lines) | stat: -rw-r--r-- 12,215 bytes parent folder | download | duplicates (2)
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
/*
 * This file is part of gitg
 *
 * Copyright (C) 2012 - 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/>.
 */

namespace Gitg
{

public class Lanes : Object
{
	public int inactive_max { get; set; default = 30; }
	public int inactive_collapse { get; set; default = 10; }
	public int inactive_gap { get; set; default = 10; }
	public bool inactive_enabled { get; set; default = true; }
	public Gee.LinkedList<Commit> miss_commits {get; set; }

	private SList<weak Commit> d_previous;
	private Gee.LinkedList<LaneContainer> d_lanes;
	private HashTable<Ggit.OId, CollapsedLane> d_collapsed;
	private Gee.HashSet<Ggit.OId>? d_roots;

	class LaneContainer
	{
		public Lane lane;
		public int inactive;
		public Ggit.OId? from;
		public Ggit.OId? to;

		public LaneContainer.with_color(Ggit.OId? from,
		                                Ggit.OId? to,
		                                Color?    color)
		{
			this.from = from;
			this.to = to;
			this.lane = new Lane.with_color(color);
			this.inactive = 0;
		}

		public LaneContainer(Ggit.OId? from,
		                     Ggit.OId? to)
		{
			this.with_color(from, to, null);
		}

		public void next(int index)
		{
			var hidden = is_hidden;
			lane = lane.copy();

			lane.tag = LaneTag.NONE;
			lane.from = new SList<int>();

			if (!hidden)
			{
				lane.from.prepend(index);
			}

			is_hidden = hidden;

			if (to != null && inactive >= 0)
			{
				++inactive;
			}
		}

		public bool is_hidden
		{
			get { return (lane.tag & LaneTag.HIDDEN) != 0; }
			set
			{
				if (value)
				{
					lane.tag |= LaneTag.HIDDEN;
				}
				else
				{
					lane.tag &= ~LaneTag.HIDDEN;
				}
			}
		}
	}

	[Compact]
	class CollapsedLane
	{
		public Color color;
		public uint index;
		public Ggit.OId? from;
		public Ggit.OId? to;

		public CollapsedLane(LaneContainer container)
		{
			color = container.lane.color;
			from = container.from;
			to = container.to;
		}
	}

	public Lanes()
	{
		d_collapsed = new HashTable<Ggit.OId, CollapsedLane>(Ggit.OId.hash,
		                                                     Ggit.OId.equal);

		var settings = new Settings(Gitg.Config.APPLICATION_ID + ".preferences.history");

		settings.bind("collapse-inactive-lanes-enabled",
		              this,
		              "inactive-enabled",
		              SettingsBindFlags.GET | SettingsBindFlags.SET);

		settings.bind("collapse-inactive-lanes",
		              this,
		              "inactive-collapse",
		              SettingsBindFlags.GET | SettingsBindFlags.SET);

		reset();
	}

	public void reset(Ggit.OId[]?            reserved = null,
	                  Gee.HashSet<Ggit.OId>? roots    = null)
	{
		d_lanes = new Gee.LinkedList<LaneContainer>();
		miss_commits = new Gee.LinkedList<Commit>();
		d_roots = roots;

		Color.reset();

		if (reserved != null)
		{
			foreach (var r in reserved)
			{
				var ct = new LaneContainer(null, r);
				ct.inactive = -1;
				ct.is_hidden = true;

				d_lanes.add(ct);
			}
		}

		d_collapsed.remove_all();
		d_previous = new SList<weak Commit>();
	}

	public bool next(Commit           next,
	                 out SList<Lane> lanes,
	                 out int         nextpos,
	                 bool save_miss = false)
	{
		var myoid = next.get_id();

		if (inactive_enabled)
		{
			collapse_lanes();
			expand_lanes(next);
		}

		debug("commit: %s %s", next.get_subject(), next.get_id().to_string());
		LaneContainer? mylane = find_lane_by_oid(myoid, out nextpos);
		if (mylane == null && d_roots != null && !d_roots.contains(myoid))
		{
			lanes = null;
			if (save_miss) {
				debug ("saving miss %s %s", next.get_id().to_string(), next.get_id().to_string());
				miss_commits.add(next);
			}

			return false;
		}

		if (mylane == null)
		{
			// there is no lane reserved for this commit, add a new lane
			mylane = new LaneContainer(myoid, null);

			d_lanes.add(mylane);
			nextpos = (int)d_lanes.size - 1;
		}
		else
		{
			// copy the color here because the commit is a new stop
			mylane.lane.color = mylane.lane.color.copy();

			mylane.to = null;
			mylane.from = next.get_id();

			if (mylane.is_hidden && d_roots != null && d_roots.contains(myoid))
			{
				mylane.is_hidden = false;
				mylane.lane.from = new SList<int>();
			}

			if (mylane.inactive >= 0)
			{
				mylane.inactive = 0;
			}
		}

		var hidden = mylane.is_hidden;

		lanes = lanes_list();
		prepare_lanes(next, nextpos, hidden);

		return !hidden;
	}

	private void prepare_lanes(Commit next, int pos, bool hidden)
	{
		var parents = next.get_parents();
		var myoid = next.get_id();

		if (!hidden)
		{
			init_next_layer();
		}

		var mylane = d_lanes[pos];

		for (uint i = 0; i < parents.size; ++i)
		{
			int lnpos;
			var poid = parents.get_id(i);

			var container = find_lane_by_oid(poid, out lnpos);

			if (container != null)
			{
				// there is already a lane for this parent. This means that
				// we add pos as a merge for the lane.
				if (i == 0 && pos < lnpos)
				{
					// we are at the mainline of a merge, and this parent has
					// already been assigned to an existing lane, if our
					// lane's pos is smaller, then the this parent should be in
					// our lane instead.
					mylane.to = poid;
					mylane.from = myoid;

					if (!container.is_hidden)
					{
						mylane.lane.from.append(lnpos);
						mylane.is_hidden = false;
					}

					mylane.lane.color = mylane.lane.color.copy();

					if (mylane.inactive >= 0)
					{
						mylane.inactive = 0;
					}

					d_lanes.remove(container);
				}
				else
				{
					container.from = myoid;

					if (!hidden)
					{
						container.lane.from.append(pos);
					}

					container.lane.color = container.lane.color.copy();

					if (!hidden)
					{
						container.is_hidden = false;
					}

					if (container.inactive >= 0)
					{
						container.inactive = 0;
					}
				}

				continue;
			}
			else if (mylane != null && mylane.to == null)
			{
				// there is no parent yet which can proceed on the current
				// commit lane, so set it now
				mylane.to = poid;

				mylane.lane.color = mylane.lane.color.copy();
			}
			else if (!hidden)
			{
				// generate a new lane for this parent
				var newlane = new LaneContainer(myoid, poid);

				newlane.lane.from.prepend(pos);
				d_lanes.add(newlane);
			}
		}

		if (mylane != null && mylane.to == null)
		{
			// remove current lane if no longer needed (i.e. merged)
			d_lanes.remove(mylane);
		}

		// store new commit in track list
		if (d_previous.length() == inactive_collapse + inactive_gap + 1)
		{
			d_previous.delete_link(d_previous.last());
		}

		d_previous.prepend(next);
	}

	private void add_collapsed(LaneContainer container,
	                           int           index)
	{
		var collapsed = new CollapsedLane(container);
		collapsed.index = index;

		d_collapsed.insert(container.to, (owned)collapsed);
	}

	private void collapse_lane(LaneContainer container,
	                           int           index)
	{
		add_collapsed(container, index);

		unowned SList<weak Commit> item = d_previous;

		while (item != null)
		{
			var commit = item.data;
			unowned SList<Lane> lns = commit.get_lanes();

			if (lns != null && index <= lns.length())
			{
				unowned Lane lane = lns.nth_data(index);

				if (item.next != null)
				{
					var newindex = lane.from.data;

					lns = commit.remove_lane(lane);

					if (item.next.next != null)
					{
						update_merge_indices(lns, newindex, -1);
					}

					var mylane = commit.mylane;

					if (mylane > index)
					{
						--commit.mylane;
					}

					index = newindex;
				}
				else
				{
					lane.tag |= LaneTag.END;
					lane.boundary_id = container.to;
				}
			}

			item = item.next;
		}
	}

	private void collapse_lanes()
	{
		int index = 0;

		var iter = d_lanes.iterator();

		while (iter.next())
		{
			var container = iter.get();

			if (container.inactive != inactive_max + inactive_gap)
			{
				++index;
				continue;
			}

			collapse_lane(container, container.lane.from.data);
			update_current_lane_merge_indices(index, -1);

			iter.remove();
		}
	}

	private int ensure_correct_index(Commit commit,
	                                 int    index)
	{
		var len = commit.get_lanes().length();

		if (index > len)
		{
			return (int)len;
		}
		else
		{
			return index;
		}
	}

	private void update_lane_merge_indices(SList<int> from,
	                                       int        index,
	                                       int        direction)
	{
		while (from != null)
		{
			int idx = from.data;

			if (idx > index || (direction > 0 && idx == index))
			{
				from.data = idx + direction;
			}

			from = from.next;
		}
	}

	private void update_merge_indices(SList<Lane> lanes,
	                                  int         index,
	                                  int         direction)
	{
		foreach (unowned Lane lane in lanes)
		{
			update_lane_merge_indices(lane.from, index, direction);
		}
	}
	private void update_current_lane_merge_indices(int index,
	                                               int direction)
	{
		foreach (var container in d_lanes)
		{
			update_lane_merge_indices(container.lane.from,
			                          index,
			                          direction);
		}
	}

	private void expand_lane(CollapsedLane lane)
	{
		var index = lane.index;
		var ln = new Lane.with_color(lane.color);
		var len = d_lanes.size;

		if (index > len)
		{
			index = len;
		}

		var next = ensure_correct_index(d_previous.data, (int)index);

		var container = new LaneContainer.with_color(lane.from,
		                                             lane.to,
		                                             lane.color);

		update_current_lane_merge_indices((int)index, 1);

		container.lane.from.prepend(next);
		d_lanes.insert((int)index, container);

		index = next;
		uint cnt = 0;

		unowned SList<weak Commit> ptr = d_previous;

		while (ptr != null)
		{
			var commit = ptr.data;

			if (cnt == inactive_collapse)
			{
				break;
			}

			// Insert new lane at the index
			Lane copy = ln.copy();
			unowned SList<Lane> lns = commit.get_lanes();

			if (ptr.next == null || cnt + 1 == inactive_collapse)
			{
				copy.boundary_id = lane.from;
				copy.tag |= LaneTag.START;
			}
			else
			{
				next = ensure_correct_index(ptr.next.data, (int)index);
				copy.from.prepend(next);

				update_merge_indices(lns, (int)index, 1);
			}

			commit.insert_lane(copy, (int)index);

			var mylane = commit.mylane;

			if (mylane >= index)
			{
				++commit.mylane;
			}

			index = next;
			++cnt;

			ptr = ptr.next;
		}
	}

	private void expand_lane_from_oid(Ggit.OId id)
	{
		unowned CollapsedLane? collapsed = d_collapsed.lookup(id);

		if (collapsed != null)
		{
			expand_lane(collapsed);
			d_collapsed.remove(id);
		}
	}

	private void expand_lanes(Commit commit)
	{
		expand_lane_from_oid(commit.get_id());

		var parents = commit.get_parents();

		for (uint i = 0; i < parents.size; ++i)
		{
			expand_lane_from_oid(parents.get_id(i));
		}
	}

	private void init_next_layer()
	{
		int index = 0;

		foreach (var container in d_lanes)
		{
			container.next(index++);
		}
	}

	private LaneContainer? find_lane_by_oid(Ggit.OId id,
	                                        out int  pos)
	{
		int p = 0;

		foreach (var container in d_lanes)
		{
			if (container != null &&
			    id.equal(container.to))
			{
				pos = p;
				return container;
			}

			++p;
		}

		pos = -1;
		return null;
	}

	private SList<Lane> lanes_list()
	{
		var ret = new SList<Lane>();

		foreach (var container in d_lanes)
		{
			ret.prepend(container.lane.copy());
		}

		ret.reverse();
		return ret;
	}
}

}

// ex:set ts=4 noet