Forked from HackingGate/restore_last_git_modified_time.sh
Last active
December 6, 2021 05:22
-
-
Save margani/dee5287115484e6acbb2efa204d2a913 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 #git #date
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 -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 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 |
Author
margani
commented
Oct 26, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment