xsel: new script
[bin.git] / git-mirror
1 #!/usr/bin/env sh
2 # Mirror some Git repos
3
4 usage() {
5                 echo 'Usage: git-mirror [command] [arguments]
6   git-mirror run [path to repos] [path to log file] [html directory]
7       optional: [sleep duration]
8   git-mirror gen-url [path to repo] -- generate url file in repo
9   git-mirror help -- show this message
10
11   relative paths are not supported, but globs are.
12   To use, first clone the wanted somewhere repos with the --mirror flag.'
13 }
14
15 run() {
16                 [ -z ${3} ] && echo "not enough arguments" && exit 1
17                 [ -z $(which stagit) ] && echo "please install stagit" && exit 1
18                 [ -z $(which minify) ] && echo "please install minify" && exit 1
19
20                 cd ${1}
21
22                 [ -d ${3} ] || mkdir ${3}
23                 cd ${3}
24
25                 while :; do
26                                 stagit-index ${1}/*
27                                 find "$1" -name '*.git' -type d | \
28                                                 while read dir; do
29                                                                 newdir=${3}/$(basename ${dir} .git)
30                                                                 [ -d ${newdir} ] || mkdir ${newdir}
31                                                                 cd ${newdir}
32                                                                 stagit ${dir}
33                                                                 cp "$1/logo.png" .
34                                                                 ln -s logo.png favicon.png
35                                                                 find . -type f \( -name '*.html' -or -name '*.xml' \) | \
36                                                                                 while read i ; do
37                                                                                                 minify ${i} > ${i}.tmp
38                                                                                                 mv ${i}.tmp ${i}
39                                                                                 done
40                                                                 git -C ${dir} fetch --tags
41                                                                 echo $(date -Iseconds) ' | ' ${dir} | tee -a ${2}
42                                                                 echo $(tail -n 1500 ${2}) > ${2}
43                                                 done
44         sleep ${4:-600}
45     done
46 }
47
48 gen_url() {
49     find ${1} -maxdepth 2 -name '*.git' | \
50                                 while read dir; do
51                                                 $(git -C $dir remote get-url origin) > $dir/url
52                                 done
53 }
54
55 case ${1} in
56     -h | --help | help)
57                                 usage
58                                 ;;
59     run)
60                                 shift 1
61                                 run $@
62                                 ;;
63     gen-url)
64                                 shift 1
65                                 gen_url $@
66                                 ;;
67     *)
68                                 usage
69                                 exit 1
70                                 ;;
71 esac