Skip to content

Instantly share code, notes, and snippets.

@goncalomb
Created January 19, 2022 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goncalomb/a71fd8581c330e4e2f86a3c15af1e3f7 to your computer and use it in GitHub Desktop.
Save goncalomb/a71fd8581c330e4e2f86a3c15af1e3f7 to your computer and use it in GitHub Desktop.
Save file modification times on git repository.
#!/bin/bash
set -e
# check if is valid repo
git rev-parse HEAD >/dev/null
# cd to top level
cd -- "$(git rev-parse --show-toplevel)"
list_all_files() {
# wierd way to get all files (that are not ignored by .mtimeignore)
D=
{
git ls-files --exclude-per-directory .mtimeignore -x .mtimeignore -x .gitignore -zci
git ls-files --exclude-standard -zci
} | sort -z | comm -z23 <(git ls-files -zc | sort -z) - | while IFS= read -rd $'\0' F; do
# add directories
DIR=$(dirname -- "$F")
if [ "$DIR" != "$D" ]; then
echo -n "$DIR"
echo -ne "\x00"
D=$DIR
fi
echo -n "$F"
echo -ne "\x00"
done | sort -z | uniq -z
}
cmd_save() {
{
echo "# git-mtime v1.0"
echo "# by goncalomb"
echo "# https://github.com/goncalomb/dotfiles"
list_all_files | while IFS= read -rd $'\0' F; do
echo "touch -cmd @$(stat -c %.Y "$F") $(printf "%q" "$F")"
done
} >.mtimes
}
cmd_touch() {
[ ! -f .mtimes ] && echo "'.mtimes' file not found" && exit 1
[ "$(head -n 1 .mtimes)" != "# git-mtime v1.0" ] && echo "'.mtimes' file is invalid" && exit 1
bash .mtimes
}
case "$1" in
save) cmd_save ;;
touch) cmd_touch ;;
*)
echo "usage: ${0##*/} save"
echo " ${0##*/} touch"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment