perl / Git.xson commit Introduce Git.pm (v4) (b1edc53)
   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
  10/* XS and Perl interface */
  11#include "EXTERN.h"
  12#include "perl.h"
  13#include "XSUB.h"
  14
  15#include "ppport.h"
  16
  17
  18MODULE = Git            PACKAGE = Git
  19
  20PROTOTYPES: DISABLE
  21
  22# /* TODO: xs_call_gate(). See Git.pm. */
  23
  24char *
  25xs_hash_object(file, type = "blob")
  26        SV *file;
  27        char *type;
  28CODE:
  29{
  30        unsigned char sha1[20];
  31
  32        if (SvTYPE(file) == SVt_RV)
  33                file = SvRV(file);
  34
  35        if (SvTYPE(file) == SVt_PVGV) {
  36                /* Filehandle */
  37                PerlIO *pio;
  38
  39                pio = IoIFP(sv_2io(file));
  40                if (!pio)
  41                        croak("You passed me something weird - a dir glob?");
  42                /* XXX: I just hope PerlIO didn't read anything from it yet.
  43                 * --pasky */
  44                if (index_pipe(sha1, PerlIO_fileno(pio), type, 0))
  45                        croak("Unable to hash given filehandle");
  46                /* Avoid any nasty surprises. */
  47                PerlIO_close(pio);
  48
  49        } else {
  50                /* String */
  51                char *path = SvPV_nolen(file);
  52                int fd = open(path, O_RDONLY);
  53                struct stat st;
  54
  55                if (fd < 0 ||
  56                    fstat(fd, &st) < 0 ||
  57                    index_fd(sha1, fd, &st, 0, type))
  58                        croak("Unable to hash %s", path);
  59                close(fd);
  60        }
  61        RETVAL = sha1_to_hex(sha1);
  62}
  63OUTPUT:
  64        RETVAL