title: Detecting if a patch is applied or not
body: <p>I am working on a project that requires me to patch 3rd party open source libraries for use in my application. To make things simple, I am only using Makefiles and a simple configuration script to build the application. I have multiple patches that I need to apply, so therefore I use Makefile dependencies to patch:</p>
  
  <pre><code>all: patch1 patch2 patch3
      do-build-stuff-here
  patch1:
      patch -p0 &lt; patch1.patch &amp;&amp; touch patch1
  patch2:
      patch -p0 &lt; patch2.patch &amp;&amp; touch patch2
  patch3:
      patch -p0 &lt; patch3.patch &amp;&amp; touch patch3
  </code></pre>
  
  <p>I know about Quilt, which the Debian packaging system uses for maintainer patches. I've used it, and it really works. However, it introduces an unnecessary dependency, especially for those who don't have Debian.</p>
  
  <p>Sometimes, not all the patches are applied to allow me (and other developers) retest the building and the application in certain conditions. When I call "clean" or "distclean", I usually reverse the patches:</p>
  
  <pre><code>clean: rmpatch1 rmpatch2 rmpatch3
      do-clean-stuff-here
  rmpatch1:
      patch -p0 -R &lt; patch1.patch
  rmpatch2:
      patch -p0 -R &lt; patch2.patch
  rmpatch3:
      patch -p0 -R &lt; patch3.patch
  </code></pre>
  
  <p>(Ignore the fact that I could just delete the original source and start fresh.)</p>
  
  <p>However, patch will likely fail if patch #3 is not applied, and patch #1 and #2 are. Therefore, I depend on the patch counterparts for each rmpatch:</p>
  
  <pre><code>rmpatch1: patch1
      patch -p0 -R &lt; patch1.patch
  rmpatch2: patch2
      patch -p0 -R &lt; patch2.patch
  rmpatch3: patch3
      patch -p0 -R &lt; patch3.patch
  </code></pre>
  
  <p>But this still doesn't solve my problem, and "make clean" ends up patching the source first, then unpatching again!</p>
  
  <p>I guess my question would be: is there any way I can detect if a patch is applied or not? I know patch, when run interactively, will tell you that it found a previously applied patch. Can this behavior be reproduced non-interactively?</p>
  
  <p>The other question (somewhat related) is whether or not this method of patching is valid, and if not, what method of sequential patching (via only Makefiles) I could use.</p>