eliminate redundant fileindex open for status during commit and rebase -a
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
diff --git a/lib/worktree.c b/lib/worktree.c
index cf78841..1bd7e85 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2153,23 +2153,18 @@ status_new(void *arg, struct dirent *de, const char *parent_path)
return err;
}
-const struct got_error *
-got_worktree_status(struct got_worktree *worktree, const char *path,
- struct got_repository *repo, got_worktree_status_cb status_cb,
- void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
+static const struct got_error *
+worktree_status(struct got_worktree *worktree, const char *path,
+ struct got_fileindex *fileindex, struct got_repository *repo,
+ got_worktree_status_cb status_cb, void *status_arg,
+ got_worktree_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
DIR *workdir = NULL;
- char *fileindex_path = NULL;
- struct got_fileindex *fileindex = NULL;
struct got_fileindex_diff_dir_cb fdiff_cb;
struct diff_dir_cb_arg arg;
char *ondisk_path = NULL;
- err = open_fileindex(&fileindex, &fileindex_path, worktree);
- if (err)
- return err;
-
if (asprintf(&ondisk_path, "%s%s%s",
worktree->root_path, path[0] ? "/" : "", path) == -1) {
err = got_error_from_errno("asprintf");
@@ -2210,6 +2205,24 @@ done:
if (workdir)
closedir(workdir);
free(ondisk_path);
+ return err;
+}
+
+const struct got_error *
+got_worktree_status(struct got_worktree *worktree, const char *path,
+ struct got_repository *repo, got_worktree_status_cb status_cb,
+ void *status_arg, got_worktree_cancel_cb cancel_cb, void *cancel_arg)
+{
+ const struct got_error *err = NULL;
+ char *fileindex_path = NULL;
+ struct got_fileindex *fileindex = NULL;
+
+ err = open_fileindex(&fileindex, &fileindex_path, worktree);
+ if (err)
+ return err;
+
+ err = worktree_status(worktree, path, fileindex, repo,
+ status_cb, status_arg, cancel_cb, cancel_arg);
free(fileindex_path);
got_fileindex_free(fileindex);
return err;
@@ -3262,6 +3275,10 @@ got_worktree_commit(struct got_object_id **new_commit_id,
if (err)
goto done;
+ err = open_fileindex(&fileindex, &fileindex_path, worktree);
+ if (err)
+ goto done;
+
err = got_ref_open(&head_ref, repo, worktree->head_ref_name, 0);
if (err)
goto done;
@@ -3272,8 +3289,8 @@ got_worktree_commit(struct got_object_id **new_commit_id,
cc_arg.commitable_paths = &commitable_paths;
cc_arg.worktree = worktree;
cc_arg.repo = repo;
- err = got_worktree_status(worktree, relpath ? relpath : "",
- repo, collect_commitables, &cc_arg, NULL, NULL);
+ err = worktree_status(worktree, relpath ? relpath : "",
+ fileindex, repo, collect_commitables, &cc_arg, NULL, NULL);
if (err)
goto done;
@@ -3379,10 +3396,6 @@ got_worktree_commit(struct got_object_id **new_commit_id,
if (err)
goto done;
- err = open_fileindex(&fileindex, &fileindex_path, worktree);
- if (err)
- goto done;
-
err = update_fileindex_after_commit(&commitable_paths,
*new_commit_id, fileindex, worktree);
if (err)
@@ -4026,14 +4039,14 @@ got_worktree_rebase_abort(struct got_worktree *worktree,
if (err)
goto done;
- crp_arg.revertible_paths = &revertible_paths;
- crp_arg.worktree = worktree;
- err = got_worktree_status(worktree, "", repo,
- collect_revertible_paths, &crp_arg, NULL, NULL);
+ err = open_fileindex(&fileindex, &fileindex_path, worktree);
if (err)
goto done;
- err = open_fileindex(&fileindex, &fileindex_path, worktree);
+ crp_arg.revertible_paths = &revertible_paths;
+ crp_arg.worktree = worktree;
+ err = worktree_status(worktree, "", fileindex, repo,
+ collect_revertible_paths, &crp_arg, NULL, NULL);
if (err)
goto done;