Move .fix-metadata generation into a function.
[jelmer/etckeeper.git] / pre-commit.d / 30store-metadata
index 55f12728b223a6724867ec855999e080488aeeaa..2f6a2fc4355b204103b64ca8413026d6853630e5 100755 (executable)
@@ -1,54 +1,56 @@
 #!/bin/sh
 set -e
 
-if [ "$VCS" = git ] || [ "$VCS" = hg ] || [ "$VCS" = bzr ]; then
-       # ensure the file exists so that it will list its own metadata
-       if [ ! -e .fix-metadata ]; then
-               touch .fix-metadata
-               # Make sure the file is not readable by others, since it can leak
-               # information about contents of non-readable directories in /etc.
-               chmod 700 .fix-metadata
-       fi
-
-       echo "# Generated by etckeeper." > .fix-metadata
-       echo >> .fix-metadata
-
 
-       # Any files that aren't owner=root, group=root, or mode=0644 or 0755
-       # will be fixed by the .fix-metadata script.  Let's generate it.
+generate_metadata() {
+       # This function generates the script commands to fix any files
+       # that aren't owner=root, group=root, or mode=0644 or 0755.
+       # Script is produced on stdout.  Errors go to stderr.
 
        # Find all files and directories that don't have root as the owner
-       # Need to be sure UNKNOWN users and groups don't end up in the .fix-metadata
-       # file because chown and chgrp will choke on it.
+       # Need to be sure UNKNOWN users and groups don't end up in the
+       # .fix-metadata file because chown and chgrp will choke on it.
        output=$(find . \! -user root -exec stat --format="chown %U {}" {} \; | sort)
        if [ -n "$output" ]; then
                echo "$output" | grep "^chown UNKNOWN" >&2 || true
-               echo "$output" | grep -v "^chown UNKNOWN" >> .fix-metadata || true
+               echo "$output" | grep -v "^chown UNKNOWN" || true
        fi
 
        # Find all files and directories that don't have root as the group
        output=$(find . \! -group root -exec stat --format="chgrp %G {}" {} \; | sort)
        if [ -n "$output" ]; then
                echo "$output" | grep "^chgrp UNKNOWN" >&2 || true
-               echo "$output" | grep -v "^chgrp UNKNOWN" >> .fix-metadata || true
+               echo "$output" | grep -v "^chgrp UNKNOWN" || true
        fi
 
        # Find all directories that aren't 0755
-       find . -type d \! -perm 0755 -exec stat --format="chmod %a {}" {} \; \
-               | sort >> .fix-metadata
+       find . -type d \! -perm 0755 -exec stat --format="chmod %a {}" {} \; | sort
 
-       # Find all files that aren't either 0644 or 0755 (git keeps track of the
+       # Find all files that aren't 0644 or 0755 (git keeps track of the
        # executable bit so we don't have to).  All the files in the
        # /etc/.git/objects directory are 0444 so we'll specifically avoid it.
        find . -wholename ./.git -prune -o \
                -type f \! -perm 0644 \! -perm 0755 -exec stat --format="chmod %a {}" {} \; \
-               | sort >> .fix-metadata
-
+               | sort
 
-       # NOTE: we don't handle xattrs!
+       # We don't handle xattrs.
        # Maybe check for getfattr/setfattr and use them if they're available?
+}
 
 
+if [ "$VCS" = git ] || [ "$VCS" = hg ] || [ "$VCS" = bzr ]; then
+       # ensure the file exists so that it will list its own metadata
+       if [ ! -e .fix-metadata ]; then
+               touch .fix-metadata
+               # Make sure the file is not readable by others, since it can leak
+               # information about contents of non-readable directories in /etc.
+               chmod 700 .fix-metadata
+       fi
+
+       echo "# Generated by etckeeper." > .fix-metadata
+       echo >> .fix-metadata
+       generate_metadata >> .fix-metadata
+
        # stage the file as part of the current commit
        if [ "$VCS" = git ]; then
                # this will do nothing if the metadata file is unchanged.