Commit e7de893ef899705f7b7e2d9894a7fda147b6d091

Axel Rasmussen 2015-06-01T13:43:54

cmake: add USE_NSEC, and only check nanosec m/ctime if enabled

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 713640d..5790a03 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -37,6 +37,7 @@ OPTION( LIBGIT2_FILENAME	"Name of the produced binary"			OFF )
 OPTION( USE_ICONV			"Link with and use iconv library" 		OFF )
 OPTION( USE_SSH				"Link with libssh to enable SSH support" ON )
 OPTION( USE_GSSAPI			"Link with libgssapi for SPNEGO auth"   OFF )
+OPTION( USE_NSEC			"Care about sub-second file mtimes and ctimes"	OFF )
 OPTION( VALGRIND			"Configure build for valgrind"			OFF )
 OPTION( CURL			"User curl for HTTP if available" ON)
 
@@ -500,6 +501,10 @@ IF (THREADSAFE)
 	ADD_DEFINITIONS(-DGIT_THREADS)
 ENDIF()
 
+IF (USE_NSEC)
+	ADD_DEFINITIONS(-DGIT_USE_NSEC)
+ENDIF()
+
 ADD_DEFINITIONS(-D_FILE_OFFSET_BITS=64)
 
 # Collect sourcefiles
diff --git a/src/diff.c b/src/diff.c
index d97dcd9..b2259c7 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -835,7 +835,11 @@ static int maybe_modified(
 	 */
 	} else if (git_oid_iszero(&nitem->id) && new_is_workdir) {
 		bool use_ctime = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_CTIME) != 0);
+#ifdef GIT_USE_NSEC
 		bool use_nanos = ((diff->diffcaps & GIT_DIFFCAPS_TRUST_NANOSECS) != 0);
+#else
+		bool use_nanos = false;
+#endif
 		git_index *index;
 		git_iterator_index(&index, info->new_iter);
 
diff --git a/src/index.c b/src/index.c
index 2e89347..54a8b1e 100644
--- a/src/index.c
+++ b/src/index.c
@@ -858,8 +858,11 @@ void git_index_entry__init_from_stat(
 {
 	entry->ctime.seconds = (git_time_t)st->st_ctime;
 	entry->mtime.seconds = (git_time_t)st->st_mtime;
-	/* entry->mtime.nanoseconds = st->st_mtimensec; */
-	/* entry->ctime.nanoseconds = st->st_ctimensec; */
+#if !defined(GIT_WIN32) && !defined(__APPLE__)
+	/* Apple and Windows doesn't provide these struct stat fields. */
+	entry->mtime.nanoseconds = st->st_mtim.tv_nsec;
+	entry->ctime.nanoseconds = st->st_ctim.tv_nsec;
+#endif
 	entry->dev  = st->st_rdev;
 	entry->ino  = st->st_ino;
 	entry->mode = (!trust_mode && S_ISREG(st->st_mode)) ?
diff --git a/tests/merge/workdir/dirty.c b/tests/merge/workdir/dirty.c
index 4bf984c..ed402bd 100644
--- a/tests/merge/workdir/dirty.c
+++ b/tests/merge/workdir/dirty.c
@@ -163,9 +163,15 @@ static void hack_index(char *files[])
 		cl_git_pass(p_stat(path.ptr, &statbuf));
 
 		entry->ctime.seconds = (git_time_t)statbuf.st_ctime;
-		entry->ctime.nanoseconds = 0;
 		entry->mtime.seconds = (git_time_t)statbuf.st_mtime;
+#if !defined(GIT_WIN32) && !defined(__APPLE__)
+		/* Apple and Windows doesn't provide these struct stat fields. */
+		entry->ctime.nanoseconds = statbuf.st_ctim.tv_nsec;
+		entry->mtime.nanoseconds = statbuf.st_mtim.tv_nsec;
+#else
+		entry->ctime.nanoseconds = 0;
 		entry->mtime.nanoseconds = 0;
+#endif
 		entry->dev = statbuf.st_dev;
 		entry->ino = statbuf.st_ino;
 		entry->uid  = statbuf.st_uid;