Commit 72151b04d0a9eb62823b0769601163f9ecf224d1

Stefan Sperling 2019-05-11T09:37:08

introduce got_path_strip_trailing_slashes()

diff --git a/got/got.c b/got/got.c
index 0aa6d64..e3fbc8b 100644
--- a/got/got.c
+++ b/got/got.c
@@ -322,7 +322,7 @@ cmd_checkout(int argc, char *argv[])
 	char *worktree_path = NULL;
 	const char *path_prefix = "";
 	char *commit_id_str = NULL;
-	int ch, same_path_prefix, x;
+	int ch, same_path_prefix;
 
 	while ((ch = getopt(argc, argv, "c:p:")) != -1) {
 		switch (ch) {
@@ -389,8 +389,7 @@ cmd_checkout(int argc, char *argv[])
 	} else
 		usage_checkout();
 
-	while (worktree_path[x = strlen(worktree_path) - 1] == '/')
-		worktree_path[x] = '\0';
+	got_path_strip_trailing_slashes(worktree_path);
 
 	error = got_repo_open(&repo, repo_path);
 	if (error != NULL)
diff --git a/include/got_path.h b/include/got_path.h
index d796646..e4dc262 100644
--- a/include/got_path.h
+++ b/include/got_path.h
@@ -92,3 +92,6 @@ const struct got_error *got_path_mkdir(const char *);
 
 /* dirname(3) with error handling and dynamically allocated result. */
 const struct got_error *got_path_dirname(char **, const char *);
+
+/* Strip trailing slashes from a path; path will be modified in-place. */
+void got_path_strip_trailing_slashes(char *);
diff --git a/lib/path.c b/lib/path.c
index 253851f..4c0f563 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -343,3 +343,12 @@ got_path_dirname(char **parent, const char *path)
 
 	return NULL;
 }
+
+void
+got_path_strip_trailing_slashes(char *path)
+{
+	int x;
+
+	while (path[x = strlen(path) - 1] == '/')
+		path[x] = '\0';
+}