Forked from HackingGate/restore_last_git_modified_time.sh
Last active
November 25, 2020 17:43
-
-
Save biaocy/a1b423e60a72d5d9c579d52534fb532f to your computer and use it in GitHub Desktop.
Retrieve and set the last modification date of all files in a git repository. Solution for https://stackoverflow.com/a/55609950/4063462
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh -e | |
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 -z -r --name-only HEAD > .git_ls-tree_r_name-only_HEAD | |
# modified git files | |
git diff -z --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 -z -2 -3 .git_ls-tree_r_name-only_HEAD .git_diff_name-only | while read 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment