I'm of making a pull request that allows patch to exit(0) when all rejected hunks are already applied.

This will allow idempotent patching, ie, patch to return 0 when a patch has already been applied.

I propose an option:
-z   Zero exit status when all rejected hunks are already applied

Currently, code like this is required, which doesn't exactly make for ease of use when ensuring robustness:

# If we could reverse the patch, then it has already been applied; skip it
if patch --dry-run --reverse -d "$gem_dir" -fp1 --ignore-whitespace \
  -i "$patch" >/dev/null 2>&1; then
  echo "skipping (already applied)"
  continue
else # patch not yet applied
  echo "patching..."
  # Patch returns non-zero if some hunks have already been applied
  if ! patch -d "$gem_dir" -fsp1 --ignore-whitespace -i "$patch"; then
    # Check that the patch was fully applied by pretending to reverse it
    if patch --dry-run --reverse -d "$gem_dir" -fp1 --ignore-whitespace \
    -i "$patch" >/dev/null 2>&1; then
      echo "Ignoring failure: hunk(s) were already applied"
    else
      echo "Patch failed for $gem_dir" >&2; exit 1;
    fi
  fi
fi


Would you consider including such a pull request?

Regards,

Tom Hale

Reply via email to