Software patching


"Software patching" process in general means updating software parts (not as a whole). Sometimes you are not able to directly modify software. Or you want to distribute your changes to other people (but only changes, not whole code). For example to install your change somebody needs to modify directly his own version of module (which can be already modified for other reasons). Or you just need to provide update to very large software package and it would be waste on time for clients to download whole package when just few bytes where modified.

In these situations you can use patching. There are several methods for this to be done.

Fully manual method (based on phpBB)

It's a method that you can use to any source codes (C++, PHP, etc.) can be done manually or automatically. I present it here because it's universal and can be used for various types of software changes.

Your patch package can include several files or just one. Name of patch file is not important. Can be meaningful for users though. Each file consists of list of instructions to be performed for successful patching.

These instructions are:

#-----[ OPEN ]------------------------------------------

Means: open file "hello.h".

#-----[ FIND ]------------------------------------------
# around line 244
// limit of words to be counted
int wordLimit = aWordLimit;

Means: find line described in instruction

#-----[ BEFORE, ADD]--------------------------------------
assert(aWordLimit < 10);

Means: insert following code before code block / line you found

#-----[ AFTER ADD]------------------------------------------
if (wordLimit == 0) 
  return 0;

Means: insert following code after code block / line you found

#-----[ REPLACE WITH ]------------------------------------------
// limit of words to be counted
int wordLimit = (aWordLimit < 0)?0:aWordLimit;

Means: replace found code with the one provided below instruction

#-----[ COPY ]------------------------------------------
copy mail_config.h to common/include/mail_config.h
copy mail.cpp to plugins/mail/mail.cpp

Means: copy files from patch directory to your main software directory (or it's sub-directories).

#-----[ DIY INSTRUCTIONS ]------------------------------------------
rename folder "backup" to "backup_old"

Means: perform non-standard actions described in instruction.

#-----[ SQL ]------------------------------------------
CREATE TABLE `mail_config` (
  `param_name` varchar(100) NOT NULL default '',
  `param_value` varchar(255) NOT NULL default '',
  PRIMARY KEY  (`param_name`)

Means: perform SQL statement on database related to the software being patched.

There are also versions of these commands for finding & patching part of code inside line (with prefix "IN-LINE") - see link below.


diff/patch method

This method is used for various Open Source packages for a long time. It's automatic method. There is a lot of information on this topic on internet, but most important commands are:

# generates .patch file which can be distributed
diff -ruN /path-to/base/file.c my-version/file.c > file.c.patch

# apply patch to file in current directory
patch -p5 < file.c.patch


GIT method

Git as many other version control systems can automatically prepare a patch for you basing on source code you have stored in git database. See resources below for details:


SVN method

CVS method


Binary patches

When you need to patch binary files:

Other tools

  • WinMerge - file compare tool with patch generation support (Tools -> Generate patch)

See also