-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestore_commit_times.py
More file actions
83 lines (62 loc) · 2.1 KB
/
Copy pathrestore_commit_times.py
File metadata and controls
83 lines (62 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# encoding: utf-8
"""Restore files mtime from Git commit times
Usage: %s [-q] <repo1> [<repo2>]
"""
import logging
import os
import sys
import git
log = logging.getLogger(__name__)
def restore_commit_times(path):
log.debug("Restoring file mtimes for path: %s", path)
def walk(tree):
ret = list()
for i in tree:
ret.append(i)
if i.type == 'tree':
ret.extend(walk(i))
return ret
repo = git.Repo(path)
def find_mtimes(repo):
objects = walk(repo.tree())
t = repo.head.commit
tt = t.traverse()
ret = {}
while objects:
hashes = set(i.binsha for i in walk(t.tree))
# iterate over reversed list to be able to remove elements by index
for n, i in reversed(list(enumerate(objects))):
if i.binsha not in hashes:
del objects[n]
else:
if i.path not in ret or t.authored_date < ret[i.path]:
ret[i.path] = t.authored_date
try:
t = next(tt)
except StopIteration:
break
return ret
for i, mtime in find_mtimes(repo).items():
fname = os.path.join(path, i)
log.debug("%s %s", mtime, fname)
os.utime(fname, (mtime, mtime), follow_symlinks=False)
for sm in repo.submodules:
sm_repo = git.Repo(os.path.join(repo.git_dir, 'modules', sm.name))
for i, mtime in find_mtimes(sm_repo).items():
fname = os.path.join(path, sm.path, i)
log.debug("%s %s", mtime, fname)
os.utime(fname, (mtime, mtime), follow_symlinks=False)
def main():
if '-q' in sys.argv:
logging.basicConfig(level=logging.INFO, format="%(message)s")
sys.argv.remove('-q')
else:
logging.basicConfig(level=logging.DEBUG, format="%(message)s")
if len(sys.argv) < 2:
sys.stderr.write(__doc__ % sys.argv[0])
return 1
for path in sys.argv[1:]:
restore_commit_times(path)
if __name__ == "__main__":
main()