#!/bin/sh
if [ "$1" == "" ]; then
FILE="/dev/stdin"
else
FILE="$1"
# make sure file exists and is readable
if [ ! -f $FILE ]; then
echo "$FILE : does not exists"
exit 1
elif [ ! -r $FILE ]; then
echo "$FILE: is not readable"
exit 2
fi
fi
num=0;
mkdir -p chunks
# Set loop separator to end of line
BAKIFS=$IFS
IFS=$(echo -en "\n\b")
cat "$FILE" | while read -r l; do
processed=0
if echo "$l" | grep -e "^--- " 2>&1 >/dev/null ; then
minus="$l"
minus_file=`echo "$l" | sed -r -e 's|---\ [^/]*/||g'`
echo "MINUS: $minus_file"
processed=1
fi
if echo "$l" | grep -e "^+++ " 2>&1 >/dev/null ; then
plus="$l"
plus_file=`echo "$l" | sed -r -e 's|\+\+\+\ [^/]*/||g'`
echo "PLUS: $plus_file"
processed=1
fi
if echo "$l" | grep -e "^@@ " 2>&1 >/dev/null ; then
test "$minus_file" != "$plus_file" && { echo "ERROR!"; IFS=$BAKIFS; exit 1; }
num=$(($num+1))
fname=`printf "chunk_%03d.patch" $num`
echo "PATCH: $fname"
echo "$minus" > chunks/$fname
echo "$plus" >> chunks/$fname
echo "$l" >> chunks/$fname
processed=1
fi
if [ $processed -ne 1 ]; then
test "$minus_file" != "$plus_file" && { echo "ERROR!"; IFS=$BAKIFS; exit 1; }
echo "$l" >> chunks/$fname
fi
done
# restore $IFS which was used to determine what the field separators are
IFS=$BAKIFS
exit 0
{August 20, 2009} How to break a big patch in its individual chunks

Leave a Reply