Created
April 25, 2022 21:02
-
-
Save huntfx/ebf35d366d977371b01e8704a7309ac1 to your computer and use it in GitHub Desktop.
Use past commits to restore the modified date on files
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
# Very small changes to an existing script, copying here for personal use | |
# Run file from next to the .git folder | |
import subprocess, shlex | |
import sys, os.path | |
os.chdir(os.path.dirname(__file__)) | |
filelist = set() | |
for path in (sys.argv[1:] or [os.getcwd()]): | |
if os.path.isfile(path) or os.path.islink(path): | |
filelist.add(os.path.relpath(path)) | |
elif os.path.isdir(path): | |
for root, subdirs, files in os.walk(path): | |
if '.git' in subdirs: | |
subdirs.remove('.git') | |
for file in files: | |
filelist.add(os.path.relpath(os.path.join(root, file))) | |
mtime = 0 | |
gitobj = subprocess.Popen(shlex.split('git whatchanged --pretty=%at'), | |
stdout=subprocess.PIPE) | |
for line in gitobj.stdout: | |
line = line.decode('ascii').strip() | |
if not line: continue | |
if line.startswith(':'): | |
file = os.path.normpath(line.split('\t')[-1]) | |
if file in filelist: | |
filelist.remove(file) | |
print(f'Setting date on {file}...') | |
os.utime(file, (mtime, mtime)) | |
else: | |
mtime = int(line) | |
# All files done? | |
if not filelist: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment