1 | #!/usr/local/bin/python |
---|
2 | |
---|
3 | import sys |
---|
4 | import os |
---|
5 | import re |
---|
6 | import subprocess |
---|
7 | from datetime import datetime |
---|
8 | from string import join |
---|
9 | |
---|
10 | commit_re = re.compile('^commit [a-f0-9]+') |
---|
11 | directive_re = re.compile('^\w+:\s+.*') |
---|
12 | date_re = re.compile('Date:\s+(\w+\s+\w+\s+\d+\s+\d+:\d+:\d+\s+\d+)') |
---|
13 | file_re= re.compile('^(\w.*)$') |
---|
14 | |
---|
15 | proc = subprocess.Popen(['git', 'log', '--name-only'], stdout=subprocess.PIPE) |
---|
16 | |
---|
17 | # map from filename to last modification time |
---|
18 | files = { } |
---|
19 | |
---|
20 | # Walk backward in time through log messages |
---|
21 | for l in proc.stdout: |
---|
22 | # Date line, set current date to it |
---|
23 | m = date_re.match(l) |
---|
24 | if m: |
---|
25 | d = datetime.strptime(m.group(1), '%a %b %d %H:%M:%S %Y') |
---|
26 | continue |
---|
27 | elif directive_re.match(l) or commit_re.match(l): |
---|
28 | # Ignore this stuff |
---|
29 | continue |
---|
30 | elif file_re.match(l): |
---|
31 | # Found a file. If we've never seen it, add its time to the dict |
---|
32 | f = l.strip() |
---|
33 | if f not in files: |
---|
34 | files[f] = d |
---|
35 | |
---|
36 | # Walk through the times captured above. If the file's still in the file |
---|
37 | # system, adjust its time using the touch program. This is the BSD touch |
---|
38 | # syntax. If there is a problem, the touch error will go to stderr and we'll |
---|
39 | # print a message. |
---|
40 | for f, d in files.items(): |
---|
41 | if os.access(f, os.F_OK): |
---|
42 | cmd = [ 'touch', '-t', d.strftime('%Y%m%d%H%M.%S'), f] |
---|
43 | rv = subprocess.call(cmd) |
---|
44 | if rv != 0: |
---|
45 | print "%s returned %d" % (join(cmd), rv) |
---|