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