Commit 26a98ec8a2ebd6d54ad12466552442fde1c6a9d0

unknown 2011-06-03T20:47:56

Fileops: Added a fourth argument to the path prettifying functions to use an alternate basepath. Fixed a Windows TO-DO in the prettifying functions.

diff --git a/src/fileops.c b/src/fileops.c
index da4e97e..409d1cb 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -402,7 +402,7 @@ static int retrieve_previous_path_component_start(const char *path)
 	return offset;
 }
 
-int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path)
+int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path, const char *base_path)
 {
 	int len = 0, segment_len, only_dots, root_path_offset, error = GIT_SUCCESS;
 	char *current;
@@ -414,9 +414,18 @@ int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path)
 
 	root_path_offset = gitfo_retrieve_path_root_offset(path);
 	if (root_path_offset < 0) {
-		error = gitfo_getcwd(buffer_out, size);
-		if (error < GIT_SUCCESS)
-			return error;	/* The callee already takes care of setting the correct error message. */
+		if (base_path == NULL) {
+			error = gitfo_getcwd(buffer_out, size);
+			if (error < GIT_SUCCESS)
+				return error;	/* The callee already takes care of setting the correct error message. */
+		} else {
+			if (size < (strlen(base_path) + 1) * sizeof(char))
+				return git__throw(GIT_EOVERFLOW, "Failed to prettify dir path: the base path is too long for the buffer.");
+
+			strcpy(buffer_out, base_path);
+			posixify_path(buffer_out);
+			git__joinpath(buffer_out, buffer_out, "");
+		}
 
 		len = strlen(buffer_out);
 		buffer_out += len;
@@ -480,9 +489,9 @@ int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path)
 	return GIT_SUCCESS;
 }
 
-int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path)
+int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path, const char *base_path)
 {
-	int error, path_len, i;
+	int error, path_len, i, root_offset;
 	const char* pattern = "/..";
 
 	path_len = strlen(path);
@@ -497,12 +506,13 @@ int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path)
 			return git__throw(GIT_EINVALIDPATH, "Failed to normalize file path `%s`. The path points to a folder", path);
 	}
 
-	error =  gitfo_prettify_dir_path(buffer_out, size, path);
+	error =  gitfo_prettify_dir_path(buffer_out, size, path, base_path);
 	if (error < GIT_SUCCESS)
 		return error;	/* The callee already takes care of setting the correct error message. */
 
 	path_len = strlen(buffer_out);
-	if (path_len < 2)	/* TODO: Fixme. We should also take of detecting Windows rooted path (probably through usage of retrieve_path_root_offset) */
+	root_offset = gitfo_retrieve_path_root_offset(buffer_out) + 1;
+	if (path_len == root_offset)
 		return git__throw(GIT_EINVALIDPATH, "Failed to normalize file path `%s`. The path points to a folder", path);
 
 	/* Remove the trailing slash */
diff --git a/src/fileops.h b/src/fileops.h
index d271f31..1b70fc8 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -170,7 +170,7 @@ extern int gitfo_getcwd(char *buffer_out, size_t size);
  * - GIT_SUCCESS on success;
  * - GIT_ERROR when the input path is invalid or escapes the current directory.
  */
-int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path);
+int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path, const char *base_path);
 
 /**
  * Clean up a provided absolute or relative file path.
@@ -193,7 +193,7 @@ int gitfo_prettify_dir_path(char *buffer_out, size_t size, const char *path);
  * - GIT_SUCCESS on success;
  * - GIT_ERROR when the input path is invalid or escapes the current directory.
  */
-int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path);
+int gitfo_prettify_file_path(char *buffer_out, size_t size, const char *path, const char *base_path);
 
 int gitfo_retrieve_path_root_offset(const char *path);
 
diff --git a/src/repository.c b/src/repository.c
index 32ca8dd..76042c2 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -65,7 +65,7 @@ static int assign_repository_dirs(
 	if (git_dir == NULL)
 		return git__throw(GIT_ENOTFOUND, "Failed to open repository. Git dir not found");
 
-	error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_dir);
+	error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_dir, NULL);
 	if (error < GIT_SUCCESS)
 		return git__rethrow(error, "Failed to open repository");
 
@@ -78,7 +78,7 @@ static int assign_repository_dirs(
 	if (git_object_directory == NULL)
 		git__joinpath(path_aux, repo->path_repository, GIT_OBJECTS_DIR);
 	else {
-		error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_object_directory);
+		error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_object_directory, NULL);
 		if (error < GIT_SUCCESS)
 			return git__rethrow(error, "Failed to open repository");
 	}
@@ -92,7 +92,7 @@ static int assign_repository_dirs(
 	if (git_work_tree == NULL)
 		repo->is_bare = 1;
 	else {
-		error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_work_tree);
+		error = gitfo_prettify_dir_path(path_aux, sizeof(path_aux), git_work_tree, NULL);
 		if (error < GIT_SUCCESS)
 			return git__rethrow(error, "Failed to open repository");
 
@@ -105,7 +105,7 @@ static int assign_repository_dirs(
 		if (git_index_file == NULL)
 			git__joinpath(path_aux, repo->path_repository, GIT_INDEX_FILE);
 		else {
-			error = gitfo_prettify_file_path(path_aux, sizeof(path_aux), git_index_file);
+			error = gitfo_prettify_file_path(path_aux, sizeof(path_aux), git_index_file, NULL);
 			if (error < GIT_SUCCESS)
 				return git__rethrow(error, "Failed to open repository");
 		}
@@ -390,7 +390,7 @@ static int repo_init_find_dir(repo_init *results, const char* path)
 	char temp_path[GIT_PATH_MAX];
 	int error = GIT_SUCCESS;
 
-	error = gitfo_prettify_dir_path(temp_path, sizeof(temp_path), path);
+	error = gitfo_prettify_dir_path(temp_path, sizeof(temp_path), path, NULL);
 	if (error < GIT_SUCCESS)
 		return git__rethrow(error, "Failed to find directory to initialize repository");
 
diff --git a/tests/t00-core.c b/tests/t00-core.c
index ab9a683..9498178 100644
--- a/tests/t00-core.c
+++ b/tests/t00-core.c
@@ -151,7 +151,7 @@ BEGIN_TEST(path2, "get the latest component in a path")
 #undef TOPDIR_TEST
 END_TEST
 
-typedef int (normalize_path)(char *, size_t, const char *);
+typedef int (normalize_path)(char *, size_t, const char *, const char *);
 
 /* Assert flags */
 #define CWD_AS_PREFIX 1
@@ -168,7 +168,7 @@ static int ensure_normalized(const char *input_path, const char *expected_path, 
 	if (error < GIT_SUCCESS)
 		return error;
 
-	error = normalizer(buffer_out, sizeof(buffer_out), input_path);
+	error = normalizer(buffer_out, sizeof(buffer_out), input_path, NULL);
 	if (error < GIT_SUCCESS)
 		return error;
 
@@ -417,7 +417,7 @@ BEGIN_TEST(path7, "prevent a path which escapes the root directory from being pr
 	for (i = 0; i < number_to_escape + 1; i++)
 		git__joinpath(current_workdir, current_workdir, "../");
 
-	must_fail(gitfo_prettify_dir_path(prettified, sizeof(prettified), current_workdir));
+	must_fail(gitfo_prettify_dir_path(prettified, sizeof(prettified), current_workdir, NULL));
 END_TEST
 
 typedef struct name_data {