Merge branch 'ds/more-index-cleanups'
[git] / builtin / unpack-file.c
1 #include "builtin.h"
2 #include "config.h"
3 #include "object-store.h"
4
5 static char *create_temp_file(struct object_id *oid)
6 {
7         static char path[50];
8         void *buf;
9         enum object_type type;
10         unsigned long size;
11         int fd;
12
13         buf = read_object_file(oid, &type, &size);
14         if (!buf || type != OBJ_BLOB)
15                 die("unable to read blob object %s", oid_to_hex(oid));
16
17         xsnprintf(path, sizeof(path), ".merge_file_XXXXXX");
18         fd = xmkstemp(path);
19         if (write_in_full(fd, buf, size) < 0)
20                 die_errno("unable to write temp-file");
21         close(fd);
22         return path;
23 }
24
25 int cmd_unpack_file(int argc, const char **argv, const char *prefix)
26 {
27         struct object_id oid;
28
29         if (argc != 2 || !strcmp(argv[1], "-h"))
30                 usage("git unpack-file <sha1>");
31         if (get_oid(argv[1], &oid))
32                 die("Not a valid object name %s", argv[1]);
33
34         git_config(git_default_config, NULL);
35
36         puts(create_temp_file(&oid));
37         return 0;
38 }