Fossil User Forum

Apply Diff as Patch
Login

Apply Diff as Patch

Apply Diff as Patch

(1.2) By NotSoBright (Skrikerunge) on 2021-08-27 13:28:56 edited from 1.1 [source]

Is there a way to apply changes stored in a diff?

Reason I need this:
Today I accidentally lost several source code files that had uncommitted changes. The previous version of those files were already checked in, so I retrieved them.
By a stroke of luck, I had recently performed a "fossil diff" to the file "myChanges.diff", and I still have a copy of that diff. Thus this question.

BTW: I am new to Fossil, and this is my first post here.

EDIT 1: Sorry, I should have mentioned that I am using Windows 10.

EDIT 2: Thanks for your help so far.
  I should probably have mentioned that my single Fossil generated diff file contains changes to multiple files. It seems to me that this is causing issues for patch.
The given syntax: patch [OPTION]... [ORIGFILE [PATCHFILE]]
So I try:
patch --verbose --dry-run *.py ..\myChanges.diff
/usr/bin/patch: myFirstCodeFile.py: extra operand
/usr/bin/patch: Try '/usr/bin/patch --help' for more information.

Does patch require that there is one diff file per ORIGFILE to modify?

(2.1) By jamsek on 2021-08-23 17:20:04 edited from 2.0 in reply to 1.0 [link] [source]

If you're on a Unix-like OS, just patch it.

ETA: If it were a fossil patch, you could run fossil patch apply.

(3) By Warren Young (wyoung) on 2021-08-23 17:18:53 in reply to 1.0 [link] [source]

Say:

  $ patch -p0 < diff.txt

(4) By Daniel Dumitriu (danield) on 2021-08-23 20:20:48 in reply to 2.1 [link] [source]

If you are on Windows, there is no native patch tool. Your easiest (as in having the biggest chance to be already installed) option is the utility packaged in git - mine lives under C:\Program Files\Git\usr\bin\patch.exe. Other tools I know about that package an older-or-newer patch are SourceTree, Gow, UnxUtils, GnuWin. Another guy wrote his own patch tool in Python, but since he's not Larry Wall, I cannot vouch for him :-) (although some found it useful on StackOverflow)

I am sure Richard could write a fossil subcommand for that (despite the low need and high hairiness), but unfortunately the name is already taken and does something much more useful for him :-)

(5) By NotSoBright (Skrikerunge) on 2021-08-27 11:08:06 in reply to 3 [link] [source]

Thanks, but I am not sure I understand.
What is the purpose of [-p0]?
Strip 0 leading components from file names?

Quotes from patch --help :
Usage: /usr/bin/patch [OPTION]... [ORIGFILE [PATCHFILE]]
  -p NUM  --strip=NUM  Strip NUM leading components from file names.

(6) By NotSoBright (Skrikerunge) on 2021-08-27 11:09:43 in reply to 4 [link] [source]

Thank you! I have GIT installed, so there was a patch command at that exact location.

(7) By Daniel Dumitriu (danield) on 2021-08-27 14:19:44 in reply to 5 [link] [source]

Warren's suggestion assumed that you run the patch command from the root of your working directory; given how fossil writes the file names in the diff files it generates, yes, that means, skip no leading components when looking for those files.

In theory you could run patch with -p1 from a directory below, if all affected files are under that. But that is just unneeded complication.

Try and look up some patch examples on the web, although the entry for -p in man patch is clear enough.

(8) By Warren Young (wyoung) on 2021-08-27 15:57:15 in reply to 7 [link] [source]

Without -p0, the tool assumes all files mentioned are in CWD, so it only works for the case where every changed file happens to be at the top of the checkout directory. When using patch to apply unified diffs created by a version control system, this is almost never the case in my experience, since you've usually got your sources buried at least one level below the root, most commonly in a src/ subdir.

Adding -p0 is almost reflexive for me these days. If we could go back in time and get Larry Wall to redesign the patch command interface, I think it would make a better default, but for compatibility reasons, we're stuck with this.