commit 6c7ab9213e39b6a690152fe8ffb18fe1f15a9ccb from: Stefan Sperling date: Mon Mar 18 15:48:18 2019 UTC introduce got_worktree_resolve_path() commit - cbd1af7a5ac90edbfeec7507187896b76710c60e commit + 6c7ab9213e39b6a690152fe8ffb18fe1f15a9ccb blob - fd0fb74b2c6a1723f8aca3e8c9c832e065f1d6d1 blob + 32ed25930a57db98595d201cd260b1d73b46142a --- got/got.c +++ got/got.c @@ -200,47 +200,6 @@ apply_unveil(const char *repo_path, int repo_read_only return NULL; } -static const struct got_error * -resolve_path_in_worktree(char **wt_path, struct got_worktree *worktree, - const char *arg) -{ - const struct got_error *err = NULL; - char *resolved, *path = NULL; - size_t len; - - *wt_path = NULL; - - resolved = realpath(arg, NULL); - if (resolved == NULL) - return got_error_from_errno(); - - if (strncmp(got_worktree_get_root_path(worktree), resolved, - strlen(got_worktree_get_root_path(worktree)))) { - err = got_error(GOT_ERR_BAD_PATH); - goto done; - } - - path = strdup(resolved + strlen(got_worktree_get_root_path(worktree))); - if (path == NULL) { - err = got_error_from_errno(); - goto done; - } - - /* XXX status walk can't deal with trailing slash! */ - len = strlen(path); - while (path[len - 1] == '/') { - path[len - 1] = '\0'; - len--; - } -done: - free(resolved); - if (err == NULL) - *wt_path = path; - else - free(path); - return err; -} - __dead static void usage_checkout(void) { @@ -879,7 +838,7 @@ cmd_log(int argc, char *argv[]) if (argc == 0) path = strdup(""); else if (argc == 1) { - error = resolve_path_in_worktree(&path, worktree, argv[0]); + error = got_worktree_resolve_path(&path, worktree, argv[0]); if (error) goto done; } else @@ -1124,7 +1083,7 @@ cmd_diff(int argc, char *argv[]) goto done; } if (argc == 1) { - error = resolve_path_in_worktree(&path, worktree, + error = got_worktree_resolve_path(&path, worktree, argv[0]); if (error) goto done; @@ -1657,7 +1616,7 @@ cmd_status(int argc, char *argv[]) goto done; } } else if (argc == 1) { - error = resolve_path_in_worktree(&path, worktree, argv[0]); + error = got_worktree_resolve_path(&path, worktree, argv[0]); if (error) goto done; } else blob - fe7760b8b5c088e55c141d45fe73ad788eef4e63 blob + 3908cf70ae58c988b8d20322cb755f391ac31264 --- include/got_worktree.h +++ include/got_worktree.h @@ -125,3 +125,10 @@ const struct got_error * got_worktree_status(struct got_worktree *, const char *, struct got_repository *, got_worktree_status_cb, void *, got_worktree_cancel_cb cancel_cb, void *); + +/* + * Try to resolve a user-provided path to an on-disk path in the work tree. + * The caller must dispose of the resolved path with free(3). + */ +const struct got_error *got_worktree_resolve_path(char **, + struct got_worktree *, const char *); blob - 8ce73ba0ba53813a0ee2a1585db0e7432279b738 blob + 12e2eb8b8f429c6ceffa39338e06773b13190057 --- lib/worktree.c +++ lib/worktree.c @@ -1543,3 +1543,44 @@ done: got_fileindex_free(fileindex); return err; } + +const struct got_error * +got_worktree_resolve_path(char **wt_path, struct got_worktree *worktree, + const char *arg) +{ + const struct got_error *err = NULL; + char *resolved, *path = NULL; + size_t len; + + *wt_path = NULL; + + resolved = realpath(arg, NULL); + if (resolved == NULL) + return got_error_from_errno(); + + if (strncmp(got_worktree_get_root_path(worktree), resolved, + strlen(got_worktree_get_root_path(worktree)))) { + err = got_error(GOT_ERR_BAD_PATH); + goto done; + } + + path = strdup(resolved + strlen(got_worktree_get_root_path(worktree))); + if (path == NULL) { + err = got_error_from_errno(); + goto done; + } + + /* XXX status walk can't deal with trailing slash! */ + len = strlen(path); + while (path[len - 1] == '/') { + path[len - 1] = '\0'; + len--; + } +done: + free(resolved); + if (err == NULL) + *wt_path = path; + else + free(path); + return err; +}