Start the 2.48 cycle
[git/gitster.git] / builtin / unpack-file.c
blob6da282575391b94c4b2a908eee3223a81d4a9ff1
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "hex.h"
5 #include "object-name.h"
6 #include "object-store-ll.h"
8 static char *create_temp_file(struct object_id *oid)
10 static char path[50];
11 void *buf;
12 enum object_type type;
13 unsigned long size;
14 int fd;
16 buf = repo_read_object_file(the_repository, oid, &type, &size);
17 if (!buf || type != OBJ_BLOB)
18 die("unable to read blob object %s", oid_to_hex(oid));
20 xsnprintf(path, sizeof(path), ".merge_file_XXXXXX");
21 fd = xmkstemp(path);
22 if (write_in_full(fd, buf, size) < 0)
23 die_errno("unable to write temp-file");
24 close(fd);
25 free(buf);
26 return path;
29 int cmd_unpack_file(int argc,
30 const char **argv,
31 const char *prefix UNUSED,
32 struct repository *repo UNUSED)
34 struct object_id oid;
36 if (argc != 2 || !strcmp(argv[1], "-h"))
37 usage("git unpack-file <blob>");
38 if (repo_get_oid(the_repository, argv[1], &oid))
39 die("Not a valid object name %s", argv[1]);
41 git_config(git_default_config, NULL);
43 puts(create_temp_file(&oid));
44 return 0;