Skip to content

Commit 80d9e2e

Browse files
committed
feat(git-utimes): add ability to update directory timestamp
New flag `--touch-dirs` or `-d` allows to update directory timestamp when using `git utimes` command. Refs: #1249
1 parent 8d1c970 commit 80d9e2e

5 files changed

Lines changed: 70 additions & 20 deletions

File tree

Commands.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,8 @@ $ git browse-ci upstream
16241624
16251625
Change files modification time to their last commit date. Does not touch files that are in the working tree or index.
16261626
1627+
To also update the timestamps for directories, use the `--touch-dirs` or `-d` flag.
1628+
16271629
The `--newer` flag preserves the original modification time of files that were committed from the local repo, by only touching files that are newer than their last commit date.
16281630
16291631
```bash

bin/git-utimes

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,30 @@
77
set -euo pipefail
88
IFS=$'\n\t'
99

10-
if [[ "${1:-}" == "--newer" ]]; then
11-
op=le
12-
shift
13-
else
14-
op=eq
15-
fi
10+
# %ct: committer date, UNIX timestamp / %at: author date, UNIX timestamp
11+
# --diff-filter=d: exclude deleted files
12+
log_opts=(
13+
--format='%ct'
14+
--diff-filter=d
15+
)
16+
op=eq
17+
18+
for arg in "$@"; do
19+
case "$arg" in
20+
--newer)
21+
op=le
22+
;;
23+
--touch-dirs | -d)
24+
log_opts+=(--dirstat=files,0)
25+
;;
26+
*)
27+
>&2 echo "unknown argument $arg found"
28+
exit 1
29+
;;
30+
esac
31+
32+
shift
33+
done
1634

1735
# BSD systems
1836
if date -j &>/dev/null; then
@@ -33,8 +51,6 @@ if bash --help 2>&1 | grep -q -- '--norc'; then
3351
fi
3452

3553
status_opts=(--porcelain --short)
36-
# %ct: committer date, UNIX timestamp / %at: author date, UNIX timestamp
37-
log_opts=(--format='%ct')
3854
if git status --help 2>&1 | grep -q -- "--no-renames"; then
3955
status_opts+=(--no-renames)
4056
log_opts+=(--no-renames)
@@ -91,13 +107,17 @@ FILENAME==tmpfile {
91107
}
92108
# skip blank lines
93109
!/^$/ {
94-
# skip deletes
95-
if (substr($1, length($1), 1) ~ /D/) {
96-
next
97-
}
98110
if (NF == 1) {
99-
ct=$1
100-
next
111+
# dirstat line
112+
if ($1 ~ /^.+% .+\/$/) {
113+
# remove percentage before path
114+
gsub(/^.+% /, "", $1)
115+
$2 = $1
116+
# timestamp line
117+
} else {
118+
ct=$1
119+
next
120+
}
101121
}
102122
$2 = substr($2, strip, length($2)- strip + 1)
103123
if ($2 in seen) {

man/git-utimes.1

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
.\" generated with Ronn-NG/v0.9.1
2-
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
3-
.TH "GIT\-UTIMES" "1" "May 2022" "" "Git Extras"
1+
.\" generated with Ronn-NG/v0.10.1
2+
.\" http://github.com/apjanke/ronn-ng/tree/0.10.1
3+
.TH "GIT\-UTIMES" "1" "January 1980" "" "Git Extras"
44
.SH "NAME"
55
\fBgit\-utimes\fR \- Change files modification time to their last commit date
66
.SH "SYNOPSIS"
@@ -11,6 +11,10 @@ Change files modification time to their last commit date\. Does not touch files
1111
\-\-newer
1212
.P
1313
Preserves the original modification time of files that were committed from the local repo, by only touching files that are newer than their last commit date\.
14+
.P
15+
\-\-touch\-dirs or \-d
16+
.P
17+
Also update the modification time of directories to the latest commit date of any file within them\. This can be useful for build systems that rely on directory timestamps\.
1418
.SH "EXAMPLES"
1519
Update all files' modification time to their last commit date, except those in working tree or index:
1620
.IP "" 4
@@ -25,6 +29,13 @@ As above, but preserve original modification time of files that were committed f
2529
$ git utimes \-\-newer
2630
.fi
2731
.IP "" 0
32+
.P
33+
Update all files' and directories' modification time to their last commit date, except those in working tree or index:
34+
.IP "" 4
35+
.nf
36+
$ git utimes \-\-touch\-dirs
37+
.fi
38+
.IP "" 0
2839
.SH "AUTHOR"
2940
Written by Vitaly Chikunov <\fIvt@altlinux\.org\fR>, inspired by Stackexchange comments\. Updated by Bill Wood <\fIwpwoodjr@gmail\.com\fR> to add \fB\-\-newer\fR flag and ignore files in the working tree or index\.
3041
.SH "REPORTING BUGS"

man/git-utimes.html

Lines changed: 12 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/git-utimes.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ git-utimes(1) -- Change files modification time to their last commit date
1414
--newer
1515

1616
Preserves the original modification time of files that were committed from the local repo, by only touching files that are newer than their last commit date.
17+
18+
--touch-dirs or -d
19+
20+
Also update the modification time of directories to the latest commit date of any file within them. This can be useful for build systems that rely on directory timestamps.
1721

1822
## EXAMPLES
1923

@@ -24,6 +28,10 @@ Update all files' modification time to their last commit date, except those in w
2428
As above, but preserve original modification time of files that were committed from local repo:
2529

2630
$ git utimes --newer
31+
32+
Update all files' and directories' modification time to their last commit date, except those in working tree or index:
33+
34+
$ git utimes --touch-dirs
2735

2836
## AUTHOR
2937

0 commit comments

Comments
 (0)