summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/util/salvage-credits.pl
blob: ab0f4fe6cce86cefd6a552397ee4f95fa1b8643d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#! /usr/bin/perl

use strict;
use warnings;
use Encode;

binmode STDOUT, ':utf8';

print "Fetching all historical contributors for CREDITS\n";
my @shas = qx/git log --pretty=oneline --follow CREDITS.txt/;
s/^(\w+).*/$1/ for @shas;

my $contributors = '';

fetch_contributors($_) for @shas;

print "Going back to master\n";
system "git checkout master" and die "git checkout master failed\n";

open my $outf, '>>', 'CREDITS.txt' or die "Can't write CREDITS.txt\n";
binmode $outf, ':utf8';
print $outf "\n$contributors\n";
close $outf;

print "Contributors appended to CREDITS.txt!\n";

sub fetch_contributors {
  my $sha = shift;
  print "Visiting $sha credits.\n";
  system "git checkout $sha" and die "git checkout $sha failed\n";
  $contributors .= credits_names();
}

sub find_credits {
  my @tests = grep(-f, ('CREDITS.txt', 'CREDITS'));
  die "Cannot find CREDITS file\n" unless @tests;
  return $tests[0];
}

sub decodeText {
  my $bytes = shift;
  eval {
    $bytes = decode('utf-8', $bytes, 1);
    return $bytes;
  };
  eval {
    $bytes = decode('iso-8859-1', $bytes, 1);
    return $bytes;
  };
  $bytes
}

sub credits_names {
  my $file = find_credits();
  open my $inf, '<', $file or die "Can't read $file: $!\n";
  binmode $inf;
  my @lines = <$inf>;
  close $inf;
  my $res = '';
  my $incontributors;
  for (@lines) {
    chomp;
    $_ = decodeText($_);
    $_ .= "\n";
    $res .= $_ if $incontributors && /\S/;
    $incontributors = 1 if !$incontributors && /contributed.*:\s*$/;
  }
  $res
}