ocaml-core / bin / make-hgignore

#!/bin/bash

# sort(1) depends on LANG, thus we must set it here so that the user setting will not
# result in .hgignore being regenerated every time.
export LANG=C

set -e -u -o pipefail

function die {
    echo >&2 "$1"
    exit 1
}

me=$(basename "$0")
root_marker=build-and-install

# XCR smclaughlin: doesn't work for sub-repos of depth other than 1
#
# sweeks: fixed
function cd-to-root {
    if ! [ -r "$root_marker" ]; then
        if [ $(pwd) = '/' ]; then
            die "Could not find $root_marker."
        fi
        cd ..
        cd-to-root
    fi
}

root=$(cd-to-root && pwd -P)

if ! [ -d "$root/.hg" ]; then
    die "Not an hg repo: $root"
fi

hgignore_in=".hgignore.in"

root_hgignore_in="$root/$hgignore_in"

if ! [ -r $root_hgignore_in ]; then
    die "Missing $root_hgignore_in"
fi

function hg-repos {
    find . -type d \
        | grep -E '/\.hg$' \
        | while read dot_hg_dir; do
        echo $(dirname $dot_hg_dir)
    done
}
        
function all-hgignore-dot-ins {
    hg status --added --clean --modified --unknown --no-status \
        | grep -E '\.hgignore\.in$'
}

function remove-blank-lines {
    grep -v '^\s*$' || true
}

function maybe-generate-hgignore {
    hgignore=".hgignore"
    # Only generate the hgignore if there is an hgignore.in.
    if [ -r $hgignore_in ]; then
        rm -f $hgignore
        {
            cat <<EOF
# THIS FILE IS AUTOMATICALLY GENERATED BY $me.  DO NOT EDIT.
EOF
            # All sub projects inherit the root .hgignore.in
            if [ $(pwd -P) != "$root" ]; then
                cat $root_hgignore_in
            fi
            # Include my hgignore.in first.
            cat $hgignore_in
            all-hgignore-dot-ins \
                | (grep -v '^.hgignore.in$' || true) \
                | while read hgignore_in; do
                dir=$(dirname $hgignore_in)
                cat $hgignore_in \
                    | remove-blank-lines  \
                    | sed 's;^\(.*\)$;'glob:$dir'/\1;'
            done | sort
        } >$hgignore
        # We would like to prevent people from accidentally editing it, but
        # setting the file read-only causes problems with hg, so we don't do it.
        # chmod a-w $hgignore
    fi
}

cd $(hg root)
hg-repos \
    | while read repo; do
    (
        cd $repo
        maybe-generate-hgignore
    )
done
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.