Commit fdfa9bf28f9d53053ad4687f6cd06c86cfe31b33

Stefan Sperling 2019-08-03T13:41:43

provide a way for API users to override staged file content

diff --git a/include/got_worktree.h b/include/got_worktree.h
index 9963b82..c985f53 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -372,7 +372,13 @@ const struct got_error *got_worktree_histedit_abort(struct got_worktree *,
 const struct got_error *got_worktree_get_histedit_script_path(char **,
     struct got_worktree *);
 
-/* Stage the specified paths for commit. */
+/*
+ * Stage the specified paths for commit.
+ * If the 'data' pointer of a pathlist element on the path list is NULL then
+ * stage the content of the entire file at this path. Otherwise, the 'data'
+ * pointer is expected to point at a const char * path of a file which
+ * contains alternative content to be staged instead.
+*/
 const struct got_error *got_worktree_stage_paths(struct got_worktree *,
     struct got_pathlist_head *, struct got_repository *,
     got_worktree_status_cb, void *,
diff --git a/lib/worktree.c b/lib/worktree.c
index e2d15ef..a88831b 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -4858,8 +4858,9 @@ done:
 }
 
 static const struct got_error *
-stage_path(const char *path, size_t path_len, struct got_worktree *worktree,
-    struct got_fileindex *fileindex, struct got_repository *repo,
+stage_path(const char *path, size_t path_len, const char *path_content,
+    struct got_worktree *worktree, struct got_fileindex *fileindex,
+    struct got_repository *repo,
     got_worktree_status_cb status_cb, void *status_arg)
 {
 	const struct got_error *err = NULL;
@@ -4886,8 +4887,8 @@ stage_path(const char *path, size_t path_len, struct got_worktree *worktree,
 	switch (status) {
 	case GOT_STATUS_ADD:
 	case GOT_STATUS_MODIFY:
-		err = got_object_blob_create(&blob_id, ondisk_path,
-		    repo);
+		err = got_object_blob_create(&blob_id,
+		    path_content ? path_content : ondisk_path, repo);
 		if (err)
 			goto done;
 		memcpy(ie->staged_blob_sha1, blob_id->sha1,
@@ -4940,8 +4941,8 @@ got_worktree_stage_paths(struct got_worktree *worktree,
 			if (err)
 				break;
 		}
-		err = stage_path(pe->path, pe->path_len, worktree, fileindex,
-		    repo, status_cb, status_arg);
+		err = stage_path(pe->path, pe->path_len, (const char *)pe->data,
+		    worktree, fileindex, repo, status_cb, status_arg);
 		if (err)
 			break;
 	}