Commit | Line | Data |
fbf3eb7e |
1 | package Gitalist::Model::Git; |
2 | |
3 | use Moose; |
4 | use namespace::autoclean; |
5 | |
37997f11 |
6 | extends 'Catalyst::Model'; |
7 | with 'Catalyst::Component::InstancePerContext'; |
86382b95 |
8 | |
fbf3eb7e |
9 | use DateTime; |
10 | use Path::Class; |
1feb3d6b |
11 | use File::Which; |
fbf3eb7e |
12 | use Carp qw/croak/; |
13 | use File::Find::Rule; |
14 | use DateTime::Format::Mail; |
15 | use File::Stat::ModeString; |
ad8884fc |
16 | use List::MoreUtils qw/any zip/; |
1feb3d6b |
17 | use Scalar::Util qw/blessed/; |
18 | use MooseX::Types::Common::String qw/NonEmptySimpleStr/; # FIXME, use Types::Path::Class and coerce |
fbf3eb7e |
19 | |
a7cc1ede |
20 | use Git::PurePerl; |
21 | |
27e05d7b |
22 | =head1 NAME |
23 | |
24 | Gitalist::Model::Git - the model for git interactions |
25 | |
26 | =head1 DESCRIPTION |
27 | |
28 | [enter your description here] |
29 | |
30 | =head1 METHODS |
31 | |
32 | =cut |
33 | |
b3ad9e63 |
34 | # Should these live in a separate module? Or perhaps extended Regexp::Common? |
35 | our $SHA1RE = qr/[0-9a-fA-F]{40}/; |
36 | |
a7cc1ede |
37 | # These are static and only need to be setup on app start. |
8c032474 |
38 | has repo_dir => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 ); # Fixme - path::class |
39 | has git => ( isa => NonEmptySimpleStr, is => 'ro', lazy_build => 1 ); |
a7cc1ede |
40 | # These are dynamic and can be different from one request to the next. |
41 | has project => ( isa => NonEmptySimpleStr, is => 'rw'); |
42 | has gpp => ( isa => 'Git::PurePerl', is => 'rw', lazy_build => 1 ); |
43 | |
37997f11 |
44 | sub build_per_context_instance { |
a7cc1ede |
45 | my ( $self, $c ) = @_; |
5a2f0948 |
46 | |
47 | # If we don't have a project param it probably means we're at / |
48 | return $self |
49 | unless $c->req->param('p'); |
50 | |
a7cc1ede |
51 | $self->project( $c->req->param('p') ); |
52 | |
53 | (my $pd = $self->project_dir( $self->project )) =~ s{/\.git$}(); |
54 | $self->gpp( Git::PurePerl->new(directory => $pd) ); |
55 | |
56 | return $self; |
57 | } |
1feb3d6b |
58 | |
27e05d7b |
59 | =head2 BUILD |
60 | |
61 | =cut |
62 | |
8c032474 |
63 | sub BUILD { |
64 | my ($self) = @_; |
65 | $self->git; # Cause lazy value build. |
1feb3d6b |
66 | $self->repo_dir; |
8c032474 |
67 | } |
68 | |
69 | sub _build_git { |
1feb3d6b |
70 | my $git = File::Which::which('git'); |
d7c9a32f |
71 | |
1feb3d6b |
72 | if (!$git) { |
73 | die <<EOR; |
fbf3eb7e |
74 | Could not find a git executable. |
75 | Please specify the which git executable to use in gitweb.yml |
76 | EOR |
1feb3d6b |
77 | } |
fbf3eb7e |
78 | |
1feb3d6b |
79 | return $git; |
04d1d917 |
80 | } |
1feb3d6b |
81 | |
8c032474 |
82 | sub _build_repo_dir { |
1ef8dc7d |
83 | return Gitalist->config->{repo_dir}; |
84 | } |
85 | |
27e05d7b |
86 | =head2 get_object |
87 | |
88 | A wrapper for the equivalent L<Git::PurePerl> method. |
89 | |
90 | =cut |
91 | |
1ef8dc7d |
92 | sub get_object { |
a7cc1ede |
93 | my($self, $sha1) = @_; |
94 | |
9dc3b9a5 |
95 | # We either want an object or undef, *not* an empty list. |
a7cc1ede |
96 | return $self->gpp->get_object($sha1) || undef; |
8c032474 |
97 | } |
d7c9a32f |
98 | |
27e05d7b |
99 | =head2 is_git_repo |
100 | |
101 | Determine whether a given directory (as a L<Path::Class::Dir> object) is a |
102 | C<git> repo. |
103 | |
104 | =cut |
105 | |
fbf3eb7e |
106 | sub is_git_repo { |
1feb3d6b |
107 | my ($self, $dir) = @_; |
fbf3eb7e |
108 | |
1feb3d6b |
109 | return -f $dir->file('HEAD') || -f $dir->file('.git/HEAD'); |
fbf3eb7e |
110 | } |
111 | |
27e05d7b |
112 | =head2 run_cmd |
113 | |
114 | Call out to the C<git> binary and return a string consisting of the output. |
115 | |
116 | =cut |
117 | |
1ef8dc7d |
118 | sub run_cmd { |
119 | my ($self, @args) = @_; |
120 | |
121 | print STDERR 'RUNNING: ', $self->git, qq[ @args], $/; |
122 | |
123 | open my $fh, '-|', $self->git, @args |
124 | or die "failed to run git command"; |
125 | binmode $fh, ':encoding(UTF-8)'; |
126 | |
127 | my $output = do { local $/ = undef; <$fh> }; |
128 | close $fh; |
129 | |
130 | return $output; |
131 | } |
132 | |
27e05d7b |
133 | =head2 project_dir |
134 | |
135 | The directory under which the given project will reside i.e C<.git/..> |
136 | |
137 | =cut |
138 | |
1ef8dc7d |
139 | sub project_dir { |
140 | my($self, $project) = @_; |
141 | |
142 | my $dir = blessed($project) && $project->isa('Path::Class::Dir') |
143 | ? $project->stringify |
27e05d7b |
144 | : $self->dir_from_project_name($project); |
1ef8dc7d |
145 | |
27e05d7b |
146 | $dir .= '/.git' |
1e805b5e |
147 | if -f dir($dir)->file('.git/HEAD'); |
1ef8dc7d |
148 | |
149 | return $dir; |
150 | } |
151 | |
27e05d7b |
152 | =head2 run_cmd_in |
153 | |
154 | Run a C<git> command in a given project and return the output as a string. |
155 | |
156 | =cut |
157 | |
1ef8dc7d |
158 | sub run_cmd_in { |
159 | my ($self, $project, @args) = @_; |
160 | |
27e05d7b |
161 | return $self->run_cmd('--git-dir' => $self->project_dir($project), @args); |
1ef8dc7d |
162 | } |
163 | |
27e05d7b |
164 | =head2 command |
165 | |
166 | Run a C<git> command for the project specified in the C<p> parameter and |
167 | return the output as a list of strings corresponding to the lines of output. |
168 | |
169 | =cut |
170 | |
b3ad9e63 |
171 | sub command { |
172 | my($self, @args) = @_; |
173 | |
27e05d7b |
174 | my $output = $self->run_cmd('--git-dir' => $self->project_dir($self->project), @args); |
b3ad9e63 |
175 | |
176 | return $output ? split(/\n/, $output) : (); |
177 | } |
178 | |
27e05d7b |
179 | =head2 project_info |
180 | |
181 | Returns a hash corresponding to a given project's properties. The keys will |
182 | be: |
183 | |
1e805b5e |
184 | name |
185 | description (empty if .git/description is empty/unnamed) |
186 | owner |
187 | last_change |
27e05d7b |
188 | |
189 | =cut |
190 | |
fbf3eb7e |
191 | sub project_info { |
1feb3d6b |
192 | my ($self, $project) = @_; |
fbf3eb7e |
193 | |
1feb3d6b |
194 | return { |
195 | name => $project, |
196 | $self->get_project_properties( |
27e05d7b |
197 | $self->dir_from_project_name($project), |
198 | ), |
199 | }; |
fbf3eb7e |
200 | } |
201 | |
27e05d7b |
202 | =head2 get_project_properties |
203 | |
204 | Called by C<project_info> to get a project's properties. |
205 | |
206 | =cut |
207 | |
fbf3eb7e |
208 | sub get_project_properties { |
1feb3d6b |
209 | my ($self, $dir) = @_; |
210 | my %props; |
fbf3eb7e |
211 | |
1feb3d6b |
212 | eval { |
213 | $props{description} = $dir->file('description')->slurp; |
214 | chomp $props{description}; |
fbf3eb7e |
215 | }; |
216 | |
1feb3d6b |
217 | if ($props{description} && $props{description} =~ /^Unnamed repository;/) { |
218 | delete $props{description}; |
219 | } |
fbf3eb7e |
220 | |
1feb3d6b |
221 | ($props{owner} = (getpwuid $dir->stat->uid)[6]) =~ s/,+$//; |
fbf3eb7e |
222 | |
1feb3d6b |
223 | my $output = $self->run_cmd_in($dir, qw{ |
224 | for-each-ref --format=%(committer) |
225 | --sort=-committerdate --count=1 refs/heads |
226 | }); |
fbf3eb7e |
227 | |
1feb3d6b |
228 | if (my ($epoch, $tz) = $output =~ /\s(\d+)\s+([+-]\d+)$/) { |
229 | my $dt = DateTime->from_epoch(epoch => $epoch); |
230 | $dt->set_time_zone($tz); |
231 | $props{last_change} = $dt; |
232 | } |
fbf3eb7e |
233 | |
1feb3d6b |
234 | return %props; |
fbf3eb7e |
235 | } |
236 | |
27e05d7b |
237 | =head2 list_projects |
238 | |
239 | For the C<repo_dir> specified in the config return an array of projects where |
240 | each item will contain the contents of L</project_info>. |
241 | |
242 | =cut |
243 | |
fbf3eb7e |
244 | sub list_projects { |
27e05d7b |
245 | my ($self, $dir) = @_; |
fbf3eb7e |
246 | |
27e05d7b |
247 | my $base = dir($dir || $self->repo_dir); |
fbf3eb7e |
248 | |
1feb3d6b |
249 | my @ret; |
250 | my $dh = $base->open; |
251 | while (my $file = $dh->read) { |
252 | next if $file =~ /^.{1,2}$/; |
253 | |
254 | my $obj = $base->subdir($file); |
255 | next unless -d $obj; |
256 | next unless $self->is_git_repo($obj); |
257 | |
258 | # XXX Leaky abstraction alert! |
259 | my $is_bare = !-d $obj->subdir('.git'); |
d7c9a32f |
260 | |
1feb3d6b |
261 | my $name = (File::Spec->splitdir($obj))[-1]; |
262 | push @ret, { |
27e05d7b |
263 | name => ($name . ( $is_bare ? '' : '/.git' )), |
1feb3d6b |
264 | $self->get_project_properties( |
265 | $is_bare ? $obj : $obj->subdir('.git') |
266 | ), |
267 | }; |
268 | } |
269 | |
270 | return [sort { $a->{name} cmp $b->{name} } @ret]; |
fbf3eb7e |
271 | } |
272 | |
27e05d7b |
273 | =head2 dir_from_project_name |
274 | |
275 | Get the corresponding directory of a given project. |
276 | |
277 | =cut |
278 | |
279 | sub dir_from_project_name { |
1feb3d6b |
280 | my ($self, $project) = @_; |
fbf3eb7e |
281 | |
1feb3d6b |
282 | return dir($self->repo_dir)->subdir($project); |
fbf3eb7e |
283 | } |
284 | |
27e05d7b |
285 | =head2 head_hash |
286 | |
0ee97fec |
287 | Find the hash of a given head (defaults to HEAD) of given (or current) project. |
27e05d7b |
288 | |
289 | =cut |
290 | |
c8870bd3 |
291 | sub head_hash { |
1e805b5e |
292 | my ($self, $head) = @_; |
fbf3eb7e |
293 | |
1e805b5e |
294 | my($output) = $self->command(qw/rev-parse --verify/, $head || 'HEAD' ); |
295 | return unless $output; |
fbf3eb7e |
296 | |
a7cc1ede |
297 | my($sha1) = $output =~ /^($SHA1RE)$/; |
298 | return $sha1; |
fbf3eb7e |
299 | } |
300 | |
27e05d7b |
301 | =head2 list_tree |
302 | |
303 | For a given tree sha1 return an array describing the tree's contents. Where |
304 | the keys for each item will be: |
305 | |
1e805b5e |
306 | mode |
307 | type |
308 | object |
309 | file |
27e05d7b |
310 | |
311 | =cut |
312 | |
fbf3eb7e |
313 | sub list_tree { |
1e805b5e |
314 | my ($self, $sha1) = @_; |
fbf3eb7e |
315 | |
1e805b5e |
316 | $sha1 = $self->head_hash($sha1) |
317 | if !$sha1 or $sha1 !~ $SHA1RE; |
fbf3eb7e |
318 | |
1e805b5e |
319 | my($output) = $self->command(qw/ls-tree -z/, $sha1); |
320 | return |
321 | unless $output; |
fbf3eb7e |
322 | |
1feb3d6b |
323 | my @ret; |
324 | for my $line (split /\0/, $output) { |
325 | my ($mode, $type, $object, $file) = split /\s+/, $line, 4; |
d7c9a32f |
326 | |
1feb3d6b |
327 | push @ret, { |
b3fa97cd |
328 | mode => oct $mode, |
1e805b5e |
329 | # XXX I wonder why directories always turn up as 040000 ... |
b3fa97cd |
330 | modestr => $self->get_object_mode_string({mode=>oct $mode}), |
331 | type => $type, |
332 | object => $object, |
333 | file => $file, |
27e05d7b |
334 | }; |
1feb3d6b |
335 | } |
336 | |
337 | return @ret; |
fbf3eb7e |
338 | } |
339 | |
27e05d7b |
340 | =head2 get_object_mode_string |
341 | |
342 | Provide a string equivalent of an octal mode e.g 0644 eq '-rw-r--r--'. |
343 | |
344 | =cut |
345 | |
fbf3eb7e |
346 | sub get_object_mode_string { |
1feb3d6b |
347 | my ($self, $object) = @_; |
fbf3eb7e |
348 | |
1feb3d6b |
349 | return unless $object && $object->{mode}; |
350 | return mode_to_string($object->{mode}); |
fbf3eb7e |
351 | } |
352 | |
27e05d7b |
353 | =head2 get_object_type |
354 | |
355 | =cut |
356 | |
fbf3eb7e |
357 | sub get_object_type { |
1e805b5e |
358 | my ($self, $object) = @_; |
1feb3d6b |
359 | |
1e805b5e |
360 | my($output) = $self->command(qw/cat-file -t/, $object) |
361 | or return; |
1feb3d6b |
362 | |
1feb3d6b |
363 | return $output; |
364 | } |
365 | |
27e05d7b |
366 | =head2 cat_file |
367 | |
368 | Return the contents of a given file. |
369 | |
370 | =cut |
371 | |
372 | sub cat_file { |
1e805b5e |
373 | my ($self, $object) = @_; |
27e05d7b |
374 | |
375 | my $type = $self->get_object_type($object); |
376 | die "object `$object' is not a file\n" |
377 | if (!defined $type || $type ne 'blob'); |
378 | |
1e805b5e |
379 | my($output) = $self->command(qw/cat-file -p/, $object) |
380 | or return; |
27e05d7b |
381 | |
382 | return $output; |
383 | } |
384 | |
385 | =head2 hash_by_path |
386 | |
387 | For a given sha1 and path find the corresponding hash. Useful for find blobs. |
388 | |
389 | =cut |
390 | |
c8870bd3 |
391 | sub hash_by_path { |
1feb3d6b |
392 | my($self, $base, $path, $type) = @_; |
fbf3eb7e |
393 | |
1feb3d6b |
394 | $path =~ s{/+$}(); |
fbf3eb7e |
395 | |
c8870bd3 |
396 | my($line) = $self->command('ls-tree', $base, '--', $path) |
1feb3d6b |
397 | or return; |
398 | |
1e805b5e |
399 | #'100644 blob 0fa3f3a66fb6a137f6ec2c19351ed4d807070ffa panic.c' |
b3ad9e63 |
400 | $line =~ m/^([0-9]+) (.+) ($SHA1RE)\t/; |
1feb3d6b |
401 | return defined $type && $type ne $2 |
402 | ? () |
c8870bd3 |
403 | : $3; |
295c9703 |
404 | } |
405 | |
27e05d7b |
406 | =head2 valid_rev |
fbf3eb7e |
407 | |
27e05d7b |
408 | Check whether a given rev is valid i.e looks like a sha1. |
fbf3eb7e |
409 | |
27e05d7b |
410 | =cut |
fbf3eb7e |
411 | |
412 | sub valid_rev { |
1feb3d6b |
413 | my ($self, $rev) = @_; |
fbf3eb7e |
414 | |
1feb3d6b |
415 | return unless $rev; |
b3ad9e63 |
416 | return ($rev =~ /^($SHA1RE)$/); |
fbf3eb7e |
417 | } |
418 | |
9c0984d1 |
419 | =head2 raw_diff |
27e05d7b |
420 | |
9c0984d1 |
421 | Provides the raw output of a diff. |
27e05d7b |
422 | |
423 | =cut |
424 | |
ad8884fc |
425 | # gitweb uses the following sort of command for diffing merges: |
426 | # /home/dbrook/apps/bin/git --git-dir=/home/dbrook/dev/app/.git diff-tree -r -M --no-commit-id --patch-with-raw --full-index --cc 316cf158df3f6207afbae7270bcc5ba0 -- |
427 | # and for regular diffs |
428 | # /home/dbrook/apps/bin/git --git-dir=/home/dbrook/dev/app/.git diff-tree -r -M --no-commit-id --patch-with-raw --full-index 2e3454ca0749641b42f063730b0090e1 316cf158df3f6207afbae7270bcc5ba0 -- |
429 | |
9c0984d1 |
430 | sub raw_diff { |
6cf4366a |
431 | my ($self, @args) = @_; |
fbf3eb7e |
432 | |
ad8884fc |
433 | return $self->command( |
1e805b5e |
434 | qw(diff-tree -r -M --no-commit-id --full-index), |
435 | @args |
ad8884fc |
436 | ); |
9c0984d1 |
437 | } |
fbf3eb7e |
438 | |
ad8884fc |
439 | =pod |
9c0984d1 |
440 | diff --git a/TODO b/TODO |
441 | index 6a05e77..2071fd0 100644 |
442 | --- a/TODO |
443 | +++ b/TODO |
444 | @@ -2,4 +2,3 @@ |
445 | * An action to find what branches have been merged, either as a list or through a search mechanism. |
446 | * An action to find which branches a given commit is on. |
447 | * Fix any not text/html bits e.g the patch action. |
448 | -* Simplify the creation of links. |
449 | diff --git a/lib/Gitalist/Controller/Root.pm b/lib/Gitalist/Controller/Root.pm |
450 | index 706d024..7fac165 100644 |
451 | --- a/lib/Gitalist/Controller/Root.pm |
452 | +++ b/lib/Gitalist/Controller/Root.pm |
453 | @@ -157,23 +157,6 @@ sub shortlog : Local { |
454 | ); |
455 | } |
456 | |
457 | -=head2 tree |
458 | - |
459 | -The tree of a given commit. |
460 | =cut |
461 | |
462 | =head2 diff |
463 | |
464 | Returns a list of diff chunks corresponding to the files contained in the diff |
465 | and some associated metadata. |
466 | |
467 | =cut |
468 | |
ad8884fc |
469 | # XXX Ideally this would return a wee object instead of ad hoc structures. |
9c0984d1 |
470 | sub diff { |
ad8884fc |
471 | my($self, %args) = @_; |
472 | |
473 | # So either a parent is specifed, or we use the commit's parent if there's |
474 | # only one, otherwise it was a merge commit. |
475 | my $parent = $args{parent} |
1e805b5e |
476 | ? $args{parent} |
477 | : @{$args{commit}->parents} <= 1 |
478 | ? $args{commit}->parent_sha1 |
479 | : '-c'; |
ad8884fc |
480 | my @etc = ( |
481 | ( $args{file} ? ('--', $args{file}) : () ), |
482 | ); |
483 | |
484 | my @out = $self->raw_diff( |
1e805b5e |
485 | ( $args{patch} ? '--patch-with-raw' : () ), |
486 | $parent, $args{commit}->sha1, @etc |
ad8884fc |
487 | ); |
9c0984d1 |
488 | |
ad8884fc |
489 | # XXX Yes, there is much wrongness having parse_diff_tree be destructive. |
490 | my @difftree = $self->parse_diff_tree(\@out); |
491 | |
492 | return \@difftree |
1e805b5e |
493 | unless $args{patch}; |
ad8884fc |
494 | |
495 | # The blank line between the tree and the patch. |
496 | shift @out; |
497 | |
498 | # XXX And no I'm not happy about having diff return tree + patch. |
499 | return \@difftree, [$self->parse_diff(@out)]; |
6cf4366a |
500 | } |
501 | |
502 | sub parse_diff { |
503 | my($self, @diff) = @_; |
9c0984d1 |
504 | |
505 | my @ret; |
9dc3b9a5 |
506 | for (@diff) { |
5a2f0948 |
507 | # This regex is a little pathological. |
508 | if(m{^diff --git (a/(.*?)) (b/\2)}) { |
9c0984d1 |
509 | push @ret, { |
5a2f0948 |
510 | head => $_, |
511 | a => $1, |
512 | b => $3, |
513 | file => $2, |
514 | diff => '', |
9c0984d1 |
515 | }; |
5a2f0948 |
516 | next; |
9c0984d1 |
517 | } |
5a2f0948 |
518 | |
519 | if(/^index (\w+)\.\.(\w+) (\d+)$/) { |
520 | @{$ret[-1]}{qw(index src dst mode)} = ($_, $1, $2, $3); |
521 | next |
522 | } |
523 | |
524 | # XXX Somewhat hacky. Ahem. |
525 | $ret[@ret ? -1 : 0]{diff} .= "$_\n"; |
9c0984d1 |
526 | } |
527 | |
528 | return @ret; |
fbf3eb7e |
529 | } |
530 | |
ad8884fc |
531 | # $ git diff-tree -r --no-commit-id -M b222ff0a7260cc1777c7e455dfcaf22551a512fc 7e54e579e196c6c545fee1030175f65a111039d4 |
532 | # :100644 100644 6a85d6c6315b55a99071974eb6ce643aeb2799d6 44c03ed6c328fa6de4b1d9b3f19a3de96b250370 M templates/blob.tt2 |
533 | |
534 | =head2 parse_diff_tree |
535 | |
536 | Given a L<Git::PurePerl> commit object return a list of hashes corresponding |
537 | to the C<diff-tree> output. |
538 | |
539 | =cut |
540 | |
541 | sub parse_diff_tree { |
542 | my($self, $diff) = @_; |
543 | |
544 | my @keys = qw(modesrc modedst sha1src sha1dst status src dst); |
545 | my @ret; |
b4b4d0fd |
546 | while(@$diff and $diff->[0] =~ /^:\d+/) { |
1e805b5e |
547 | my $line = shift @$diff; |
ad8884fc |
548 | # see. man git-diff-tree for more info |
549 | # mode src, mode dst, sha1 src, sha1 dst, status, src[, dst] |
b4b4d0fd |
550 | my @vals = $line =~ /^:(\d+) (\d+) ($SHA1RE) ($SHA1RE) ([ACDMRTUX]\d*)\t([^\t]+)(?:\t([^\n]+))?$/; |
ad8884fc |
551 | my %line = zip @keys, @vals; |
552 | # Some convenience keys |
553 | $line{file} = $line{src}; |
554 | $line{sha1} = $line{sha1dst}; |
b4b4d0fd |
555 | $line{is_new} = $line{sha1src} =~ /^0+$/ |
1e805b5e |
556 | if $line{sha1src}; |
557 | @line{qw/status sim/} = $line{status} =~ /(R)(\d+)/ |
b4b4d0fd |
558 | if $line{status} =~ /^R/; |
ad8884fc |
559 | push @ret, \%line; |
560 | } |
561 | |
562 | return @ret; |
563 | } |
564 | |
27e05d7b |
565 | =head2 parse_rev_list |
566 | |
567 | Given the output of the C<rev-list> command return a list of hashes. |
568 | |
569 | =cut |
570 | |
47495599 |
571 | sub parse_rev_list { |
572 | my ($self, $output) = @_; |
47495599 |
573 | |
30e4d46d |
574 | return |
575 | map $self->get_object($_), |
576 | grep $self->valid_rev($_), |
577 | map split(/\n/, $_, 6), split /\0/, $output; |
fbf3eb7e |
578 | } |
579 | |
27e05d7b |
580 | =head2 list_revs |
581 | |
582 | Calls the C<rev-list> command (a low-level from of C<log>) and returns an |
583 | array of hashes. |
584 | |
585 | =cut |
586 | |
fbf3eb7e |
587 | sub list_revs { |
27e05d7b |
588 | my ($self, %args) = @_; |
fbf3eb7e |
589 | |
30e4d46d |
590 | $args{sha1} = $self->head_hash($args{sha1}) |
591 | if !$args{sha1} || $args{sha1} !~ $SHA1RE; |
592 | |
593 | my @search_opts; |
594 | if($args{search}) { |
595 | my $sargs = $args{search}; |
596 | $sargs->{type} = 'grep' |
597 | if $sargs->{type} eq 'commit'; |
598 | @search_opts = ( |
599 | # This seems a little fragile ... |
600 | qq[--$sargs->{type}=$sargs->{text}], |
601 | '--regexp-ignore-case', |
602 | $sargs->{regexp} ? '--extended-regexp' : '--fixed-strings' |
603 | ); |
604 | } |
fbf3eb7e |
605 | |
27e05d7b |
606 | my $output = $self->run_cmd_in($args{project} || $self->project, 'rev-list', |
1feb3d6b |
607 | '--header', |
30e4d46d |
608 | (defined $args{ count } ? "--max-count=$args{count}" : ()), |
609 | (defined $args{ skip } ? "--skip=$args{skip}" : ()), |
610 | @search_opts, |
0ee97fec |
611 | $args{sha1}, |
1feb3d6b |
612 | '--', |
27e05d7b |
613 | ($args{file} ? $args{file} : ()), |
614 | ); |
1feb3d6b |
615 | return unless $output; |
fbf3eb7e |
616 | |
1feb3d6b |
617 | my @revs = $self->parse_rev_list($output); |
fbf3eb7e |
618 | |
790ce598 |
619 | return @revs; |
fbf3eb7e |
620 | } |
621 | |
27e05d7b |
622 | =head2 rev_info |
623 | |
624 | Get a single piece of revision information for a given sha1. |
625 | |
626 | =cut |
627 | |
fbf3eb7e |
628 | sub rev_info { |
27e05d7b |
629 | my($self, $rev, $project) = @_; |
fbf3eb7e |
630 | |
1feb3d6b |
631 | return unless $self->valid_rev($rev); |
c5065c66 |
632 | |
27e05d7b |
633 | return $self->list_revs( |
1e805b5e |
634 | rev => $rev, count => 1, |
635 | ( $project ? (project => $project) : () ) |
27e05d7b |
636 | ); |
1feb3d6b |
637 | } |
638 | |
27e05d7b |
639 | =head2 reflog |
640 | |
641 | Calls the C<reflog> command and returns a list of hashes. |
642 | |
643 | =cut |
644 | |
1feb3d6b |
645 | sub reflog { |
646 | my ($self, @logargs) = @_; |
647 | |
648 | my @entries |
649 | = $self->run_cmd_in($self->project, qw(log -g), @logargs) |
650 | =~ /(^commit.+?(?:(?=^commit)|(?=\z)))/msg; |
651 | |
ad8884fc |
652 | =pod |
1feb3d6b |
653 | commit 02526fc15beddf2c64798a947fecdd8d11bf993d |
654 | Reflog: HEAD@{14} (The Git Server <git@git.dev.venda.com>) |
655 | Reflog message: push |
1ef8dc7d |
656 | Author: Foo Barsby <fbarsby@example.com> |
1feb3d6b |
657 | Date: Thu Sep 17 12:26:05 2009 +0100 |
658 | |
1ef8dc7d |
659 | Merge branch 'abc123' |
1feb3d6b |
660 | =cut |
661 | |
662 | return map { |
663 | |
664 | # XXX Stuff like this makes me want to switch to Git::PurePerl |
665 | my($sha1, $type, $author, $date) |
666 | = m{ |
b3ad9e63 |
667 | ^ commit \s+ ($SHA1RE)$ |
1feb3d6b |
668 | .*? |
669 | Reflog[ ]message: \s+ (.+?)$ \s+ |
670 | Author: \s+ ([^<]+) <.*?$ \s+ |
671 | Date: \s+ (.+?)$ |
27e05d7b |
672 | }xms; |
1feb3d6b |
673 | |
674 | pos($_) = index($_, $date) + length $date; |
675 | |
676 | # Yeah, I just did that. |
677 | |
678 | my($msg) = /\G\s+(\S.*)/sg; |
679 | |
680 | { |
681 | hash => $sha1, |
682 | type => $type, |
683 | author => $author, |
684 | |
685 | # XXX Add DateTime goodness. |
686 | date => $date, |
687 | message => $msg, |
688 | }; |
1ef8dc7d |
689 | } @entries; |
c5065c66 |
690 | } |
691 | |
790ce598 |
692 | =head2 heads |
27e05d7b |
693 | |
694 | Returns an array of hashes representing the heads (aka branches) for the |
695 | given, or current, project. |
696 | |
697 | =cut |
698 | |
790ce598 |
699 | sub heads { |
1feb3d6b |
700 | my ($self, $project) = @_; |
fbf3eb7e |
701 | |
790ce598 |
702 | my @output = $self->command(qw/for-each-ref --sort=-committerdate /, '--format=%(objectname)%00%(refname)%00%(committer)', 'refs/heads'); |
fbf3eb7e |
703 | |
1feb3d6b |
704 | my @ret; |
790ce598 |
705 | for my $line (@output) { |
1feb3d6b |
706 | my ($rev, $head, $commiter) = split /\0/, $line, 3; |
707 | $head =~ s!^refs/heads/!!; |
fbf3eb7e |
708 | |
790ce598 |
709 | push @ret, { sha1 => $rev, name => $head }; |
fbf3eb7e |
710 | |
1feb3d6b |
711 | #FIXME: That isn't the time I'm looking for.. |
2247133f |
712 | if (my ($epoch, $tz) = $line =~ /\s(\d+)\s+([+-]\d+)$/) { |
1feb3d6b |
713 | my $dt = DateTime->from_epoch(epoch => $epoch); |
714 | $dt->set_time_zone($tz); |
715 | $ret[-1]->{last_change} = $dt; |
fbf3eb7e |
716 | } |
1feb3d6b |
717 | } |
fbf3eb7e |
718 | |
790ce598 |
719 | return @ret; |
fbf3eb7e |
720 | } |
721 | |
1ef8dc7d |
722 | =head2 refs_for |
723 | |
27e05d7b |
724 | For a given sha1 check which branches currently point at it. |
1ef8dc7d |
725 | |
726 | =cut |
727 | |
728 | sub refs_for { |
1e805b5e |
729 | my($self, $sha1) = @_; |
1ef8dc7d |
730 | |
1e805b5e |
731 | my $refs = $self->references->{$sha1}; |
1ef8dc7d |
732 | |
1e805b5e |
733 | return $refs ? @$refs : (); |
1ef8dc7d |
734 | } |
735 | |
27e05d7b |
736 | =head2 references |
1ef8dc7d |
737 | |
738 | A wrapper for C<git show-ref --dereference>. Based on gitweb's |
739 | C<git_get_references>. |
740 | |
741 | =cut |
742 | |
743 | sub references { |
1e805b5e |
744 | my($self) = @_; |
1ef8dc7d |
745 | |
1e805b5e |
746 | return $self->{references} |
747 | if $self->{references}; |
1ef8dc7d |
748 | |
1e805b5e |
749 | # 5dc01c595e6c6ec9ccda4f6f69c131c0dd945f8c refs/tags/v2.6.11 |
750 | # c39ae07f393806ccf406ef966e9a15afc43cc36a refs/tags/v2.6.11^{} |
751 | my @reflist = $self->command(qw(show-ref --dereference)) |
752 | or return; |
1ef8dc7d |
753 | |
1e805b5e |
754 | my %refs; |
755 | for(@reflist) { |
756 | push @{$refs{$1}}, $2 |
757 | if m!^($SHA1RE)\srefs/(.*)$!; |
758 | } |
1ef8dc7d |
759 | |
1e805b5e |
760 | return $self->{references} = \%refs; |
1ef8dc7d |
761 | } |
762 | |
fbf3eb7e |
763 | 1; |
764 | |
765 | __PACKAGE__->meta->make_immutable; |