Which means distributing a modified program as patches?

Viewed 25

I've read articles from the GNU project about open-source and other licenses. Some licenses allow you to post your changes as patches, not complete source code (for example Q Public License or gnuplot license). What does it mean? What do such patches look like? Can you get an example? I do not want to focus on the legal situation, but on the programming - general definition, etc.

1 Answers

A patch is the result of a diff between a reference source tree (typically the original version of a project) and your modified version.

It's typically obtained with the POSIX diff command. But nowadays, with the ubiquitous use of versioning tools like SVN, Mercurial or git, the patch is generally generated with these tools (svn diff, hg diff, git diff, etc.)

On the receiver end, the patch is typically applied with the POSIX patch command.

Here is an example. Let's suppose you are working on a project (the orig_version directory below). Before making any change, you wisely create a copy of it (the my_version directory below) and modify a file from the copy:

cp -a orig_version my_version

# Change "This is the original version"
# ... to "This is my modified version"
$ vi my_version/README.txt

Let's see the differences:

$ diff -ruN orig_version my_version
diff -ruN orig_version/README.txt my_version/README.txt
--- orig_version/README.txt 2021-02-16 10:53:05.303423169 +0100
+++ my_version/README.txt   2021-02-16 10:53:00.243495007 +0100
@@ -1 +1 @@
-This is the original version
+This is my modified version

# Rredirect this output so as to
# create a patch file we can send
$ diff -ruN orig_version my_version > /tmp/my.patch

The output above shows the difference between the two directory trees. We have put that difference in a file that we can later send (e.g. by mail) to someone else who will then be able to take a look at the changes and apply them with patch:

# First we create a copy of our project
$ cp -a orig_version test_version

$ cd test_version

$ cat README.txt 
This is the original version

# Then we apply the patch
$ patch -p1 < /tmp/my.patch
patching file README.txt

# Let's see the result
$ cat README.txt 
This is my modified version

Note: since we are in the test_version directory, we used the -p1 option so that patch ignores the first element of the file paths found in the output of diff (--- orig_version/README.txt becomes --- README.txt, same for the +++ line).

25 years ago, people used to send and receive patches by mail and apply them like I just shown. Today, with the advent of distributed version control systems and of websites like Bitbucket and Github, people clone repositories, use their tool to pull the last changes, to get a diff, to push their changes and they send pull requests instead of sending patches by mail.

Related