* Read a previously-exported (and possibly edited) object back from "filename", * interpreting it as "type", and writing the result to the object database. * The sha1 of the written object is returned via sha1. */
| 261 | * The sha1 of the written object is returned via sha1. |
| 262 | */ |
| 263 | static int import_object(struct object_id *oid, enum object_type type, |
| 264 | int raw, const char *filename) |
| 265 | { |
| 266 | int fd; |
| 267 | |
| 268 | fd = open(filename, O_RDONLY); |
| 269 | if (fd < 0) |
| 270 | return error_errno(_("unable to open %s for reading"), filename); |
| 271 | |
| 272 | if (!raw && type == OBJ_TREE) { |
| 273 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 274 | struct strbuf result = STRBUF_INIT; |
| 275 | |
| 276 | strvec_push(&cmd.args, "mktree"); |
| 277 | cmd.git_cmd = 1; |
| 278 | cmd.in = fd; |
| 279 | cmd.out = -1; |
| 280 | |
| 281 | if (start_command(&cmd)) { |
| 282 | close(fd); |
| 283 | return error(_("unable to spawn mktree")); |
| 284 | } |
| 285 | |
| 286 | if (strbuf_read(&result, cmd.out, the_hash_algo->hexsz + 1) < 0) { |
| 287 | error_errno(_("unable to read from mktree")); |
| 288 | close(fd); |
| 289 | close(cmd.out); |
| 290 | return -1; |
| 291 | } |
| 292 | close(cmd.out); |
| 293 | |
| 294 | if (finish_command(&cmd)) { |
| 295 | strbuf_release(&result); |
| 296 | return error(_("mktree reported failure")); |
| 297 | } |
| 298 | if (get_oid_hex(result.buf, oid) < 0) { |
| 299 | strbuf_release(&result); |
| 300 | return error(_("mktree did not return an object name")); |
| 301 | } |
| 302 | |
| 303 | strbuf_release(&result); |
| 304 | } else { |
| 305 | struct stat st; |
| 306 | int flags = INDEX_FORMAT_CHECK | INDEX_WRITE_OBJECT; |
| 307 | |
| 308 | if (fstat(fd, &st) < 0) { |
| 309 | error_errno(_("unable to fstat %s"), filename); |
| 310 | close(fd); |
| 311 | return -1; |
| 312 | } |
| 313 | if (index_fd(the_repository->index, oid, fd, &st, type, NULL, flags) < 0) |
| 314 | return error(_("unable to write object to database")); |
| 315 | /* index_fd close()s fd for us */ |
| 316 | } |
| 317 | |
| 318 | /* |
| 319 | * No need to close(fd) here; both run-command and index-fd |
| 320 | * will have done it for us. |
no test coverage detected