make got_worktree_schedule_add() report 'added' via status_cb
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
diff --git a/got/got.c b/got/got.c
index c37a9a2..b6b5185 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1855,6 +1855,7 @@ static const struct got_error *
cmd_add(int argc, char *argv[])
{
const struct got_error *error = NULL;
+ struct got_repository *repo = NULL;
struct got_worktree *worktree = NULL;
char *cwd = NULL, *path = NULL, *relpath = NULL;
int ch;
@@ -1888,15 +1889,22 @@ cmd_add(int argc, char *argv[])
if (error)
goto done;
- error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
+ error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
+ if (error != NULL)
+ goto done;
+
+ error = apply_unveil(got_repo_get_path(repo), 1,
+ got_worktree_get_root_path(worktree));
if (error)
goto done;
- error = got_worktree_schedule_add(&relpath, worktree, path);
+ error = got_worktree_schedule_add(worktree, path, print_status, NULL,
+ repo);
if (error)
goto done;
- printf("%c %s\n", GOT_STATUS_ADD, relpath);
done:
+ if (repo)
+ got_repo_close(repo);
if (worktree)
got_worktree_close(worktree);
free(path);
diff --git a/include/got_worktree.h b/include/got_worktree.h
index 51e30c8..d6cae67 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -132,13 +132,9 @@ const struct got_error *got_worktree_status(struct got_worktree *,
const struct got_error *got_worktree_resolve_path(char **,
struct got_worktree *, const char *);
-/*
- * Schedule a file at an on-disk path for addition in the next commit.
- * Return the added file's path relative to the root of the work tree.
- * The caller must dispose of this relative path with free(3).
- */
-const struct got_error *got_worktree_schedule_add(char **,
- struct got_worktree *, const char *);
+/* Schedule a file at an on-disk path for addition in the next commit. */
+const struct got_error *got_worktree_schedule_add(struct got_worktree *,
+ const char *, got_worktree_status_cb, void *, struct got_repository *);
/*
* Remove a file from disk and schedule it to be deleted in the next commit.
diff --git a/lib/worktree.c b/lib/worktree.c
index 9b86570..45ca7be 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -1601,27 +1601,27 @@ done:
}
const struct got_error *
-got_worktree_schedule_add(char **relpath, struct got_worktree *worktree,
- const char *ondisk_path)
+got_worktree_schedule_add(struct got_worktree *worktree,
+ const char *ondisk_path, got_worktree_status_cb status_cb, void *status_arg,
+ struct got_repository *repo)
{
struct got_fileindex *fileindex = NULL;
struct got_fileindex_entry *ie = NULL;
- char *fileindex_path = NULL, *new_fileindex_path = NULL;
+ char *relpath, *fileindex_path = NULL, *new_fileindex_path = NULL;
FILE *index = NULL, *new_index = NULL;
const struct got_error *err = NULL, *unlockerr = NULL;
-
- *relpath = NULL;
+ int ie_added = 0;
err = lock_worktree(worktree, LOCK_EX);
if (err)
return err;
- err = got_path_skip_common_ancestor(relpath,
+ err = got_path_skip_common_ancestor(&relpath,
got_worktree_get_root_path(worktree), ondisk_path);
if (err)
goto done;
- err = got_fileindex_entry_alloc(&ie, ondisk_path, *relpath, NULL, NULL);
+ err = got_fileindex_entry_alloc(&ie, ondisk_path, relpath, NULL, NULL);
if (err)
goto done;
@@ -1651,7 +1651,7 @@ got_worktree_schedule_add(char **relpath, struct got_worktree *worktree,
err = got_fileindex_entry_add(fileindex, ie);
if (err)
goto done;
- ie = NULL; /* now owned by fileindex; don't free separately */
+ ie_added = 1; /* now owned by fileindex; don't free separately */
err = got_opentemp_named(&new_fileindex_path, &new_index,
fileindex_path);
@@ -1669,6 +1669,8 @@ got_worktree_schedule_add(char **relpath, struct got_worktree *worktree,
free(new_fileindex_path);
new_fileindex_path = NULL;
+
+ err = report_file_status(ie, ondisk_path, status_cb, status_arg, repo);
done:
if (index) {
if (fclose(index) != 0 && err == NULL)
@@ -1679,17 +1681,14 @@ done:
err = got_error_from_errno();
free(new_fileindex_path);
}
- if (ie)
+ if (!ie_added)
got_fileindex_entry_free(ie);
if (fileindex)
got_fileindex_free(fileindex);
unlockerr = lock_worktree(worktree, LOCK_SH);
if (unlockerr && err == NULL)
err = unlockerr;
- if (err) {
- free(*relpath);
- *relpath = NULL;
- }
+ free(relpath);
return err;
}