File: repository.vala

package info (click to toggle)
gitg 44-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,244 kB
  • sloc: ansic: 1,636; ruby: 1,445; sh: 314; python: 261; xml: 57; makefile: 15
file content (455 lines) | stat: -rw-r--r-- 8,080 bytes parent folder | download | duplicates (7)
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
/*
 * This file is part of gitg
 *
 * Copyright (C) 2013 - 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/>.
 */

using Gitg.Test.Assert;

class Gitg.Test.Repository : Gitg.Test.Test
{
	protected Gitg.Repository? d_repository;
	private uint d_current_time;

	struct File
	{
		public string filename;
		public string? contents;
	}

	private File[] files_from_varargs(string? filename, va_list l)
	{
		File[] files = new File[] {};

		while (filename != null)
		{
			string contents = l.arg();

			files += File() {
				filename = filename,
				contents = contents
			};

			filename = l.arg();
		}

		return files;
	}

	private File[] filenames_from_varargs(string? filename, va_list l)
	{
		File[] files = new File[] {};

		while (filename != null)
		{
			files += File() {
				filename = filename,
				contents = null
			};

			filename = l.arg();
		}

		return files;
	}

	public void assert_file_contents(string filename, string expected_contents)
	{
		var wd = d_repository.get_workdir();

		Assert.assert_file_contents(Path.build_filename(wd.get_path(), filename), expected_contents);
	}

	public bool file_exists(string filename)
	{
		return d_repository.get_workdir().get_child(filename).query_exists();
	}

	private void write_files(File[] files)
	{
		var wd = d_repository.get_workdir();

		foreach (var f in files)
		{
			var fp = wd.get_child(f.filename);

			try
			{
				var os = fp.replace(null, false, GLib.FileCreateFlags.NONE, null);
				os.write(f.contents.data);
				os.close();
			}
			catch (GLib.Error e)
			{
				Assert.assert_no_error(e);
			}
		}
	}

	protected void write_file(string filename, string contents)
	{
		write_files(new File[] {
			File() {
				filename = filename,
				contents = contents
			}
		});
	}

	public Ggit.Signature? get_verified_committer()
	{
		try
		{
			return new Ggit.Signature("gitg tester", "gitg-tester@gnome.org", new DateTime.from_unix_utc(d_current_time++));
		}
		catch (Error e)
		{
			Assert.assert_no_error(e);
			return null;
		}
	}

	protected void commit(string? filename, ...)
	{
		if (d_repository == null)
		{
			return;
		}

		var files = files_from_varargs(filename, va_list());
		write_files(files);

		// use the index to stage the files

		Ggit.Index index;

		try
		{
			index = d_repository.get_index();
		}
		catch (GLib.Error e)
		{
			Assert.assert_no_error(e);
			return;
		}

		foreach (var f in files)
		{
			try
			{
				index.add_path(f.filename);
			}
			catch (GLib.Error e)
			{
				Assert.assert_no_error(e);
			}
		}

		Ggit.OId treeoid;

		try
		{
			index.write();
			treeoid = index.write_tree();
		}
		catch (GLib.Error e)
		{
			Assert.assert_no_error(e);
			return;
		}

		// create commit
		var sig = get_verified_committer();

		Ggit.Tree tree;

		try
		{
			tree = d_repository.lookup<Ggit.Tree>(treeoid);
		}
		catch (GLib.Error e)
		{
			Assert.assert_no_error(e);
			return;
		}

		Ggit.OId commitoid;

		Ggit.Ref? head = null;
		Ggit.Commit? parent = null;

		try
		{
			head = d_repository.get_head();
			parent = head.lookup() as Ggit.Commit;
		} catch {}

		try
		{
			Ggit.Commit[] parents;

			if (parent != null)
			{
				parents = new Ggit.Commit[] { parent };
			}
			else
			{
				parents = new Ggit.Commit[] {};
			}

			commitoid = d_repository.create_commit("HEAD",
			                                       sig,
			                                       sig,
			                                       null,
			                                       "commit " + filename,
			                                       tree,
			                                       parents);
		}
		catch (GLib.Error e)
		{
			Assert.assert_no_error(e);
			return;
		}
	}

	protected void workdir_remove(string? filename, ...)
	{
		if (d_repository == null)
		{
			return;
		}

		var files = filenames_from_varargs(filename, va_list());
		var wd = d_repository.get_workdir();

		foreach (var f in files)
		{
			var fs = wd.get_child(f.filename);

			try
			{
				fs.delete();
			}
			catch (GLib.Error e)
			{
				Assert.assert_no_error(e);
			}
		}
	}

	protected void workdir_modify(string? filename, ...)
	{
		if (d_repository == null)
		{
			return;
		}

		var files = files_from_varargs(filename, va_list());
		write_files(files);
	}

	protected void index_modify(string? filename, ...)
	{
		if (d_repository == null)
		{
			return;
		}

		var files = files_from_varargs(filename, va_list());

		Ggit.OId id;

		Ggit.Index index;

		try
		{
			index = d_repository.get_index();
		}
		catch (GLib.Error e)
		{
			Assert.assert_no_error(e);
			return;
		}

		// Stage modifications in the index
		foreach (var f in files)
		{
			try
			{
				id = d_repository.create_blob_from_buffer(f.contents.data);
			}
			catch (GLib.Error e)
			{
				Assert.assert_no_error(e);
				continue;
			}

			try
			{
				var entry = d_repository.create_index_entry_for_path(f.filename, id);
				index.add(entry);
			}
			catch (GLib.Error e)
			{
				Assert.assert_no_error(e);
			}
		}

		try
		{
			index.write();
		}
		catch (GLib.Error e)
		{
			Assert.assert_no_error(e);
		}
	}

	protected Gitg.Branch? create_branch(string name)
	{
		try
		{
			var commit = d_repository.lookup<Gitg.Commit>(d_repository.get_head().get_target());
			return d_repository.create_branch(name, commit, Ggit.CreateFlags.NONE);
		}
		catch (Error e)
		{
			Assert.assert_no_error(e);
			return null;
		}
	}

	protected void checkout_branch(string name)
	{
		try
		{
			var branch = d_repository.lookup_reference_dwim(name) as Gitg.Branch;
			var commit = branch.resolve().lookup() as Ggit.Commit;
			var tree = commit.get_tree();

			var opts = new Ggit.CheckoutOptions();
			opts.set_strategy(Ggit.CheckoutStrategy.SAFE);

			d_repository.checkout_tree(tree, opts);
			d_repository.set_head(branch.get_name());
		}
		catch (Error e)
		{
			Assert.assert_no_error(e);
			return;
		}
	}

	protected override void set_up()
	{
		string wd;
		d_current_time = 0;

		try
		{
			wd = GLib.DirUtils.make_tmp("gitg-test-XXXXXX");
		}
		catch (GLib.Error e)
		{
			Assert.assert_no_error(e);
			return;
		}

		var f = GLib.File.new_for_path(wd);

		try
		{
			d_repository = (Gitg.Repository)Ggit.Repository.init_repository(f, false);
		}
		catch (GLib.Error e)
		{
			GLib.DirUtils.remove(wd);
			Assert.assert_no_error(e);
		}
	}

	private void remove_recursively(GLib.File f)
	{
		try
		{
			var info = f.query_info("standard::*", FileQueryInfoFlags.NONE);

			if (info.get_file_type() == FileType.DIRECTORY)
			{
				var e = f.enumerate_children("standard::*", FileQueryInfoFlags.NONE);

				while ((info = e.next_file()) != null)
				{
					var c = f.get_child(info.get_name());
					remove_recursively(c);
				}
			}

			f.delete();
		}
		catch (Error e)
		{
			stderr.printf("Failed to remove %s: %s\n", f.get_path(), e.message);
		}
	}

	protected Gitg.Branch? lookup_branch(string name)
	{
		try
		{
			var ret = d_repository.lookup_reference_dwim(name) as Gitg.Branch;
			assert_nonnull(ret);

			return ret;
		}
		catch (Error e)
		{
			Assert.assert_no_error(e);
		}

		return null;
	}

	protected Gitg.Commit? lookup_commit(string name)
	{
		try
		{
			var ret = lookup_branch(name).lookup() as Gitg.Commit;
			assert_nonnull(ret);

			return ret;
		}
		catch (Error e)
		{
			Assert.assert_no_error(e);
		}

		return null;
	}

	protected override void tear_down()
	{
		if (d_repository == null)
		{
			return;
		}

		remove_recursively(d_repository.get_workdir());
		d_repository = null;
	}
}

// ex:set ts=4 noet