jlpoole Guru


Joined: 01 Nov 2005 Posts: 491 Location: Salem, OR
|
Posted: Thu Mar 01, 2018 6:18 pm Post subject: Sample Patch Study |
|
|
I was wrestling with creating a new patch to add in addition to my old patch for apps-emulation/xen. I learned, painfully, that in order to deploy my patch under /etc/portage/patches/app-emulation/xen, I had to have an initial forward slash ("/") in the file name because the patch command issued uses the parameter p1:
To better understand patching, I created this bash script as a study and share it with the hope that whoever else finds themselves wrestling the patches, they can avail themselves with this script to experiment and learn:
Code: | #!/bin/bash
#
# Creates two files with minor difference, creates a patch,
# and then attempts to patch.
#
#
WORKDIR=/tmp/jlpoole
SOURCEFILE=caesar.txt
REVISEDFILE=caesar_corrected.txt
PATCH=test1.patch
#
# dev simulates a working area, whereas prod will be the
# tree we'll patch to
#
TREE=a/b/c
DEV=dev
PROD=prod
WORK_DEV=$WORKDIR/$DEV
WORK_PROD=$WORKDIR/$PROD
#
# cleanup previous attempts
#
rm -r $WORK_DEV
rm -r $WORK_PROD
mkdir -p $WORK_DEV/$TREE
mkdir -p $WORK_PROD/$TREE
#
# Create original file
#
cat >$WORK_DEV/$TREE/$SOURCEFILE <<EOF
Veni
Vidi
dunno
EOF
#
# Create a copy to be used as the source we'll patch
#
cp $WORK_DEV/$TREE/$SOURCEFILE $WORK_PROD/$TREE/$SOURCEFILE
#
# Create the revision
#
cat >$WORK_DEV/$TREE/$REVISEDFILE <<EOF
Veni
Vidi
Vici
EOF
#
# Create the patch. Note within which directory we do so is critical
# as the patch file will inherit the relative file names we feed on the
# command line to "diff"
#
cd $WORK_DEV
echo Changed to $WORK_DEV
#
# Below works
#
diff -u $TREE/$SOURCEFILE $TREE/$REVISEDFILE > $PATCH
#
# Below fails. Conclusion: we need the "-u"
#
#diff $TREE/$SOURCEFILE $TREE/$REVISEDFILE > $PATCH
#
# Copy the patch to PROD
#
cp $WORK_DEV/$PATCH $WORK_PROD/$PATCH
#
# attempt to apply the patch
#
cd $WORK_PROD
echo Changed dir to: $WORK_PROD
echo cat $WORK_PROD/$TREE/$SOURCEFILE
echo
echo
echo cat $WORK_PROD/test1.patch
#
# Apply the patch. Remember, we have to be in the directory
# where the relative files listed in the top of patch will appear.
#
#patch -p0 --dry-run --verbose < $PATCH
patch -p0 --verbose < $PATCH
#
# show the change
#
echo Below should be "Vici"
tail -n 1 $WORK_PROD/$TREE/$SOURCEFILE
|
"AS IS". Use at your own risk. |
|