introduce got_worktree_resolve_path()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
diff --git a/got/got.c b/got/got.c
index fd0fb74..32ed259 100644
--- a/got/got.c
+++ b/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
diff --git a/include/got_worktree.h b/include/got_worktree.h
index fe7760b..3908cf7 100644
--- a/include/got_worktree.h
+++ b/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 *);
diff --git a/lib/worktree.c b/lib/worktree.c
index 8ce73ba..12e2eb8 100644
--- a/lib/worktree.c
+++ b/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;
+}