应用多个补丁以获得累积的更改

时间:2013-09-18 09:43:30

标签: merge diff patch diff3

我有一个基本源文件base.hs,我使用它来创建同一程序的不同扩展版本(foo.hsbar.hsbaz.hs)。现在我想创建每个修改版本的补丁文件,但补丁应该累积以获得包含所有扩展的程序。

base.hs

-- @@BEGIN_IMPORTS@@
-- @@END_IMPORTS@@

main = do
    -- @@BEGIN_EXTENSIONS@@
    -- @@END_EXTENSIONS@@
    putStrLn "Hello World"

foo.hs(注意它基本上是同一个文件)

-- @@BEGIN_IMPORTS@@
import Foo
-- @@END_IMPORTS@@

main = do
    -- @@BEGIN_EXTENSIONS@@
    putStrLn foo
    -- @@END_EXTENSIONS@@
    putStrLn "Hello World"

bar.hs

-- @@BEGIN_IMPORTS@@
import Bar
-- @@END_IMPORTS@@

main = do
    -- @@BEGIN_EXTENSIONS@@
    putStrLn bar
    -- @@END_EXTENSIONS@@
    putStrLn "Hello World"

baz.hs

-- @@BEGIN_IMPORTS@@
import Baz
-- @@END_IMPORTS@@

main = do
    -- @@BEGIN_EXTENSIONS@@
    putStrLn baz
    -- @@END_EXTENSIONS@@
    putStrLn "Hello World"

=>

extended.hs

-- @@BEGIN_IMPORTS@@
import Foo
import Bar
import Baz
-- @@END_IMPORTS@@

main = do
    -- @@BEGIN_EXTENSIONS@@
    putStrLn foo
    putStrLn bar
    putStrLn baz
    -- @@END_EXTENSIONS@@
    putStrLn "Hello World"

差异+补丁?

我知道diffpatch实用程序,但问题是如果我应用多个补丁它们互相取消(因此我会得到baz.hs)。

不工作

diff -c base.hs foo.hs > foo.hs.c3
diff -c base.hs bar.hs > bar.hs.c3
diff -c base.hs baz.hs > baz.hs.c3
patch -o base_foo.hs base.hs foo.hs.c3
patch -o base_foo_bar.hs base_foo.hs bar.hs.c3
patch -o base_foo_bar_baz.hs base_foo_bar.hs baz.hs.c3

combinediff?

我也知道combinediff但这只有在补丁按顺序应用时才有效,因此我需要从base_foo.hs生成差异。

的diff3?

Diff + patch - Sum instead of replace是指diff3实用程序(patchutils包),但我似乎没有得到所需的行为,而且我不知道这对多个有用补丁。

不工作

touch null
diff3 foo.hs.c3 null bar.hs.c3 > foo_bar.hs.c3
patch -o base_foo_bar.hs base.hs foo_bar.hs.c3

(这里的想法是组合空文件中的差异)

不工作

diff3 -m foo.hs.c3 base.hs bar.hs.c3 > base_foo_bar.hs

(显然它产生了一个包含所有三个版本的大冲突文件)

杂项?

我也想过使用SCM的合并工具,比如git,但显然它们只能在提交时使用,但我的文件不在版本控制中。

我目前的解决方案是通过perl -pi -e插入代码段,但这是相当费力和错误的。

1 个答案:

答案 0 :(得分:0)

# diffs can be created beforehand

diff base.hs foo.hs > foo.hs.diff
diff base.hs bar.hs > bar.hs.diff
diff base.hs baz.hs > baz.hs.diff

# *creating a mybase.hs here which is slightly different (like "Hello Mars")*

# the follow steps need to be done for every run

patch -o myfoo.hs mybase.hs foo.hs.diff
patch -o mybar.hs mybase.hs bar.hs.diff 
patch -o mybaz.hs mybase.hs baz.hs.diff

# we create a temp file here because it seems that merge-file and the stream operator '>'
# operate on the same file immediately
cp mybase.hs mybase_union.hs
git merge-file -p --union myfoo.hs mybase.hs mybase_union.hs         > mybase_union_foo.hs
git merge-file -p --union mybar.hs mybase.hs mybase_union_foo.hs     > mybase_union_foo_bar.hs
git merge-file -p --union mybaz.hs mybase.hs mybase_union_foo_bar.hs > mybase_union_foo_bar_baz.hs
cp mybase_union_foo_bar_baz.hs mybase_extended.hs

谢谢armel!