/
mine
executable file
·84 lines (70 loc) · 2.54 KB
/
mine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/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