]> Lady’s Gitweb - Git/blob - post-receive
Initial commit
[Git] / post-receive
1 #!/usr/bin/env -S sh
2 # SPDX-FileCopyrightText: 2024 Lady <https://www.ladys.computer/about/#lady>
3 # SPDX-License-Identifier: MPL-2.0
4
5 # This Source Code Form is subject to the terms of the Mozilla Public License, v 2.0.
6 # If a copy of the M·P·L was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
7
8 IGNORE_PATTERN='README\..*|LICENSES/.*|post-receive'
9 LIVE_BRANCH=live
10
11 if grep -q " refs/heads/$LIVE_BRANCH"
12 then
13 # Live branch was updated; copy resulting files.
14 #
15 # This isn’t super robust; if there is a failure partway thru then
16 # some files will be old and some will be new.
17 hashfiles=$(git ls-tree -rz "$LIVE_BRANCH" | tr '\000\t' '\n;' | sed 's/^[^ ]* [^ ]* //')
18 files=$(printf '%s' "$hashfiles" | sed 's|.*;|/var/www/gitweb/|')
19 existings=$(find /var/www/gitweb -type f)
20
21 echo '`'"$LIVE_BRANCH"'´ was updated; updating files…'
22
23 while test -n "$hashfiles"
24 do
25 # Iterate over each file and extract it into the appropriate
26 # location.
27 hashfile=$(printf '%s' "$hashfiles" | head -n 1)
28 hash=$(printf '%s\n' "$hashfile" | sed 's/;.*//')
29 file=$(printf '%s\n' "$hashfile" | sed 's|.*;|/var/www/gitweb/|')
30 dir=$(printf '%s\n' "$file" | sed 's|[^/]*$||')
31
32 if test '!' -d "$dir"
33 then
34 # The containing directory does not exist yet.
35 mkdir -p "$dir"
36 fi
37
38 if printf '%s\n' "$file" | grep -E -q '^/var/www/gitweb/('"$IGNORE_PATTERN"')$'
39 then
40 # The current file matches the ignore pattern.
41 :
42 else
43 # The current file does not match the ignore pattern.
44 printf 'Updating <%s>…\n' "$file"
45 git cat-file blob "$hash" > "$file"
46 fi
47
48 hashfiles=$(printf '%s' "$hashfiles" | tail -n +2)
49 done
50
51 while test -n "$existings"
52 do
53 # Iterate over existing files and remove ones which aren’t in the
54 # repo.
55 existing=$(printf '%s' "$existings" | head -n 1)
56 if printf '%s' "$files" | grep -F -q -x "$existing"
57 then
58 # The file matches a file in the repo.
59 :
60 else
61 # The file does not match a file in the repo.
62 printf 'Removing <%s>…\n' "$file"
63 rm "$existing"
64 fi
65 existings=$(printf '%s' "$existings" | tail -n +2)
66 done
67 fi
This page took 0.063667 seconds and 5 git commands to generate.