git-credential-netrc: minor whitespace cleanup in test script
[git/gitster.git] / builtin / unpack-file.c
blob32e01555774c838e489fd33c675488e754c3e8e2
1 #include "builtin.h"
2 #include "config.h"
4 static char *create_temp_file(struct object_id *oid)
6 static char path[50];
7 void *buf;
8 enum object_type type;
9 unsigned long size;
10 int fd;
12 buf = read_sha1_file(oid->hash, &type, &size);
13 if (!buf || type != OBJ_BLOB)
14 die("unable to read blob object %s", oid_to_hex(oid));
16 xsnprintf(path, sizeof(path), ".merge_file_XXXXXX");
17 fd = xmkstemp(path);
18 if (write_in_full(fd, buf, size) < 0)
19 die_errno("unable to write temp-file");
20 close(fd);
21 return path;
24 int cmd_unpack_file(int argc, const char **argv, const char *prefix)
26 struct object_id oid;
28 if (argc != 2 || !strcmp(argv[1], "-h"))
29 usage("git unpack-file <sha1>");
30 if (get_oid(argv[1], &oid))
31 die("Not a valid object name %s", argv[1]);
33 git_config(git_default_config, NULL);
35 puts(create_temp_file(&oid));
36 return 0;