Training Missions

Missions: Using diff and patch

Diffing individual files

As a popular cookbook author, you often receive requests for modified recipes. You're a generous person, so you're happy to help by providing patches with the changes they asked for.

One enthusiastic reader wrote in with this question:

Dear Cook,

Thanks to your cookbook, I've been delighting everyone with tasty pancakes. People go bananas when I make the nutty pancake recipe!

The one problem is that I'm allergic to almonds, so I can't eat the tasty things. Is there some other way to get the nuttiness that everyone loves?

Nutty as it sounds, the answer is walnuts. Your mission is to:

  • Download the original recipe to your computer.
  • On your own computer, make a copy of the recipe, and open that copy in a text editor.
  • Replace "almond" with "walnut". You should expect to find "almond" twice in the original recipe.
  • Invoke the diff program to summarize the changes you made, creating a "unified diff" (the universally preferred flavor of diff output). Paste that diff into the box below! (See Hints for help.)

Context in open source

When you are submitting changes to a project, you will often need to run diff to create a patch that you can submit. Large patches can be stored in files and attached to whatever medium you use to submit the patch; for short patches, contributors often paste them into email messages or bug reports. So instead of uploading the patch in a file, your mission will be to make your patch and copy and paste it into the box below.

Paste the unified diff here:

If you need help with this step, try clicking through these hints.

The "low" hint level is perfect if you're just confused as to what to do.

The "high" level is great if you are unfamiliar with the commands and tools you'd use.

Read the man page for diff online,

or, if you're on Linux or Mac, use the terminal command:

man diff

In this mission, you have to do a few things. Tips:

  • Make sure to keep a copy of the original around while modifying a copy!
  • Use diff -u to create the diff
  • Copy and paste can be tricky in terminal programs. Click around in the menus of your terminal app until you find the right options. If you have trouble, redirect the output to a file and then open it in your favorite text editor.

Open a command prompt, and type these commands in, one at a time. You need to copy them exactly. Make sure to press enter between each one.

First, create a directory you'll work in and change into it.

	mkdir singlediff
	cd singlediff

Then, download the original file twice. (You'll only modify one.)

	curl -0
	curl -0 -o original

Open nutty-pancake.txt in your favorite text editor, and look for the two occurrences of "almond". Replace them both with "walnut".

Go back to the terminal and run this command.

	diff -u original nutty-pancake.txt

Then select that text with your mouse and use your terminal program's copy-paste functionality to copy it to the clipboard. Paste it into the form above. Then, submit!

If you are a Windows user, redirect the output of your diff to a new file with this command:

      	diff -u original new > output.txt

And then, copy and paste from that file. This will avoid stripping of white spaces, which usually happens when copying from the terminal.