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