Name

svn patch — Apply changes represented in a unidiff patch to the working copy.

Synopsis

svn patch PATCHFILE [WCPATH]

Description

This subcommand will apply changes described a unidiff-formatted patch file PATCHFILE to the working copy WCPATH. As with most other working copy subcommands, if WCPATH is omitted, the changes are applied to the current working directory. A unidiff patch suitable for application to a working copy can be produced with the svn diff command or third-party differencing tools. Any non-unidiff content found in the patch file is ignored.

Changes listed in the patch file will either be applied or rejected. If a change does not match at its exact line offset, it may be applied earlier or later in the file if a match is found elsewhere for the surrounding lines of context provided by the patch. A change may also be applied with fuzz—meaning, one or more lines of context are ignored when attempting to match the change location. If no matching context can be found for a change, the change conflicts and will be written to a reject file which bears the extension .svnpatch.rej.

svn patch reports a status line for patched file or directory using letter codes, very similar to the way that svn update provides notification. The letter codes have the following meanings:

A

Added

D

Deleted

C

Conflicted

G

Merged

U

Updated

Changes applied with an offset or fuzz are reported on lines starting with the '>' symbol. You should review such changes carefully.

If the patch removes all content from a file, that file is automatically scheduled for deletion. Likewise, if the patch creates a new file, that file is automatically scheduled for addition. Use svn revert to undo undesired deletions and additions.

Examples

Apply a simple patch file generated by the svn diff command. Our patch file will create a new file, delete another file, and modify a third's contents and properties. Here's the patch file itself (which we'll assume is creatively named PATCH):

Index: deleted-file
===================================================================
--- deleted-file	(revision 3)
+++ deleted-file	(working copy)
@@ -1 +0,0 @@
-This file will be deleted.
Index: changed-file
===================================================================
--- changed-file	(revision 4)
+++ changed-file	(working copy)
@@ -1,6 +1,6 @@
 The letters in a line of text
 Could make your day much better.
 But expanded into paragraphs,
-I'd tell of kangaroos and calves
+I'd tell of monkeys and giraffes
 Until you were all smiles and laughs
 From my letter made of letters.

Property changes on: changed-file
___________________________________________________________________
Added: propname
## -0,0 +1 ##
+propvalue
Index: added-file
===================================================================
--- added-file	(revision 0)
+++ added-file	(working copy)
@@ -0,0 +1 @@
+This is an added file.

We can apply the previous patch file to another working copy from our repository using svn patch, and verify that it did the right thing by using svn diff:

$ cd /some/other/workingcopy
$ svn patch /path/to/PATCH
D         deleted-file
UU        changed-file
A         added-file
$ svn diff
Index: deleted-file
===================================================================
--- deleted-file	(revision 3)
+++ deleted-file	(working copy)
@@ -1 +0,0 @@
-This file will be deleted.
Index: changed-file
===================================================================
--- changed-file	(revision 4)
+++ changed-file	(working copy)
@@ -1,6 +1,6 @@
 The letters in a line of text
 Could make your day much better.
 But expanded into paragraphs,
-I'd tell of kangaroos and calves
+I'd tell of monkeys and giraffes
 Until you were all smiles and laughs
 From my letter made of letters.

Property changes on: changed-file
___________________________________________________________________
Added: propname
## -0,0 +1 ##
+propvalue
Index: added-file
===================================================================
--- added-file	(revision 0)
+++ added-file	(working copy)
@@ -0,0 +1 @@
+This is an added file.
$

Sometimes you might need Subversion to interpret a patch in reverse—where added things get treated as removed things, and vice-versa. Use the --reverse-diff option for this purpose. In the following example, we'll squirrel away a patch file which describes the changes in our working copy, and then use a reverse patch operation to undo those changes.

$ svn status
M       foo.c
$ svn diff > PATCH
$ cat PATCH
Index: foo.c
===================================================================
--- foo.c	(revision 128)
+++ foo.c	(working copy)
@@ -1003,7 +1003,7 @@
     return ERROR_ON_THE_G_STRING;
 
   /* Do something in a loop. */
-  for (i = 0; i < txns->nelts; i++)
+  for (i = 0; i < txns->nelts; i--)
     {
       status = do_something(i);
       if (status)
$ svn patch --reverse-diff PATCH
U         foo.c
$ svn status
$