unpack-file.con commit correct argument checking test for git hash-object (8101407)
   1#include "cache.h"
   2#include "blob.h"
   3
   4static char *create_temp_file(unsigned char *sha1)
   5{
   6        static char path[50];
   7        void *buf;
   8        enum object_type type;
   9        unsigned long size;
  10        int fd;
  11
  12        buf = read_sha1_file(sha1, &type, &size);
  13        if (!buf || type != OBJ_BLOB)
  14                die("unable to read blob object %s", sha1_to_hex(sha1));
  15
  16        strcpy(path, ".merge_file_XXXXXX");
  17        fd = xmkstemp(path);
  18        if (write_in_full(fd, buf, size) != size)
  19                die("unable to write temp-file");
  20        close(fd);
  21        return path;
  22}
  23
  24int main(int argc, char **argv)
  25{
  26        unsigned char sha1[20];
  27
  28        if (argc != 2)
  29                usage("git-unpack-file <sha1>");
  30        if (get_sha1(argv[1], sha1))
  31                die("Not a valid object name %s", argv[1]);
  32
  33        setup_git_directory();
  34        git_config(git_default_config, NULL);
  35
  36        puts(create_temp_file(sha1));
  37        return 0;
  38}