perl / Git.xson commit Make it possible to set up libgit directly (instead of from the environment) (0270083)
   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
 114char *
 115xs_hash_object_pipe(type, fd)
 116        char *type;
 117        int fd;
 118CODE:
 119{
 120        unsigned char sha1[20];
 121
 122        if (index_pipe(sha1, fd, type, 0))
 123                croak("Unable to hash given filehandle");
 124        RETVAL = sha1_to_hex(sha1);
 125}
 126OUTPUT:
 127        RETVAL
 128
 129char *
 130xs_hash_object_file(type, path)
 131        char *type;
 132        char *path;
 133CODE:
 134{
 135        unsigned char sha1[20];
 136        int fd = open(path, O_RDONLY);
 137        struct stat st;
 138
 139        if (fd < 0 ||
 140            fstat(fd, &st) < 0 ||
 141            index_fd(sha1, fd, &st, 0, type))
 142                croak("Unable to hash %s", path);
 143        close(fd);
 144
 145        RETVAL = sha1_to_hex(sha1);
 146}
 147OUTPUT:
 148        RETVAL