문제

I have a base sourcefile base.hs which I use create different extended versions of the same program (foo.hs, bar.hs, baz.hs). Now I want to create a patch file of every modified version but the patches should accumulate to get a program which includes all extensions.

base.hs

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

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

foo.hs (note it's basically the same file)

-- @@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"

diff + patch?

I'm aware of the diff and patch utilities but the problem is if I apply multiple patches they cancel each other out (hence I'd get baz.hs).

NOT WORKING

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?

I'm also aware of combinediff but this only works if the patches have been applied in sequential order, hence I'd need to produce the diff from base_foo.hs.

diff3?

Diff + patch - Sum instead of replace refers to the diff3 utility (patchutils package) but I don't seem to get the required behaviour working and besides I don't know how this would help with multiple patches.

NOT WORKING

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

(the idea here is to combine the diffs from an empty file)

NOT WORKING

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

(apparently it produces a big conflict file included all three versions)

misc?

I also had the idea of using the merge tools of SCMs like git but apparently they can only be used on commits but my files are not in version control.

My current solution is inserting the code snippets via perl -pi -e but it's rather laborous and errorprone.

도움이 되었습니까?

해결책

# 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

Thx armel!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top