perl / Git.xson commit Git.pm: Introduce fast get_object() method (3c479c3)
   1/* By carefully stacking #includes here (even if WE don't really need them)
   2 * we strive to make the thing actually compile. Git header files aren't very
   3 * nice. Perl headers are one of the signs of the coming apocalypse. */
   4#include <ctype.h>
   5/* Ok, it hasn't been so bad so far. */
   6
   7/* libgit interface */
   8#include "../cache.h"
   9#include "../exec_cmd.h"
  10
  11/* XS and Perl interface */
  12#include "EXTERN.h"
  13#include "perl.h"
  14#include "XSUB.h"
  15
  16
  17static char *
  18report_xs(const char *prefix, const char *err, va_list params)
  19{
  20        static char buf[4096];
  21        strcpy(buf, prefix);
  22        vsnprintf(buf + strlen(prefix), 4096 - strlen(prefix), err, params);
  23        return buf;
  24}
  25
  26static void NORETURN
  27die_xs(const char *err, va_list params)
  28{
  29        char *str;
  30        str = report_xs("fatal: ", err, params);
  31        croak(str);
  32}
  33
  34static void
  35error_xs(const char *err, va_list params)
  36{
  37        char *str;
  38        str = report_xs("error: ", err, params);
  39        warn(str);
  40}
  41
  42
  43MODULE = Git            PACKAGE = Git
  44
  45PROTOTYPES: DISABLE
  46
  47
  48BOOT:
  49{
  50        set_error_routine(error_xs);
  51        set_die_routine(die_xs);
  52}
  53
  54
  55void
  56xs__call_gate(repoid, git_dir)
  57        long repoid;
  58        char *git_dir;
  59CODE:
  60{
  61        static long last_repoid;
  62        if (repoid != last_repoid) {
  63                setup_git(git_dir,
  64                          getenv(DB_ENVIRONMENT),
  65                          getenv(INDEX_ENVIRONMENT),
  66                          getenv(GRAFT_ENVIRONMENT));
  67                last_repoid = repoid;
  68        }
  69}
  70
  71
  72char *
  73xs_version()
  74CODE:
  75{
  76        RETVAL = GIT_VERSION;
  77}
  78OUTPUT:
  79        RETVAL
  80
  81
  82char *
  83xs_exec_path()
  84CODE:
  85{
  86        RETVAL = (char *)git_exec_path();
  87}
  88OUTPUT:
  89        RETVAL
  90
  91
  92void
  93xs__execv_git_cmd(...)
  94CODE:
  95{
  96        const char **argv;
  97        int i;
  98
  99        argv = malloc(sizeof(const char *) * (items + 1));
 100        if (!argv)
 101                croak("malloc failed");
 102        for (i = 0; i < items; i++)
 103                argv[i] = strdup(SvPV_nolen(ST(i)));
 104        argv[i] = NULL;
 105
 106        execv_git_cmd(argv);
 107
 108        for (i = 0; i < items; i++)
 109                if (argv[i])
 110                        free((char *) argv[i]);
 111        free((char **) argv);
 112}
 113
 114
 115SV *
 116xs_get_object(type, id)
 117        char *type;
 118        char *id;
 119CODE:
 120{
 121        unsigned char sha1[20];
 122        unsigned long size;
 123        void *buf;
 124
 125        if (strlen(id) != 40 || get_sha1_hex(id, sha1) < 0)
 126                XSRETURN_UNDEF;
 127
 128        buf = read_sha1_file(sha1, type, &size);
 129        if (!buf)
 130                XSRETURN_UNDEF;
 131        RETVAL = newSVpvn(buf, size);
 132        free(buf);
 133}
 134OUTPUT:
 135        RETVAL
 136
 137
 138char *
 139xs_hash_object_pipe(type, fd)
 140        char *type;
 141        int fd;
 142CODE:
 143{
 144        unsigned char sha1[20];
 145
 146        if (index_pipe(sha1, fd, type, 0))
 147                croak("Unable to hash given filehandle");
 148        RETVAL = sha1_to_hex(sha1);
 149}
 150OUTPUT:
 151        RETVAL
 152
 153char *
 154xs_hash_object_file(type, path)
 155        char *type;
 156        char *path;
 157CODE:
 158{
 159        unsigned char sha1[20];
 160        int fd = open(path, O_RDONLY);
 161        struct stat st;
 162
 163        if (fd < 0 ||
 164            fstat(fd, &st) < 0 ||
 165            index_fd(sha1, fd, &st, 0, type))
 166                croak("Unable to hash %s", path);
 167        close(fd);
 168
 169        RETVAL = sha1_to_hex(sha1);
 170}
 171OUTPUT:
 172        RETVAL