blob: 58652229f273bf93e55b0ff5eef1421096cfe719 [file] [log] [blame]
Stephen Boydc2e86ad2011-03-22 00:51:05 -07001#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07002#include "config.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07003#include "object-store.h"
Linus Torvalds3407bb42005-04-18 14:11:01 -07004
brian m. carlsonc300b1e2017-07-13 23:49:25 +00005static char *create_temp_file(struct object_id *oid)
Linus Torvalds3407bb42005-04-18 14:11:01 -07006{
7 static char path[50];
8 void *buf;
Nicolas Pitre21666f12007-02-26 14:55:59 -05009 enum object_type type;
Linus Torvalds3407bb42005-04-18 14:11:01 -070010 unsigned long size;
11 int fd;
12
brian m. carlsonb4f5aca2018-03-12 02:27:53 +000013 buf = read_object_file(oid, &type, &size);
Nicolas Pitre21666f12007-02-26 14:55:59 -050014 if (!buf || type != OBJ_BLOB)
brian m. carlsonc300b1e2017-07-13 23:49:25 +000015 die("unable to read blob object %s", oid_to_hex(oid));
Linus Torvalds3407bb42005-04-18 14:11:01 -070016
Jeff King5096d492015-09-24 17:06:08 -040017 xsnprintf(path, sizeof(path), ".merge_file_XXXXXX");
Luiz Fernando N. Capitulino7647b172007-08-14 16:45:58 -030018 fd = xmkstemp(path);
Jeff King06f46f22017-09-13 13:16:03 -040019 if (write_in_full(fd, buf, size) < 0)
Thomas Rast0721c312009-06-27 17:58:47 +020020 die_errno("unable to write temp-file");
Linus Torvalds3407bb42005-04-18 14:11:01 -070021 close(fd);
22 return path;
23}
24
Linus Torvaldsb5325812010-01-22 07:38:03 -080025int cmd_unpack_file(int argc, const char **argv, const char *prefix)
Linus Torvalds3407bb42005-04-18 14:11:01 -070026{
brian m. carlsonc300b1e2017-07-13 23:49:25 +000027 struct object_id oid;
Linus Torvalds3407bb42005-04-18 14:11:01 -070028
Jonathan Nieder15073012009-11-09 09:04:56 -060029 if (argc != 2 || !strcmp(argv[1], "-h"))
Alexander Potashev34263de2009-01-04 21:39:27 +030030 usage("git unpack-file <sha1>");
brian m. carlsonc300b1e2017-07-13 23:49:25 +000031 if (get_oid(argv[1], &oid))
Dmitry V. Levin31fff302006-05-09 01:43:38 +040032 die("Not a valid object name %s", argv[1]);
Linus Torvalds3407bb42005-04-18 14:11:01 -070033
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010034 git_config(git_default_config, NULL);
Junio C Hamano53228a52005-11-26 00:50:02 -080035
brian m. carlsonc300b1e2017-07-13 23:49:25 +000036 puts(create_temp_file(&oid));
Linus Torvalds3407bb42005-04-18 14:11:01 -070037 return 0;
38}