| 1 | package HiD::Generator::GitRepositories::GitRepository; | ||
| 2 | # ABSTRACT: HiD GitRepository Class | ||
| 3 | use Moose; | ||
| 4 | use File::Basename; | ||
| 5 | use File::ShareDir ':ALL'; | ||
| 6 | use JSON; | ||
| 7 | use DateTime; | ||
| 8 | use DateTime::TimeZone; | ||
| 9 | use Data::Dumper; | ||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | our $VERSION = "0.3"; | ||
| 14 | |||
| 15 | binmode STDOUT, ':utf8'; | ||
| 16 | |||
| 17 | has 'git_dir' => (is => 'ro', isa=> 'Str', default=>""); | ||
| 18 | |||
| 19 | has 'gitcmd' => (is => 'rw', isa=>'Str', default=>'git'); | ||
| 20 | |||
| 21 | has 'bare' => (is => 'rw', default=>0); | ||
| 22 | |||
| 23 | |||
| 24 | has 'name' => (is => 'rw', isa=>'Str'); | ||
| 25 | |||
| 26 | |||
| 27 | has 'branches' => (is => 'rw', default => sub{return [];}); | ||
| 28 | |||
| 29 | has 'languages' => (is => 'rw', default => sub{return [];}); | ||
| 30 | |||
| 31 | sub isGit | ||
| 32 | { | ||
| 33 | my $self = shift; | ||
| 34 | |||
| 35 | return 0 unless -d $self->git_dir(); | ||
| 36 | |||
| 37 | if (-d $self->git_dir . "/.git") | ||
| 38 | { | ||
| 39 | $self->bare(0); | ||
| 40 | return 1; | ||
| 41 | } | ||
| 42 | |||
| 43 | my $help=readpipe($self->gitcmd() . ' rev-parse --git-dir 1>/dev/null 2> /dev/null;echo $?'); | ||
| 44 | chomp($help); | ||
| 45 | |||
| 46 | if ($help == 0) | ||
| 47 | { | ||
| 48 | $self->bare(1); | ||
| 49 | return 1; | ||
| 50 | } | ||
| 51 | |||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub parse_branches | ||
| 56 | { | ||
| 57 | my $self = shift; | ||
| 58 | |||
| 59 | my $help = readpipe($self->gitcmd() . " show-ref --dereference"); | ||
| 60 | my @lines = split /\n/, $help; | ||
| 61 | |||
| 62 | $self->branches([]); | ||
| 63 | |||
| 64 | for my $line (@lines) | ||
| 65 | { | ||
| 66 | if ($line =~ m!^([0-9a-fA-F]{40})\srefs/(.*)$!) { | ||
| 67 | |||
| 68 | my $branch = { | ||
| 69 | name => $2, | ||
| 70 | ref => $1 | ||
| 71 | }; | ||
| 72 | |||
| 73 | if ($branch->{name} !~ /^tags/ && $branch->{name} !~ /^remote/) | ||
| 74 | { | ||
| 75 | push(@{$self->branches},$branch); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | sub hasBranch | ||
| 83 | { | ||
| 84 | my $self = shift; | ||
| 85 | my $branch= shift; | ||
| 86 | |||
| 87 | for my $b (@{$self->branches}) | ||
| 88 | { | ||
| 89 | if ($b->{name} eq $branch) | ||
| 90 | { | ||
| 91 | return 1; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub getBranchRef | ||
| 99 | { | ||
| 100 | my $self = shift; | ||
| 101 | my $branch = shift; | ||
| 102 | |||
| 103 | for my $b (@{$self->branches}) | ||
| 104 | { | ||
| 105 | if ($b->{name} eq $branch) | ||
| 106 | { | ||
| 107 | return $b->{ref}; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | return; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub getFileRef | ||
| 115 | { | ||
| 116 | my $self = shift; | ||
| 117 | my $branch = shift; | ||
| 118 | my $file = shift; | ||
| 119 | |||
| 120 | my $help = readpipe($self->gitcmd() . " ls-tree " . $branch . " " . $file); | ||
| 121 | chomp($help); | ||
| 122 | if ($help =~ /^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/) | ||
| 123 | { | ||
| 124 | return $3; | ||
| 125 | } | ||
| 126 | |||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub hasMetaData | ||
| 131 | { | ||
| 132 | my $self = shift; | ||
| 133 | |||
| 134 | return 0 unless $self->hasBranch("heads/metadata"); | ||
| 135 | } | ||
| 136 | |||
| 137 | sub tags { | ||
| 138 | my $self = shift; | ||
| 139 | my @tags; | ||
| 140 | |||
| 141 | return [] unless $self->hasMetaData; | ||
| 142 | |||
| 143 | my $ref = $self->getFileRef("metadata","tags"); | ||
| 144 | |||
| 145 | warn("No tags file found in metadata branch (". $self->git_dir .")") unless $ref; | ||
| 146 | return unless $ref; | ||
| 147 | |||
| 148 | my $help = readpipe($self->gitcmd() . " cat-file blob " . $ref); | ||
| 149 | my @lines = split /\n/,$help; | ||
| 150 | |||
| 151 | for my $line (@lines) | ||
| 152 | { | ||
| 153 | push(@tags, $line); | ||
| 154 | } | ||
| 155 | |||
| 156 | return @tags; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub description { | ||
| 160 | my $self = shift; | ||
| 161 | my @tags; | ||
| 162 | |||
| 163 | return [] unless $self->hasMetaData; | ||
| 164 | |||
| 165 | my $ref = $self->getFileRef("metadata","description"); | ||
| 166 | |||
| 167 | warn("No description file found in metadata branch (". $self->git_dir .")") unless $ref; | ||
| 168 | return unless $ref; | ||
| 169 | |||
| 170 | my $help = readpipe($self->gitcmd() . " cat-file blob " . $ref); | ||
| 171 | chomp($help); | ||
| 172 | |||
| 173 | return $help; | ||
| 174 | } | ||
| 175 | |||
| 176 | sub hasLanguage | ||
| 177 | { | ||
| 178 | my $self = shift; | ||
| 179 | my $lang = shift; | ||
| 180 | |||
| 181 | for my $l (@{$self->languages}) | ||
| 182 | { | ||
| 183 | if ($l eq $lang) | ||
| 184 | { | ||
| 185 | return 1; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | return 0; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub parse_languages | ||
| 193 | { | ||
| 194 | my $self = shift; | ||
| 195 | my $shared = dist_dir('HiD-Generator-GitRepositories'); | ||
| 196 | $self->languages([]); | ||
| 197 | |||
| 198 | # only parse programming languages if metadata is available | ||
| 199 | return unless $self->hasMetaData(); | ||
| 200 | |||
| 201 | my $json_text = do { | ||
| 202 | open(my $json_fh, "<:encoding(UTF-8)", $shared."/extensions.json") | ||
| 203 | or die("Can't open \$filename\": $!\n"); | ||
| 204 | local $/; | ||
| 205 | <$json_fh> | ||
| 206 | }; | ||
| 207 | |||
| 208 | my $extensions=from_json($json_text); | ||
| 209 | |||
| 210 | for my $b (@{$self->branches}) | ||
| 211 | { | ||
| 212 | #get all files of current branch | ||
| 213 | my $help = readpipe($self->gitcmd() . " ls-tree " . $b->{name} . " -r" ); | ||
| 214 | my @lines = split /\n/,$help; | ||
| 215 | for my $l (@lines) | ||
| 216 | { | ||
| 217 | if ($l =~ /^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/) | ||
| 218 | { | ||
| 219 | my $filename = fileparse($4); | ||
| 220 | $filename=~/^.*(\..+)$/; | ||
| 221 | my $extension=$1; | ||
| 222 | |||
| 223 | if ($extension=~/\S+/) { | ||
| 224 | for my $ee (@{$extensions}) | ||
| 225 | { | ||
| 226 | if ($ee->{type} eq "programming") { | ||
| 227 | for my $e (@{$ee->{extensions}}) | ||
| 228 | { | ||
| 229 | if ($e eq $extension) | ||
| 230 | { | ||
| 231 | push(@{$self->languages}, $ee->{name}) unless $self->hasLanguage($ee->{name}); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | } | ||
| 241 | |||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | sub lastActivity | ||
| 246 | { | ||
| 247 | my $self = shift; | ||
| 248 | |||
| 249 | my $help = readpipe($self->gitcmd() . " for-each-ref --format=\'%(committer)\' --sort=-committerdate --count=1" ); | ||
| 250 | chomp($help); | ||
| 251 | |||
| 252 | if ( $help=~ / (\d+) [-+][01]\d\d\d$/) | ||
| 253 | { | ||
| 254 | my $dt = DateTime->from_epoch( epoch => $1 ); | ||
| 255 | |||
| 256 | return $dt->month_name . " " . $dt->day . " " . $dt->year; | ||
| 257 | } | ||
| 258 | |||
| 259 | return; | ||
| 260 | } | ||
| 261 | |||
| 262 | |||
| 263 | sub lastChange | ||
| 264 | { | ||
| 265 | my $self = shift; | ||
| 266 | |||
| 267 | my $help = readpipe($self->gitcmd() . " for-each-ref --format=\'%(committer)\' --sort=-committerdate --count=1" ); | ||
| 268 | chomp($help); | ||
| 269 | |||
| 270 | if ( $help=~ / (\d+) ([-+][01]\d\d\d)$/) | ||
| 271 | { | ||
| 272 | my $tz=DateTime::TimeZone->new( name => 'local' ); | ||
| 273 | my $dt = DateTime->from_epoch( epoch => $1 , time_zone=>$tz); | ||
| 274 | |||
| 275 | return $dt; | ||
| 276 | } | ||
| 277 | |||
| 278 | return; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub lastChangeBranch | ||
| 282 | { | ||
| 283 | my $self = shift; | ||
| 284 | my $branch = shift || "heads/master"; | ||
| 285 | |||
| 286 | my $help = readpipe($self->gitcmd() . " for-each-ref --format=\'%(refname) %(committer)\' --sort=-committerdate | grep \"" . $branch ."\""); | ||
| 287 | chomp($help); | ||
| 288 | |||
| 289 | if ( $help=~ / (\d+) [-+][01]\d\d\d$/) | ||
| 290 | { | ||
| 291 | my $dt = DateTime->from_epoch( epoch => $1 ); | ||
| 292 | |||
| 293 | return $dt; | ||
| 294 | } | ||
| 295 | |||
| 296 | return; | ||
| 297 | } | ||
| 298 | |||
| 299 | sub getFileLog | ||
| 300 | { | ||
| 301 | my $self = shift; | ||
| 302 | my $branch = shift || "master"; | ||
| 303 | my $file = shift || "."; | ||
| 304 | my $nr = shift || 20; | ||
| 305 | my $skip = shift || 0; | ||
| 306 | |||
| 307 | my @log; | ||
| 308 | my $help = readpipe(" LC_ALL=C " . $self->gitcmd() . " log --follow --date=raw --max-count=" . $nr . " --skip=" .$skip . " " . $branch . " -- " .$file); | ||
| 309 | my @lines = split /\n/,$help; | ||
| 310 | |||
| 311 | my %entry; | ||
| 312 | my $start_comment=0; | ||
| 313 | |||
| 314 | for my $line (@lines) | ||
| 315 | { | ||
| 316 | if ($line =~ /^commit\s([a-fA-F0-9]+)/) | ||
| 317 | { | ||
| 318 | my $commit = $1; | ||
| 319 | if (defined($entry{commit})) | ||
| 320 | { | ||
| 321 | push(@log, \%entry); | ||
| 322 | } | ||
| 323 | |||
| 324 | %entry=(); | ||
| 325 | $entry{commit}=$commit; | ||
| 326 | } | ||
| 327 | |||
| 328 | if ($line =~ /^Author:\s(.*)\s<(.*)>$/) | ||
| 329 | { | ||
| 330 | $entry{author}->{name}=$1; | ||
| 331 | $entry{author}->{email}=$2; | ||
| 332 | } | ||
| 333 | |||
| 334 | if ($line =~/^Date:\s+(\d+)/) | ||
| 335 | { | ||
| 336 | $entry{date}=$1; | ||
| 337 | } | ||
| 338 | |||
| 339 | if ($line=~/^\s*$/ && $start_comment==0) | ||
| 340 | { | ||
| 341 | $start_comment=1; | ||
| 342 | } | ||
| 343 | elsif ($line=~/^\s*$/ && $start_comment==1) | ||
| 344 | { | ||
| 345 | $start_comment=0; | ||
| 346 | } | ||
| 347 | elsif ($start_comment == 1) | ||
| 348 | { | ||
| 349 | $entry{comment} .= $line; | ||
| 350 | } | ||
| 351 | |||
| 352 | } | ||
| 353 | push(@log,\%entry); | ||
| 354 | |||
| 355 | return @log; | ||
| 356 | } | ||
| 357 | |||
| 358 | sub getTree | ||
| 359 | { | ||
| 360 | my $self = shift; | ||
| 361 | my $branch = shift || "master"; | ||
| 362 | my $root = shift; | ||
| 363 | my @tree; | ||
| 364 | |||
| 365 | my $help = readpipe($self->gitcmd() . " ls-tree " . $branch . " " . $root); | ||
| 366 | |||
| 367 | my @lines = split /\n/,$help; | ||
| 368 | |||
| 369 | for my $l (@lines) | ||
| 370 | { | ||
| 371 | $l =~ /^([0-9]+) (.+) ([0-9a-fA-F]{40})\t(.+)$/; | ||
| 372 | my $path = $4; | ||
| 373 | my $type = $2; | ||
| 374 | my $name = fileparse($path); | ||
| 375 | my @log = $self->getFileLog($branch, $path, 1, 0); | ||
| 376 | |||
| 377 | my $entry={ | ||
| 378 | type => $type, | ||
| 379 | ref => $3, | ||
| 380 | path => $path, | ||
| 381 | name => $name, | ||
| 382 | mode => $1, | ||
| 383 | commit => $log[0] | ||
| 384 | }; | ||
| 385 | push(@tree, $entry); | ||
| 386 | } | ||
| 387 | |||
| 388 | return @tree; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub getBlob | ||
| 392 | { | ||
| 393 | my $self = shift; | ||
| 394 | my $ref = shift; | ||
| 395 | |||
| 396 | my $help = readpipe($self->gitcmd() . " cat-file blob " . $ref); | ||
| 397 | |||
| 398 | return $help; | ||
| 399 | } | ||
| 400 | |||
| 401 | # subroutine to parse information from the git repository after calling new | ||
| 402 | sub BUILD { | ||
| 403 | my $self = shift; | ||
| 404 | |||
| 405 | die("directory parameter missing (" .$self->git_dir . " )") unless length($self->git_dir())>0; | ||
| 406 | die("directory does not exist (".$self->git_dir . " )") unless -d $self->git_dir(); | ||
| 407 | die("directory is not a git repository (" .$self->git_dir . " )") unless $self->isGit(); | ||
| 408 | |||
| 409 | #update gitcmd attribute | ||
| 410 | $self->gitcmd($self->gitcmd . ' --git-dir='.$self->git_dir()); | ||
| 411 | if ($self->bare()==0) | ||
| 412 | { | ||
| 413 | $self->gitcmd($self->gitcmd . "/.git"); | ||
| 414 | } | ||
| 415 | |||
| 416 | # set the repository name | ||
| 417 | $self->name(fileparse($self->git_dir)); | ||
| 418 | |||
| 419 | # parse all available branches | ||
| 420 | $self->parse_branches(); | ||
| 421 | |||
| 422 | }; | ||
| 423 | |||
| 424 | sub generate { | ||
| 425 | my( $self , $site ) = @_; | ||
| 426 | |||
| 427 | |||
| 428 | } | ||
| 429 | |||
| 430 | |||
| 431 | |||
| 432 | 1; | ||
| 433 | |||
| 434 | __END__ |