-
-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathapply-commit-times.sh
More file actions
executable file
·59 lines (53 loc) · 1.84 KB
/
Copy pathapply-commit-times.sh
File metadata and controls
executable file
·59 lines (53 loc) · 1.84 KB
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
57
58
59
#!/bin/sh
#
# Sets the modification times of the given files and the files in the given
# directories to their respective git commit timestamps.
#
# Usage: ./apply-commit-times.sh directory|file [...]
#
# Copyright 2017, Sebastian Tschan
# https://blueimp.net
#
# Licensed under the MIT license:
# https://opensource.org/licenses/MIT
#
set -e
# Set the timezone to UTC so `git log` and `touch` do not diverge:
export TZ=UTC0
apply_commit_time() {
# Extract the commit date for the file in `touch -t` format (CCYYMMDDhhmm.ss):
timestamp=$(git log -1 --format=%cd --date=format-local:%Y%m%d%H%M.%S -- "$1")
if [ -n "$timestamp" ]; then
# Set the modification time of the given file to the commit timestamp:
touch -t "$timestamp" "$1"
fi
}
# Check if the shell supports the "-d" option for the `read` built-in:
# shellcheck disable=SC2039
if printf '%s\0' 1 2 | read -r -d '' 2>/dev/null; then
iterate() {
# Disable the internal field separator (IFS) and iterate over the null byte
# separated file paths using the "-d" option of the `read` built-in:
while IFS= read -r -d '' FILE; do apply_commit_time "$FILE"; done
}
else
iterate() {
# Transform the null byte separated files paths into command-line arguments
# to the script itself via `xargs`.
# The system-defined command-line arguments constraints will limit the
# number of files that can be processed for a given directory, which should
# be below the number defined via "-n" option for xargs:
xargs -0 -n 100000 "$0"
}
fi
while [ $# -gt 0 ]; do
# Is the argument a directory path?
if [ -d "$1" ]; then
# The "-z" option of `git ls-tree` outputs null byte separated file paths:
git ls-tree -r -z --name-only HEAD -- "$1" | iterate
# Else is the argument a path to a readable file?
elif [ -r "$1" ]; then
apply_commit_time "$1"
fi
shift
done