tj.git-extras/bin/git-cp
2020-03-19 15:49:07 +01:00

62 lines
1.7 KiB
Bash
Executable file

#!/usr/bin/env bash
PROGRAM=$0
CURRENT_FILENAME=""
DESTINATION_FILENAME=""
function current_branch() {
git rev-parse --abbrev-ref HEAD
}
function usage()
{
echo "USAGE: ${PROGRAM} CURRENT_FILENAME DESTINATION_FILENAME"
}
while [[ $# > 0 ]]
do
key="$1"
if [[ "$CURRENT_FILENAME" == "" ]]; then
CURRENT_FILENAME=$key
elif [[ "$DESTINATION_FILENAME" == "" ]]; then
DESTINATION_FILENAME=$key
else
usage
exit 30 # Error during arguments parsing
fi
shift # past argument or value
done
if [[ "$DESTINATION_FILENAME" == "" ]]; then
usage
exit 20 # Missing arguments CURRENT_FILENAME
elif [[ "$CURRENT_FILENAME" == "" ]]; then
usage
exit 10 # Missing arguments CURRENT_FILENAME
else
echo "Coping $CURRENT_FILENAME into $DESTINATION_FILENAME"
INTERMEDIATE_FILENAME="${CURRENT_FILENAME//\//__}-move-to-${DESTINATION_FILENAME//\//__}"
# We keep the existing file on the side in a commit
git mv "${CURRENT_FILENAME}" "${INTERMEDIATE_FILENAME}"
git commit -nm "Keep $CURRENT_FILENAME"
SAVED=$(git rev-parse HEAD)
# We come back to the previous state
git reset --hard HEAD^ # Revert that change
# We move the file to its new destination
git mv "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}"
git add "${DESTINATION_FILENAME}"
git commit -nm "Copy $CURRENT_FILENAME into $DESTINATION_FILENAME"
# We get back our copy
rm -f "${DESTINATION_FILENAME}" # Make sure the file doesn't exists somehow
git merge ${SAVED}
git commit -a -m "Duplicate ${CURRENT_FILENAME} history." # Keeping both files
# We get back our original name
git mv "${INTERMEDIATE_FILENAME}" "${CURRENT_FILENAME}"
git commit -nm "Set back $CURRENT_FILENAME file."
fi