ClassRooms/PatchBasics

From Wiki.ooo4kids.org

Jump to: navigation, search


Patch basics

Back to ClassRooms


Here will be put a lesson about patch tool basics. Any improvement is welcome, and everybody is warmly welcome to participate.

Teacher information

Time 30 minutes (presentation + questions)
Pre-requisites Be able to use the shell Bash. patch is supposed to be installed, and working.
Associated tools diff, svn (e.g. doing svn diff)
Difficulty level TAG DEV LEVEL1.png


Goal

Introduction

In this lesson, we'll discover how work the universal and essential tool (dev point of view) named patch.

How things work

Imagine you downloaded a brand new tree from the repo, and add some changes. To summarize, we can consider the delta (diff between original file and the new modified one), as a text file, containing 3 lines of context before the changes, and 3 lines of context, after the changes. Something modified or removed in the original file will be a line starting with - (minus), and something added, will be a line starting with + (plus).

Apply a patch(fast approach)

Below, some examples, to help the newcomer :

First fake the application

patch -p0 --dry-run <  aPatch.diff 

Then apply it for true

patch -p0 --dry-run <  aPatch.diff 

Reverse a patch

Fake reversing it

patch -p1 -R < anotherPatch.patch

Apply it for true

patch -p1 -R < anotherPatch.patch


Most usefull patch options

==> most usefull reference : man patch


Strip prefix containing num leading slashes in the path

- pnum (e.g. : -p0)

Every hunk contains the path where changes have to be applied. The patch might not match with the current one, and sometimes, the path must be modified.


Taken from man patch :

-pnum  or  --strip=num : 
          Strip the smallest prefix containing num leading slashes  from  each
          file  name found in the patch file.  A sequence of one or more adja-
          cent slashes is counted as a single slash.  This controls  how  file
          names  found  in  the  patch file are treated, in case you keep your
          files in a different directory than the  person  who  sent  out  the
          patch.  For example, supposing the file name in the patch file was

             /u/howard/src/blurfl/blurfl.c

          setting -p0 gives the entire file name unmodified, -p1 gives

             u/howard/src/blurfl/blurfl.c

          without the leading slash, -p4 gives

             blurfl/blurfl.c


Fake the patch application

--dry-run option is the safest way to avoid garbage, when e.g. appling a malformed or simply a wrong patch.

man patch says :

 
      --dry-run
          Print the results of applying the patches without actually  changing
          any files.


Reverse a patch application

Suppose you already applied a patch, and want to exactly reverse it. This action can be achieved, using the -R (for reverse) option.

e.g. : patch -R -p0 < aPatch.diff

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox