summaryrefslogtreecommitdiff
path: root/set_mtimes
blob: 7157e7f5f371534e83e57912eace1de6ab9de9b1 (plain)
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
#!/bin/sh

# Set the checked-out files' mtimes according to their last Git revision.

# Written by Thomas Schwinge <tschwinge@gnu.org>

trap '
  if [ x"$tmp_dir" = x ]; then :; else
    rm -rf -- "$tmp_dir"
  fi
' EXIT &&

# TODO: handle arguments meaning to only process a subset (directories / files)
# of the repository.
if [ x"$#" = x0 ]; then :; else
  echo >&2 No command line arguments expected.
  exit 1
fi &&

tmp_dir=$(mktemp -d) &&

tmp_ignore=$tmp_dir/ignore &&
# TODO: have to add more flags?
git ls-files \
  > "$tmp_ignore" \
  -d -m &&
while read file; do
  echo >&2 "*** WARNING: file <$file> locally changed or deleted, not touching"
done < "$tmp_ignore" &&

tmp_known=$tmp_dir/known &&
git ls-files \
  > "$tmp_known" \
  -c &&

tmp_consider=$tmp_dir/consider &&
grep \
  < "$tmp_known" \
  > "$tmp_consider" \
  -f "$tmp_ignore" -x -v &&

while read file; do
  # TODO: use %ci?  TODO: can we optimize this to not have to invoke git log
  # individually for every single file?
  date_git=$(git log -1 --pretty=format:%ai -- "$file") &&
  date_git=$(date --rfc-3339=ns -d "$date_git") &&
  date_file=$(date --rfc-3339=ns -r "$file") &&
  if [ x"$date_git" = x"$date_file" ]; then :; else
    echo >&2 "*** INFO: file $file: mtime <$date_file> -> <$date_git>"
    touch -m -d "$date_git" "$file"
  fi \
  || {
    echo >&2 "*** ERROR: file <$file>, date_git <$date_git>, date_file <$date_file>"
    exit 1
  }
done < "$tmp_consider"