| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Git::Open::Util; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | has remote_url => ( | ||
| 7 | is => 'ro', | ||
| 8 | isa => 'Str', | ||
| 9 | default => sub { | ||
| 10 | my $git_url = `git ls-remote --get-url`; | ||
| 11 | |||
| 12 | $git_url =~ s/\n//; | ||
| 13 | $git_url =~ s/:/\//; # Change : to / | ||
| 14 | $git_url =~ s/^git@/http:\/\//; # Change protocal to http | ||
| 15 | $git_url =~ s/\.git$//; # Remove .git at the end | ||
| 16 | return $git_url; | ||
| 17 | } | ||
| 18 | ); | ||
| 19 | |||
| 20 | has current_branch => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => 'Str', | ||
| 23 | default => sub { | ||
| 24 | my $current_branch = `git symbolic-ref --short HEAD`; | ||
| 25 | return $current_branch; | ||
| 26 | } | ||
| 27 | ); | ||
| 28 | |||
| 29 | has service => ( | ||
| 30 | is => 'ro', | ||
| 31 | lazy => 1, | ||
| 32 | default => sub { | ||
| 33 | my $self = shift; | ||
| 34 | $self->remote_url =~ m|//([a-z]+)\.|; | ||
| 35 | return $1; | ||
| 36 | } | ||
| 37 | ); | ||
| 38 | |||
| 39 | sub generate_url { | ||
| 40 | my $self = shift; | ||
| 41 | my $args = shift; | ||
| 42 | |||
| 43 | my $suffix = ''; | ||
| 44 | if( defined $args->{compare} ) { | ||
| 45 | if( $args->{compare} eq '' ) { | ||
| 46 | $suffix = 'compare'; | ||
| 47 | }else { | ||
| 48 | my @branches = split( /-/, $args->{compare} ); | ||
| 49 | $suffix = $self->_url_pattern( 'compare' ); | ||
| 50 | foreach my $i ( 0..1 ) { | ||
| 51 | $suffix =~ s/_$i/$branches[$i]/; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | }elsif ( defined $args->{branch} ) { | ||
| 55 | my $branch = $args->{branch} || $self->current_branch; | ||
| 56 | $suffix = $self->_url_pattern( 'branch' ); | ||
| 57 | $suffix =~ s/_0/$branch/; | ||
| 58 | } | ||
| 59 | |||
| 60 | return $self->remote_url.'/'.$suffix; | ||
| 61 | }; | ||
| 62 | |||
| 63 | sub _url_pattern { | ||
| 64 | my $self = shift; | ||
| 65 | my $view = shift; | ||
| 66 | my $mapping_pattern = { | ||
| 67 | github => { | ||
| 68 | compare => 'compare/_0..._1', | ||
| 69 | branch => 'tree/_0' | ||
| 70 | }, | ||
| 71 | bitbucket => { | ||
| 72 | compare => 'compare/_0.._1', | ||
| 73 | branch => 'src?at=_0' | ||
| 74 | } | ||
| 75 | }; | ||
| 76 | |||
| 77 | return $mapping_pattern->{$self->service}->{$view}; | ||
| 78 | }; | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | __END__ | ||
| 83 | |||
| 84 | =pod | ||
| 85 | |||
| 86 | =encoding UTF-8 | ||
| 87 | |||
| 88 | =head1 NAME | ||
| 89 | |||
| 90 | Git::Open::Util | ||
| 91 | |||
| 92 | =head1 VERSION | ||
| 93 | |||
| 94 | version 0.1.9 | ||
| 95 | |||
| 96 | =head1 AUTHOR | ||
| 97 | |||
| 98 | Pattarawat Chormai <pat.chormai@gmail.com> | ||
| 99 | |||
| 100 | =head1 COPYRIGHT AND LICENSE | ||
| 101 | |||
| 102 | This software is copyright (c) 2014 by Pattarawat Chormai. | ||
| 103 | |||
| 104 | This is free software; you can redistribute it and/or modify it under | ||
| 105 | the same terms as the Perl 5 programming language system itself. | ||
| 106 | |||
| 107 | =cut |