Skip to content

Instantly share code, notes, and snippets.

@luiscoms
Created April 17, 2015 14:56
Show Gist options
  • Save luiscoms/71bd932f801689bf0aef to your computer and use it in GitHub Desktop.
Save luiscoms/71bd932f801689bf0aef to your computer and use it in GitHub Desktop.
CVS Utils
#!/bin/bash
PROJECT_PATH=$(pwd);
APPLY=false
IDENTIFIER=""
echoerror() {
printf "$@\n" 1>&2
}
apply() {
cd "$PROJECT_PATH" && patch --dry-run -f -p0 < "$PATCH_DIR/patch.diff"
if [ $? -ne 0 ]; then
echoerror "Invalid Patch!"
exit 1
fi
local files_dir="$PATCH_DIR/files"
if [ -d $files_dir ]; then
local files=$(ls "$files_dir");
for file in $files; do
cp -rf "$files_dir/$file" "$PROJECT_PATH"
done
fi
cd "$PROJECT_PATH" && patch -p0 < "$PATCH_DIR/patch.diff"
}
create() {
if [ -d "$PATCH_DIR" ]; then
read -p"The '$IDENTIFIER' patch already exists! Are you want proceed?[Y/n] " -er proceed
if [ "$proceed" != "Y" -a "$proceed" != "y" -a "$proceed" != "" ]; then
exit 0
fi
rm -rf "$PATCH_DIR"
fi
echo "Creating patch..."
local diff_path="$PROJECT_PATH"
if [ "$PROJECT_PATH" == "$(pwd)" ]; then
diff_path="."
fi
DIFF=$(cvs diff "$diff_path" 2>/dev/null | grep -v '\(.so\|.so[.0-9]\+\|.log\|.o\)$' | sed "s%^\(\?\|Index:\) $PROJECT_PATH/%\1 %")
if [ "$DIFF" == "" ]; then
echoerror "Diffs weren't found at '$PROJECT_PATH'"
exit 1
fi
mkdir -p "$PATCH_DIR"
echo "$DIFF" | grep -v ^? > "$PATCH_DIR/patch.diff"
FILES=$(echo "$DIFF" | grep ^? | sed 's/^?//')
for file in $FILES; do
mkdir -p "$PATCH_DIR/files/$(dirname $file)"
cp "$PROJECT_PATH/$file" "$PATCH_DIR/files/$file"
done
echo "Patch created at '$PATCH_DIR'"
}
usage() {
echo "Usage: cvs-patch [options] <identifier>"
echo ""
echo "Options:"
echo " -p --project Sets the project path."
echo " --apply Apply an existing patch to the project."
echo ""
}
parse_args() {
local ARGS=$(getopt -o p:h -l "project:,apply,help" -n "cvs-patch" -- "$@")
if [ $? -ne 0 ]; then
echoerror "\n$(usage)"
exit 1
fi
eval set -- "$ARGS"
while [ $# -gt 0 ]; do
case "$1" in
-p|--project)
shift
if [ -n "$1" ]; then
PROJECT_PATH="$(echo "$1" | sed 's_/$__')"
fi
;;
--apply)
shift
APPLY=true
;;
-h|--help)
shift
usage
exit
;;
--)
shift
break;
;;
*)
if [ "$IDENTIFIER" == "" ]; then
IDENTIFIER=$1
else
echoerror "Just one identifier must be passed!"
exit 1;
fi
;;
esac
shift
done
if [ "$IDENTIFIER" == "" ]; then
if [ "$1" != "" ]; then
if [ $# -eq 1 ]; then
IDENTIFIER="$1"
else
echoerror "Please pass only one identifier!"
echoerror "\n$(usage)"
exit 1
fi
else
echoerror "Please pass an identifier!"
echoerror "\n$(usage)"
exit 1
fi
fi
}
# ========= MAIN =========
parse_args $@
PATCH_DIR="$HOME/tmp/patches/$IDENTIFIER"
if $APPLY; then
apply
else
create
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment