File: 13-checkout.t

package info (click to toggle)
libgit-raw-perl 0.90%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,456 kB
  • sloc: perl: 5,426; ansic: 182; makefile: 6
file content (122 lines) | stat: -rw-r--r-- 2,915 bytes parent folder | download | duplicates (3)
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
#!perl

use Test::More;

use Git::Raw;
use File::Slurp::Tiny qw(write_file read_file);
use File::Spec::Functions qw(catfile rel2abs);

my $path = rel2abs(catfile('t', 'test_repo'));
my $repo = Git::Raw::Repository -> open($path);

my $fst = ($repo -> head -> target -> parents())[0];
is $fst -> message, "second commit\n";

$repo -> checkout($fst, {
	'ancestor_label' => 'ancestor',
	'our_label'      => 'ours',
	'their_label'    => 'theirs',
	'checkout_strategy' => { 'none' => 1 }
});

is_deeply $repo -> status({}) -> {'test3/under/the/tree/test3'}, undef;

$repo -> checkout($repo -> head, {});

$repo -> checkout($fst, {
	'checkout_strategy' => { 'safe' => 1, 'remove_untracked' => 1 }
});

is_deeply $repo -> status({}) -> {'test3/under/the/tree/test3'}, {'flags' => ['index_deleted']};

$repo -> checkout($repo -> head, {});

my $file  = $repo -> workdir . 'test';
write_file($file, 'this is a checkout test');

my $file2 = $repo -> workdir . 'test2';
write_file($file2, 'this is a checkout test');

my $index = $repo -> index;
$index -> add('test');
$index -> add('test2');
$index -> write;
$index -> read;

my $tree = $index -> write_tree;

my $name   = $repo -> config -> str('user.name');
my $email  = $repo -> config -> str('user.email');
my $me   = Git::Raw::Signature -> now($name, $email);

my $commit = $repo -> commit(
	"checkout commit\n", $me, $me, [$repo -> head -> target], $tree
);

# checkout index
write_file($file, 'this is a checkout test with modifications');
is read_file($file), 'this is a checkout test with modifications';

$index -> checkout({
	'checkout_strategy' => { 'force' => 1 },
	'paths'     => [
		'test'
	],
});

is read_file($file),  'this is a checkout test';
is read_file($file2), 'this is a checkout test';

my $untracked_file = $repo -> workdir . 'untracked_file';
write_file($untracked_file, 'this is a checkout test');

my $files_expected = [ 'test2', 'untracked_file' ];
my $files_checked_out = [];
$repo -> checkout($fst, {
	'checkout_strategy' => { 'safe' => 1, 'remove_untracked' => 1 },
	'paths'     => [
		undef,
		@$files_expected
	],
	'notify'    => [
		'conflict',
		'dirty',
		'updated',
		'untracked',
		'ignored',
		'all'
	],
	'callbacks' => {
		'notify' => sub {
			my ($path, $flags) = @_;

			if ($path eq 'untracked_file') {
				is_deeply $flags, ['untracked'];
			} elsif ($path eq 'test2') {
				is_deeply $flags, ['updated'];
			}

			push @$files_checked_out, $path;

			return 0;
		},
		'progress' => sub {
			my ($path, $completed_steps, $total_steps) = @_;

			if (!defined $path) {
				is $completed_steps, 0;
				is $total_steps, scalar(@$files_expected);
			} else {
				ok $completed_steps <= $total_steps;
				is scalar(grep { $_ eq $path } @$files_expected), 1;
			}
		}
	}
});

is_deeply $files_checked_out, $files_expected;

is read_file($file),  'this is a checkout test';
is read_file($file2), 'this is a second test';

done_testing;