Raw
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "environment.h"
5 #include "hex.h"
6 #include "object-file.h"
7 #include "object-name.h"
8 #include "odb.h"
9
10 static char *create_temp_file(struct object_id *oid)
11 {
12 static char path[50];
13 void *buf;
14 enum object_type type;
15 unsigned long size;
16 int fd;
17
18 buf = odb_read_object(the_repository->objects, oid, &type, &size);
19 if (!buf || type != OBJ_BLOB)
20 die("unable to read blob object %s", oid_to_hex(oid));
21
22 xsnprintf(path, sizeof(path), ".merge_file_XXXXXX");
23 fd = xmkstemp(path);
24 if (write_in_full(fd, buf, size) < 0)
25 die_errno("unable to write temp-file");
26 close(fd);
27 free(buf);
28 return path;
29 }
30
31 static const char usage_msg[] =
32 "git unpack-file <blob>";
33
34 int cmd_unpack_file(int argc,
35 const char **argv,
36 const char *prefix UNUSED,
37 struct repository *repo UNUSED)
38 {
39 struct object_id oid;
40
41 show_usage_if_asked(argc, argv, usage_msg);
42 if (argc != 2)
43 usage(usage_msg);
44 if (repo_get_oid(the_repository, argv[1], &oid))
45 die("Not a valid object name %s", argv[1]);
46
47 repo_config(the_repository, git_default_config, NULL);
48
49 puts(create_temp_file(&oid));
50 return 0;
51 }