Mercurial チュートリアル hginit.com の和訳 (Fixing Goofs)

Mercurial によって得られる最も大きな恩恵の一つは、プライベートなクローンを使って実験を試してみたり、 新しい機能を開発する事が出来ることだ。もしそれらが失敗したとしても、あなたはすぐにそれを元に戻すことが出来る。

Fixing Goofs

Mercurial は自由に実験させてくれる。通常の編集作業であなたのエディタが面倒な事になり、何か致命的な事が起こったと想像して欲しい。

guac
* 2 ripe avocados
* 1/2 red onion, minced (about 1/2 cup)
* 1-2 habanero chiles, stems and seeds removed, minced
* 2 tablespoons cilantro leaves, finely chopped
* 1 tablespoon of fresh lime or lemon juice
* 1/2 teaspoon coarse salt
* A dash of freshly grated black pepper
* 1/2 ripe tomato, seeds and pulp removed, chopped

Smoosh all ingredients together.
Serve with potato chips.

・・・このように致命的なことになる。

guac
* 2 iperay avocadosway
* 1/2 edray onionway, incedmay (aboutway 1/2 upcay)
* 1-2 abanerohay ileschay, emsstay andway eedssay
emovedray, incedmay
* 2 ablespoonstay ilantrocay eaveslay, inelyfay oppedchay
* 1 ablespoontay ofway eshfray imelay orway emonlay
uicejay * 1/2 easpoontay oarsecay altsay
* Away ashday ofway eshlyfray atedgray ackblay epperpay
* 1/2 iperay omatotay, eedssay andway ulppay emovedray,
oppedchay

Ooshsmay allway ingredientsway ogethertay.
Ervesay ithway otatopay ipschay.

emacs はいいね。どんな時でも何も失われない。こういった事態から回復するための一般的なやり方が hg revert だ。

hg revert

変更したファイルをコミットしたバージョンに戻す

C:\Users\joel\recipes> hg revert guac

これはファイルを最後にコミットした時点まで正確に戻してくれる。Mercurial は何かを削除したがらないので、Pig Latin のレシピをザッピングする代わりにその名前を変更する。

C:\Users\joel\recipes> dir
 Volume in drive C has no label.
 Volume Serial Number is 84BD-9C2C

 Directory of C:\Users\joel\recipes

02/11/2010  11:16 AM    <DIR>          .
02/11/2010  11:16 AM    <DIR>          ..
02/11/2010  11:16 AM    <DIR>          .hg
02/11/2010  11:16 AM               393 guac
02/11/2010  11:15 AM               510 guac.orig
               2 File(s)            903 bytes
               3 Dir(s)  40,958,005,248 bytes free

C:\Users\joel\recipes> del guac

C:\Users\joel\recipes> rename guac.orig guac

もしあなたがここで余計なステップを実行してしまい、実際にコミットしたらどうなってしまうだろうか?

C:\Users\joel\recipes> hg com -m "Pig Latin ftw"

C:\Users\joel\recipes> hg log -l 3
changeset:   5:c7af1973de6d
tag:         tip
user:        Joel Spolsky <joel@joelonsoftware.com>
date:        Thu Feb 11 11:32:27 2010 -0500
summary:     Pig Latin ftw

changeset:   4:0849ca96c304
parent:      2:4ecdb2401ab4
parent:      3:689026657682
user:        Joel Spolsky <joel@joelonsoftware.com>
date:        Mon Feb 08 16:07:23 2010 -0500
summary:     merge

changeset:   3:689026657682
parent:      1:a52881ed530d
user:        Rose Hillman <rose@example.com>
date:        Mon Feb 08 15:29:09 2010 -0500
summary:     spicier kind of chile

hg rollback と呼ばれるコマンドがある。もしあなたが誰か他の人にこの変更を push していないのであれば、あなたを無事に逃がしてくれる。このコマンドはコミットを一度だけ取り消してくれる。

hg rollback

あなたが他人に対して push していない限り、一回のコミットを取り消す。

C:\Users\joel\recipes> hg rollback
rolling back last transaction

C:\Users\joel\recipes> hg log -l 3
changeset:   4:0849ca96c304
tag:         tip
parent:      2:4ecdb2401ab4
parent:      3:689026657682
user:        Joel Spolsky <joel@joelonsoftware.com>
date:        Mon Feb 08 16:07:23 2010 -0500
summary:     merge

changeset:   3:689026657682
parent:      1:a52881ed530d
user:        Rose Hillman <rose@example.com>
date:        Mon Feb 08 15:29:09 2010 -0500
summary:     spicier kind of chile

changeset:   2:4ecdb2401ab4
user:        Joel Spolsky <joel@joelonsoftware.com>
date:        Mon Feb 08 15:32:01 2010 -0500
summary:     potato chips. No one can eat just one.


C:\Users\joel\recipes> hg stat
M guac

C:\Users\joel\recipes> hg revert guac

あなたがひそかに重要な実験をしたいと考えているところを想像して欲しい。あなたの上司が新しいデザイナーのジムを雇ったのだが、彼から最近手に入れた仕様書がただただ馬鹿げた内容だった。そこには蛍光グリーンのテキストがあるが、何もリストアップされておらず("芸術的な理由により")、使い勝手は最悪だった。あなたは休日出勤して全てやり直したいのだが、あのアホなグラフィックデザイナーのアイデアより絶対によいものであると確信できなかったのでコミットするのが怖かった。ジムは基本的に起きた瞬間から寝るまでマリファナを吸っている。あなたは彼を責めたいとは思わないし、彼のデザインがよくなるまでは他の誰にも立ち入って欲しいとも思えなかった、がしかし、限界だった。わかるだろうか?また、彼のデザインはよいものではない。さらに彼はイライラしていた。

Mercurial では、リポジトリ全体の実験的なクローンを作ることが出来る。

C:\Users\joel\recipes> cd ..

C:\Users\joel> hg clone recipes recipes-experiment
updating to branch default
1 files updated, 0 files merged, 0 files removed, 0 files unresolved

これは思ったほど効率の悪い操作ではない。なぜなら recipes と recipes-experiment の両方は全ての履歴を共有しているため(今までのところ)、 Mercurial では"ハードリンク"と呼ばれるファイルシステムの機能を使って非常に高速にコピーを作成するので、余計なディスク容量を占有しない。

今、私たちは実験用ブランチに変更の束を作ることが出来る。

C:\Users\joel> cd recipes-experiment

これは私の素晴らしいグアカマロの実験だ。

guac :

Smoosh all ingredients together.
Serve with potato chips.

・・・このような実験的レシピを作る。

guac :

Smoosh all ingredients together.
Serve with potato chips.

This recipe is really good served with QUESO.

QUESO is Spanish for "cheese," but in Texas, it's just Kraft Slices melted in the microwave with some salsa from a jar. MMM!

この実験用リポジトリに対して、私たちは自由にコミットできる。

C:\Users\joel\recipes-experiment> hg com -m "Queso = Cheese!"

あなたは変更を作って自由に作業をし、好きな時にコミットしてよい。これは、あなたがやろうとする内容が見当違いのものであったとしても、他人に影響を与えること無くソースコードを管理する全ての力をあなたに与えてくれる。

もしあなたがあなたの実験は誤りだったと決断しても、あなたはただその実験用ディレクトリ全体を削除するだけでよい。問題は解決した。消え去ったのだ。

しかし、もしそれが有効だったなら、あなたはあなたの変更を push しなければならない。

C:\Users\joel\recipes-experiment> hg push
pushing to c:\Users\joel\recipes
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files

どこに行った?

hg paths

既知のリモートリポジトリの一覧を表示する。

C:\Users\joel\recipes-experiment> hg paths
default = c:\Users\joel\recipes

"default"に登録されているのは、もしあなたが特に指定していないのであれば、hg pushが変更を push しようとするディレクトリだ。通常、これはクローン元のディレクトリである。この場合はローカルディレクトリだが、URL の場合もある。

C:\Users\joel\recipes-experiment> cd ..\recipes

忘れないで。変更はただリポジトリに対して push されるだけだってことを・・・

C:\Users\joel\recipes> hg log -l 3
changeset:   5:9545248f3fc9
tag:         tip
user:        Joel Spolsky <joel@joelonsoftware.com>
date:        Thu Feb 11 12:59:11 2010 -0500
summary:     Queso = Cheese!

changeset:   4:0849ca96c304
parent:      2:4ecdb2401ab4
parent:      3:689026657682
user:        Joel Spolsky <joel@joelonsoftware.com>
date:        Mon Feb 08 16:07:23 2010 -0500
summary:     merge

changeset:   3:689026657682
parent:      1:a52881ed530d
user:        Rose Hillman <rose@example.com>
date:        Mon Feb 08 15:29:09 2010 -0500
summary:     spicier kind of chile

・・・私たちが、そのバージョンでの作業から外れたわけではない。

C:\Users\joel\recipes> type guac
* 2 ripe avocados
* 1/2 red onion, minced (about 1/2 cup)
* 1-2 habanero chiles, stems and seeds removed, minced
* 2 tablespoons cilantro leaves, finely chopped
* 1 tablespoon of fresh lime or lemon juice
* 1/2 teaspoon coarse salt
* A dash of freshly grated black pepper
* 1/2 ripe tomato, seeds and pulp removed, chopped

Smoosh all ingredients together.
Serve with potato chips.

C:\Users\joel\recipes> hg parent
changeset:   4:0849ca96c304
parent:      2:4ecdb2401ab4
parent:      3:689026657682
user:        Joel Spolsky <joel@joelonsoftware.com>
date:        Mon Feb 08 16:07:23 2010 -0500
summary:     merge

hg parent

あなたが作業している changeset を表示する

わかる? "Queso"の材料が changeset 5 に入っている。だけど私のメインリポジトリは changeset 4 で、これは誰かがリポジトリに新しい変更を push したためで、私の作業中のディレクトリにそれらが現れたわけでは無く、私は依然として changeset 4 で作業している。

私が changeset 5 の何かを見たい場合には、私は hg update コマンドを使わなければならない。

C:\Users\joel\recipes> hg up
1 files updated, 0 files merged, 0 files removed, 0 files unresolved

C:\Users\joel\recipes> hg parent
changeset:   5:9545248f3fc9
tag:         tip
user:        Joel Spolsky <joel@joelonsoftware.com>
date:        Thu Feb 11 12:59:11 2010 -0500
summary:     Queso = Cheese!


C:\Users\joel\recipes> type guac
* 2 ripe avocados
* 1/2 red onion, minced (about 1/2 cup)
* 1-2 habanero chiles, stems and seeds removed, minced
* 2 tablespoons cilantro leaves, finely chopped
* 1 tablespoon of fresh lime or lemon juice
* 1/2 teaspoon coarse salt
* A dash of freshly grated black pepper
* 1/2 ripe tomato, seeds and pulp removed, chopped

Smoosh all ingredients together.
Serve with potato chips.

This recipe is really good served with QUESO.

QUESO is Spanish for "cheese," but in Texas,
it's just Kraft Slices melted in the microwave
with some salsa from a jar. MMM!

何が起こったかわかる? 変更が入ってきたけど、私が作業しているバージョンの上に来るんだ。 push と pull はただ変更をあるリポジトリから他のリポジトリに送るだけで、私がこの瞬間に作業しているファイルには何も影響しない。


そしてこれがリポジトリの状態だ。

Mercurialリポジトリからリポジトリへ変更を移動させることに関して柔軟だ。あなたは実験用リポジトリから中央リポジトリへ直接変更を push 出来る。

C:\Users\joel\recipes> cd ..\recipes-experiment

C:\Users\joel\recipes-experiment> hg outgoing http://joel.example.com:8000/
comparing with http://joel.example.com:8000/
searching for changes
changeset:   5:9545248f3fc9
tag:         tip
user:        Joel Spolsky <joel@joelonsoftware.com>
date:        Thu Feb 11 12:59:11 2010 -0500
summary:     Queso = Cheese!


C:\Users\joel\recipes-experiment> hg push http://joel.example.com:8000/
pushing to http://joel.example.com:8000/
searching for changes
adding changesets
adding manifests
adding file changes
added 1 changesets with 1 changes to 1 files

実験リポジトリから change 5を直接、中央リポジトリに push した。もし私が私のリポジトリに戻っても、そこには私が push するような物は残っていない。

C:\Users\joel\recipes-experiment> cd ..\recipes

C:\Users\joel\recipes> hg out
comparing with http://joel.example.com:8000/
searching for changes
no changes found

これは、Mercurial が中央リポジトリが既にどこからか、この特別な変更を手に入れている事を知っているからだ。これは本当に便利で、そうでないと再び適用しようとして非常に混乱してしまうだろう。

デザイナーのジムにある仕事を依頼すると、彼はすぐに取り掛かるといったが、2ヶ月間姿を現さなかった。人々はほとんど彼と彼に出した仕事の依頼について忘れていて、彼が初めてオフィスに現れて仕事を始めた時には、彼はよく日焼けしていて、正直で、でも彼がどんな奴で何が好きなのかとか誰もよく知らなかった。笑えるよね。彼は普通の奴だった。結局そういったことはわかったが、彼は変わってしまい、誰も彼にどんな事態が起こっていたのかを聞く勇気は無かった。彼の顔の打撲跡と引っかき傷について彼に訪ねる事は決して無い。ただそれだけだ。何があったとしても。私たちはあんな奴が大嫌いなんだ。

あなたは数ヶ月前にミスをしたことに気づくかもしれない。

C:\Users\joel\recipes> hg diff -r 1:2 guac
diff -r a52881ed530d -r 4ecdb2401ab4 guac
--- a/guac      Mon Feb 08 14:51:18 2010 -0500
+++ b/guac      Mon Feb 08 15:32:01 2010 -0500
@@ -8,4 +8,4 @@
 * 1/2 ripe tomato, seeds and pulp removed, chopped

 Smoosh all ingredients together.
-Serve with tortilla chips.
+Serve with potato chips.

ポテトチップス? なんてこった!

Mercurial はあなたのために過去から一つ古い changeset を取り除くことが出来る。 Mercurial は changeset を見ていて、対象を理解し、あなたの作業中のディレクトリに操作をする。それでは古いリビジョン 2 を取り消してみよう。

C:\Users\joel\recipes> hg backout -r 2 --merge
reverting guac
created new head
changeset 6:d828920f7f85 backs out changeset 2:4ecdb2401ab4
merging with changeset 6:d828920f7f85
merging guac
0 files updated, 1 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)

クソ、何が起きた?

C:\Users\joel\recipes> hg diff
diff -r 9545248f3fc9 guac
--- a/guac      Thu Feb 11 12:59:11 2010 -0500
+++ b/guac      Thu Feb 11 14:19:34 2010 -0500
@@ -8,7 +8,7 @@
 * 1/2 ripe tomato, seeds and pulp removed, chopped

 Smoosh all ingredients together.
-Serve with potato chips.
+Serve with tortilla chips.

 This recipe is really good served with QUESO.

C:\Users\joel\recipes> hg com -m "undo thing from the past"

C:\Users\joel\recipes> hg push
pushing to http://joel.example.com:8000/
searching for changes
adding changesets
adding manifests
adding file changes
added 2 changesets with 2 changes to 1 files

今、多くの時間が過ぎた。チップスがレシピから既に取り去られたかもしれない。あらゆる種類の不気味な材料がこの変更に関してマージを不可能にしているかもしれない。この場合にはマージコンフリクトになり、どうにかして解決させなければならないだろう。この事に関しては次のチュートリアルで説明する。

自己診断テスト

このチュートリアルを読んだ後で、あなたが理解しておかなければならないやり方は以下の通りだ。

  1. 事故的な変更を、チェックインの前か後の状態に戻す。
  2. 実験用にローカルにリポジトリのクローンを作る。
  3. リポジトリ間で push する。
  4. リポジトリの履歴にある古い失敗を元に戻す。

次回は、Mercurial でどのようにマージするのかについて話そう。

目次へ

Mercurial チュートリアル hginit.com の和訳 (Contents)

このチュートリアルは Joel Spolsky さんの書いた http://hginit.com の和訳です。
わかりやすくて楽しいチュートリアルを書いてくださった Joel Spolsky さんに感謝します。

Mercurial はモダンなオープンソースの分散バージョン管理システムで、Subversion のような古いシステムから素晴らしい発展をしたものだ。このユーザーフレンドリーな、6章からなるチュートリアルで、Joel Spolsky がキーコンセプトを教えるよ。

目次
Subversion Re-education (Subversion 再教育)
Ground up Mercurial (Mercurial の基礎)
Setting up for a Team (チームのために設定する)
Fixing Goofs (失態に備える)
Merging (マージする)
Repository Architecture (リポジトリ構成)