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