git和append-mostly文件的策略

时间:2012-08-07 07:23:43

标签: git git-merge

我的存储库中有一些底层增长的文件:大多数更改都涉及在文件底部添加新行。这主要是语言和其他属性文件。

作为一种恼人的副作用,每当两个人同时添加内容时,我就会发生合并冲突,并且解析始终涉及手动复制粘贴,以便包含两个版本的行。

是否有一个提示,技巧或方法可以减轻这个过程中的一些痛苦?

例如,一个简单的解决方案是告诉开发人员在文件中间的随机位置添加新行。这可能会奏效,但它涉及到有意识的努力,以及一个奇怪的历史。

3 个答案:

答案 0 :(得分:16)

您可以使用gitattributes机制定义自定义合并驱动程序(例如this one for instance),以便自动复制相关部分。

[merge "aggregate"]
        name = agregate both new sections
        driver = aggregate.sh %O %A %B

这将是一个3向合并,这意味着您可以轻松地%A%B%O(共同祖先)进行区分,以便隔离所述新部分,并将它们聚合在一起在结果合并文件中。

聚合合并驱动程序只需执行:

comm -13 $1 $3 >> $2

(如果你在Windows上,那么comm实用程序是GoW -- Gnu on Windows - 发行版的一部分)


这是一个小小的演示:

首先,让我们设置一个Git仓库,并在两个分支(“master”和“abranch”中修改一个文件:

C:\prog\git\tests>mkdir agg
C:\prog\git\tests>cd agg
C:\prog\git\tests\agg>git init r1
Initialized empty Git repository in C:/prog/git/tests/agg/r1/.git/
C:\prog\git\tests\agg>cd r1

# Who am I?
C:\prog\git\tests\agg\r1>git config user.name VonC
C:\prog\git\tests\agg\r1>git config user.email vonc@xxx

# one file, first commit:
C:\prog\git\tests\agg\r1>echo test > test.txt
C:\prog\git\tests\agg\r1>git add .
C:\prog\git\tests\agg\r1>git commit -m "first commit"
[master c34668d] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

# Let's add one more common line:
C:\prog\git\tests\agg\r1>echo base >> test.txt
C:\prog\git\tests\agg\r1>more test.txt
test
base
C:\prog\git\tests\agg\r1>git add .
C:\prog\git\tests\agg\r1>git commit -m "base"
[master d1cde8d] base
 1 file changed, 1 insertion(+)

现在我们创建一个新的分支,并在该文件的两个版本中进行并发修改,就像OP itsadok在问题中指定的一样。

C:\prog\git\tests\agg\r1>git checkout -b abranch
Switched to a new branch 'abranch'
C:\prog\git\tests\agg\r1>echo "modif from abranch" >> test.txt
C:\prog\git\tests\agg\r1>git add .
C:\prog\git\tests\agg\r1>git commit -m "abranch contrib"
[abranch a4d2632] abranch contrib
 1 file changed, 1 insertion(+)
C:\prog\git\tests\agg\r1>type test.txt
test
base
"modif from abranch"

# back to master
C:\prog\git\tests\agg\r1>git checkout master
Switched to branch 'master'
C:\prog\git\tests\agg\r1>echo "contrib from master" >> test.txt
C:\prog\git\tests\agg\r1>git add .
C:\prog\git\tests\agg\r1>git commit -m "contrib from master"
[master 45bec4d] contrib from master
 1 file changed, 1 insertion(+)
C:\prog\git\tests\agg\r1>type test.txt
test
base
"contrib from master"

我们有两个分支(注:git lg is an alias of mine

C:\prog\git\tests\agg\r1>git lg
* 45bec4d - (HEAD, master) contrib from master (86 minutes ago) VonC
| * a4d2632 - (abranch) abranch contrib (86 minutes ago) VonC
|/
* d1cde8d - base (87 minutes ago) VonC
* c34668d - first commit (89 minutes ago) VonC

现在让我们尝试合并:

C:\prog\git\tests\agg\r1>git merge abranch
Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Automatic merge failed; fix conflicts and then commit the result.

C:\prog\git\tests\agg\r1>more test.txt
test
base
<<<<<<< HEAD
"contrib from master"
=======
"modif from abranch"
>>>>>>> abranch

...宣传失败;) git merge --abort 会重置这种情况。

让我们的合并驱动程序

C:\prog\git\tests\agg\r1>git config merge.aggregate.name "aggregate both new sections"
C:\prog\git\tests\agg\r1>git config merge.aggregate.driver "aggregate.sh %O %A %B"
C:\prog\git\tests\agg\r1>echo test.txt merge=aggregate > .gitattributes

此时,合并仍然失败:

C:\prog\git\tests\agg\r1>git merge abranch
aggregate.sh .merge_file_a09308 .merge_file_b09308 .merge_file_c09308: aggregate.sh: command not found
fatal: Failed to execute internal merge

正常:我们需要编写该脚本,并将其添加到PATH

vim aggregate.sh:
#!/bin/bash

# echo O: $1
# echo A: $2
# echo B: $3

# After http://serverfault.com/q/68684/783
# How can I get diff to show only added and deleted lines?
# On Windows, install GoW (https://github.com/bmatzelle/gow/wiki/)
ob=$(comm -13 $1 $3)
# echo "ob: ${ob}"

echo ${ob} >> $2

----

C:\prog\git\tests\agg\r1>set PATH=%PATH%;C:\prog\git\tests\agg\r1

现在, aggregate合并驱动程序可以运行

C:\prog\git\tests\agg\r1>git merge --no-commit abranch
Auto-merging test.txt
Automatic merge went well; stopped before committing as requested

C:\prog\git\tests\agg\r1>type test.txt
test
base
"contrib from master"
"modif from abranch"

现在开始:来自test.txt的{​​{1}}文件的末尾已添加到abranch上的文件中。

答案 1 :(得分:4)

另一种解决方案是使用管道命令--union的{​​{1}}选项。

git-merge-file

这比使用[merge "aggregate"] name = aggregate both new sections driver = git merge-file --union -L %P %A %O %B 更可靠:当输入行未按comm排序时,comm可能会生成错误的输出。

答案 2 :(得分:1)

如果您不想重新添加已删除的行,则必须使用稍微复杂一点的comm

tmp=$(mktemp)
(comm -12 %A %B ; comm -13 %O %A ; comm -13 %O %B ) >| $tmp
mv $tmp %A

首先,选择本地和远程修订版共有的所有行;这可确保删除所有已删除的行。然后添加远程修订添加的行,然后添加本地修订添加的行。

这可以是使用单个命令在.gitconfig中安装策略:

git config merge.aggregate.driver 'tmp=$(mktemp) ; (comm -12 %A %B ; comm -13 %O %A ; comm -13 %O %B ) >| $tmp ; mv $tmp %A'