git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: Junio C Hamano <junkio@cox.net>
Cc: git@vger.kernel.org, Eric Wong <normalperson@yhbt.net>
Subject: [PATCH] Add test-chmtime: a utility to change mtime on files
Date: Sat, 24 Feb 2007 16:59:51 -0800	[thread overview]
Message-ID: <11723651923476-git-send-email-normalperson@yhbt.net> (raw)

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_1> [<file_2>]"

  '+' increments the mtime on the file by <seconds>
  '-' decrements the mtime on the file by <seconds>
  '=' sets the mtime on the file to exactly <seconds>

Signed-off-by: Eric Wong <normalperson@yhbt.net>
---
 .gitignore     |    1 +
 Makefile       |    4 +++-
 test-chmtime.c |   48 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 1 deletions(-)
 create mode 100644 test-chmtime.c

diff --git a/.gitignore b/.gitignore
index f15155d..eb8a1f8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -139,6 +139,7 @@ git-whatchanged
 git-write-tree
 git-core-*/?*
 gitweb/gitweb.cgi
+test-chmtime
 test-date
 test-delta
 test-dump-cache-tree
diff --git a/Makefile b/Makefile
index e51b448..105f3ec 100644
--- a/Makefile
+++ b/Makefile
@@ -829,7 +829,7 @@ GIT-CFLAGS: .FORCE-GIT-CFLAGS
 
 export NO_SVN_TESTS
 
-test: all
+test: all test-chmtime$X
 	$(MAKE) -C t/ all
 
 test-date$X: test-date.c date.o ctype.o
@@ -844,6 +844,8 @@ test-dump-cache-tree$X: dump-cache-tree.o $(GITLIBS)
 test-sha1$X: test-sha1.o $(GITLIBS)
 	$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
 
+test-chmtime$X: test-chmtime.o
+
 check-sha1:: test-sha1$X
 	./test-sha1.sh
 
diff --git a/test-chmtime.c b/test-chmtime.c
new file mode 100644
index 0000000..69b3ba9
--- /dev/null
+++ b/test-chmtime.c
@@ -0,0 +1,48 @@
+#include "git-compat-util.h"
+#include <utime.h>
+
+static const char usage_str[] = "(+|=|-)<seconds> <file_1> [<file_2>]";
+
+int main(int argc, const char *argv[])
+{
+	int i;
+	int set_eq;
+	long int set_time;
+	char *test;
+
+	if (argc < 3)
+		goto usage;
+
+	set_eq = (argv[1][0] == '=') ? 1 : 0;
+	set_time = strtol(argv[1] + set_eq, &test, 10);
+	if (*test) {
+		fprintf(stderr, "Not a base-10 integer: %s\n", argv[1] + 1);
+		goto usage;
+	}
+
+	for (i = 2; i < argc; i++) {
+		struct stat sb;
+		struct utimbuf utb;
+
+		if (stat(argv[i], &sb) < 0) {
+			fprintf(stderr, "Failed to stat %s: %s\n",
+			        argv[i], strerror(errno));
+			return -1;
+		}
+
+		utb.actime = sb.st_atime;
+		utb.modtime = set_eq ? set_time : sb.st_mtime + set_time;
+
+		if (utime(argv[i], &utb) < 0) {
+			fprintf(stderr, "Failed to modify time on %s: %s\n",
+			        argv[i], strerror(errno));
+			return -1;
+		}
+	}
+
+	return 0;
+
+usage:
+	fprintf(stderr, "Usage: %s %s\n", argv[0], usage_str);
+	return -1;
+}
-- 
1.5.0.137.ge6502

             reply	other threads:[~2007-02-25  0:59 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-25  0:59 Eric Wong [this message]
2007-02-25  0:59 ` [PATCH] Update tests to use test-chmtime Eric Wong
2007-02-25  7:09   ` Junio C Hamano
2007-02-25 15:20     ` Johannes Schindelin
2007-02-25 19:23     ` Eric Wong
2007-02-25 19:33       ` Junio C Hamano
2007-02-25  1:27 ` [PATCH] Add test-chmtime: a utility to change mtime on files Johannes Schindelin
2007-02-25  2:15   ` Eric Wong
2007-02-25  2:18 ` [PATCH (try 2)] " Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11723651923476-git-send-email-normalperson@yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=git@vger.kernel.org \
    --cc=junkio@cox.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).