プログラマーのメモ書き

伊勢在住のプログラマーが気になることを気ままにメモったブログです

svn -> git への移行

最近本格的にgitを使い始めたところ、予想以上に快適でした。いままでは、ほとんど一人で開発しているし、gitの必要あるかな?と思ってたけど大間違いでしたね。ということで、徐々にリポジトリをgitに切り替えていこうと思います。

といっても、既存の複雑なリポジトリはすぐには手が出せないので、まずは、作業を開始したばかりのtrunkだけしかない単純なリポジトリをgitに移行してみたいと思います。

 

なお、gitのリポジトリですが、無料でプライベートリポジトリが作れるBitbucketを使います(便利ですね、これ)。あと、ローカル環境は、ubuntu 14.04 LTS を使っています。SVN のリポジトリは自前のサーバーで建てたものです。

 

あと、下記記事内のメールアドレスの部分は、@の前にスペースを一文字入れています。joomlaのメールアドレス自動暗号化(email cloakingというらしい)が働くんだけど、<pre>タグの関係からかうまく表示できないためです。

 

手順

svn->gitの移行手順なんですが、Googleで調べればいろいろと出てきます。今回はBitbucket さんがいろいろと解説してくれてますので、それに従ってやってみました。

まあ、リンク先の元Webページを読んでもらえればそれでいいんですが、自分用に一応まとめてみます。

 

(1)移行処理スクリプトのダウンロード

Bitbucket さんが簡単に移行するためのツールをまとめてくれています。なので、まずはこれをダウンロードしておきます。

このスクリプトの詳しい解説が必要な方は、概要ページに載っていますので、そちらをご覧ください。

 

(2)必要なものが揃ってるか確認

次のコマンドを実行して、必要なものがインストール済みであるか確認します。

mor@ubuntu:~$ java -jar ~/svn-migration-scripts.jar verify
svn-migration-scripts: using version 0.1.56bbc7f
Git: ERROR: Unable to determine version.
Subversion: ERROR: Unable to determine version.
mor@ubuntu:~$ 

で、今回は上記のようにエラーが表示されたので、必要なものをインストールします(svnもgitもEclipseから使っていたので入っていなかったようです)。

mor@ubuntu:~$ sudo apt-get update
mor@ubuntu:~$ sudo apt-get install subversion
mor@ubuntu:~$ sudo apt-get install git

 もう一度確認します。

mor@ubuntu:~$ java -jar ~/svn-migration-scripts.jar verify
svn-migration-scripts: using version 0.1.56bbc7f
Git: using version 1.9.1
Subversion: using version 1.8.8
git: 'svn' is not a git command. See 'git --help'.

Did you mean one of these?
    fsck
    mv
    show
git-svn: ERROR: Unable to determine version.
mor@ubuntu:~$ 

 git-svnもなかったようなので、インストールします。

mor@ubuntu:~$ sudo apt-get install git-svn

 また、確認。

mor@ubuntu:~$ java -jar ~/svn-migration-scripts.jar verify
svn-migration-scripts: using version 0.1.56bbc7f
Git: using version 1.9.1
Subversion: using version 1.8.8
git-svn: using version 1.9.1
mor@ubuntu:~$ 

 これで大丈夫なようです。

 

(3)オーサー情報の抽出

svnへのコミットで使われるオーサー(author)情報は、ユーザー名だけらしいですが、gitはフルネームとメールアドレスを使うそうです。なので、これの対応を用意しておく必要があります。

まず、移行作業用の作業ディレクトリを作成して、移動します。

mor@ubuntu:~$ mkdir GitMigration
mor@ubuntu:~$ cd GitMigration

 下記コマンドを実行します。

mor@ubuntu:~/GitMigration$ java -jar ~/svn-migration-scripts.jar authors https://development.mori-soft.com/svn/sample_repo username password > authors.txt
About to create the authors file.
mor@ubuntu:~/GitMigration$ 

ここでは、svnリポジトリに認証が必要なので、ユーザー名とパスワードを追加してコマンドを実行しています。

 

なお、もともとの記事では、

mor@ubuntu:~/GitMigration$ java -jar ~/svn-migration-scripts.jar authors https://development.mori-soft.com/svn/sample_repo > authors.txt

 のように実行とあったのですが、これだとsvnで認証に失敗するため、

About to create the authors file.
svn: E215004: Authentication failed and interactive prompting is disabled; see the --force-interactive option
svn: E215004: Unable to connect to a repository at URL 'https://development.mori-soft.com/svn/sample_repo'
svn: E215004: No more credentials or we tried too many times.
Authentication failed
Could not communicate with Subversion: check the URL of the repository, the username and the password are all valid
mor@ubuntu:~/GitMigration$ 

のようなエラーが表示されてしまいます。

ユーザー名とパスワードの指定方法は、前述の概要ページか、

mor@ubuntu:~/GitMigration$ java -jar ~/svn-migration-scripts.jar --help authors
authors
<URL> [<username> [<password>]]

Generates an initial authors mapping for the committers to a Subversion
repository.

Each line of the author mapping associates a Subversion commiter with their
full name and e-mail address. For example:

jane.doe = Jane Doe <jane.d @example.org>
mor@ubuntu:~/GitMigration$ 

とヘルプを実行すれば、表示されます。

 

無事にauthors.txtを抽出できれば、OKです。こんな感じになりました。

mor@ubuntu:~/GitMigration$ cat authors.txt 
daemon = daemon <daemon @mycompany.com>
mor = mor <mor @mycompany.com>
mor@ubuntu:~/GitMigration$ 

(Bitbucketの移行説明では、ここでユーザー名をフルネームで書き換えてください、とありますが、このままでも特に問題なかったです。)

 

(4)svnリポジトリのクローン

さて、いよいよリモートのsvnリポジトリをローカルのgitリポジトリにクローンします。

今回対象としてはリポジトリは、標準的なtrunk/branches/tagsの構成である上に、開発を始めたばかりなのでtrunkにしかコミットがなく、branches/tagsは空っぽです。複雑なリポジトリの移行だと、かなり面倒なようで、先が思いやられます。

 

次のコマンドを実行します。

mor@ubuntu:~/GitMigration$ git svn clone --stdlayout --authors-file=authors.txt https://development.mori-soft.com/svn/sample_repo sample_repo

パスワードを聞かれるので入力すると、リポジトリのクローンが始まります。

r185 = abeb918c2f6fc1c00cf43dc31be7ef1c6e9f2514 (refs/remotes/trunk)
Checked out HEAD:
  https://development.mori-soft.com/svn/sample_repo/trunk r185
mor@ubuntu:~/GitMigration$ 

 こんな感じでクローンが終われば完了です。

 

(5)gitリポジトリのクリーンアップ

今回のリポジトリでは、trunkのみなので、意味はないようですが、Bitbucketの移行記事にあるように、一応gitリポジトリのクリーンアップをしておきます。

mor@ubuntu:~/GitMigration$ cd sample_repo/
mor@ubuntu:~/GitMigration/sample_repo$ java -Dfile.encoding=utf-8 -jar ~/svn-migration-scripts.jar clean-git --force
# Creating annotated tags...
# Creating local branches...
# Checking for obsolete tags...
No obsolete tags to remove.
# Checking for obsolete branches...
No obsolete branches to remove.
# Cleaning tag names
# Cleaning branch names
mor@ubuntu:~/GitMigration/sample_repo$ 

 

(6)Bitbucket側にリポジトリを作成

詳しい手順は省略しますが、Bitbucket側にリポジトリを作成しておきます。

 

(7)Bitbucketのリポジトリをリモートに登録

下記コマンドでOKです。

mor@ubuntu:~/GitMigration/sample_repo$ git remote add origin https://username @bitbucket.org/username/sample_repo.git

 リモートが登録されていることが確認できます。

mor@ubuntu:~/GitMigration/sample_repo$ git remote
origin
mor@ubuntu:~/GitMigration/sample_repo$ git remote -v
origin  https://username @bitbucket.org/username/sample_repo.git (fetch)
origin  https://username @bitbucket.org/username/sample_repo.git (push)
mor@ubuntu:~/GitMigration/sample_repo$ 

 (8)Bitbucketへプッシュ

これで、ローカルリポジトリをBitbucketへプッシュできるようになりました。

mor@ubuntu:~/GitMigration/sample_repo$ git push -u origin --all
Password for 'https://username @bitbucket.org': 
Counting objects: 2430, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (696/696), done.
Writing objects: 100% (2430/2430), 868.06 KiB | 0 bytes/s, done.
Total 2430 (delta 1494), reused 2430 (delta 1494)
To https://username @bitbucket.org/username/sample_repo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
mor@ubuntu:~/GitMigration/sample_repo$ 

 プッシュが終われば、Bitbucket上でもリポジトリが確認できるようになります。

もともとのsvnリポジトリの構成が簡単だったので、手順どおりにすれば意外と簡単にできました。

 

その後

実は、リポジトリの移行そのものは簡単に終わったのですが、移行したプロジェクトを改めて、Bitbucketからクローンしてeclipseで動かそうとしたら、ちょっとはまってしまいました。

というのは、今回移行したリポジトリは、Play Framework で書いていたやつなんですが、自分ひとりで使うからと思って、Eclipseの.projectやら.classpathやらもリポジトリに登録していました。

で、Bitbucketから移行後のリポジトリをローカルにクローンしてきて、再度eclipseにインポートしたら、ライブラリパスが変わったため、うまく認識されなくなってしまいました。移行前のプロジェクトの場所からgitリポジトリ(とワーキングディレクトリ)の場所が、変わってしまったことが原因でした。

結局、Play Framework のプロジェクトディレクトリ(gitのワーキングディレクトリ)へ移動してコンソールから、

eclipse with-source=true

 として、再度eclipse用のプロジェクトファイルを生成してから、インポートすれば問題なく認識されました。

あと始末

上記のことがあったので、今後のことを考えて、登録済みのプロジェクト関連のファイルはgitリポジトリから消します。

まずは、.gitignoreを適切に編集しておきます。

次に、消し方は、こちらの記事にあるように、

git rm -r --cached .
git add .

を実行後、コミットすればOKです。

 

個人用だからといって、いい加減にしていると痛い目に会いますね。

更新履歴

2017/11/8 Bitbucket のSVN -> Git の説明ページへのリンク先が切れていたので、英文のチュートリアルのURLに更新

2018/7/5 証明書の期限切れがあった場合の対応方法を下記にまとめた blog.mori-soft.com