diff options
Diffstat (limited to 'bin/git-mirror')
-rwxr-xr-x | bin/git-mirror | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/bin/git-mirror b/bin/git-mirror new file mode 100755 index 0000000..af54a14 --- /dev/null +++ b/bin/git-mirror | |||
@@ -0,0 +1,84 @@ | |||
1 | #!/bin/sh | ||
2 | # | ||
3 | # Copyright (c) 2013 Nagios Plugins Development Team | ||
4 | # | ||
5 | # Originally written by Holger Weiss <holger@zedat.fu-berlin.de>. | ||
6 | # | ||
7 | # This file is free software; the Nagios Plugins Development Team gives | ||
8 | # unlimited permission to copy and/or distribute it, with or without | ||
9 | # modifications, as long as this notice is preserved. | ||
10 | # | ||
11 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
12 | # ANY WARRANTY, to the extent permitted by law; without even the implied | ||
13 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
14 | |||
15 | set -e | ||
16 | set -u | ||
17 | |||
18 | export PATH='/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin' | ||
19 | |||
20 | temp_prefix='/run/shm' | ||
21 | fourty_zeros=$(printf '%.40u' 0) | ||
22 | myself=${0##*/} | ||
23 | |||
24 | check_refs() | ||
25 | { | ||
26 | turn=$1 | ||
27 | temp_dir=$2 | ||
28 | |||
29 | git show-ref | while read object ref | ||
30 | do | ||
31 | ref_dir="$temp_dir/${ref%/*}" | ||
32 | ref_file="$temp_dir/$ref" | ||
33 | |||
34 | if [ $turn -eq 2 -a -f "$ref_file" ] \ | ||
35 | && grep "^1 $object$" "$ref_file" >'/dev/null' | ||
36 | then # The ref has not been modified. | ||
37 | rm -f "$ref_file" | ||
38 | else | ||
39 | mkdir -p "$ref_dir" | ||
40 | echo "$turn $object" >>"$ref_file" | ||
41 | fi | ||
42 | done | ||
43 | } | ||
44 | |||
45 | if [ $# -lt 1 ] | ||
46 | then | ||
47 | echo >&2 "Usage: $myself <repository> ..." | ||
48 | exit 2 | ||
49 | fi | ||
50 | |||
51 | temp_dir=$(mktemp -d "$temp_prefix/$myself.XXXXXX") | ||
52 | temp_file=$(mktemp "$temp_prefix/$myself.XXXXXX") | ||
53 | trap 'rm -rf "$temp_dir" "$temp_file"' EXIT | ||
54 | |||
55 | for repository in "$@" | ||
56 | do | ||
57 | hook="$repository/hooks/post-receive" | ||
58 | cd "$repository" | ||
59 | |||
60 | check_refs 1 "$temp_dir" | ||
61 | if ! git remote update --prune >"$temp_file" 2>&1 | ||
62 | then | ||
63 | cat >&2 "$temp_file" | ||
64 | exit 1 | ||
65 | fi | ||
66 | git fetch --quiet --tags | ||
67 | check_refs 2 "$temp_dir" | ||
68 | |||
69 | if [ -x "$hook" ] | ||
70 | then | ||
71 | find "$temp_dir" -type 'f' -print | while read ref_file | ||
72 | do | ||
73 | ref=${ref_file#$temp_dir/} | ||
74 | old=$(awk '$1 == "1" { print $2; exit }' "$ref_file") | ||
75 | new=$(awk '$1 == "2" { print $2; exit }' "$ref_file") | ||
76 | old=${old:-$fourty_zeros} | ||
77 | new=${new:-$fourty_zeros} | ||
78 | |||
79 | echo "$old" "$new" "$ref" | ||
80 | done >"$temp_file" | ||
81 | test -s "$temp_file" && "$hook" <"$temp_file" | ||
82 | fi | ||
83 | cd "$OLDPWD" | ||
84 | done | ||