3 # Find commit date/time for every commit, list files in the commit
4 # and set the file's modification time to the date/time of the latest commit.
6 # Adapted from https://git.wiki.kernel.org/index.php/ExampleScripts#Setting_the_timestamps_of_the_files_to_the_commit_timestamp_of_the_commit_which_last_touched_them # noqa
11 git_root = subprocess.check_output(['git', 'rev-parse', '--show-toplevel'],
12 universal_newlines=True).rstrip('\n')
15 separator = '----- GIT LOG SEPARATOR -----'
17 git_log = subprocess.Popen(['git', 'log', '-m', '--first-parent',
18 '--name-status', '--no-color',
19 '--format=%s%%n%%ct' % separator],
20 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
21 universal_newlines=True)
25 # stages: 1 - start of commit, 2 - timestamp, 3 - empty line, 4 - files
27 for line in git_log.stdout:
29 if (stage in (1, 4)) and (line == separator): # Start of a commit
35 if line == separator: # Null-merge (git merge -s ours), no files
39 assert line == '', line
41 if line.startswith('A') or line.startswith('M') \
42 or line.startswith('D'):
43 filename = line.split(None, 2)[1]
44 elif line.startswith('R'):
45 filename = line.split(None, 3)[2] # renamed to
46 if line.startswith('D'):
47 if filename not in changed_files: # The file was not readded
48 deleted_files.add(filename)
49 if filename not in deleted_files and filename not in changed_files:
50 changed_files.add(filename)
51 if os.path.exists(filename):
52 os.utime(filename, (time, time))
54 raise ValueError("stage: %d, line: %s" % (stage, line))
57 git_log.stdout.close()