got patch: avoid open/sync/close the fileindex over and over again Instead of flushing the fileindex after every patch in the patchfile just reuse the same fileindex and sync it only at the end of the patch operation. This speeds up 'got patch' on large repositories by quite a lot.
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 83c0f25..df7a870 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -549,7 +549,8 @@ got_worktree_path_info(struct got_worktree *, struct got_pathlist_head *,
* Prepare for applying a patch.
*/
const struct got_error *
-got_worktree_patch_prepare(struct got_fileindex **, struct got_worktree *);
+got_worktree_patch_prepare(struct got_fileindex **, char **,
+ struct got_worktree *);
/*
* Lookup paths for the "old" and "new" file before patching and check their
@@ -559,6 +560,16 @@ const struct got_error *
got_worktree_patch_check_path(const char *, const char *, char **, char **,
struct got_worktree *, struct got_repository *, struct got_fileindex *);
+const struct got_error *
+got_worktree_patch_schedule_add(const char *, struct got_repository *,
+ struct got_worktree *, struct got_fileindex *, got_worktree_checkout_cb,
+ void *);
+
+const struct got_error *
+got_worktree_patch_schedule_rm(const char *, struct got_repository *,
+ struct got_worktree *, struct got_fileindex *, got_worktree_delete_cb,
+ void *);
+
/* Complete the patch operation. */
const struct got_error *
-got_worktree_patch_complete(struct got_fileindex *);
+got_worktree_patch_complete(struct got_fileindex *, char *);
diff --git a/lib/patch.c b/lib/patch.c
index 3ed493e..cf2c864 100644
--- a/lib/patch.c
+++ b/lib/patch.c
@@ -575,28 +575,17 @@ patch_add(void *arg, unsigned char status, const char *path)
static const struct got_error *
apply_patch(struct got_worktree *worktree, struct got_repository *repo,
- const char *old, const char *new, struct got_patch *p, int nop,
- struct patch_args *pa, got_cancel_cb cancel_cb, void *cancel_arg)
+ struct got_fileindex *fileindex, const char *old, const char *new,
+ struct got_patch *p, int nop, struct patch_args *pa,
+ got_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
- struct got_pathlist_head oldpaths, newpaths;
- struct got_pathlist_entry *pe;
int file_renamed = 0;
char *oldpath = NULL, *newpath = NULL;
char *tmppath = NULL, *template = NULL, *parent = NULL;;
FILE *tmp = NULL;
mode_t mode = GOT_DEFAULT_FILE_MODE;
- TAILQ_INIT(&oldpaths);
- TAILQ_INIT(&newpaths);
-
- err = got_pathlist_insert(&pe, &oldpaths, old, NULL);
- if (err)
- goto done;
- err = got_pathlist_insert(&pe, &newpaths, new, NULL);
- if (err)
- goto done;
-
if (asprintf(&oldpath, "%s/%s", got_worktree_get_root_path(worktree),
old) == -1) {
err = got_error_from_errno("asprintf");
@@ -629,8 +618,8 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
goto done;
if (p->old != NULL && p->new == NULL) {
- err = got_worktree_schedule_delete(worktree, &oldpaths,
- 0, NULL, patch_delete, pa, repo, 0, 0);
+ err = got_worktree_patch_schedule_rm(old, repo, worktree,
+ fileindex, patch_delete, pa);
goto done;
}
@@ -660,24 +649,23 @@ apply_patch(struct got_worktree *worktree, struct got_repository *repo,
}
if (file_renamed) {
- err = got_worktree_schedule_delete(worktree, &oldpaths,
- 0, NULL, patch_delete, pa, repo, 0, 0);
+ err = got_worktree_patch_schedule_rm(old, repo, worktree,
+ fileindex, patch_delete, pa);
if (err == NULL)
- err = got_worktree_schedule_add(worktree, &newpaths,
- patch_add, pa, repo, 1);
+ err = got_worktree_patch_schedule_add(new, repo,
+ worktree, fileindex, patch_add,
+ pa);
if (err)
unlink(newpath);
} else if (p->old == NULL) {
- err = got_worktree_schedule_add(worktree, &newpaths,
- patch_add, pa, repo, 1);
+ err = got_worktree_patch_schedule_add(new, repo, worktree,
+ fileindex, patch_add, pa);
if (err)
unlink(newpath);
} else
err = report_progress(pa, old, new, GOT_STATUS_MODIFY, NULL);
done:
- got_pathlist_free(&oldpaths);
- got_pathlist_free(&newpaths);
free(parent);
free(template);
if (tmppath != NULL)
@@ -722,8 +710,9 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
int nop, int strip, int reverse, got_patch_progress_cb progress_cb,
void *progress_arg, got_cancel_cb cancel_cb, void *cancel_arg)
{
- const struct got_error *err = NULL;
+ const struct got_error *err = NULL, *complete_err;
struct got_fileindex *fileindex = NULL;
+ char *fileindex_path = NULL;
char *oldpath, *newpath;
struct imsgbuf *ibuf;
int imsg_fds[2] = {-1, -1};
@@ -763,7 +752,8 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
if (err)
goto done;
- err = got_worktree_patch_prepare(&fileindex, worktree);
+ err = got_worktree_patch_prepare(&fileindex, &fileindex_path,
+ worktree);
if (err)
goto done;
@@ -785,8 +775,8 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
err = got_worktree_patch_check_path(p.old, p.new, &oldpath,
&newpath, worktree, repo, fileindex);
if (err == NULL)
- err = apply_patch(worktree, repo, oldpath, newpath,
- &p, nop, &pa, cancel_cb, cancel_arg);
+ err = apply_patch(worktree, repo, fileindex, oldpath,
+ newpath, &p, nop, &pa, cancel_cb, cancel_arg);
if (err != NULL) {
failed = 1;
/* recoverable errors */
@@ -808,8 +798,9 @@ got_patch(int fd, struct got_worktree *worktree, struct got_repository *repo,
}
done:
- if (fileindex)
- got_worktree_patch_complete(fileindex);
+ complete_err = got_worktree_patch_complete(fileindex, fileindex_path);
+ if (complete_err && err == NULL)
+ err = complete_err;
if (fd != -1 && close(fd) == -1 && err == NULL)
err = got_error_from_errno("close");
if (ibuf != NULL)
diff --git a/lib/worktree.c b/lib/worktree.c
index bbe95ae..e01fe52 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -8858,14 +8858,9 @@ patch_can_edit(const char *path, unsigned char status,
const struct got_error *
got_worktree_patch_prepare(struct got_fileindex **fileindex,
- struct got_worktree *worktree)
+ char **fileindex_path, struct got_worktree *worktree)
{
- const struct got_error *err;
- char *fileindex_path = NULL;
-
- err = open_fileindex(fileindex, &fileindex_path, worktree);
- free(fileindex_path);
- return err;
+ return open_fileindex(fileindex, fileindex_path, worktree);
}
const struct got_error *
@@ -8916,8 +8911,56 @@ done:
}
const struct got_error *
-got_worktree_patch_complete(struct got_fileindex *fileindex)
+got_worktree_patch_schedule_add(const char *path, struct got_repository *repo,
+ struct got_worktree *worktree, struct got_fileindex *fileindex,
+ got_worktree_checkout_cb progress_cb, void *progress_arg)
{
- got_fileindex_free(fileindex);
- return NULL;
+ struct schedule_addition_args saa;
+
+ memset(&saa, 0, sizeof(saa));
+ saa.worktree = worktree;
+ saa.fileindex = fileindex;
+ saa.progress_cb = progress_cb;
+ saa.progress_arg = progress_arg;
+ saa.repo = repo;
+
+ return worktree_status(worktree, path, fileindex, repo,
+ schedule_addition, &saa, NULL, NULL, 1, 0);
+}
+
+const struct got_error *
+got_worktree_patch_schedule_rm(const char *path, struct got_repository *repo,
+ struct got_worktree *worktree, struct got_fileindex *fileindex,
+ got_worktree_delete_cb progress_cb, void *progress_arg)
+{
+ struct schedule_deletion_args sda;
+
+ memset(&sda, 0, sizeof(sda));
+ sda.worktree = worktree;
+ sda.fileindex = fileindex;
+ sda.progress_cb = progress_cb;
+ sda.progress_arg = progress_arg;
+ sda.repo = repo;
+ sda.delete_local_mods = 0;
+ sda.keep_on_disk = 0;
+ sda.ignore_missing_paths = 0;
+ sda.status_codes = NULL;
+
+ return worktree_status(worktree, path, fileindex, repo,
+ schedule_for_deletion, &sda, NULL, NULL, 1, 1);
+}
+
+const struct got_error *
+got_worktree_patch_complete(struct got_fileindex *fileindex,
+ char *fileindex_path)
+{
+ const struct got_error *err = NULL;
+
+ if (fileindex) {
+ err = sync_fileindex(fileindex, fileindex_path);
+ got_fileindex_free(fileindex);
+ }
+
+ free(fileindex_path);
+ return err;
}