root/floppyfw/patches/isapplied

Revision 1, 1.8 KB (checked in by root, 6 years ago)

First post

Line 
1#! /bin/sh
2
3# Script to tell whether a patch has been applied to a tree or not.
4# Assume patch is in diff -u form, and applies in dir with -p1.
5
6if [ $# != 2 ] || [ ! -d $1 ] || [ ! -f $2 ]
7then
8    echo Need directory and patch. >&2
9    exit 1
10fi
11
12# Convert $2 to absolute.
13case "$2"
14in
15    /*) PATCH="$2" ;;
16    *) PATCH="`pwd`/$2" ;;
17esac
18
19[ -d tmp-isapplied ] && rm -rf tmp-isapplied
20
21# Copy only files which are mentioned in patch.
22# eg. +++ working-2.4.0-test11-5/net/ipv4/netfilter/ipt_MASQUERADE.c    Fri Dec  1 17:56:02 2000
23mkdir tmp-isapplied
24for f in `grep '^+++ ' $2 | cut -d/ -f2- | cut -d'      ' -f1`
25do
26    mkdir -p `dirname tmp-isapplied/$f` 2>/dev/null
27    cp $1/$f tmp-isapplied/$f 2>/dev/null
28done
29
30cd tmp-isapplied || (echo Can\'t change into tmp-isapplied >&2; exit 1)
31
32patch -R -f -p1 < $PATCH > ../tmp-isapplied-result 2>&1
33
34MISSING_FILES=`grep -c "No file to patch" < ../tmp-isapplied-result`
35REJECTS=`grep -c "FAILED at" < ../tmp-isapplied-result`
36HUNKS="`grep -c ^@@ $PATCH`"
37NOTEMPTY_FILES=`grep -c "not empty after patch, as expected" < ../tmp-isapplied-result`
38
39if [ "$MISSING_FILES" -ne 0 ]
40then
41    echo `basename $PATCH` NOT APPLIED \($MISSING_FILES missing files\)
42    cd .. && rm -rf tmp-isapplied tmp-isapplied-result
43    exit 1
44elif [ "$NOTEMPTY_FILES" -ne 0 ]
45then
46    echo `basename $PATCH` ALREADY APPLIED \($REJECTS rejects out of $HUNKS hunks\).
47    cd .. && rm -rf tmp-isapplied tmp-isapplied-result
48    exit 0
49elif [ `expr $REJECTS \* 2` -gt $HUNKS ]
50then
51    echo `basename $PATCH` NOT APPLIED \($REJECTS rejects out of $HUNKS hunks\)
52    cd .. && rm -rf tmp-isapplied tmp-isapplied-result
53    exit 1
54else
55    echo `basename $PATCH` ALREADY APPLIED \($REJECTS rejects out of $HUNKS hunks\).
56    cd .. && rm -rf tmp-isapplied tmp-isapplied-result
57    exit 0
58fi
Note: See TracBrowser for help on using the browser.