git.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. use Encode;
  2. use JSON;
  3. use POE;
  4. my $dolink = sub { BotPlugin::call('templink', 'make', shift); };
  5. my %repo_providers = (
  6. github => {
  7. shortlog => "https://github.com/{key}/commits/{ref}",
  8. commit => "https://github.com/{key}/commit/{ref}",
  9. blob => "https://github.com/{key}/blob/{ref}/{path}",
  10. tree => "https://github.com/{key}/tree/{ref}/{path}",
  11. main => "https://github.com/{key}",
  12. },
  13. gitlab => {
  14. shortlog => "https://gitlab.com/{key}/commits/{ref}",
  15. commit => "https://gitlab.com/{key}/commit/{ref}",
  16. blob => "https://gitlab.com/{key}/blob/{ref}/{path}",
  17. tree => "https://gitlab.com/{key}/tree/{ref}/{path}",
  18. main => "https://gitlab.com/{key}",
  19. },
  20. bitbucket => {
  21. # Bitbucket doesn't have a nice generic shortlog by base
  22. # commit, show commit instead
  23. shortlog => "https://bitbucket.org/{key}/commits/{ref}",
  24. commit => "https://bitbucket.org/{key}/commits/{ref}",
  25. blob => "https://bitbucket.org/{key}/src/{ref}/{path}",
  26. tree => "https://bitbucket.org/{key}/src/{ref}/{path}",
  27. main => "https://bitbucket.org/{key}",
  28. },
  29. kernel => {
  30. shortlog => "https://git.kernel.org/cgit/{key}.git/log/?h={ref}",
  31. commit => "https://git.kernel.org/cgit/{key}.git/commit/?id={ref}",
  32. blob => "https://git.kernel.org/cgit/{key}.git/tree/{path}?h={ref}",
  33. tree => "https://git.kernel.org/cgit/{key}.git/tree/{path}?h={ref}",
  34. main => "https://git.kernel.org/cgit/{key}.git",
  35. },
  36. repo => {
  37. shortlog => "http://repo.or.cz/w/{key}/shortlog/{ref}",
  38. commit => "http://repo.or.cz/w/{key}/commit/{ref}",
  39. blob => "http://repo.or.cz/w/{key}/blob/{ref}:/{path}",
  40. tree => "http://repo.or.cz/w/{key}/tree/{ref}:/{path}",
  41. main => "http://repo.or.cz/w/{key}",
  42. },
  43. notabug => {
  44. shortlog => "https://notabug.org/{key}/commits/{ref}",
  45. commit => "https://notabug.org/{key}/commit/{ref}",
  46. blob => "https://notabug.org/{key}/src/{ref}/{path}",
  47. tree => "https://notabug.org/{key}/src/{ref}/{path}",
  48. main => "https://notabug.org/{key}",
  49. },
  50. );
  51. {
  52. irc_on_raw_anymsg => sub {
  53. my $ev = $_[ARG0];
  54. BotIrc::check_ctx(wisdom_auto_redirect => 1) or return 1;
  55. my $text = $ev->{params}[1];
  56. $text =~ s/git::(\S+)/git:github:git\/git:$1/g;
  57. $text =~ s/git\[(\S+)\s+(\S+?)\]\s*(\S+)/git:$1:$2:$3/g;
  58. while ($text =~ /git:(\S+)/ig) {
  59. my $query = $1;
  60. $query =~ s/^"(.+?)"$/$1/;
  61. my ($type, $key, $spec) = split(/:/, $query, 3);
  62. next unless exists $repo_providers{$type};
  63. my $prov = $repo_providers{$type};
  64. my $ref = 'HEAD';
  65. my $path;
  66. my $commit;
  67. if ($spec =~ s/^([^:]*)://) {
  68. $ref = $1 unless $1 eq '';
  69. } elsif (defined $spec) {
  70. $ref = $spec;
  71. $path = []; # sticky undef
  72. } else {
  73. $ref = [];
  74. $path = [];
  75. }
  76. unless (ref $ref) {
  77. $commit = 1 if $ref =~ /^[0-9a-fA-F]{4,}$/;
  78. $commit = 1 if $ref =~ s/\^\{commit\}$|\^c$//;
  79. $commit = 0 if $ref =~ s/\^\{log\}$|\^l$//;
  80. }
  81. $path = $spec unless ref $path;
  82. undef $path if ref $path;
  83. my $mode = defined $path ? 'blob' : 'shortlog';
  84. $mode = 'tree' if defined $path && $path =~ m#(?:^|/)$#;
  85. $mode = 'main' if ref $ref;
  86. $mode = 'commit' if $mode eq 'shortlog' and $commit;
  87. my $frag;
  88. ($path, $frag) = split(/#/, ($path||''), 2);
  89. $frag ||= '';
  90. $frag = "#$frag" if $frag;
  91. my $url = $repo_providers{$type}{$mode};
  92. $url =~ s/\{key\}/$key/;
  93. $url =~ s/\{ref\}/$ref/;
  94. $url =~ s/\{path\}/$path/;
  95. BotIrc::send_wisdom("Git web link: $url$frag");
  96. }
  97. return 0;
  98. },
  99. };