git-2.22.0/builtin/replace.c
/*
 * 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.
 */
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.
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 .
static int import_object(struct object_id *oid, enum object_type type,
			  int raw, const char *filename)
{
	int fd;
	fd = open(filename, O_RDONLY);
	if (fd < 0)
		return error_errno(_("unable to open %s for reading"), filename);
	if (!raw && type == OBJ_TREE) {
		const char *argv[] = { "mktree", NULL };
		struct child_process cmd = CHILD_PROCESS_INIT;
		struct strbuf result = STRBUF_INIT;
		cmd.argv = argv;
		cmd.git_cmd = 1;
		cmd.in = fd;
		cmd.out = -1;
		if (start_command(&cmd)) {
			close(fd);
			return error(_("unable to spawn mktree"));
		}
		if (strbuf_read(&result, cmd.out, 41) < 0) {
			error_errno(_("unable to read from mktree"));
			close(fd);
			close(cmd.out);
			return -1;
		}
		close(cmd.out);
		if (finish_command(&cmd)) {
			strbuf_release(&result);
			return error(_("mktree reported failure"));
		}
		if (get_oid_hex(result.buf, oid) < 0) {
			strbuf_release(&result);
			return error(_("mktree did not return an object name"));
		}
		strbuf_release(&result);
	} else {
		struct stat st;
		int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
		if (fstat(fd, &st) < 0) {
			error_errno(_("unable to fstat %s"), filename);
			close(fd);
			return -1;
		}
		if (index_fd(the_repository->index, oid, fd, &st, type, NULL, flags) < 0)
			return error(_("unable to write object to database"));
	}
	return 0;
}
static int import_object ( struct object_id * oid , enum object_type type , int raw , const char * filename ) { int fd ; fd = open ( filename , O_RDONLY ) ; if ( fd < 0 ) return error_errno ( _ ( " " ) , filename ) ; if ( ! raw && type == OBJ_TREE ) { const char * argv [ ] = { " " , NULL } ; struct child_process cmd = CHILD_PROCESS_INIT ; struct strbuf result = STRBUF_INIT ; cmd . argv = argv ; cmd . git_cmd = 1 ; cmd . in = fd ; cmd . out = -1 ; if ( start_command ( & cmd ) ) { close ( fd ) ; return error ( _ ( " " ) ) ; } if ( strbuf_read ( & result , cmd . out , 41 ) < 0 ) { error_errno ( _ ( " " ) ) ; close ( fd ) ; close ( cmd . out ) ; return -1 ; } close ( cmd . out ) ; if ( finish_command ( & cmd ) ) { strbuf_release ( & result ) ; return error ( _ ( " " ) ) ; } if ( get_oid_hex ( result . buf , oid ) < 0 ) { strbuf_release ( & result ) ; return error ( _ ( " " ) ) ; } strbuf_release ( & result ) ; } else { struct stat st ; int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT ; if ( fstat ( fd , & st ) < 0 ) { error_errno ( _ ( " " ) , filename ) ; close ( fd ) ; return -1 ; } if ( index_fd ( the_repository -> index , oid , fd , & st , type , NULL , flags ) < 0 ) return error ( _ ( " " ) ) ; } return 0 ; }
Read a previously-exported (and possibly edited) object back from "filename",
 interpreting it as "type", and writing the result to the object database.
Read a previously - exported ( and possibly edited ) object back from " filename " interpreting it as " type " and writing the result to the object database .
/* index_fd close()s fd for us */ /*
	 * No need to close(fd) here; both run-command and index-fd
	 * will have done it for us.
	 */