Commit f2dd780737c47f6d92e6fe01cbd51bf93c91b3b3

Omar Polo 2022-05-17T13:28:02

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.

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;
 }