Commit 7839bc15b2c146222489bd8d61a1dede43fecbbf

Stefan Sperling 2019-01-06T10:42:56

make got_repo_get_path() return const char *

diff --git a/got/got.c b/got/got.c
index 40d1c25..bb5861b 100644
--- a/got/got.c
+++ b/got/got.c
@@ -422,7 +422,6 @@ cmd_update(int argc, char *argv[])
 	struct got_repository *repo = NULL;
 	struct got_worktree *worktree = NULL;
 	char *worktree_path = NULL;
-	char *repo_path = NULL;
 	struct got_object_id *commit_id = NULL;
 	char *commit_id_str = NULL;
 	int ch;
@@ -471,13 +470,7 @@ cmd_update(int argc, char *argv[])
 	if (error != NULL)
 		goto done;
 
-	repo_path = got_repo_get_path(repo);
-	if (repo_path == NULL) {
-		error = got_error_from_errno();
-		goto done;
-	}
-
-	error = apply_unveil(repo_path, worktree_path);
+	error = apply_unveil(got_repo_get_path(repo), worktree_path);
 	if (error)
 		goto done;
 
@@ -521,7 +514,6 @@ done:
 	free(worktree_path);
 	free(commit_id);
 	free(commit_id_str);
-	free(repo_path);
 	return error;
 }
 
diff --git a/include/got_repository.h b/include/got_repository.h
index 37f50bb..2c7f872 100644
--- a/include/got_repository.h
+++ b/include/got_repository.h
@@ -20,11 +20,12 @@ struct got_repository;
 const struct got_error *got_repo_open(struct got_repository**, const char *);
 const struct got_error *got_repo_close(struct got_repository*);
 
+const char *got_repo_get_path(struct got_repository *);
+
 /*
  * Obtain paths to various directories within a repository.
  * The caller must dispose of a path with free(3).
  */
-char *got_repo_get_path(struct got_repository *);
 char *got_repo_get_path_git_dir(struct got_repository *);
 char *got_repo_get_path_objects(struct got_repository *);
 char *got_repo_get_path_objects_pack(struct got_repository *);
diff --git a/lib/repository.c b/lib/repository.c
index 941438e..bd30c00 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -66,10 +66,10 @@
 #define GOT_ORIG_HEAD_FILE	"ORIG_HEAD"
 #define GOT_OBJECTS_PACK_DIR	"objects/pack"
 
-char *
+const char *
 got_repo_get_path(struct got_repository *repo)
 {
-	return strdup(repo->path);
+	return repo->path;
 }
 
 char *
@@ -462,10 +462,10 @@ got_repo_map_path(char **in_repo_path, struct got_repository *repo,
     const char *input_path, int check_disk)
 {
 	const struct got_error *err = NULL;
-	char *repo_abspath = NULL, *cwd = NULL;
+	const char *repo_abspath = NULL;
 	struct stat sb;
 	size_t repolen, cwdlen, len;
-	char *canonpath, *path = NULL;
+	char *cwd, *canonpath, *path = NULL;
 
 	*in_repo_path = NULL;
 
@@ -483,10 +483,6 @@ got_repo_map_path(char **in_repo_path, struct got_repository *repo,
 		goto done;
 
 	repo_abspath = got_repo_get_path(repo);
-	if (repo_abspath == NULL) {
-		err = got_error_from_errno();
-		goto done;
-	}
 
 	/* TODO: Call "get in-repository path of work-tree node" API. */
 
@@ -584,7 +580,6 @@ got_repo_map_path(char **in_repo_path, struct got_repository *repo,
 	}
 
 done:
-	free(repo_abspath);
 	free(cwd);
 	free(canonpath);
 	if (err)
diff --git a/lib/worktree.c b/lib/worktree.c
index 79520bd..6e5fc2e 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -187,7 +187,6 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 	int obj_type;
 	char *path_got = NULL;
 	char *refstr = NULL;
-	char *repo_path = NULL;
 	char *formatstr = NULL;
 	char *absprefix = NULL;
 	char *basestr = NULL;
@@ -251,12 +250,8 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 		goto done;
 
 	/* Store path to repository. */
-	repo_path = got_repo_get_path(repo);
-	if (repo_path == NULL) {
-		err = got_error_from_errno();
-		goto done;
-	}
-	err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
+	err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY,
+	    got_repo_get_path(repo));
 	if (err)
 		goto done;
 
@@ -280,7 +275,6 @@ done:
 	free(path_got);
 	free(formatstr);
 	free(refstr);
-	free(repo_path);
 	free(absprefix);
 	free(basestr);
 	return err;