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 separator = '----- GIT LOG SEPARATOR -----'
13 git_log = subprocess.Popen(['git', 'log', '-m', '--first-parent',
14 '--name-only', '--no-color',
15 '--format=%s%%n%%ct' % separator],
16 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
17 universal_newlines=True)
19 # stages: 1 - start of commit, 2 - timestamp, 3 - empty line, 4 - files
22 line = git_log.stdout.readline()
26 if (stage in (1, 4)) and (line == separator): # Start of a commit
32 if line == separator: # Null-merge (git merge -s ours), no files
36 assert line == '', line
39 if filename not in filenames:
40 filenames.add(filename)
41 if os.path.exists(filename):
42 os.utime(filename, (time, time))
44 raise ValueError("stage: %d, line: %s" % (stage, line))
47 git_log.stdout.close()