Miriam Ruiz
random thoughts on technology and life











{August 20, 2009}   How to break a big patch in its individual chunks
#!/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


Leave a Reply


about

This is a personal webpage that belongs to Miriam Ruiz.
If you want to contact her, you can do at:
webmistress(at)miriamruiz(dot)es.

pages
categories
archive
twitter
calendar
August 2019
M T W T F S S
« Nov    
 1234
567891011
12131415161718
19202122232425
262728293031  
credits
WikiLeaks

La Lista de Sinde