[Linux](EN) How to make patch file and apply it

Make patch file and apply it.


Environment and Prerequisite

  • Linux base system
  • Bash shell(/bin/bash)
  • patch, diff and git command


patch, diff and git command

patch command

patch -pNUM < patchfile
  • patch: Apply a patch file to an project or file.
  • Patch file can be made by using diff or git diff.


diff command

diff [OPTION]... FILES
  • diff: Compare files or projects line by line
  • Detail example is in below.


git diff command

git diff
  • git diff: Show current git project’s diff status.
  • You can make patch file using git diff command.


Make patch file using diff

Make patch file between two files

Usage

diff -urN [old file] [patched file]

Example

# Make diff
diff -urN test-old.txt test-new.txt

# Make patch file
diff -urN test-old.txt test-new.txt > patch.patch


Make patch file between two project directories

Usage

diff -urN [old directory] [patched directory]

Example

# Make diff
diff -urN neutron neutron_patched

# Make diff file
diff -urN neutron neutron_patched > neutron_patch.patch


Make patch file using git diff

  • Directly use git command
git diff > [patch file name]


Apply patch file

Usage

patch -pNUM < patchfile
  • In project or file directory that you want to apply patch, use above command.
  • NUM means that how many root directories you are going to remove in patch file. For example, in patch file there is file patch like a/neutron/service.py. If we use -p1 option, then patch will be applied file path neutron/service.py. If we use -p2 option, then patch will be applied to file path service.py.


Example

  • Apply patch in neutron project directory
  • neutron_patch.patch file must exist just out of neutron project
# In neutron project
cd neutron

# Apply patch file
# Option -p1 could be different depends on your patch file
patch -p1 < ../neutron_patch.patch


Usage Example

0. Process

  • Clone neutron project and edit it for making patch file. Make patch file from git diff.
  • Revert all diffs in neutron project and apply patch file. Check patch is applied.

1. Clone neutron project and modify it

git clone https://github.com/openstack/neutron.git
cd neutron

vi [any file in this project directory]

2. Make patch file

  • Make patch file freely.
git diff > ../neutron_patch.patch

neutron_patch.patch

diff --git a/neutron/service.py b/neutron/service.py
index 06bf4cd..67f93fe 100644
--- a/neutron/service.py
+++ b/neutron/service.py
@@ -1,4 +1,4 @@
-# Copyright 2011 VMware, Inc
+#aidfjaldfja;ifdjsaoe# Copyright 2011 VMware, Inc
 # All Rights Reserved.
 #
 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
diff --git a/neutron/worker.py b/neutron/worker.py
index 81e7110..a330fe0 100644
--- a/neutron/worker.py
+++ b/neutron/worker.py
@@ -9,7 +9,7 @@
 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 #    License for the specific language governing permissions and limitations
 #    under the License.
-
+#akdfjakl;dfja;kfld
 from neutron_lib import worker
 from oslo_config import cfg
 from oslo_service import loopingcall

2. Apply patch file

  • Revert all diffs and run below command to apply patch file
patch -p1 < ../neutron_patch.patch


Reference