Commit f0207f6a4f16fab481f483d0db43ed50ca670e7e

Stefan Sperling 2020-10-19T23:14:59

switch to got_path_basename() in cmd_checkout() ok millert

diff --git a/got/got.c b/got/got.c
index 6535a4b..9ff5486 100644
--- a/got/got.c
+++ b/got/got.c
@@ -2600,7 +2600,7 @@ cmd_checkout(int argc, char *argv[])
 	const char *path_prefix = "";
 	const char *branch_name = GOT_REF_HEAD;
 	char *commit_id_str = NULL;
-	char *cwd = NULL, *path = NULL;
+	char *cwd = NULL;
 	int ch, same_path_prefix, allow_nonempty = 0;
 	struct got_pathlist_head paths;
 	struct got_checkout_progress_arg cpa;
@@ -2639,6 +2639,7 @@ cmd_checkout(int argc, char *argv[])
 #endif
 	if (argc == 1) {
 		char *base, *dotgit;
+		const char *path;
 		repo_path = realpath(argv[0], NULL);
 		if (repo_path == NULL)
 			return got_error_from_errno2("realpath", argv[0]);
@@ -2648,25 +2649,21 @@ cmd_checkout(int argc, char *argv[])
 			goto done;
 		}
 		if (path_prefix[0])
-			path = strdup(path_prefix);
+			path = path_prefix;
 		else
-			path = strdup(repo_path);
-		if (path == NULL) {
-			error = got_error_from_errno("strdup");
-			goto done;
-		}
-		base = basename(path);
-		if (base == NULL) {
-			error = got_error_from_errno2("basename", path);
+			path = repo_path;
+		error = got_path_basename(&base, path);
+		if (error)
 			goto done;
-		}
 		dotgit = strstr(base, ".git");
 		if (dotgit)
 			*dotgit = '\0';
 		if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
 			error = got_error_from_errno("asprintf");
+			free(base);
 			goto done;
 		}
+		free(base);
 	} else if (argc == 2) {
 		repo_path = realpath(argv[0], NULL);
 		if (repo_path == NULL) {
@@ -2785,7 +2782,6 @@ done:
 	free(repo_path);
 	free(worktree_path);
 	free(cwd);
-	free(path);
 	return error;
 }