diff --git a/.github/scripts/fix-mtime b/.github/scripts/fix-mtime index 5c8983e..401506e 100755 --- a/.github/scripts/fix-mtime +++ b/.github/scripts/fix-mtime @@ -1,7 +1,31 @@ -#!/usr/bin/env sh +#!/bin/sh -e -git ls-tree -r --name-only HEAD | while read filename; do - unixtime=$(git log -1 --format="%at" -- "${filename}") - touchtime=$(date -d @$unixtime +'%Y%m%d%H%M.%S') - touch -t ${touchtime} "${filename}" +OS=${OS:-$(uname)} + +if [ "$OS" = 'Darwin' ]; then + get_touch_time() { + date -r "${unixtime}" +'%Y%m%d%H%M.%S' + } +else + # default Linux + get_touch_time() { + date -d @"${unixtime}" +'%Y%m%d%H%M.%S' + } +fi + +# all git files +git ls-tree -r --name-only HEAD >.git_ls-tree_r_name-only_HEAD + +# modified git files +git diff --name-only >.git_diff_name-only + +# minus https://stackoverflow.com/questions/1370996/minus-operation-on-two-files-using-linux-commands +# only restore files not modified +comm -2 -3 .git_ls-tree_r_name-only_HEAD .git_diff_name-only | while read -r filename; do + unixtime=$(git log -1 --format="%at" -- "${filename}") + touchtime=$(get_touch_time) + echo "${touchtime}" "${filename}" + touch -t "${touchtime}" "${filename}" done + +rm .git_ls-tree_r_name-only_HEAD .git_diff_name-only