私も興味があります。答えはわかりませんが…
機能する複雑なシステムは、常に機能する単純なシステムから進化したことがわかっています。
git のマージは非常に洗練されており、理解するのが非常に難しいと思いますが、これにアプローチする 1 つの方法は、その前身から、懸念の核心に焦点を当てることです。つまり、共通の祖先を持たない 2 つのファイルが与えられた場合、git merge はどのようにそれらをマージする方法を見つけ、競合はどこにあるのでしょうか?
いくつかの前駆体を見つけようとしましょう。からgit help merge-file
:
git merge-file is designed to be a minimal clone of RCS merge; that is,
it implements all of RCS merge's functionality which is needed by
git(1).
ウィキペディアから: http://en.wikipedia.org/wiki/Git_%28software%29 -> http://en.wikipedia.org/wiki/Three-way_merge#Three-way_merge -> http://en.wikipedia .org/wiki/Diff3 -> http://www.cis.upenn.edu/~bcpierce/papers/diff3-short.pdf
最後のリンクは、diff3
アルゴリズムを詳細に説明した論文の pdf です。これはGoogle pdf-viewerバージョンです。長さはわずか 12 ページで、アルゴリズムはわずか数ページですが、完全な数学的処理です。少し堅苦しすぎるように思えるかもしれませんが、git のマージを理解したい場合は、まず単純なバージョンを理解する必要があります。まだ確認していませんが、 のような名前の場合diff3
、おそらく diff (最長共通部分列アルゴリズムを使用する) も理解する必要があります。diff3
ただし、 Google をお持ちの場合は、より直感的な説明があるかもしれません...
ここで、 と を比較する実験diff3
を行いgit merge-file
ました。彼らは同じ 3 つの入力ファイルversion1 oldversion version2を取り、競合を同じ方法でマークし<<<<<<< version1
、 , =======
, >>>>>>> version2
(diff3
また||||||| oldversion
) を付けて、共通の遺産を示します。
oldversionには空のファイルを使用し、version1とversion2にはほぼ同一のファイルを使用し、 version2に 1 行だけ追加しました。
結果:git merge-file
単一の変更された行が競合として識別されました。しかしdiff3
、2 つのファイル全体を競合として扱いました。したがって、diff3 が洗練されているのと同様に、git のマージは、この最も単純なケースであっても、さらに洗練されています。
これが実際の結果です(テキストには@twalbergの回答を使用しました)。必要なオプションに注意してください (それぞれのマンページを参照してください)。
$ git merge-file -p fun1.txt fun0.txt fun2.txt
You might be best off looking for a description of a 3-way merge algorithm. A
high-level description would go something like this:
Find a suitable merge base B - a version of the file that is an ancestor of
both of the new versions (X and Y), and usually the most recent such base
(although there are cases where it will have to go back further, which is one
of the features of gits default recursive merge) Perform diffs of X with B and
Y with B. Walk through the change blocks identified in the two diffs. If both
sides introduce the same change in the same spot, accept either one; if one
introduces a change and the other leaves that region alone, introduce the
change in the final; if both introduce changes in a spot, but they don't match,
mark a conflict to be resolved manually.
<<<<<<< fun1.txt
=======
THIS IS A BIT DIFFERENT
>>>>>>> fun2.txt
The full algorithm deals with this in a lot more detail, and even has some
documentation (/usr/share/doc/git-doc/technical/trivial-merge.txt for one,
along with the git help XXX pages, where XXX is one of merge-base, merge-file,
merge, merge-one-file and possibly a few others). If that's not deep enough,
there's always source code...
$ diff3 -m fun1.txt fun0.txt fun2.txt
<<<<<<< fun1.txt
You might be best off looking for a description of a 3-way merge algorithm. A
high-level description would go something like this:
Find a suitable merge base B - a version of the file that is an ancestor of
both of the new versions (X and Y), and usually the most recent such base
(although there are cases where it will have to go back further, which is one
of the features of gits default recursive merge) Perform diffs of X with B and
Y with B. Walk through the change blocks identified in the two diffs. If both
sides introduce the same change in the same spot, accept either one; if one
introduces a change and the other leaves that region alone, introduce the
change in the final; if both introduce changes in a spot, but they don't match,
mark a conflict to be resolved manually.
The full algorithm deals with this in a lot more detail, and even has some
documentation (/usr/share/doc/git-doc/technical/trivial-merge.txt for one,
along with the git help XXX pages, where XXX is one of merge-base, merge-file,
merge, merge-one-file and possibly a few others). If that's not deep enough,
there's always source code...
||||||| fun0.txt
=======
You might be best off looking for a description of a 3-way merge algorithm. A
high-level description would go something like this:
Find a suitable merge base B - a version of the file that is an ancestor of
both of the new versions (X and Y), and usually the most recent such base
(although there are cases where it will have to go back further, which is one
of the features of gits default recursive merge) Perform diffs of X with B and
Y with B. Walk through the change blocks identified in the two diffs. If both
sides introduce the same change in the same spot, accept either one; if one
introduces a change and the other leaves that region alone, introduce the
change in the final; if both introduce changes in a spot, but they don't match,
mark a conflict to be resolved manually.
THIS IS A BIT DIFFERENT
The full algorithm deals with this in a lot more detail, and even has some
documentation (/usr/share/doc/git-doc/technical/trivial-merge.txt for one,
along with the git help XXX pages, where XXX is one of merge-base, merge-file,
merge, merge-one-file and possibly a few others). If that's not deep enough,
there's always source code...
>>>>>>> fun2.txt
あなたが本当にこれに興味があるなら、それはちょっとうさぎの穴です. 私には、正規表現、diff の最長共通部分列アルゴリズム、文脈自由文法、または関係代数と同じくらい深いように思えます。真相を究明したいのなら、できると思いますが、それには断固たる研究が必要です。