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