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