projtool.pl: add update subcommand
[girocco/readme.git] / hooks / pre-receive
blob34c10ec4ee55292ae45f681df59850109cbe6d9f
1 #!/bin/sh
3 # Keep track of the last time we modified the object store
5 # Beware, we MAY be running in a chroot!
7 set -e
9 # Make sure the current directory is where we expect to be
10 [ "${GIT_DIR+set}" != "set" ] || cd "${GIT_DIR:-.}" || exit 1
11 case "${PWD%/*}" in */worktrees)
12 # Gah!
14 # But it COULD just be a coincidence...
15 [ -s commondir ] && [ -s HEAD ] &&
16 _cmndir= && read -r _cmndir <commondir 2>/dev/null &&
17 [ -n "$_cmndir" ] && [ -d "$_cmndir" ]
18 then
19 # ...it is not, fix it!
20 cd "$_cmndir" || exit 1
22 esac
23 [ "${GIT_DIR+set}" != "set" ] || GIT_DIR="."
25 # Get out of the mob
26 case "$PWD" in *?/mob)
27 cd ..
28 GIROCCO_PERSONAL_MOB=1
29 esac
31 umask 002
33 if [ -x @perlbin@ ]; then
34 # We are NOT inside the chroot
35 basedir=@basedir@
36 list_packs() { command "$basedir/bin/list_packs" "$@"; }
37 strftime() { command "$basedir/bin/strftime" "$@"; }
40 git config gitweb.lastreceive "$(date '+%a, %d %b %Y %T %z')"
42 # Read the incoming refs and freshen old loose objects
43 # If we waited until post-receive a gc could have already nuked them
44 # We freshen the new ref in case it's being resurrected to protect it from gc
45 # We probably do not need to do it for new refs as Git tries to do that,
46 # but since we're already doing it for old refs (which Git does not do),
47 # it's almost no extra work for new refs, just in case. We also attempt
48 # to make all packs user and group writable so they can be touched later.
50 # Starting with Git v2.11.0 receive-pack packs/objects end up in a quarantine
51 # object directory that is just discarded immediately if pre-receive declines
52 # to accept the push. This is a good thing. However, it means that the
53 # incoming objects are NOT located in objects/... as GIT_OBJECT_DIRECTORY and
54 # GIT_QUARANTINE_PATH are both set to the quarantine objects directory and the
55 # original objects directory is appended to GIT_ALTERNATE_OBJECT_DIRECTORIES
56 # (but it will just be the absolute path to objects). The simple bottom line
57 # is that we should also try everything in the GIT_QUARANTINE_PATH directory if
58 # it's set.
59 [ -z "$GIT_QUARANTINE_PATH" ] || [ -d "$GIT_QUARANTINE_PATH" ] || unset GIT_QUARANTINE_PATH
61 # We also record changes to a ref log. We do it here rather than in
62 # post-receive so that we can guarantee all potential changes are recorded in
63 # the log before they take place. It's possible that the update hook will
64 # ultimately deny one or more updates but waiting until post-receive could
65 # result in updates being left out of the log.
67 hexdig='[0-9a-f]'
68 octet="$hexdig$hexdig"
69 octet4="$octet$octet$octet$octet"
70 octet20="$octet4$octet4$octet4$octet4$octet4"
71 _make_packs_ugw() {
72 find -L "$1" -maxdepth 1 -type f ! -perm -ug+w \
73 -name "pack-$octet20*.pack" -exec chmod ug+w '{}' + || :
74 } 2>/dev/null
75 _make_packs_ugw objects/pack
76 [ -z "$GIT_QUARANTINE_PATH" ] || _make_packs_ugw "$GIT_QUARANTINE_PATH/pack"
78 # Trigger a mini-gc if there are at least 20 packs present.
79 # Our current pack that contains this push's data will have a .keep while
80 # this hook is running so it will be excluded from the count, but the count
81 # is only an approximate guide anyway and being off by one or two will not
82 # cause any performance problems. We could drop the check to 19 instead to
83 # compensate but there could be other simultaneous pushes and it's highly
84 # unlikely that it would end up making any difference anyway. We also
85 # deliberately ignore anything in the "$GIT_QUARANTINE_PATH/pack" area because
86 # at this point it will have a .keep (if it exists) and because of the way
87 # it's created there can never be more than one pack in there anyway.
88 # The mini-gc code contains the logic to sort out small packs vs. non-small
89 # packs and which should be combined in what order so we do not need to
90 # do any more complicated testing here.
91 if ! [ -e .needsgc ]; then
92 packs=
93 { packs="$(list_packs --quiet --count --exclude-no-idx --exclude-keep objects/pack || :)" || :; } 2>/dev/null
94 if [ -n "$packs" ] && [ "$packs" -ge 20 ]; then
95 >.needsgc
99 # Make sure we have a reflogs directory and abort the update if we cannot
100 # create one. Normally project creation will make sure this directory exists.
101 [ -d reflogs ] || mkdir -p reflogs >/dev/null 2>&1 || :
102 [ -d reflogs ]
104 # Multiple push operations could be occurring simultaneously so we need to
105 # guarantee they do not step on one another and we do this by generating a
106 # unique log file name. We use a microseconds timestamp and the current process
107 # id and we guarantee that this process is kept alive for the entire microsecond
108 # of the timestamp thereby guaranteeing that we are the only possible process
109 # that could use that pid during that particular microsecond (ignoring leap seconds).
110 # To do this we need to sleep until the microsecond turns over, grab the timestamp
111 # and then sleep until the microsecond turns over again. This will introduce a
112 # guaranteed 2 microsecond delay into every push. This should not generally be
113 # noticeable. The strftime utility does this for us when using %N with current time.
114 # We always use UTC for the timestamp so that chroot and non-chroot match up.
115 # Log entries are the lines sent to the pre-receive hook with hhmmss prepended.
116 lognamets="$(TZ=UTC strftime '%Y%m%d_%H%M%S%N')"
117 lognamets="${lognamets%???}"
118 loghhmmss="${lognamets##*_}"
119 loghhmmss="${loghhmmss%??????}"
121 # We write to a temp ref log and then move it into place so that the reflogs
122 # collector can assume that log files with their final name are immutable
123 logname="reflogs/$lognamets.$$"
124 lognametmp="reflogs/tmp_$lognamets.$$"
126 while read -r old new ref; do
127 echo "$loghhmmss $old $new $ref" >&3
128 args=
129 if [ "$old" != "0000000000000000000000000000000000000000" ]; then
130 # freshen mod time on recently unref'd loose objects
131 fn="${old#??}"
132 shard="${old%$fn}"
133 args="$args 'objects/$shard/$fn'"
134 [ -z "$GIT_QUARANTINE_DIRECTORY" ] || args="$args '$GIT_QUARANTINE_DIRECTORY/$shard/$fn'"
136 if [ "$new" != "0000000000000000000000000000000000000000" ]; then
137 # prevent imminent pruning of a ref being resurrected
138 fn="${new#??}"
139 shard="${new%$fn}"
140 args="$args 'objects/$shard/$fn'"
141 [ -z "$GIT_QUARANTINE_DIRECTORY" ] || args="$args '$GIT_QUARANTINE_DIRECTORY/$shard/$fn'"
143 eval "chmod ug+w $args" 2>/dev/null || :
144 eval "touch -c $args" 2>/dev/null || :
145 done 3>"$lognametmp"
146 mv "$lognametmp" "$logname"
148 # While unlikely, it is conceivable that several ref updates have occurred that
149 # did not actually push any packs. In that case we could build up a large
150 # number of log files so request a mini gc if there are 50 or more of them now.
151 if ! [ -e .needsgc ]; then
152 logfiles=
153 { logfiles="$(($(find -L reflogs -maxdepth 1 -type f -print | wc -l || :)+0))" || :; } 2>/dev/null
154 if [ -n "$logfiles" ] && [ "$logfiles" -ge 50 ]; then
155 >.needsgc