File Coverage

blib/lib/Types/Git.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             package Types::Git;
2              
3             $Types::Git::VERSION = '0.04';
4              
5             =head1 NAME
6              
7             Types::Git - Type::Tiny types for git stuff.
8              
9             =head1 SYNOPSIS
10              
11             package Foo;
12            
13             use Types::Git -types;
14            
15             use Moo;
16             use strictures 1;
17             use namespace::clean;
18            
19             has ref => (
20             is => 'ro',
21             isa => GitRef,
22             );
23              
24             =head1 DESCRIPTION
25              
26             This module provides several L types for some of
27             git's data types.
28              
29             =cut
30              
31 1     1   185289 use Type::Library -base;
  1         23002  
  1         9  
32 1     1   857 use Type::Utils -all;
  1         4106  
  1         11  
33 1     1   3262 use Types::Common::String -types;
  1         60240  
  1         15  
34 1     1   2134 use List::MoreUtils qw( any );
  1         7836  
  1         7  
35              
36 1     1   900 use strictures 2;
  1         10  
  1         39  
37 1     1   623 use namespace::clean;
  1         12650  
  1         7  
38              
39             =head1 TYPES
40              
41             =head2 GitSHA
42              
43             A SHA1 hex, must be 40 characters or less long and contain
44             only hex characters.
45              
46             =cut
47              
48             my $GitSHA = declare 'GitSHA',
49             as NonEmptySimpleStr,
50             where {
51             length($_) <= 40 and
52             $_ =~ m{^[a-f0-9]+$}
53             };
54              
55             =head2 GitLooseRef
56              
57             Just like L except one-level refs (those without any forward slashes)
58             are allowed. This is useful for validating a branch or tag name.
59              
60             =cut
61              
62             my $GitLooseRef = declare 'GitLooseRef',
63             as NonEmptySimpleStr,
64             where {
65             # 1. They can include slash / for hierarchical (directory) grouping,
66             # but no slash-separated component can begin with a dot . or end
67             # with the sequence .lock.
68             ( ! any { $_ =~ m{^\.} or $_ =~ m{\.lock$} } split(/\//, $_) ) and
69             # 3. They cannot have two consecutive dots .. anywhere.
70             $_ !~ m{\.\.} and
71             # 4. They cannot have ASCII control characters (i.e. bytes whose
72             # values are lower than \040, or \177 DEL), space, tilde ~, caret
73             # ^, or colon : anywhere.
74             $_ !~ m{[\000-\040\177 ~^:]} and
75             # 5. They cannot have question-mark ?, asterisk *, or open bracket [
76             # anywhere.
77             $_ !~ m{[?*[]} and
78             # 6. They cannot begin or end with a slash / or contain multiple
79             # consecutive slashes.
80             $_ !~ m{^/} and $_ !~ m{/$} and $_ !~ m{//} and
81             # 7. They cannot end with a dot ..
82             $_ !~ m{\.$} and
83             # 8. They cannot contain a sequence @{.
84             $_ !~ m{\@\{} and
85             # 9. They cannot be the single character @.
86             $_ ne '@' and
87             # 10. They cannot contain a \.
88             $_ !~ m{\\}
89             };
90              
91             =head2 GitRef
92              
93             Matches a ref against the same rules that
94             L uses.
95              
96             =cut
97              
98             my $GitRef = declare 'GitRef',
99             as $GitLooseRef,
100             where {
101             # 2. They must contain at least one /.
102             $_ =~ m{/}
103             };
104              
105             =head2 GitBranchRef
106              
107             A L which begins with C and ends with a
108             L.
109              
110             =cut
111              
112             declare 'GitBranchRef',
113             as $GitRef,
114             where { $_ =~ m{^refs/heads/} };
115              
116             =head2 GitTagRef
117              
118             A L which begins with C and ends with a
119             L.
120              
121             =cut
122              
123             declare 'GitTagRef',
124             as $GitRef,
125             where { $_ =~ m{^refs/tags/} };
126              
127             =head2 GitObject
128              
129             This is a union type of L and L. In the future
130             this type may be expanded to include other types as more of
131             L is incorporated
132             with this module.
133              
134             =cut
135              
136             my $GitObject = declare 'GitObject',
137             as $GitSHA | $GitLooseRef;
138              
139             =head2 GitRevision
140              
141             Currenlty this is an alias for L but may be extended in
142             the future to include other types as more of
143             L is incorporated
144             with this module.
145              
146             This type is meant to be the same as L except with extended
147             rules for date ranges and such.
148              
149             =cut
150              
151             my $GitRevision = declare 'GitRevision',
152             as $GitObject;
153              
154             1;
155             __END__