Howto apply a patch/diff file

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

kotssmurf
Traffic Manager
Traffic Manager
Posts: 158
Joined: 27 May 2008 20:33

Re: Howto apply a patch/diff file

Post by kotssmurf »

Hi all!

After spending untill way too late at night yesterday I finally managed to compile Openttd myself.
I'm using MinGW to do this, and even though I got it to work I would like to understand what I'm actually doing so therefor a few questions:

- To apply the patch to the source I started by using this code:

Code: Select all

svn update -r(Version number)
Can anybody explain me exactly what this line of code does?

- Second line to apply the patch to the source:

Code: Select all

patch -p0 -i name.diff
I've tried the help command but can't seem to find what -p0 and -i do as attributes...
Does anybody have the answer? :-)

- Lastly: When compiling you should change directory to source folder and execute './configure' and 'make'
I found a very small file named configure (6kb), what exactly does this point to, or execute?
And I assume the make command created the exe file?

I know it's lots of question at once which are more or less irrelevant, but some help in understanding the process would be very much appreciated!!
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Howto apply a patch/diff file

Post by Yexo »

kotssmurf wrote:

Code: Select all

svn update -r(Version number)
Can anybody explain me exactly what this line of code does?
It updates your local copy of the source code to a specific version. Every time one of the developers makes a commit (=changes something to the official source) the revision number is increased by one. A source code patch is basically a file that lists a number of changes compared to some specific version. That's why you have to get that specific version first before applying a patch. Versions that are close enough might work if the official changes don't conflict with the changes made in the patch, but there is no way to guarantee that.
- Second line to apply the patch to the source:

Code: Select all

patch -p0 -i name.diff
I've tried the help command but can't seem to find what -p0 and -i do as attributes...
-p0 tells the program patch to strip 0 leading directories from each path found in the diff. -i is just an indication that the next argument will be the filename of the diff to apply.
- Lastly: When compiling you should change directory to source folder and execute './configure' and 'make'
I found a very small file named configure (6kb), what exactly does this point to, or execute?
You can open that file in a text editor and see for yourself, it's a shell script.
And I assume the make command created the exe file?
Yes, but indirectly. make is a program that helps in compiling by determining which files have to be rebuild. If after compiling the first time you only change one file and execute make again, it'll only recompile that file to see you a lot of time. The actual compiling is done by a compiler, in your case gnu g++.
Eddi
Tycoon
Tycoon
Posts: 8258
Joined: 17 Jan 2007 00:14

Re: Howto apply a patch/diff file

Post by Eddi »

kotssmurf wrote: - To apply the patch to the source I started by using this code:

Code: Select all

svn update -r(Version number)
Can anybody explain me exactly what this line of code does?
A patch file contains information like "change line XXX from AAA to BBB", when there were too many changes to the code since the patch was created, it might not cleanly apply anymore, so this command makes you get an older version of the code. you might miss some new features of the past few weeks, but the patch should apply correctly.
- Second line to apply the patch to the source:

Code: Select all

patch -p0 -i name.diff
I've tried the help command but can't seem to find what -p0 and -i do as attributes...
Does anybody have the answer? :-)
"-i <name.diff>" means "input file", otherwise patch reads from the console, i.e. you have to type in everything by hand.
"-p 0" means "strip 0 sections from paths", i.e. when you are in the "trunk" directory, and the patch file has paths like "src/blah.cpp", then use "-p 0" (use the whole path), if it has "a/src/blah.cpp" then use "-p 1" (strip the "a/" from path), if you omit "-p" completely, then only the filename is used, without any paths.
using the wrong option here will get you notices like "file blah.cpp not found, enter correct path:"
- Lastly: When compiling you should change directory to source folder and execute './configure' and 'make'
I found a very small file named configure (6kb), what exactly does this point to, or execute?
And I assume the make command created the exe file?
"configure" is a script that detects your operating system, position of some required programs, libraries, etc. and other options, and creates a file called "Makefile" taking these things into account.
"make" takes this "Makefile", which now contains information how to compile all parts of the program, and usually creates the .exe for you
"make" can take parameters like "make install" to copy the program to a previously defined install location (check "./configure --help"), "make run" to execute the program directly after compiling, "make run-gdb" to start a debug session, or many more
User avatar
FooBar
Tycoon
Tycoon
Posts: 6553
Joined: 21 May 2007 11:47
Location: The Netherlands
Contact:

Re: Howto apply a patch/diff file

Post by FooBar »

Darn. I was typing something nice and now Yexo beat me to it :(
I'm not going to throw out a perfectly good reply, but you can ignore it as Yexo said the same :)
Well I'll be darned again, now Eddi has written the same as well.
Still not throwing it out... :P

kotssmurf wrote:svn update -r(Version number)
This changes the souce files to the state of that particular revision number.
So if you have a patch against say r18000, chances are it doesn't apply any more to r21000 because there are too many changes to OpenTTD since the patch was written. The svn update command gives you the OpenTTD source of an older revision so you can build an older version with the patch.
kotssmurf wrote:patch -p0 -i name.diff
If you look inside a diff/patch file, you see the paths and filenames of the files that are changed. Usually, these will be A/path/to/file.ex and B/path/to/file.ext (first for the original version, second for the changes). Because your source doesn't have a folder A or B, you need to strip this from the path. The -p option does that for you and you specify how many folders to strip from the front. -p0 doesn't strip anything; -p1 strips in this case the A/ and B/. -p2 would strip A/path/ and B/path/ etc. It is common that you need to specify -p1.

-i says that the input for the patch program should be taken from the file specified after -i. By default, patch wants input from stdin (e.g. the commandline if you run patch from there), which isn't very useful as that means you have to type the diff file in by hand.
Eddi
Tycoon
Tycoon
Posts: 8258
Joined: 17 Jan 2007 00:14

Re: Howto apply a patch/diff file

Post by Eddi »

FooBar wrote:Darn. I was typing something nice and now Yexo beat me to it :(
I'm not going to throw out a perfectly good reply
that was my thought as well :p
kotssmurf
Traffic Manager
Traffic Manager
Posts: 158
Joined: 27 May 2008 20:33

Re: Howto apply a patch/diff file

Post by kotssmurf »

Wow thank you all for the quick reply full of usefull information! :-)

So if I understand it correctly the -i attribute just executes the code in the patch.
So if I were to start creating a patch and would know the nested code by heart I could just type it in the shell?
(Theoretically, I do not have the intention of ever doing this :-) )

Secondly, if the source version should allways be updated I assume that patches written to be compatible with different source codes will (in most cases) not work together? Then how are patchpacks possible? A complete manual rewrite of the code is inevitable, no?
Eddi
Tycoon
Tycoon
Posts: 8258
Joined: 17 Jan 2007 00:14

Re: Howto apply a patch/diff file

Post by Eddi »

kotssmurf wrote:I could just type it in the shell?
yes, but the typical use case is pipes, to use the output of another program

e.g.

Code: Select all

curl -L <URL> | patch -p0
directly downloads the patch file and applies it. ("-L" here basically means "follow redirects")
Secondly, if the source version should allways be updated I assume that patches written to be compatible with different source codes will (in most cases) not work together? Then how are patchpacks possible? A complete manual rewrite of the code is inevitable, no?
that depends, if only few revisions are inbetween, then there are likely no conflicts, or they can be usually be resolved manually without a lot of effort, if you know what the patch does. a "complete rewrite" is only necessary if the patch is very old, or contradicts another patch (like "grass growth" and "stuck counter" patches are incompatible, because they use the same map bits, one of them would have to be rewritten to use other map bits, if the other one were to be included in trunk. they cannot be in the same patchpack either this way)

nevertheless, keeping a patchpack together is a lot of effort, and persons who manage this surely are to be admired.
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Howto apply a patch/diff file

Post by Yexo »

Heh, now Eddi beat me to it. Same as above, not going to throw it away :)
kotssmurf wrote:So if I were to start creating a patch and would know the nested code by heart I could just type it in the shell?
(Theoretically, I do not have the intention of ever doing this :-) )
Theoretically yes. In practice all patches are made by editing the source code and letting svn figure out what exactly changed between the version in the repository and the version on your harddisk.

[quoteSecondly, if the source version should allways be updated I assume that patches written to be compatible with different source codes will (in most cases) not work together?[/quote]Correct. Even when multiple patches are written for the same version they might conflict because they change the same lines. Imagine patch 1 saying change the AA on line 3 to BB and patch 2 saying change the AA on line 3 to CC. A computer program can't solve that automatically.
Then how are patchpacks possible? A complete manual rewrite of the code is inevitable, no?
It's usually not that much work to update a patch to apply to a different revision. Merging multiple conflicting patches (as explained above) is more work. That requires some programming knowledge and understanding of both patches and the original code.
kotssmurf
Traffic Manager
Traffic Manager
Posts: 158
Joined: 27 May 2008 20:33

Re: Howto apply a patch/diff file

Post by kotssmurf »

Yexo wrote:Correct. Even when multiple patches are written for the same version they might conflict because they change the same lines. Imagine patch 1 saying change the AA on line 3 to BB and patch 2 saying change the AA on line 3 to CC. A computer program can't solve that automatically.
This i figured out already, but say we are talking about 2 patches which are compatible, but one is made with a very old source code and another is new. Can I then execute svn update -r(Very old version) and when this is done execute the same code the update to a newer source for the newer patch? Won't this overwrite the patch already executed?

BTW Thanks for you reply-war answers :-) Very informative,quick and complete!
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Howto apply a patch/diff file

Post by Yexo »

kotssmurf wrote:This i figured out already, but say we are talking about 2 patches which are compatible, but one is made with a very old source code and another is new. Can I then execute svn update -r(Very old version) and when this is done execute the same code the update to a newer source for the newer patch? Won't this overwrite the patch already executed?!
You can try, but if the difference in version is quite big you'll most likely get errors when trying to update from the old version with patch applied to a newer version. These conflicts will be marked in the source code files and will have to be solved by you before compiling or applying another patch.
User avatar
FooBar
Tycoon
Tycoon
Posts: 6553
Joined: 21 May 2007 11:47
Location: The Netherlands
Contact:

Re: Howto apply a patch/diff file

Post by FooBar »

That won't work. you either end up overwriting the first changes with the second svn update. Or if you choose not to update the patched file, you end up with a very old source file amongst a new source that most likely will not work together.

So if you want to apply multiple patches, you must apply them to the same revision. So for a very old patch and a very new one, you almost certainly need to update the old patch for it to work with the new revision.
kotssmurf
Traffic Manager
Traffic Manager
Posts: 158
Joined: 27 May 2008 20:33

Re: Howto apply a patch/diff file

Post by kotssmurf »

Aha so that means that when you perform 'SVN update -r(version number)' it does not just overwrite current source, but verifies the changes?
EDIT: nvm :-) I replied to quick
Eddi
Tycoon
Tycoon
Posts: 8258
Joined: 17 Jan 2007 00:14

Re: Howto apply a patch/diff file

Post by Eddi »

kotssmurf wrote:Aha so that means that when you perform 'SVN update -r(version number)' it does not just overwrite current source, but verifies the changes?
EDIT: nvm :-) I replied to quick
the second "svn update" will try to keep your changes, but it may detect "conflicts" when your patch changed lines that also were changed in one of the updates.

your files will then contain lines like

Code: Select all

<<< mine
aaaa
======
bbbb
>>> r12345
or similar.
User avatar
Dave
Moderator
Moderator
Posts: 17243
Joined: 26 Dec 2005 20:19
Location: North London

Re: Howto apply a patch/diff file

Post by Dave »

I'm following MeusH's method from page 1 using Tortoise. All of the files are shown in red, it says to speak to a patch developer. Can someone enlighten me as to what might be going wrong? Ta.

EDIT: Am trying to patch IS to the trunk.

EDIT2: Waitaminute...

EDIT3: I thought I might have the wrong revision as the IS patch is built for r22944, but this also returns the same behaviour. Am I being a complete dunce here? :\
Attachments
Capture.PNG
Capture.PNG (35.45 KiB) Viewed 665 times
Official TT-Dave Fan Club

Dave's Screenshot Thread! - Albion: A fictional Britain
Flickr


Why be a song when you can be a symphony? r is a...
User avatar
FooBar
Tycoon
Tycoon
Posts: 6553
Joined: 21 May 2007 11:47
Location: The Netherlands
Contact:

Re: Howto apply a patch/diff file

Post by FooBar »

What "patch" tool are you using? Be aware that the one that comes with Tortoise is silly and mustn't be used. Better use the one from MSYS (get it here if you don't have it: http://sourceforge.net/projects/mingw/f ... a/download)

Furthermore it might be good to "update" to a clean r22944, discarding all local changes.
User avatar
Dave
Moderator
Moderator
Posts: 17243
Joined: 26 Dec 2005 20:19
Location: North London

Re: Howto apply a patch/diff file

Post by Dave »

Okay I've got MSYS; do I follow the instructions on the wiki? I've tested the installation and I get outputs as the wiki states. Now do I just go down that list and install the stuff?
Official TT-Dave Fan Club

Dave's Screenshot Thread! - Albion: A fictional Britain
Flickr


Why be a song when you can be a symphony? r is a...
User avatar
Lord Aro
Tycoon
Tycoon
Posts: 2369
Joined: 25 Jun 2009 16:42
Location: Location, Location
Contact:

Re: Howto apply a patch/diff file

Post by Lord Aro »

yes, follow the wiki instructions as closely as possible
AroAI - A really feeble attempt at an AI

It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration. --Edsger Dijkstra
Logital82
Engineer
Engineer
Posts: 58
Joined: 15 Feb 2010 12:03
Location: Germany, Berlin

Re: Howto apply a patch/diff file

Post by Logital82 »

Hallo,

my first try to compile a Version with a diff file. I have token introduction from 4 post of this threas (for windows users)

First: Downloaded TortoiseSVN and installed it:
than: Downloaded source files from here: http://www.openttd.org/en/download-stable
than: extractet sourcefiles to a folder
than: right click on this folder to check out, than don`t know

Is there any step by step introduction for those people without programming experiences?
Yexo
Tycoon
Tycoon
Posts: 3663
Joined: 20 Dec 2007 12:49

Re: Howto apply a patch/diff file

Post by Yexo »

Follow either http://wiki.openttd.org/Compiling_on_MinGW or http://wiki.openttd.org/Microsoft_Visua ... s_Editions to compile the source code you got. I'd strongly recommend trying to compile a clean version of the code (without any patch applied first) so you know that works for you.
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 7 guests