move a chunk of file checkout code into a separate helper function
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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
diff --git a/lib/worktree.c b/lib/worktree.c
index 323bf4a..a0eaf35 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1531,6 +1531,109 @@ done:
return err;
}
+static const struct got_error *
+find_tree_entry_for_checkout(int *entry_type, char **tree_relpath,
+ struct got_object_id **tree_id, const char *wt_relpath,
+ struct got_worktree *worktree, struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ struct got_object_id *id = NULL;
+ char *in_repo_path = NULL;
+ int is_root_wt = got_path_is_root_dir(worktree->path_prefix);
+
+ *entry_type = GOT_OBJ_TYPE_ANY;
+ *tree_relpath = NULL;
+ *tree_id = NULL;
+
+ if (wt_relpath[0] == '\0') {
+ /* Check out all files within the work tree. */
+ *entry_type = GOT_OBJ_TYPE_TREE;
+ *tree_relpath = strdup("");
+ if (*tree_relpath == NULL) {
+ err = got_error_from_errno("strdup");
+ goto done;
+ }
+ err = got_object_id_by_path(tree_id, repo,
+ worktree->base_commit_id, worktree->path_prefix);
+ if (err)
+ goto done;
+ return NULL;
+ }
+
+ /* Check out a subset of files in the work tree. */
+
+ if (asprintf(&in_repo_path, "%s%s%s", worktree->path_prefix,
+ is_root_wt ? "" : "/", wt_relpath) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+
+ err = got_object_id_by_path(&id, repo, worktree->base_commit_id,
+ in_repo_path);
+ if (err)
+ goto done;
+
+ free(in_repo_path);
+ in_repo_path = NULL;
+
+ err = got_object_get_type(entry_type, repo, id);
+ if (err)
+ goto done;
+
+ if (*entry_type == GOT_OBJ_TYPE_BLOB) {
+ /* Check out a single file. */
+ if (strchr(wt_relpath, '/') == NULL) {
+ /* Check out a single file in work tree's root dir. */
+ in_repo_path = strdup(worktree->path_prefix);
+ if (in_repo_path == NULL) {
+ err = got_error_from_errno("strdup");
+ goto done;
+ }
+ *tree_relpath = strdup("");
+ if (*tree_relpath == NULL) {
+ err = got_error_from_errno("strdup");
+ goto done;
+ }
+ } else {
+ /* Check out a single file in a subdirectory. */
+ err = got_path_dirname(tree_relpath, wt_relpath);
+ if (err)
+ return err;
+ if (asprintf(&in_repo_path, "%s%s%s",
+ worktree->path_prefix, is_root_wt ? "" : "/",
+ *tree_relpath) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+ }
+ err = got_object_id_by_path(tree_id, repo,
+ worktree->base_commit_id, in_repo_path);
+ } else {
+ /* Check out all files within a subdirectory. */
+ *tree_id = got_object_id_dup(id);
+ if (*tree_id == NULL) {
+ err = got_error_from_errno("got_object_id_dup");
+ goto done;
+ }
+ *tree_relpath = strdup(wt_relpath);
+ if (*tree_relpath == NULL) {
+ err = got_error_from_errno("strdup");
+ goto done;
+ }
+ }
+done:
+ free(id);
+ free(in_repo_path);
+ if (err) {
+ *entry_type = GOT_OBJ_TYPE_ANY;
+ free(*tree_relpath);
+ *tree_relpath = NULL;
+ free(*tree_id);
+ *tree_id = NULL;
+ }
+ return err;
+}
+
const struct got_error *
got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
struct got_repository *repo, got_worktree_checkout_cb progress_cb,
@@ -1546,11 +1649,25 @@ got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
struct diff_cb_arg arg;
char *relpath = NULL, *entry_name = NULL;
struct bump_base_commit_id_arg bbc_arg;
+ int entry_type;
err = lock_worktree(worktree, LOCK_EX);
if (err)
return err;
+ err = find_tree_entry_for_checkout(&entry_type, &relpath, &tree_id,
+ path, worktree, repo);
+ if (err)
+ goto done;
+
+ if (entry_type == GOT_OBJ_TYPE_BLOB) {
+ entry_name = basename(path);
+ if (entry_name == NULL) {
+ err = got_error_from_errno2("basename", path);
+ goto done;
+ }
+ }
+
/*
* Read the file index.
* Checking out files is supposed to be an idempotent operation.
@@ -1569,78 +1686,6 @@ got_worktree_checkout_files(struct got_worktree *worktree, const char *path,
if (err)
goto done;
- if (path[0]) {
- char *tree_path;
- int obj_type;
- if (asprintf(&tree_path, "%s%s%s", worktree->path_prefix,
- got_path_is_root_dir(worktree->path_prefix) ? "" : "/",
- path) == -1) {
- err = got_error_from_errno("asprintf");
- goto done;
- }
- err = got_object_id_by_path(&tree_id, repo,
- worktree->base_commit_id, tree_path);
- free(tree_path);
- if (err)
- goto done;
- err = got_object_get_type(&obj_type, repo, tree_id);
- if (err)
- goto done;
- if (obj_type == GOT_OBJ_TYPE_BLOB) {
- /* Split provided path into parent dir + entry name. */
- if (strchr(path, '/') == NULL) {
- relpath = strdup("");
- if (relpath == NULL) {
- err = got_error_from_errno("strdup");
- goto done;
- }
- tree_path = strdup(worktree->path_prefix);
- if (tree_path == NULL) {
- err = got_error_from_errno("strdup");
- goto done;
- }
- } else {
- err = got_path_dirname(&relpath, path);
- if (err)
- goto done;
- if (asprintf(&tree_path, "%s%s%s",
- worktree->path_prefix,
- got_path_is_root_dir(
- worktree->path_prefix) ? "" : "/",
- relpath) == -1) {
- err = got_error_from_errno("asprintf");
- goto done;
- }
- }
- err = got_object_id_by_path(&tree_id, repo,
- worktree->base_commit_id, tree_path);
- free(tree_path);
- if (err)
- goto done;
- entry_name = basename(path);
- if (entry_name == NULL) {
- err = got_error_from_errno2("basename", path);
- goto done;
- }
- } else {
- relpath = strdup(path);
- if (relpath == NULL) {
- err = got_error_from_errno("strdup");
- goto done;
- }
- }
- } else {
- relpath = strdup("");
- if (relpath == NULL) {
- err = got_error_from_errno("strdup");
- goto done;
- }
- err = got_object_id_by_path(&tree_id, repo,
- worktree->base_commit_id, worktree->path_prefix);
- if (err)
- goto done;
- }
-
err = got_object_open_as_tree(&tree, repo, tree_id);
if (err)
goto done;