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