blob: ac9cbf7cd8ed1367151de0e8b96668f498b7f1a1 [file] [log] [blame]
Linus Torvalds3407bb42005-04-18 14:11:01 -07001#include "cache.h"
Peter Eriksen8e440252006-04-02 14:44:09 +02002#include "blob.h"
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +01003#include "exec_cmd.h"
Linus Torvalds3407bb42005-04-18 14:11:01 -07004
5static char *create_temp_file(unsigned char *sha1)
6{
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
Nicolas Pitre21666f12007-02-26 14:55:59 -050013 buf = read_sha1_file(sha1, &type, &size);
14 if (!buf || type != OBJ_BLOB)
Linus Torvalds3407bb42005-04-18 14:11:01 -070015 die("unable to read blob object %s", sha1_to_hex(sha1));
16
17 strcpy(path, ".merge_file_XXXXXX");
Luiz Fernando N. Capitulino7647b172007-08-14 16:45:58 -030018 fd = xmkstemp(path);
Andy Whitcroft93822c22007-01-08 15:58:23 +000019 if (write_in_full(fd, buf, size) != size)
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
25int main(int argc, char **argv)
26{
27 unsigned char sha1[20];
28
Steffen Prohaska2fb3f6d2009-01-18 13:00:12 +010029 git_extract_argv0_path(argv[0]);
30
Dmitry V. Levin31fff302006-05-09 01:43:38 +040031 if (argc != 2)
Alexander Potashev34263de2009-01-04 21:39:27 +030032 usage("git unpack-file <sha1>");
Dmitry V. Levin31fff302006-05-09 01:43:38 +040033 if (get_sha1(argv[1], sha1))
34 die("Not a valid object name %s", argv[1]);
Linus Torvalds3407bb42005-04-18 14:11:01 -070035
Junio C Hamano53228a52005-11-26 00:50:02 -080036 setup_git_directory();
Johannes Schindelinef90d6d2008-05-14 18:46:53 +010037 git_config(git_default_config, NULL);
Junio C Hamano53228a52005-11-26 00:50:02 -080038
Linus Torvalds3407bb42005-04-18 14:11:01 -070039 puts(create_temp_file(sha1));
40 return 0;
41}