Commit 031a5338903e1e2bbb80b6f1e17d58a5a6374271

Stefan Sperling 2019-03-26T10:46:18

make got_worktree_schedule_add() report 'added' via status_cb

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