Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make `patch` return 0 when skipping an already applied patch

Tags:

patch

I have a build tool that runs a patch command and if the patch command returns non-zero, it will cause the build to fail. I am applying a patch that may or may not already be applied, so I use the -N option to patch, which skips as it should. However, when it does skip, patch is returning non-zero. Is there a way to force it to return 0 even if it skips applying patches? I couldn't find any such capability from the man page.

like image 592
zjm555 Avatar asked Apr 30 '14 18:04

zjm555


People also ask

How do you check if a patch has been applied?

The way to check whether a patch is really applied is to use the AD_PATCH. IS_PATCH_APPLIED PL/SQL function.

What does the option to the patch command do?

The patch command reads a source file's instructions on how to change a file, then applies the changes. The source file contains difference listings (or diff listings) produced by the diff -c or -u command, and one or more sets of diff command output, customarily called hunks.

How does patch work linux?

patch takes a patch file patchfile containing a difference listing produced by the diff program and applies those differences to one or more original files, producing patched versions. Normally the patched versions are put in place of the originals. Backups can be made; see the -b or --backup option.


2 Answers

Accepted answer did not work for me because patch was returning 1 also on other types of errors (maybe different version or sth). So instead, in case of error I am checking output for "Skipping patch" message to ignore such but return error on other issues.

OUT="$(patch -p0 --forward < FILENAME)" || echo "${OUT}" | grep "Skipping patch" -q || (echo "$OUT" && false);
like image 115
fsw Avatar answered Sep 21 '22 09:09

fsw


I believe that the following recipe should do the trick, it is what I am using in the same situation;

patches: $(wildcard $(SOMEWHERE)/patches/*.patch)
    for patch_file in $^; do \
        patch --strip=2 --unified --backup --forward --directory=<somewhere> --input=$$patch_file; \
        retCode=$$?; \
        [[ $$retCode -gt 1 ]] && exit $$retCode; \
    done; \
    exit 0

This recipe loops over the dependencies (in my case the patch files) and calls patch for each one. The "trick" on which I am relying is that patch returns 1 if the patch has already been applied and other higher numbers for other errors (such as a non existent patch file). The DIAGNOSTICS section of the patch manual entry describes the return code situation. YMMV

like image 35
awol Avatar answered Sep 18 '22 09:09

awol