I would like to create a patch from a specific gcc branch comparing it to the official releases; so when I unpack the tarball from the stable release, I can apply the patch and get the equivalent of what was in that specific branch .

It's the first time I need to create a patch, so it's my very first time doing this and my main concern is to get the options and the parsing right since we are talking about an extremely important piece of software

diff -crB GccStable GccGit > /tmp/fromStabletoBranch.patch

Is this enough and the best way of doing it ?

  • The usual good practices here involve version control or some variant of these. This includes, mercurial, git, and their associated patch queue extensions. You could also consider quilt. Perhaps you could go into more detail as to what you are trying to do? – Faheem Mitha Oct 14 '14 at 21:46
  • @FaheemMitha what do you mean with "more details" ? I have a version of gcc from the official stable tar.bz2 and another unstable version of it from a git repository, I would like to create a patch, of course I would like to compare just against the master branch, not the entire repository . – user2485710 Oct 14 '14 at 21:50
  • OK, well, sure you can use something as simple as diff. but using version control is generally preferable. For one thing, it makes it much harder to lose track of what you are doing. – Faheem Mitha Oct 14 '14 at 21:57
  • @FaheemMitha I don't understand what you are suggesting, my tar.bz2 is clearly not a git repository, how do you think I should proceed ? – user2485710 Oct 14 '14 at 22:01
  • 1
    Do a search for "creating patches using version control". Two previous answers I've written which are related are unix.stackexchange.com/a/127810 and unix.stackexchange.com/a/139817 – Faheem Mitha Oct 15 '14 at 8:43
up vote 15 down vote accepted

Yes, this is a good way to create a patch.

In short:

  1. To create patch for single file your command may look like

    diff -Naru file_original file_updated > file.patch

    where

    • -N: treat absent files as empty
    • -a: treat all files as text
    • -r: recursively compare any subdirectories found
    • -u: output NUM (default 3) lines of unified context
  2. To create patch for whole directory:

    diff -crB dir_original dir_updated > dfile.patch

    where

    • -c: output NUM (default 3) lines of copied context
    • -r: recursively compare any subdirectories
    • -B: ignore changes whose lines are all blank

After all to apply this patch one can run

patch -p1 --dry-run < dfile.patch

where switch p instructs patch to strip the path prefix so that files will be identified correctly. In most cases it should be 1.

Remove --dry-run if you are happy from the result printed on the screen.

  • question: what is supposed to happen if a directory or a file gets deleted in the dir_updated compared to what there was in dir_original ? The diff takes care of that too or it gets skipped ? – user2485710 Oct 15 '14 at 7:47
  • @user2485710 diff sees which files were deleted. file.patch in just a text file, so you can open it in any editor or just cat it and you will see a line like only in dir_original: missingfile.txt – jimmij Oct 15 '14 at 14:51
  • ok, but then patch will delete that missingfile.txt or what else ? – user2485710 Oct 15 '14 at 15:14
  • It depends. If you want to remove them use diff -N ... as in my first example. Normally patch will remove empty files by default. If you don't want, just use only diff -crB as in your question. Also in some (rare) cases -E option in patch command is needed to remove empty files, after patch manual: if the input is not a context diff or if patch is conforming to POSIX, patch does not remove empty patched files unless this option is given – jimmij Oct 15 '14 at 15:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.