Hot answers tagged

60

Let's try it. Here's a trivial C program: #include <stdio.h> int main(int argc, char **argv) { puts("/usr/tmp"); } We'll build that into test: $ cc -o test test.c If we run it, it prints "/usr/tmp". Let's find out where "/usr/tmp" is in the binary: $ strings -t d test | grep /usr/tmp 1460 /usr/tmp -t d prints the offset in decimal into ...


30

The purpose is to save lots of traffic. The Linux tarball is around 75MB, whereas the patches usually just have a few KB. So if you compile your own kernel, and update to each new minor version the day it is released, instead of redownloading a new 75MB tarball for each minor update, you just download (for example) the main tarball for a given version ...


30

Diffs can be more complicated than just comparing one file versus another. The can compare entire directory hierarchies. Consider the example that I want fix a bug in GCC. My change adds a line or two in 4 or 5 files and deletes a handful of lines in those and other files. If I want to communicate these changes to someone, potentially for inclusion into ...


20

When you get a patch you can often (that is unless you have made changes to the exact same lines) apply the patch to a set of files that you have changed yourself as well. The patch has information about the old and the new state of the files. If you get a copied file you don't know what the original was (the old state) and you cannot apply the differences ...


15

Patches are usually contained in .diff files, because the patches are created using the diff command. A patch is a series of insertions and deletions into source code. For this reason, in order to use the patch, you must build the application (e.g., "foobar") from source after applying the patch. So, in steps: 1. Get the source package for foobar. ...


13

Looking at the source code of GNU patch, this behavior is built in since version 2.7. As of GNU patch 2.7.1, only relative paths not containing .. are accepted, unless the current directory is the root directory. To apply a patch containing absolute paths, you can use (cd / && sudo patch -p0) <foo.patch


12

The most common way to create a patch is to run the diff command or some version control's built-in diff-like command. Sometimes, you're just comparing two files, and you run diff like this: diff -u version_by_alice.txt version_by_bob.txt >alice_to_bob.patch Then you get a patch that contains changes for one file and doesn't contain a file name at all. ...


12

If you are using diff, you can see what exactly has changed, so using diff/patch is a way to prevent someone from slipping unwanted changes in the file.


11

The changes made to the files are usually much smaller than the files being changed. This means storing a diff can save you a lot of space. When diff was created, disk space was expensive. But it also means you can reapply a diff to a file even when that file has changed in other ways. The patch utility will do that for you and tell you when there are ...


10

From the man: -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 adjacent 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 ...


9

Don't you want the other way around? diff -Nub . /current-files | patch -b


9

Make assumes that an exit code of 0 means success, anything else means failure. This is the standard convention used by almost all command-line tools. Unfortunately, diff is not one of those. Checking the GNU diff info page, and also the Single Unix Specification "diff" entry, 0 means no differences found, 1 means differences found, and ≥2 means error. You ...


8

patch creates new file, that's why it holds effective user credentials. A workaround: use patch -o to have temporary file created, then simply cat tmp file to original file.


8

To patch a file means to modify it, with the connotation that the modification is generally small. The usage comes from the general English usage where a patch is a small modification (to a piece of cloth, for example). When it comes to files, a patch is not always a repair. A patch is a series of instructions that describe how to modify a file or a set of ...


8

If you're not giving any option to patch other than -pN, it only creates those files when a patch fails to apply cleanly. So, one option is to stop creating (or accepting) bad patches. :) Back in the real world, this is a feature. When patch(1) fails to apply a patch segment to the original file, it saves the temporary original file copy out durably as ...


8

http://marc.info/ has a link for each message to get the raw body, and https://lkml.org/ has (in the sidebar) links to download any contained diffs. There are also archives with NNTP access that may provide raw messages, though I haven't tried this.


7

You might want to take a look into patchutils [1]. For the vim part, I wrote a small vim plugin that helps with navigating in patches: diff_navigator [2]. [1] http://cyberelk.net/tim/software/patchutils/ [2] http://www.vim.org/scripts/script.php?script_id=2361


7

Just in case it helps someone, if you are using bash script then the example given by Omnifarious would not work. In bash the exit status of a successful command is 0 So the following would work: patch -p0 -N --dry-run --silent < patchfile 2>/dev/null #If the patch has not been applied then the $? which is the exit status #for last command would ...


7

You'll need patchutils installed for this. This script will split one large patch into smaller separate paches, each of them containing only one hunk for one file. You can then apply these patches with patch --forward. #!/bin/sh -eu PATCH=$1 OUTDIR=$2 test -f "$PATCH" && test -d "$OUTDIR" TDIR=$(mktemp -d) trap 'rm -rf $TDIR' 0 INDEX=0 ...


7

diff has an option to ignore whitespace changes (-w), same for patch (-l). In general, it's a bad idea to ignore whitespace though, so you should reserve its use for special cases, when someone's editor did something horrible...


6

I think you are looking for Ksplice. I haven't really followed the technology so I'm not sure how freely available the how-to information is but they certainly have freely available support for some Fedora and Ubuntu versions.


6

This is a general problem with diffs copied/pasted into text file without space indentation. You need to add space in front of each line except for symbols "+", "-" and "@@". To avoid this problem, it is always suggested to generated the diff files before in hand (by diff or version control diff tool) and then download the diff file as whole, instead of ...


6

As Gilles says, you'll need to use a tool that knows about the format of your config files. For your particular example, you can use a short Python script that uses the built-in ConfigParser module. First, let's say your original configuration file on the server is original.cfg: [config] ip=127.0.0.1 port=22 Now put your changes in a new file called ...


6

A very easy solution is to put your files under version control before applying the patch. The version control tools du jour are Git and Mercurial. I personally use and recommend Mercurial. Also, if you want to see what files a patch applies to, you can run diffstat patchname Sample output with Mercurial: apt-get source hello cd hello-2.9 hello-2.9$ hg ...


6

It depends on what you mean by "patch the binary". I change binaries using dd sometimes. Of course there is no such feature in dd, but it can open files, and read and write things at specific offsets, so if you know what to write where, voila there is your patch. For example I had this binary that contained some PNG data. Use binwalk to find the offset, dd ...


6

This is actually a very short diff command. It says to remove the line //#define DEBUG and replace it with #define DEBUG. In this diff format, lines starting with a single - are removed and those with + are added. The other lines are for context, and the @@ tells the offset into the file and number of lines referenced. (And the triple +++ --- lines tell you ...


5

Try rlwrap -c patch -i patch.diff rlwrap tries do add readline functionality to commands that lack it. There exists a couple of alternatives to rlwrap. If you're a zsh user, you may also have a look at Using zsh's line editor to wrap around subprocesses and at /usr/share/zsh/functions/Misc/nslookup for an example of how to add the zsh line editor to a ...


5

The process is clearly defined in the kernel source code. system1:/usr/src/linux/Documentation # ll SubmittingPatches -rw-r--r-- 1 root root 28473 Dec 2 2009 SubmittingPatches They go over every step you have to take. It looks like email is the only way. I would recommend getting another free account or simply mail it from your laptop using nail | mail ...


5

Somewhat off-topic, I guess but I still think it's useful. If you use git to do your development you can easily split your whole changes into smaller "hunks" that embody one feature each. You end up with one commit per feature and can use git's git-format-patch to create (and even sign and properly attribute) patches, I outlined how to do that here


5

To tell patch not to produce backups just omit the -b and any --backup-... options. To instruct it not to create .rej files add -r - option to the command.



Only top voted, non community-wiki answers of a minimum length are eligible