Commit Diff


commit - 36a387004fc6fb4f302443e39bdffc01d1c1ed47
commit + 9ba0479c9a95dc336779534de660469d6f82ce31
blob - 5b2aa32bae271bdfac6e8eaeeb67d203751b2ffb
blob + 4521a0c515bb4aa2c6c59fa451dd249093a18667
--- lib/worktree.c
+++ lib/worktree.c
@@ -2592,15 +2592,6 @@ write_tree(struct got_object_id **new_tree_id,
 			int visited = 0;
 
 			*slash = '\0'; /* trim trailing path components */
-
-			new_te = calloc(1, sizeof(*new_te));
-			new_te->mode = S_IFDIR;
-			new_te->name = strdup(child_path);
-			if (new_te->name == NULL) {
-				got_object_tree_entry_close(new_te);
-				err = got_error_from_errno();
-				goto done;
-			}
 			if (asprintf(&subtree_path, "%s%s%s", path_base_tree,
 			    got_path_is_root_dir(path_base_tree) ? "" : "/",
 			    child_path) == -1) {
@@ -2616,11 +2607,23 @@ write_tree(struct got_object_id **new_tree_id,
 			if (visited)
 				continue;
 
+			new_te = calloc(1, sizeof(*new_te));
+			new_te->mode = S_IFDIR;
+			new_te->name = strdup(child_path);
+			if (new_te->name == NULL) {
+				err = got_error_from_errno();
+				got_object_tree_entry_close(new_te);
+				new_te = NULL;
+				goto done;
+			}
 			err = write_tree(&new_te->id, NULL, subtree_path,
 			    commitable_paths, status_cb, status_arg, repo);
 			free(subtree_path);
-			if (err)
+			if (err) {
+				got_object_tree_entry_close(new_te);
+				new_te = NULL;
 				goto done;
+			}
 		}
 		err = insert_tree_entry(new_te, &paths);
 		if (err)