#! /usr/local/bin/perl
#---------------------------------------------------------------------
# $Id: git-fix-svn-tags.pl 2774 2014-05-17 00:44:48Z cjm $
# Copyright 2010 Christopher J. Madsen
#
# This program is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either the
# GNU General Public License or the Artistic License for more details.
#
# Turn git-svn tags into real tags
#---------------------------------------------------------------------

use strict;
use warnings;
use 5.008;

use Git ();

# git svn clone REPO/ -{Ttrunk,ttags,bbranches}/project -A ~/.gitauthors --localtime --no-metadata

my $tagsRef = 'refs/remotes/tags';

my $git = Git->repository('.');

my @tags = $git->command('for-each-ref',
                         '--format=%(objectname) %(refname)',
                         '--sort=authordate',
                         $tagsRef);

foreach my $tagInfo (@tags) {
  $tagInfo =~ m!^(\S+) \Q$tagsRef\E/(.+)! or die;
  my $sha = $1;
  my $tag = $2;

  print "$sha $tag\n";

  my ($parent, $subject, @message);

  ($parent,
   $ENV{GIT_AUTHOR_NAME},
   $ENV{GIT_AUTHOR_EMAIL},
   $ENV{GIT_AUTHOR_DATE},
   $ENV{GIT_COMMITTER_NAME},
   $ENV{GIT_COMMITTER_EMAIL},
   $ENV{GIT_COMMITTER_DATE},
   $subject, @message) = $git->command(
     show => $sha,
     '--format=format:%P%n%an%n%ae%n%ai%n%cn%n%ce%n%ci%n%s%n%b'
  );

  my $message = join("\n", @message);
  if ($message) {
    $message = "$subject\n\n$message\n";
  } else {
    $message = $subject;
  }

  $git->command(qw(tag -a -m), $message, $tag, $parent);
} # end foreach $tagInfo in @tags

print <<'END MSG';
You should now be able to remove the .git/refs/remotes directory, but
you should probably double-check to make sure the tags were created
properly first.
END MSG
