Add test-chmtime: a utility to change mtime on files
This is intended to be a portable replacement for our usage of date(1), touch(1), and Perl one-liners in tests. Usage: test-chtime (+|=|-|=+|=-)<seconds> <file>..." '+' increments the mtime on the files by <seconds> '-' decrements the mtime on the files by <seconds> '=' sets the mtime on the file to exactly <seconds> '=+' and '=-' sets the mtime on the file to <seconds> after or before the current time. Signed-off-by: Eric Wong <normalperson@yhbt.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
Eric Wong committed
Feb 24, 2007 at 18:18 UTC
17e48368751ae8df7ed0332ae8ef900ce983fce6
3 files changed
+66
-1
.gitignore
+1
@@ -139,6 +139,7 @@ git-whatchanged
139
git-write-tree
140
git-core-*/?*
141
gitweb/gitweb.cgi
142
+test-chmtime
143
test-date
144
test-delta
145
test-dump-cache-tree
Makefile
+4
-1
@@ -829,7 +829,7 @@ GIT-CFLAGS: .FORCE-GIT-CFLAGS
829
830
export NO_SVN_TESTS
831
832
-test: all
832
+test: all test-chmtime$X
833
$(MAKE) -C t/ all
834
835
test-date$X: test-date.c date.o ctype.o
@@ -844,6 +844,9 @@ test-dump-cache-tree$X: dump-cache-tree.o $(GITLIBS)
844
test-sha1$X: test-sha1.o $(GITLIBS)
845
$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
846
847
+test-chmtime$X: test-chmtime.c
848
+ $(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $<
849
+
850
check-sha1:: test-sha1$X
851
./test-sha1.sh
852
test-chmtime.c
new
+61
@@ -0,0 +1,61 @@
1
+#include "git-compat-util.h"
2
+#include <utime.h>
3
+
4
+static const char usage_str[] = "(+|=|=+|=-|-)<seconds> <file>...";
5
+
6
+int main(int argc, const char *argv[])
7
+{
8
+ int i;
9
+ int set_eq;
10
+ long int set_time;
11
+ char *test;
12
+ const char *timespec;
13
+
14
+ if (argc < 3)
15
+ goto usage;
16
+
17
+ timespec = argv[1];
18
+ set_eq = (*timespec == '=') ? 1 : 0;
19
+ if (set_eq) {
20
+ timespec++;
21
+ if (*timespec == '+') {
22
+ set_eq = 2; /* relative "in the future" */
23
+ timespec++;
24
+ }
25
+ }
26
+ set_time = strtol(timespec, &test, 10);
27
+ if (*test) {
28
+ fprintf(stderr, "Not a base-10 integer: %s\n", argv[1] + 1);
29
+ goto usage;
30
+ }
31
+ if ((set_eq && set_time < 0) || set_eq == 2) {
32
+ time_t now = time(NULL);
33
+ set_time += now;
34
+ }
35
+
36
+ for (i = 2; i < argc; i++) {
37
+ struct stat sb;
38
+ struct utimbuf utb;
39
+
40
+ if (stat(argv[i], &sb) < 0) {
41
+ fprintf(stderr, "Failed to stat %s: %s\n",
42
+ argv[i], strerror(errno));
43
+ return -1;
44
+ }
45
+
46
+ utb.actime = sb.st_atime;
47
+ utb.modtime = set_eq ? set_time : sb.st_mtime + set_time;
48
+
49
+ if (utime(argv[i], &utb) < 0) {
50
+ fprintf(stderr, "Failed to modify time on %s: %s\n",
51
+ argv[i], strerror(errno));
52
+ return -1;
53
+ }
54
+ }
55
+
56
+ return 0;
57
+
58
+usage:
59
+ fprintf(stderr, "Usage: %s %s\n", argv[0], usage_str);
60
+ return -1;
61
+}