Remix.run Logo
lioeters 3 days ago

Comparison with Bash, from https://github.com/modernish/modernish/blob/master/EXAMPLES....

# Plain POSIX sh

    #! /bin/sh
    git status >/dev/null || exit
    if ! git diff-index --quiet HEAD; then
        echo 'Working dir not clean' >&2
        exit 1
    fi

    find . -name .git -prune \
    -o -exec sh -c '
        # Ask Git for latest commit'\''s timestamp,
        # formatted for POSIX '\''touch -t'\''.
        timestamp=$(git log --format=%cd \
          --date=format:%Y%m%d%H%M.%S \
          -1 HEAD -- "$1") || exit
        [ -n "$timestamp" ] || exit

        set -x
        touch -t "$timestamp" "$1"
    ' dummy {} \;

# Modernish

    #! /usr/bin/env modernish
    #! use safe
    #! use sys/cmd/harden
    #! use var/loop
    harden git
    harden -e '>1' -f wd_is_clean \
        git diff-index --quiet HEAD
    harden -pt touch

    git status >/dev/null
    wd_is_clean || exit 1 'Working dir not clean'

    total=0
    LOOP find repofile in . -name .git -prune \
    -or -iterate; DO
        # Ask Git for latest commit's timestamp,
        # formatted for POSIX 'touch -t'.
        timestamp=$(git log --format=%cd \
          --date=format:%Y%m%d%H%M.%S \
          -1 HEAD -- $repofile)
        str empty $timestamp && continue

        # 'touch' is traced by 'harden -t'.
        touch -t $timestamp $repofile
        let "total+=1"
    DONE
    exit 0 "$total timestamps restored."