Commit | Line | Data |
56b6dbe6 |
1 | use MooseX::Declare; |
2 | |
44a9ed75 |
3 | class Gitalist::Git::Repository with Gitalist::Git::HasUtils { |
56b6dbe6 |
4 | # FIXME, use Types::Path::Class and coerce |
5 | use MooseX::Types::Common::String qw/NonEmptySimpleStr/; |
3a6dd969 |
6 | use MooseX::Types::Path::Class qw/Dir/; |
829d41ed |
7 | use MooseX::Types::Moose qw/Str Maybe Bool HashRef ArrayRef/; |
3a6dd969 |
8 | use Gitalist::Git::Types qw/SHA1/; |
2e79039a |
9 | use Moose::Autobox; |
77edf882 |
10 | use List::MoreUtils qw/any zip/; |
3a6dd969 |
11 | use DateTime; |
5effb2c9 |
12 | use Encode qw/decode/; |
e66671b8 |
13 | use I18N::Langinfo qw/langinfo CODESET/; |
f3083570 |
14 | use Gitalist::Git::Object::Blob; |
467fa7d9 |
15 | use Gitalist::Git::Object::Tree; |
16 | use Gitalist::Git::Object::Commit; |
17 | use Gitalist::Git::Object::Tag; |
03bf0cab |
18 | use Gitalist::Git::Head; |
cc57f1d2 |
19 | use Gitalist::Git::Tag; |
56b6dbe6 |
20 | |
32a371cc |
21 | our $SHA1RE = qr/[0-9a-fA-F]{40}/; |
22 | |
23 | around BUILDARGS (ClassName $class: Dir $dir) { |
44a9ed75 |
24 | # Allows us to be called as Repository->new($dir) |
4b6bcf44 |
25 | # Last path component becomes $self->name |
26 | # Full path to git objects becomes $self->path |
32a371cc |
27 | my $name = $dir->dir_list(-1); |
dc8badf5 |
28 | if(-f $dir->file('.git', 'HEAD')) { # Non-bare repo above .git |
29 | $dir = $dir->subdir('.git'); |
30 | $name = $dir->dir_list(-2, 1); # .../name/.git |
31 | } elsif('.git' eq $dir->dir_list(-1)) { # Non-bare repo in .git |
32 | $name = $dir->dir_list(-2); |
33 | } |
32a371cc |
34 | confess("Can't find a git repository at " . $dir) |
35 | unless ( -f $dir->file('HEAD') ); |
36 | return $class->$orig(name => $name, |
37 | path => $dir); |
38 | } |
39 | |
56b6dbe6 |
40 | has name => ( isa => NonEmptySimpleStr, |
01ced85b |
41 | is => 'ro', required => 1 ); |
b5b638f7 |
42 | |
84f31a44 |
43 | has path => ( isa => Dir, |
3a6dd969 |
44 | is => 'ro', required => 1); |
56b6dbe6 |
45 | |
0617cbd0 |
46 | has description => ( isa => Str, |
56b6dbe6 |
47 | is => 'ro', |
48 | lazy_build => 1, |
49 | ); |
b5b638f7 |
50 | |
56b6dbe6 |
51 | has owner => ( isa => NonEmptySimpleStr, |
52 | is => 'ro', |
53 | lazy_build => 1, |
54 | ); |
b5b638f7 |
55 | |
3a6dd969 |
56 | has last_change => ( isa => Maybe['DateTime'], |
56b6dbe6 |
57 | is => 'ro', |
58 | lazy_build => 1, |
59 | ); |
60 | |
b5b638f7 |
61 | has is_bare => ( isa => Bool, |
62 | is => 'ro', |
63 | lazy => 1, |
64 | default => sub { |
2c130350 |
65 | -d $_[0]->path->parent->subdir($_[0]->name) |
b5b638f7 |
66 | ? 1 : 0 |
67 | }, |
68 | ); |
03bf0cab |
69 | has heads => ( isa => ArrayRef['Gitalist::Git::Head'], |
cc88eca5 |
70 | is => 'ro', |
3a6dd969 |
71 | lazy_build => 1); |
cc57f1d2 |
72 | has tags => ( isa => ArrayRef['Gitalist::Git::Tag'], |
ea19a20c |
73 | is => 'ro', |
74 | lazy_build => 1); |
cc88eca5 |
75 | has references => ( isa => HashRef[ArrayRef[Str]], |
76 | is => 'ro', |
77 | lazy_build => 1 ); |
78 | |
01ced85b |
79 | method BUILD { |
4dacb4b2 |
80 | $self->$_() for qw/last_change owner description /; # Ensure to build early. |
01ced85b |
81 | } |
82 | |
bba40bd5 |
83 | ## Public methods |
dcb1b927 |
84 | |
32a371cc |
85 | method head_hash (Str $head?) { |
86 | my $output = $self->run_cmd(qw/rev-parse --verify/, $head || 'HEAD' ); |
87 | confess("No such head: " . $head) unless defined $output; |
29debefd |
88 | |
32a371cc |
89 | my($sha1) = $output =~ /^($SHA1RE)$/; |
90 | return $sha1; |
56b6dbe6 |
91 | } |
92 | |
8bb7649b |
93 | method get_object (NonEmptySimpleStr $sha1) { |
ceef0cf1 |
94 | unless (is_SHA1($sha1)) { |
8bb7649b |
95 | $sha1 = $self->head_hash($sha1); |
96 | } |
e1307124 |
97 | my $type = $self->run_cmd('cat-file', '-t', $sha1); |
98 | chomp($type); |
467fa7d9 |
99 | my $class = 'Gitalist::Git::Object::' . ucfirst($type); |
100 | $class->new( |
82bc0f05 |
101 | repository => $self, |
54368e9d |
102 | sha1 => $sha1, |
f3083570 |
103 | type => $type, |
54368e9d |
104 | ); |
105 | } |
8bb7649b |
106 | |
4111e151 |
107 | method list_revs ( NonEmptySimpleStr :$sha1!, |
108 | Int :$count?, |
109 | Int :$skip?, |
110 | HashRef :$search?, |
05e8b4d0 |
111 | NonEmptySimpleStr :$file? ) { |
4111e151 |
112 | $sha1 = $self->head_hash($sha1) |
113 | if !$sha1 || $sha1 !~ $SHA1RE; |
114 | |
c1a9cd73 |
115 | my @search_opts; |
53a9d6de |
116 | if ($search and exists $search->{text}) { |
4111e151 |
117 | $search->{type} = 'grep' |
118 | if $search->{type} eq 'commit'; |
119 | @search_opts = ( |
120 | # This seems a little fragile ... |
121 | qq[--$search->{type}=$search->{text}], |
122 | '--regexp-ignore-case', |
123 | $search->{regexp} ? '--extended-regexp' : '--fixed-strings' |
124 | ); |
125 | } |
126 | |
127 | my $output = $self->run_cmd( |
128 | 'rev-list', |
129 | '--header', |
130 | (defined $count ? "--max-count=$count" : ()), |
131 | (defined $skip ? "--skip=$skip" : ()), |
132 | @search_opts, |
133 | $sha1, |
134 | '--', |
135 | ($file ? $file : ()), |
136 | ); |
137 | return unless $output; |
138 | |
cc88eca5 |
139 | my @revs = $self->_parse_rev_list($output); |
4111e151 |
140 | |
141 | return @revs; |
142 | } |
143 | |
bba40bd5 |
144 | method snapshot (NonEmptySimpleStr :$sha1, |
30db8f5b |
145 | NonEmptySimpleStr :$format |
146 | ) { |
bba40bd5 |
147 | # TODO - only valid formats are 'tar' and 'zip' |
148 | my $formats = { tgz => 'tar', zip => 'zip' }; |
149 | unless ($formats->exists($format)) { |
150 | die("No such format: $format"); |
151 | } |
152 | $format = $formats->{$format}; |
153 | my $name = $self->name; |
154 | $name =~ s,([^/])/*\.git$,$1,; |
155 | my $filename = $name; |
156 | $filename .= "-$sha1.$format"; |
157 | $name =~ s/\047/\047\\\047\047/g; |
158 | |
159 | my @cmd = ('archive', "--format=$format", "--prefix=$name/", $sha1); |
160 | return ($filename, $self->run_cmd_fh(@cmd)); |
161 | # TODO - support compressed archives |
2e79039a |
162 | } |
54368e9d |
163 | |
d8abdf1c |
164 | method reflog (@logargs) { |
165 | my @entries |
166 | = $self->run_cmd(qw(log -g), @logargs) |
167 | =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg; |
168 | |
05e8b4d0 |
169 | # commit 02526fc15beddf2c64798a947fecdd8d11bf993d |
170 | # Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>) |
171 | # Reflog message: push |
172 | # Author: Foo Barsby <fbarsby@example.com> |
173 | # Date: Thu Sep 17 12:26:05 2009 +0100 |
174 | # |
175 | # Merge branch 'abc123' |
d8abdf1c |
176 | |
177 | return map { |
178 | # XXX Stuff like this makes me want to switch to Git::PurePerl |
179 | my($sha1, $type, $author, $date) |
180 | = m{ |
181 | ^ commit \s+ ($SHA1RE)$ |
182 | .*? |
183 | Reflog[ ]message: \s+ (.+?)$ \s+ |
184 | Author: \s+ ([^<]+) <.*?$ \s+ |
185 | Date: \s+ (.+?)$ |
186 | }xms; |
187 | |
188 | pos($_) = index($_, $date) + length $date; |
189 | |
190 | # Yeah, I just did that. |
191 | my($msg) = /\G\s+(\S.*)/sg; |
192 | { |
193 | hash => $sha1, |
194 | type => $type, |
195 | author => $author, |
196 | |
197 | # XXX Add DateTime goodness. |
198 | date => $date, |
199 | message => $msg, |
200 | } |
201 | ; |
202 | } @entries; |
203 | } |
204 | |
cc88eca5 |
205 | ## BUILDERS |
50d45f00 |
206 | method _build_util { |
32a371cc |
207 | Gitalist::Git::Util->new( |
82bc0f05 |
208 | repository => $self, |
32a371cc |
209 | ); |
210 | } |
211 | |
212 | method _build_description { |
76c97bf6 |
213 | my $description = ""; |
5d8568c6 |
214 | eval { |
76c97bf6 |
215 | $description = $self->path->file('description')->slurp; |
216 | chomp $description; |
5d8568c6 |
217 | }; |
3c12ecb7 |
218 | $description = "Unnamed repository, edit the .git/description file to set a description" |
219 | if $description eq "Unnamed repository; edit this file 'description' to name the repository."; |
76c97bf6 |
220 | return $description; |
32a371cc |
221 | } |
222 | |
223 | method _build_owner { |
e66671b8 |
224 | my ($gecos, $name) = map { decode(langinfo(CODESET), $_) } (getpwuid $self->path->stat->uid)[6,0]; |
32a371cc |
225 | $gecos =~ s/,+$//; |
226 | return length($gecos) ? $gecos : $name; |
227 | } |
228 | |
229 | method _build_last_change { |
230 | my $last_change; |
231 | my $output = $self->run_cmd( |
232 | qw{ for-each-ref --format=%(committer) |
233 | --sort=-committerdate --count=1 refs/heads |
234 | }); |
235 | if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) { |
3a6dd969 |
236 | my $dt = DateTime->from_epoch(epoch => $epoch); |
32a371cc |
237 | $dt->set_time_zone($tz); |
238 | $last_change = $dt; |
239 | } |
240 | return $last_change; |
241 | } |
242 | |
cc88eca5 |
243 | method _build_heads { |
244 | my @revlines = $self->run_cmd_list(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads'); |
245 | my @ret; |
246 | for my $line (@revlines) { |
72fbbef7 |
247 | push @ret, Gitalist::Git::Head->new($line); |
cc88eca5 |
248 | } |
cc88eca5 |
249 | return \@ret; |
250 | } |
251 | |
ea19a20c |
252 | method _build_tags { |
253 | my @revlines = $self->run_cmd_list('for-each-ref', |
254 | '--sort=-creatordate', |
255 | '--format=%(objectname) %(objecttype) %(refname) %(*objectname) %(*objecttype) %(subject)%00%(creator)', |
c1a9cd73 |
256 | 'refs/tags' |
ea19a20c |
257 | ); |
258 | my @ret; |
259 | for my $line (@revlines) { |
cc57f1d2 |
260 | push @ret, Gitalist::Git::Tag->new($line); |
ea19a20c |
261 | } |
ea19a20c |
262 | return \@ret; |
263 | } |
264 | |
cc88eca5 |
265 | method _build_references { |
c1a9cd73 |
266 | # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11 |
267 | # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{} |
268 | my @reflist = $self->run_cmd_list(qw(show-ref --dereference)) |
05e8b4d0 |
269 | or return; |
cc88eca5 |
270 | my %refs; |
05e8b4d0 |
271 | for (@reflist) { |
272 | push @{$refs{$1}}, $2 |
273 | if m!^($SHA1RE)\srefs/(.*)$!; |
274 | } |
cc88eca5 |
275 | |
05e8b4d0 |
276 | return \%refs; |
cc88eca5 |
277 | } |
278 | |
279 | ## Private methods |
cc88eca5 |
280 | method _parse_rev_list ($output) { |
281 | return |
282 | map $self->get_gpp_object($_), |
ceef0cf1 |
283 | grep is_SHA1($_), |
cc88eca5 |
284 | map split(/\n/, $_, 6), split /\0/, $output; |
285 | } |
286 | |
775e96e0 |
287 | } # end class |
288 | |
289 | __END__ |
290 | |
bba40bd5 |
291 | =head1 NAME |
292 | |
44a9ed75 |
293 | Gitalist::Git::Repository - Model of a git repository |
bba40bd5 |
294 | |
295 | =head1 SYNOPSIS |
296 | |
297 | my $gitrepo = dir('/repo/base/Gitalist'); |
82bc0f05 |
298 | my $repository = Gitalist::Git::Repository->new($gitrepo); |
299 | $repository->name; # 'Gitalist' |
300 | $repository->path; # '/repo/base/Gitalist/.git' |
301 | $repository->description; # 'Unnamed repository.' |
bba40bd5 |
302 | |
303 | =head1 DESCRIPTION |
304 | |
305 | This class models a git repository, referred to in Gitalist |
87581f05 |
306 | as a "Repository". |
bba40bd5 |
307 | |
8ba87261 |
308 | |
bba40bd5 |
309 | =head1 ATTRIBUTES |
310 | |
311 | =head2 name |
312 | |
87581f05 |
313 | The name of the Repository. If unspecified, this will be derived from the path to the git repository. |
bba40bd5 |
314 | |
bba40bd5 |
315 | =head2 path |
316 | |
8ba87261 |
317 | L<Path::Class:Dir> for the filesystem path to the git repository. |
bba40bd5 |
318 | |
bba40bd5 |
319 | =head2 description |
320 | |
8ba87261 |
321 | The contents of .git/description. |
bba40bd5 |
322 | |
bba40bd5 |
323 | =head2 owner |
324 | |
8ba87261 |
325 | Owner of the files on the filesystem. |
bba40bd5 |
326 | |
bba40bd5 |
327 | =head2 last_change |
328 | |
8ba87261 |
329 | The L<DateTime> of the last modification of the repository. This will be C<undef> if the repository has never been used. |
bba40bd5 |
330 | |
bba40bd5 |
331 | =head2 is_bare |
332 | |
8ba87261 |
333 | True if this is a bare git repository. |
bba40bd5 |
334 | |
bba40bd5 |
335 | =head2 heads |
336 | |
bba40bd5 |
337 | =head2 tags |
338 | |
8ba87261 |
339 | An array of the name and sha1 of all heads/tags in the repository. |
bba40bd5 |
340 | |
341 | =head2 references |
342 | |
343 | Hashref of ArrayRefs for each reference. |
344 | |
8ba87261 |
345 | |
bba40bd5 |
346 | =head1 METHODS |
347 | |
348 | =head2 head_hash ($head?) |
349 | |
350 | Return the sha1 for HEAD, or any specified head. |
351 | |
bba40bd5 |
352 | =head2 get_object ($sha1) |
353 | |
354 | Return an appropriate subclass of L<Gitalist::Git::Object> for the given sha1. |
355 | |
8ba87261 |
356 | =head2 list_revs ($sha1, $count?, $skip?, \%search?, $file?) |
bba40bd5 |
357 | |
358 | Returns a list of revs for the given head ($sha1). |
359 | |
8ba87261 |
360 | =head2 snapshot ($sha1, $format) |
bba40bd5 |
361 | |
362 | Generate an archived snapshot of the repository. |
363 | $sha1 should be a commit or tree. |
364 | Returns a filehandle to read from. |
365 | |
8ba87261 |
366 | =head2 diff ($commit, $patch?, $parent?, $file?) |
bba40bd5 |
367 | |
368 | Generate a diff from a given L<Gitalist::Git::Object>. |
369 | |
8ba87261 |
370 | =head2 reflog (@lorgargs) |
bba40bd5 |
371 | |
372 | Return a list of hashes representing each reflog entry. |
373 | |
374 | FIXME Should this return objects? |
375 | |
bba40bd5 |
376 | |
b5b638f7 |
377 | =head1 SEE ALSO |
378 | |
379 | L<Gitalist::Git::Util> L<Gitalist::Git::Object> |
380 | |
8ba87261 |
381 | |
775e96e0 |
382 | =head1 AUTHORS |
b5b638f7 |
383 | |
775e96e0 |
384 | See L<Gitalist> for authors. |
b5b638f7 |
385 | |
386 | =head1 LICENSE |
387 | |
775e96e0 |
388 | See L<Gitalist> for the license. |
b5b638f7 |
389 | |
390 | =cut |