• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

How do I create a flexible patch out of the diff of two files?

I'm looking for a way to patch text files in a more flexible manner.
The normal way is to create patch files with diff -u, like here http://stephenjungels.com/jungels.net/articles/diff-patch-ten-minutes.html
My problem is that it is based on line numbers and so I can't use it if I have new version of the file I want to patch.

Let me give you an example to make it clear:
I have a web.xml (see code) where I want to replace the security-constraint block.
I can do a diff between old and new web.xml and if I want replay the change I can apply the patch. But if the there is a new version of a web.xml the patch file can't be used any more, as it is based on line numbers. I could also write a sed script which does the job, but as I have many usecases like this, it would be great to find a solution that is generated out off the diff of the two files.

I hope this was understandable, otherwise ask.

Thanks for your answers in advance.
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">
  <display-name>Hudson</display-name>
   ...  
  <security-role>
    <!-- admins can add/remove/configure projects -->
    <role-name>admin</role-name>
  </security-role>
 
 
<!-- the following -->
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Hudson</web-resource-name>
      <url-pattern>/loginEntry</url-pattern>
    </web-resource-collection>
    <auth-constraint>
      <role-name>*</role-name>
    </auth-constraint>
  </security-constraint>
 
  <login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
      <form-login-page>/login</form-login-page>
      <form-error-page>/loginError</form-error-page>
    </form-login-config>
  </login-config>
 
---------------------------------------
 
   <!-- should be replaced by this -->
   <security-constraint>
        <web-resource-collection>
            <web-resource-name>hudson</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>
    </security-constraint>
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>myapp</realm-name>
    </login-config>
    <security-role>
        <role-name>admin</role-name>
    </security-role>
----------------------------------------------------------------
 
  <!-- if specified, this value is used as the Hudson home directory -->
  <env-entry>
   ...
</web-app>

Open in new window

0
mahome
Asked:
mahome
  • 4
  • 3
  • 2
  • +1
2 Solutions
 
ai_ja_naiCommented:
Since you want to patch only particular zones of the text file, based more on semantics than simple rows differences, you can't rely on a patcher. You need a parser that distinguish contents between tags that has to be saved and automatically replace everything else
0
 
mahomeAuthor Commented:
@ozo
I found no function of patchutils which would help me. What usage were you thinking of?

@ai_ja_nai
Yes that seems like that what I'm looking at. Is such a parser existing, or is it easy to develop?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Hugh FraserConsultantCommented:
Have a look at http://xmlpatch.sourceforge.net/ for an xml patching solution. I believ it does what you've asked. However, you cannot use diff to generate the differences, since the tool requires context information to describe the patch rather than diff's line numbers.
0
 
mahomeAuthor Commented:
@hfraser
great tool for XML, but I have to write the patches on my own.

I know that diff is to simple, but I thought there is a combination similar to diff and patch, which don't look at the line numbers but on the text wich was replaced/deleted/inserted. For this example the diff-like tool should recognize that a text-block was replaced and generate a patch-file, which could for example be a regex for sed, which does the replacement:
sed  's/<old-text-block>/<new-text-block>/'
0
 
ai_ja_naiCommented:
There are a lot of xml parsers for Java. I can recommend you a page where differences are explained (http://www.devx.com/xml/Article/16921)
0
 
mahomeAuthor Commented:
To clarify that xml is just an example, I'm looking for a solution for text-files in common (like postgresq.conf, hosts, server.xml, ...)
0
 
Hugh FraserConsultantCommented:
You might be able to use the context option for diff and patch. Try this:

diff -C 0 file1.txt file2.txt >file.diff
patch -c file3.txt file.diff

I tried this with you test file, adding an extra 20 lines of nonsense before and after the changes in file3.txt. In the context mode, patch will search through the file looking for the context of the line numbers don't match.
0
 
ai_ja_naiCommented:
You have to craft brief java programs for specific tasks ugin the libraries I linked earlier. It's not difficult
0
 
mahomeAuthor Commented:
Thanks diff -c does the job. But I'll keep xmlpath in mind for more complex work.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now