Commit fac66990b6ed5173ae89458f32d456458c086b1a

nulltoken 2012-06-05T13:56:44

repository: make git_repository_init() value the core.filemode config entry

diff --git a/src/repository.c b/src/repository.c
index 5120356..5ee64cd 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -655,6 +655,27 @@ static int repo_init_createhead(const char *git_dir)
 	return 0;
 }
 
+static bool is_chmod_supported(const char *file_path)
+{
+	struct stat st1, st2;
+	static int _is_supported = -1;
+
+	if (_is_supported > -1)
+		return _is_supported;
+
+	if (p_stat(file_path, &st1) < 0)
+		return false;
+
+	if (p_chmod(file_path, st1.st_mode ^ S_IXUSR) < 0)
+		return false;
+
+	if (p_stat(file_path, &st2) < 0)
+		return false;
+
+	_is_supported = (st1.st_mode != st2.st_mode);
+	return _is_supported;
+}
+
 static int repo_init_config(const char *git_dir, int is_bare)
 {
 	git_buf cfg_path = GIT_BUF_INIT;
@@ -670,13 +691,14 @@ static int repo_init_config(const char *git_dir, int is_bare)
 	if (git_buf_joinpath(&cfg_path, git_dir, GIT_CONFIG_FILENAME_INREPO) < 0)
 		return -1;
 
-	if (git_config_open_ondisk(&config, cfg_path.ptr) < 0) {
+	if (git_config_open_ondisk(&config, git_buf_cstr(&cfg_path)) < 0) {
 		git_buf_free(&cfg_path);
 		return -1;
 	}
 
 	SET_REPO_CONFIG(bool, "core.bare", is_bare);
 	SET_REPO_CONFIG(int32, "core.repositoryformatversion", GIT_REPO_VERSION);
+	SET_REPO_CONFIG(bool, "core.filemode", is_chmod_supported(git_buf_cstr(&cfg_path)));
 	/* TODO: what other defaults? */
 
 	git_buf_free(&cfg_path);
diff --git a/src/win32/msvc-compat.h b/src/win32/msvc-compat.h
index 3ef09c8..ccc091c 100644
--- a/src/win32/msvc-compat.h
+++ b/src/win32/msvc-compat.h
@@ -21,6 +21,7 @@
 /* stat: file mode type testing macros */
 #	define _S_IFLNK 0120000
 #	define S_IFLNK _S_IFLNK
+#	define S_IXUSR 00100
 
 #	define S_ISDIR(m)	(((m) & _S_IFMT) == _S_IFDIR)
 #	define S_ISREG(m)	(((m) & _S_IFMT) == _S_IFREG)
diff --git a/tests-clar/repo/init.c b/tests-clar/repo/init.c
index 7f16b5b..036eec9 100644
--- a/tests-clar/repo/init.c
+++ b/tests-clar/repo/init.c
@@ -165,3 +165,24 @@ void test_repo_init__additional_templates(void)
 
 	git_buf_free(&path);
 }
+
+void test_repo_init__detect_filemode(void)
+{
+	git_config *config;
+	int filemode;
+
+	cl_set_cleanup(&cleanup_repository, "filemode");
+	
+	cl_git_pass(git_repository_init(&_repo, "filemode/filemode.git", 1));
+	git_repository_config(&config, _repo);
+
+	cl_git_pass(git_config_get_bool(&filemode, config, "core.filemode"));
+
+#ifdef GIT_WIN32
+	cl_assert_equal_i(false, filemode);
+#else
+	cl_assert_equal_i(true, filemode);
+#endif
+
+	git_config_free(config);
+}