LoginSignup
6
8

More than 5 years have passed since last update.

CVSコミットログに文字コードが混在するCVSをGitリポジトリに変換する

Last updated at Posted at 2014-04-03

概要

過去を捨ててCVSからチェックアウトした状態で、1st commitしてもいいんだけどせっかくなら履歴ごと移行したい。

古いプロジェクトでCVSに歴史がありコミットログそのものがぐちゃぐちゃの文字コードでコミットされてる場合、文字コードを修正しつつGitに変換する。

流れ
1. git cvsimport install
2. CPANで必要なモジュールをインストール(Unicode::Japanese ,Term::ReadKey)
3. cvsimport scriptを修正する
4. git cvsimportする

ある程度自動判別でうまくいくんだけど自動判別できないメッセージが出てきた。
そこで判定できなかった場合プロンプト出して最後は手動で文字コードを選択する対話式のpatch当てた。

git-cvsimportの修正

ここにあるファイルいじった

/usr/libexec/git-core/git-cvsimport

最初のuseに追加

use Unicode::Japanese; #文字コード自動変換
use Term::ReadKey;     #対話式プロンプトのために入れる

自分で作ったfunction

my $utf = Unicode::Japanese->new();
my $oldmsg = '';

ReadMode('cbreak');

sub show_message($){
  print $utf->get;
}

sub set_enc($){
  while(1){
    print "Enter encoding(UTF8(u) / SJIS(s) / EUC-JP(e)):";
    my $enc = ReadKey 0;
    last unless defined $enc;

    print "\n";
    if($enc eq 'u'){
      $utf->set($_[0] , 'utf8');
      return;
    }
    elsif($enc eq 'e'){
      $utf->set($_[0] , 'euc-jp');
      return;
    }
    elsif($enc eq 's'){
      $utf->set($_[0] , 'sjis');
      return;
    }
    else{
      print "error encoding code.\n";
    }
  }

}

sub enc_prompt($){
  while(1){
    print "commit message:";
    show_message($_[0]);

    print "message ok?(y/n):";
    my $ope = ReadKey 0;
    last unless defined $ope;
    print "\n";
    if($ope eq 'y'){
      return;
    }
    set_enc($_[0]);
  }
}

sub commit 修正

#(中略)
    # loose detection of merges
    # based on the commit msg
    foreach my $rx (@mergerx) {
        next unless $logmsg =~ $rx && $1;
        my $mparent = $1 eq 'HEAD' ? $opt_o : $1;
        if (my $sha1 = get_headref("$remote/$mparent")) {
            push @commit_args, '-p', "$remote/$mparent";
            print "Merge parent branch: $mparent\n" if $opt_v;
        }
    }

    #ここから文字コード判定追加コード
    #文字コード判定追加コード
    my $noenc = $logmsg;
    $utf->set($logmsg,"auto");

    if($noenc ne $logmsg){
      enc_prompt($logmsg);
    }
    $logmsg = $utf->get;
    #ここまで追加したソース

    #(略)

git cvsimportはすげぇ時間が掛かるからcommit数の制限かけて何回かに分けてやるのが無難
分割するオプションは忘れた。

参考資料

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8