|
1 |
| -#!/usr/bin/perl |
2 |
| -use strict; |
3 |
| -use warnings; |
4 |
| - |
5 |
| -# Sets mtime and atime of files to the latest commit time in git. |
6 |
| -# |
7 |
| -# This is useful after the first clone of the rsync repository BEFORE you |
8 |
| -# do any building. It is also safe if you have done a "make distclean". |
9 |
| - |
10 |
| -my %ls; |
11 |
| -my $commit_time; |
12 |
| -my $prefix = @ARGV && $ARGV[0] =~ s/^--prefix=// ? shift : ''; |
13 |
| - |
14 |
| -my $top_dir = `git rev-parse --show-toplevel`; |
15 |
| -exit 1 unless $top_dir; |
16 |
| -chomp($top_dir); |
17 |
| - |
18 |
| -chdir $top_dir or die "Failed to chdir to $top_dir\: $!\n"; |
19 |
| - |
20 |
| -$/ = "\0"; |
21 |
| -open FH, '-|', qw( git ls-files -z ) or die "Failed to fork: $!"; |
22 |
| -while (<FH>) { |
23 |
| - chomp; |
24 |
| - $ls{$_} = $_; |
25 |
| -} |
26 |
| -close FH; |
27 |
| - |
28 |
| -$/ = "\n"; |
29 |
| -open FH, '-|', qw( git log -r --name-only --no-color --pretty=raw -z ), @ARGV or die "Failed to fork: $!"; |
30 |
| -while (<FH>) { |
31 |
| - chomp; |
32 |
| - if (/^committer .*? (\d+) (?:[\-\+]\d+)$/) { |
33 |
| - $commit_time = $1; |
34 |
| - } elsif (s/\0\0commit [a-f0-9]{40}$// || s/\0$//) { |
35 |
| - my @files = delete @ls{split(/\0/, $_)}; |
36 |
| - @files = grep { defined $_ } @files; |
37 |
| - next unless @files; |
38 |
| - map { s/^/$prefix/ } @files; |
39 |
| - utime $commit_time, $commit_time, @files; |
40 |
| - } |
41 |
| - last unless %ls; |
42 |
| -} |
43 |
| -close FH; |
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +import os, re, argparse, subprocess |
| 4 | +from datetime import datetime |
| 5 | + |
| 6 | +NULL_COMMIT_RE = re.compile(r'\0\0commit [a-f0-9]{40}$|\0$') |
| 7 | + |
| 8 | +def main(): |
| 9 | + if not args.git_dir: |
| 10 | + cmd = 'git rev-parse --show-toplevel 2>/dev/null || echo .' |
| 11 | + top_dir = subprocess.check_output(cmd, shell=True).decode('utf-8').strip() |
| 12 | + args.git_dir = os.path.join(top_dir, '.git') |
| 13 | + if not args.prefix: |
| 14 | + os.chdir(top_dir) |
| 15 | + |
| 16 | + git = [ 'git', '--git-dir=' + args.git_dir ] |
| 17 | + |
| 18 | + if args.tree: |
| 19 | + cmd = git + 'ls-tree -z -r --name-only'.split() + [ args.tree ] |
| 20 | + else: |
| 21 | + cmd = git + 'ls-files -z'.split() |
| 22 | + |
| 23 | + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 24 | + out = proc.communicate()[0].decode('utf-8') |
| 25 | + ls = set(out.split('\0')) |
| 26 | + ls.discard('') |
| 27 | + |
| 28 | + cmd = git + 'log -r --name-only --no-color --pretty=raw --no-renames -z'.split() |
| 29 | + if args.tree: |
| 30 | + cmd.append(args.tree) |
| 31 | + cmd += ['--'] + args.files |
| 32 | + |
| 33 | + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) |
| 34 | + for line in proc.stdout: |
| 35 | + line = line.decode('utf-8').strip() |
| 36 | + m = re.match(r'^committer .*? (\d+) [-+]\d+$', line) |
| 37 | + if m: |
| 38 | + commit_time = int(m[1]) |
| 39 | + elif NULL_COMMIT_RE.search(line): |
| 40 | + line = NULL_COMMIT_RE.sub('', line) |
| 41 | + files = set(fn for fn in line.split('\0') if fn in ls) |
| 42 | + if not files: |
| 43 | + continue |
| 44 | + for fn in files: |
| 45 | + if args.prefix: |
| 46 | + fn = args.prefix + fn |
| 47 | + mtime = os.lstat(fn).st_mtime |
| 48 | + if args.list: |
| 49 | + if args.list > 1: |
| 50 | + ts = str(commit_time).rjust(10) |
| 51 | + else: |
| 52 | + ts = datetime.utcfromtimestamp(commit_time).strftime("%Y-%m-%d %H:%M:%S") |
| 53 | + chg = '.' if mtime == commit_time else '*' |
| 54 | + print(chg, ts, fn) |
| 55 | + elif mtime != commit_time: |
| 56 | + if not args.quiet: |
| 57 | + print(f"Setting {fn}") |
| 58 | + os.utime(fn, (commit_time, commit_time), follow_symlinks = False) |
| 59 | + ls -= files |
| 60 | + if not ls: |
| 61 | + break |
| 62 | + proc.communicate() |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == '__main__': |
| 66 | + parser = argparse.ArgumentParser(description="Set the times of the current git checkout to their last-changed time.") |
| 67 | + parser.add_argument('--git-dir', metavar='GIT_DIR', help="The git dir to query (defaults to affecting the current git checkout).") |
| 68 | + parser.add_argument('--tree', metavar='TREE-ISH', help="The tree-ish to query (defaults to the current branch).") |
| 69 | + parser.add_argument('--prefix', metavar='PREFIX_STR', help="Prepend the PREFIX_STR to each filename we tweak.") |
| 70 | + parser.add_argument('--quiet', '-q', action='store_true', help="Don't output the changed-file information.") |
| 71 | + parser.add_argument('--list', '-l', action='count', help="List the files and their dates instead of changing them. Repeat for Unix Time instead of human reable.") |
| 72 | + parser.add_argument('files', metavar='FILE', nargs='*', help="Specify a subset of checked-out files to tweak.") |
| 73 | + args = parser.parse_args() |
| 74 | + main() |
| 75 | + |
| 76 | +# vim: sw=4 et |
0 commit comments