Commit ca2503ea3ef5aac919624b80cbae898d03c36eed

Stefan Sperling 2019-05-07T22:04:43

writing the lowest level of trees works now

diff --git a/lib/worktree.c b/lib/worktree.c
index 4ddafdd..48514a2 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2204,6 +2204,7 @@ struct commitable {
 	char *path;
 	unsigned char status;
 	struct got_object_id *id;
+	struct got_object_id *base_id;
 	struct got_object_id *tree_id;
 };
 
@@ -2212,6 +2213,7 @@ free_commitable(struct commitable *ct)
 {
 	free(ct->path);
 	free(ct->id);
+	free(ct->base_id);
 	free(ct->tree_id);
 	free(ct);
 }
@@ -2257,6 +2259,11 @@ collect_commitables(void *arg, unsigned char status, const char *path,
 
 	ct->status = status;
 	ct->id = NULL;
+	ct->base_id = got_object_id_dup(id);
+	if (ct->base_id == NULL) {
+		err = got_error_from_errno();
+		goto done;
+	}
 	err = got_object_id_by_path(&ct->tree_id, a->repo,
 	    a->worktree->base_commit_id, parent_path);
 	if (err)
@@ -2294,6 +2301,8 @@ write_tree(struct got_object_id *base_tree_id, void *data, void *arg)
 	struct got_object_id *new_tree_id = NULL;
 
 	TAILQ_INIT(&new_entries);
+	new_tree_entries.nentries = 0;
+	SIMPLEQ_INIT(&new_tree_entries.head);
 
 	err = got_object_open_as_tree(&base_tree, a->repo, base_tree_id);
 	if (err)
@@ -2302,15 +2311,16 @@ write_tree(struct got_object_id *base_tree_id, void *data, void *arg)
 	base_entries = got_object_tree_get_entries(base_tree);
 
 	SIMPLEQ_FOREACH(te, &base_entries->head, entry) {
+		struct got_tree_entry *new_te = NULL;
+		struct got_pathlist_entry *new_pe = NULL;
+		int ct_found = 0;
 		TAILQ_FOREACH(pe, a->commitable_paths, entry) {
 			struct commitable *ct = NULL;
-			struct got_tree_entry *new_te = NULL;
-			struct got_pathlist_entry *new_pe = NULL;
 			char *ct_name = NULL;
 
 			ct = pe->data;
 
-			if (got_object_id_cmp(ct->tree_id, te->id) != 0)
+			if (got_object_id_cmp(ct->base_id, te->id) != 0)
 				continue; /* not part of this tree */
 
 			ct_name = basename(pe->path);
@@ -2322,9 +2332,11 @@ write_tree(struct got_object_id *base_tree_id, void *data, void *arg)
 			if (strcmp(te->name, ct_name) != 0)
 				continue;
 
+			ct_found = 1;
+
 			if (ct->status == GOT_STATUS_DELETE) {
 				/* Deleted entries disappear. */
-				continue;
+				break;
 			}
 
 			/* Modified entries get updated mode and ID. */
@@ -2332,7 +2344,6 @@ write_tree(struct got_object_id *base_tree_id, void *data, void *arg)
 				err = got_object_tree_entry_dup(&new_te, te);
 				if (err)
 					goto done;
-				new_te->mode = GOT_DEFAULT_FILE_MODE; /* XXX */
 				free(new_te->id);
 			} else if (ct->status == GOT_STATUS_ADD) {
 				/* Added entries get... well, added. */
@@ -2341,32 +2352,39 @@ write_tree(struct got_object_id *base_tree_id, void *data, void *arg)
 					err = got_error_from_errno();
 					goto done;
 				}
-				new_te->mode = GOT_DEFAULT_FILE_MODE; /* XXX */
 				new_te->name = strdup(ct_name);
 				if (new_te->name == NULL) {
 					err = got_error_from_errno();
 					goto done;
 				}
 			}
+			new_te->mode = GOT_DEFAULT_FILE_MODE; /* XXX */
 			new_te->id = got_object_id_dup(ct->id);
 			if (new_te->id == NULL) {
 				err = got_error_from_errno();
 				goto done;
 			}
+			break;
+		}
 
-			err = got_pathlist_insert(&new_pe, &new_entries,
-			    te->name, new_te);
+		if (new_te == NULL) {
+			if (ct_found) /* GOT_STATUS_DELETE */
+				continue;
+			err = got_object_tree_entry_dup(&new_te, te);
 			if (err)
 				goto done;
-			if (new_pe == NULL) {
-				err = got_error(GOT_ERR_TREE_DUP_ENTRY);
-				goto done;
-			}
+		}
+
+		err = got_pathlist_insert(&new_pe, &new_entries,
+		    new_te->name, new_te);
+		if (err)
+			goto done;
+		if (new_pe == NULL) {
+			err = got_error(GOT_ERR_TREE_DUP_ENTRY);
+			goto done;
 		}
 	}
 
-	new_tree_entries.nentries = 0;
-	SIMPLEQ_INIT(&new_tree_entries.head);
 	TAILQ_FOREACH(pe, &new_entries, entry) {
 		struct got_tree_entry *te = pe->data;
 		new_tree_entries.nentries++;
@@ -2376,10 +2394,10 @@ write_tree(struct got_object_id *base_tree_id, void *data, void *arg)
 	err = got_object_tree_create(&new_tree_id, &new_tree_entries, a->repo);
 done:
 	free(new_tree_id);
+	got_object_tree_entries_close(&new_tree_entries);
+	got_pathlist_free(&new_entries);
 	if (base_tree)
 		got_object_tree_close(base_tree);
-	got_pathlist_free(&new_entries);
-	got_object_tree_entries_close(&new_tree_entries);
 	return err;
 }