Commit 1016ad4f98f1db7f5912c36b7922375d4c387ca0

Edward Thomson 2021-04-15T01:31:12

path: don't join paths in git_path_find_dir Let `git_path_find_dir` simply take a `git_buf` that contains a directory or a file, instead of trying to both join a path AND then deal with prettifying it or its basename. This allows consumers to join paths themselves (and apply any necessary rules - like fitting within MAX_PATH).

diff --git a/src/attr.c b/src/attr.c
index f9cd930..cab5e8b 100644
--- a/src/attr.c
+++ b/src/attr.c
@@ -523,10 +523,14 @@ static int collect_attr_files(
 		return error;
 
 	/* Resolve path in a non-bare repo */
-	if (workdir != NULL)
-		error = git_path_find_dir(&dir, path, workdir);
-	else
+	if (workdir != NULL) {
+		if (!(error = git_repository_workdir_path(&dir, repo, path)))
+			error = git_path_find_dir(&dir);
+	}
+	else {
 		error = git_path_dirname_r(&dir, path);
+	}
+
 	if (error < 0)
 		goto cleanup;
 
diff --git a/src/path.c b/src/path.c
index 50a0b46..8928e49 100644
--- a/src/path.c
+++ b/src/path.c
@@ -754,15 +754,13 @@ bool git_path_contains_file(git_buf *base, const char *file)
 	return _check_dir_contents(base, file, &git_path_isfile);
 }
 
-int git_path_find_dir(git_buf *dir, const char *path, const char *base)
+int git_path_find_dir(git_buf *dir)
 {
-	int error = git_path_join_unrooted(dir, path, base, NULL);
+	int error = 0;
+	char buf[GIT_PATH_MAX];
 
-	if (!error) {
-		char buf[GIT_PATH_MAX];
-		if (p_realpath(dir->ptr, buf) != NULL)
-			error = git_buf_sets(dir, buf);
-	}
+	if (p_realpath(dir->ptr, buf) != NULL)
+		error = git_buf_sets(dir, buf);
 
 	/* call dirname if this is not a directory */
 	if (!error) /* && git_path_isdir(dir->ptr) == false) */
diff --git a/src/path.h b/src/path.h
index 0cf2dbc..dcf5652 100644
--- a/src/path.h
+++ b/src/path.h
@@ -283,7 +283,7 @@ extern int git_path_prettify_dir(git_buf *path_out, const char *path, const char
  * appends the trailing '/'.  If the path does not exist, it is
  * treated like a regular filename.
  */
-extern int git_path_find_dir(git_buf *dir, const char *path, const char *base);
+extern int git_path_find_dir(git_buf *dir);
 
 /**
  * Resolve relative references within a path.