Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 85 lines (70 sloc) 2.54 KB
#!/usr/bin/env bash
export git_root=$(git rev-parse --show-toplevel 2> /dev/null)
export hg_root=$(hg root 2> /dev/null)
if [ ! -z "${git_root}" ]; then
project_root=${git_root}
elif [ ! -z "${hg_root}" ]; then
project_root=${hg_root}
else
echo "you must be in a git or mercurial repo to use mine"; exit 1
fi
export current_name=${project_root##*/}
export mine_config=${MINE_LOCATION}/config
export mine_backups=${MINE_LOCATION}/snapshots
export backup_dir=${mine_backups}/${current_name}
mine_dirpath(){
for name in $@; do
readlink -f $1 | sed "s!${project_root}/!!"
done
}
mine_snap(){ #HELPTEXT snapshot of the project's mine files
rsync --quiet -vr --filter="+ */" --filter="+ **_mine_**" --filter="- *" \
--prune-empty-dirs ${project_root} ${mine_backups}/
};
mine_restore(){ #HELPTEXT restore the last project snapshot
if [ -d "${backup_dir}" ]; then
rsync --quiet -vr ${mine_backups}/${current_name}/ ${project_root}/
else
echo "no backups found for ${current_name}" && exit 1
fi
};
mine_clean(){ #HELPTEXT remove all _mine_ files from the current repo
mine_list && printf "remove these files? (y/n) "
read yn
if [[ "$yn" == "y" ]]; then
find ${project_root} -path '*_mine_*' -exec rm -rf '{}' \;
fi
};
mine_purge(){ #HELPTEXT purge all mine files from the current project AND backups
mine_list && printf "purge all _mine_ files for ${current_name}? (y/n) "
read yn
if [[ "$yn" == "y" ]]; then
rm -r ${mine_backups}/${current_name}/
find ${project_root} -path '*_mine_*' -exec rm -rf '{}' \;
fi
};
mine_list(){ #HELPTEXT list all of the _mine_ files in the project
local files=$(find ${project_root} -path '*_mine_*' | \
sed -e "s!${project_root}!${current_name}!" -e 's/^/ /')
printf "\n${files:-no mine files}\n\n"
};
mine_touch(){ #HELPTEXT an alias for `touch <file>_mine_`, because `_` is a pain.
for name in $@; do touch ${name}_mine_; done
};
mine_init(){ #HELPTEXT creates a `<projectname>_mine_.md` file and a `_mine_/` directory
local newdir=${project_root}/_mine_
if [ ! -d "${newdir}" ]; then mkdir ${newdir} ; fi
touch ${current_name}_mine_.md ${newdir}/{notes,todo}.md
};
mine_help(){
hl=$(tput bold)
norm=$(tput sgr0)
printf "\n${hl}mine${norm} | manage your personal, project-related files in a versioned repo\n\n"
sed -n -e "${LINENO}q" -e '/#HELPTEXT/{s!^[^_]\+_\([a-z]\+\)[^#]\+#HELPTEXT\(.*$\)! \1\t\2!;p}' $0 \
| column -t -s $'\t'
echo
};
case $1 in
help|touch|clean|purge|list|restore|snap|init) mine_"$1" "${@:2}" ;;
*) mine_help ;;
esac