Search code examples
patchbuild-systembuild-script

Automated build and previously applied patches


How can I prevent aborted builds when a previously applied patch is detected without:

  • simply ignoring all failed patches
  • requiring user input

Patch itself is capable of identifying a previously applied patch. There's got to be a way to avoid the non-zero exit status on previously applied patches, right?

This doesn't work:

yes 'n' | patch -p<w/e> -i <w/e>

Because patch reads from /dev/tty (I'm guessing) instead of stdin. Even if it did read from stdin, it still gives an exit status of 1.

It seems like I'm missing something. I can't be the first to have run into this problem.


Solution

  • I think I may have just solved my problem.

    diff --git a/src/common.h b/src/common.h
    index 9e355fe..e1b1555 100644
    --- a/src/common.h
    +++ b/src/common.h
    @@ -108,8 +108,10 @@ XTERN bool force;
     XTERN bool batch;
     XTERN bool noreverse;
     XTERN bool reverse;
    +XTERN bool applied;
     XTERN enum { DEFAULT_VERBOSITY, SILENT, VERBOSE } verbosity;
     XTERN bool skip_rest_of_patch;
    +XTERN bool applied_is_cause;
     XTERN int strippath;
     XTERN bool canonicalize;
     XTERN int patch_get;
    diff --git a/src/patch.c b/src/patch.c
    index a60e631..3d375b3 100644
    --- a/src/patch.c
    +++ b/src/patch.c
    @@ -613,7 +613,8 @@ main (int argc, char **argv)
            if (fstat (fileno (rejfp), &rejst) != 0 || fclose (rejfp) != 0)
              write_fatal ();
            rejfp = NULL;
    -       somefailed = true;
    +       if (! somefailed && ! (applied && applied_is_cause))
    +         somefailed = true;
            say ("%d out of %d hunk%s %s", failed, hunk, "s" + (hunk == 1),
             skip_rest_of_patch ? "ignored" : "FAILED");
            if (outname && (! rejname || strcmp (rejname, "-") != 0)) {
    @@ -629,7 +630,7 @@ main (int argc, char **argv)
                  rej[len - 1] = '#';
                simple_backup_suffix = s;
            }
    -       if (! dry_run)
    +       if (! dry_run && ! (applied && applied_is_cause))
              {
                say (" -- saving rejects to file %s\n", quotearg (rej));
                if (rejname)
    @@ -706,9 +707,10 @@ reinitialize_almost_everything (void)
    
         reverse = reverse_flag_specified;
         skip_rest_of_patch = false;
    +    applied_is_cause = false;
     }
    
    -static char const shortopts[] = "bB:cd:D:eEfF:g:i:l"
    +static char const shortopts[] = "abB:cd:D:eEfF:g:i:l"
     #if 0 && defined ENABLE_MERGE
                    "m"
     #endif
    @@ -716,6 +718,7 @@ static char const shortopts[] = "bB:cd:D:eEfF:g:i:l"
    
     static struct option const longopts[] =
     {
    +  {"applied", no_argument, NULL, 'a'},
       {"backup", no_argument, NULL, 'b'},
       {"prefix", required_argument, NULL, 'B'},
       {"context", no_argument, NULL, 'c'},
    @@ -777,6 +780,7 @@ static char const *const option_help[] =
     "",
     "  -N  --forward  Ignore patches that appear to be reversed or already applied.",
     "  -R  --reverse  Assume patches were created with old and new files swapped.",
    +"  -a  --applied  Ignore error and save no rejects on applied or reversed patch.",
     "",
     "  -i PATCHFILE  --input=PATCHFILE  Read patch from PATCHFILE instead of stdin.",
     "",
    @@ -869,6 +873,9 @@ get_some_switches (void)
         while ((optc = getopt_long (Argc, Argv, shortopts, longopts, (int *) 0))
           != -1) {
        switch (optc) {
    +       case 'a':
    +       applied = true;
    +       break;
            case 'b':
            make_backups = true;
             /* Special hack for backward compatibility with CVS 1.9.
    diff --git a/src/util.c b/src/util.c
    index ee88c13..432bc5c 100644
    --- a/src/util.c
    +++ b/src/util.c
    @@ -1056,6 +1056,7 @@ ok_to_reverse (char const *format, ...)
         {
           say ("  Skipping patch.\n");
           skip_rest_of_patch = true;
    +      applied_is_cause = true;
         }
       else if (force)
         {
    @@ -1079,6 +1080,7 @@ ok_to_reverse (char const *format, ...)
              if (verbosity != SILENT)
            say ("Skipping patch.\n");
              skip_rest_of_patch = true;
    +         applied_is_cause = true;
                }
            }
             }